Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Provide logger through depinject #15818

Merged
merged 41 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
99ef23e
init
facundomedica Apr 12, 2023
07ad3de
implement + adr
facundomedica Apr 12, 2023
4fd390a
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Apr 12, 2023
f709e1a
update
facundomedica Apr 12, 2023
55dfec4
use cosmossdk.io\/log
facundomedica Apr 12, 2023
1b72a66
progress
facundomedica Apr 12, 2023
e64afbf
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Apr 12, 2023
c9cafd3
fix
facundomedica Apr 12, 2023
0262fcd
small fixes
facundomedica Apr 12, 2023
65b6a86
small fixes
facundomedica Apr 12, 2023
0998943
small fixes
facundomedica Apr 12, 2023
5e1ca87
small fixes
facundomedica Apr 12, 2023
f7e41da
small fixes
facundomedica Apr 12, 2023
db7b086
cl++
facundomedica Apr 13, 2023
e3b7073
fix app v1
facundomedica Apr 13, 2023
e58ea40
fix more tests
facundomedica Apr 13, 2023
1f9c0bf
more tests
facundomedica Apr 13, 2023
db2be7f
merge
facundomedica Apr 17, 2023
c7b25a3
tests
facundomedica Apr 18, 2023
c277e3a
update bank
facundomedica Apr 18, 2023
54bb8f6
update bank
facundomedica Apr 18, 2023
39c6030
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk into facu…
facundomedica Apr 18, 2023
87bc493
fix more test
facundomedica Apr 18, 2023
9c4c448
fix tests
facundomedica Apr 18, 2023
63bcdad
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 18, 2023
601e89f
fix
facundomedica Apr 18, 2023
7024218
Merge branch 'facu/coreapi-logger' of https://github.com/cosmos/cosmo…
facundomedica Apr 18, 2023
7312bda
fix
facundomedica Apr 18, 2023
607401a
fix conflicts
facundomedica Apr 19, 2023
cf98c07
fix some more tests
facundomedica Apr 19, 2023
046c918
im tired
facundomedica Apr 19, 2023
089e775
hopefully the last
facundomedica Apr 19, 2023
ef11a15
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 19, 2023
6261eeb
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 19, 2023
8c2cf2b
lint
facundomedica Apr 20, 2023
5b90bf4
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 20, 2023
f14a89d
Update x/bank/keeper/keeper.go
facundomedica Apr 20, 2023
c038a61
adr++ and upgrading++
facundomedica Apr 20, 2023
3a856a5
cl++
facundomedica Apr 20, 2023
f462163
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 20, 2023
a4c1aca
Merge branch 'main' into facu/coreapi-logger
facundomedica Apr 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (runtime) [#15818](https://github.com/cosmos/cosmos-sdk/pull/15818) Provide logger through `depinject` instead of appBuilder.
* (testutil/integration) [#15556](https://github.com/cosmos/cosmos-sdk/pull/15556) Introduce `testutil/integration` package for module integration testing.
* (types) [#15735](https://github.com/cosmos/cosmos-sdk/pull/15735) Make `ValidateBasic() error` method of `Msg` interface optional. Modules should validate messages directly in their message handlers ([RFC 001](https://docs.cosmos.network/main/rfc/rfc-001-tx-validation)).
* (x/genutil) [#15679](https://github.com/cosmos/cosmos-sdk/pull/15679) Allow applications to specify a custom genesis migration function for the `genesis migrate` command.
Expand Down
23 changes: 23 additions & 0 deletions docs/architecture/adr-063-core-module-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ the block or app hash is left to the runtime to specify).
Events emitted by `EmitKVEvent` and `EmitProtoEventNonConsensus` are not considered to be part of consensus and cannot be observed
by other modules. If there is a client-side need to add events in patch releases, these methods can be used.

#### Logger

A logger (`cosmossdk.io/log`) must be supplied using `depinject`, and will
be made available for modules to use via `depinject.In`.
Modules using it should follow the current pattern in the SDK by adding the module name before using it.

```go
type ModuleInputs struct {
depinject.In

Logger log.Logger
}

func ProvideModule(in ModuleInputs) ModuleOutputs {
// add the module name to the logger
logger := in.Logger.With("module", "x/"+types.ModuleName)
facundomedica marked this conversation as resolved.
Show resolved Hide resolved

keeper := keeper.NewKeeper(
logger,
)
}
```

### Core `AppModule` extension interfaces


Expand Down
2 changes: 2 additions & 0 deletions runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/log"

storetypes "cosmossdk.io/store/types"

Expand Down Expand Up @@ -49,6 +50,7 @@ type App struct {
baseAppOptions []BaseAppOption
msgServiceRouter *baseapp.MsgServiceRouter
appConfig *appv1alpha1.Config
logger log.Logger
// initChainer is the init chainer function defined by the app config.
// this is only required if the chain wants to add special InitChainer logic.
initChainer sdk.InitChainer
Expand Down
4 changes: 1 addition & 3 deletions runtime/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"io"

"cosmossdk.io/log"
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand All @@ -26,7 +25,6 @@ func (a *AppBuilder) DefaultGenesis() map[string]json.RawMessage {

// Build builds an *App instance.
func (a *AppBuilder) Build(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
baseAppOptions ...func(*baseapp.BaseApp),
Expand All @@ -35,7 +33,7 @@ func (a *AppBuilder) Build(
baseAppOptions = append(baseAppOptions, option)
}

bApp := baseapp.NewBaseApp(a.app.config.AppName, logger, db, nil, baseAppOptions...)
bApp := baseapp.NewBaseApp(a.app.config.AppName, a.app.logger, db, nil, baseAppOptions...)
bApp.SetMsgServiceRouter(a.app.msgServiceRouter)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
Expand Down
3 changes: 3 additions & 0 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"cosmossdk.io/core/store"
"cosmossdk.io/log"
"github.com/cosmos/gogoproto/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"
Expand Down Expand Up @@ -127,6 +128,7 @@ type AppInputs struct {
BaseAppOptions []BaseAppOption
InterfaceRegistry codectypes.InterfaceRegistry
LegacyAmino *codec.LegacyAmino
Logger log.Logger
}

func SetupAppBuilder(inputs AppInputs) {
Expand All @@ -135,6 +137,7 @@ func SetupAppBuilder(inputs AppInputs) {
app.config = inputs.Config
app.ModuleManager = module.NewManagerFromMap(inputs.Modules)
app.appConfig = inputs.AppConfig
app.logger = inputs.Logger

for name, mod := range inputs.Modules {
if basicMod, ok := mod.(module.AppModuleBasic); ok {
Expand Down
1 change: 1 addition & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func NewSimApp(
app.AccountKeeper,
BlockedAddresses(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
logger,
)
app.StakingKeeper = stakingkeeper.NewKeeper(
appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Expand Down
3 changes: 2 additions & 1 deletion simapp/app_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func NewSimApp(
depinject.Supply(
// supply the application options
appOpts,
logger,
facundomedica marked this conversation as resolved.
Show resolved Hide resolved

// ADVANCED CONFIGURATION

Expand Down Expand Up @@ -248,7 +249,7 @@ func NewSimApp(
// }
// baseAppOptions = append(baseAppOptions, prepareOpt)

app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...)
app.App = appBuilder.Build(db, traceStore, baseAppOptions...)

// register streaming services
if err := app.RegisterStreamingServices(appOpts, app.kvStoreKeys()); err != nil {
Expand Down
8 changes: 6 additions & 2 deletions tests/e2e/params/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ func (s *E2ETestSuite) SetupSuite() {
appBuilder *runtime.AppBuilder
paramsKeeper keeper.Keeper
)
if err := depinject.Inject(AppConfig, &appBuilder, &paramsKeeper); err != nil {
if err := depinject.Inject(
depinject.Configs(
AppConfig,
depinject.Supply(val.GetCtx().Logger),
),
&appBuilder, &paramsKeeper); err != nil {
panic(err)
}

Expand All @@ -66,7 +71,6 @@ func (s *E2ETestSuite) SetupSuite() {
subspace := paramsKeeper.Subspace(mySubspace).WithKeyTable(paramtypes.NewKeyTable().RegisterParamSet(&paramSet))

app := appBuilder.Build(
val.GetCtx().Logger,
dbm.NewMemDB(),
nil,
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
Expand Down
8 changes: 6 additions & 2 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,15 @@ func DefaultConfigWithAppConfig(appConfig depinject.Config) (Config, error) {
cfg.AppConstructor = func(val ValidatorI) servertypes.Application {
// we build a unique app instance for every validator here
var appBuilder *runtime.AppBuilder
if err := depinject.Inject(appConfig, &appBuilder); err != nil {
if err := depinject.Inject(
depinject.Configs(
appConfig,
depinject.Supply(val.GetCtx().Logger),
),
&appBuilder); err != nil {
panic(err)
}
app := appBuilder.Build(
val.GetCtx().Logger,
dbm.NewMemDB(),
nil,
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
Expand Down
10 changes: 7 additions & 3 deletions testutil/sims/app_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
dbm "github.com/cosmos/cosmos-db"

"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -119,16 +120,19 @@ func SetupWithConfiguration(appConfig depinject.Config, startupConfig StartupCon
)

if err := depinject.Inject(
appConfig,
depinject.Configs(
appConfig,
depinject.Supply(log.NewNopLogger()),
),
append(extraOutputs, &appBuilder, &codec)...,
); err != nil {
return nil, fmt.Errorf("failed to inject dependencies: %w", err)
}

if startupConfig.BaseAppOption != nil {
app = appBuilder.Build(log.NewNopLogger(), startupConfig.DB, nil, startupConfig.BaseAppOption)
app = appBuilder.Build(startupConfig.DB, nil, startupConfig.BaseAppOption)
} else {
app = appBuilder.Build(log.NewNopLogger(), startupConfig.DB, nil)
app = appBuilder.Build(startupConfig.DB, nil)
}
if err := app.Load(true); err != nil {
return nil, fmt.Errorf("failed to load app: %w", err)
Expand Down
12 changes: 7 additions & 5 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"fmt"

"cosmossdk.io/log"
"cosmossdk.io/math"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -59,6 +60,7 @@ type BaseKeeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
mintCoinsRestrictionFn MintingRestrictionFn
logger log.Logger
}

type MintingRestrictionFn func(ctx sdk.Context, coins sdk.Coins) error
Expand Down Expand Up @@ -89,6 +91,7 @@ func NewBaseKeeper(
ak types.AccountKeeper,
blockedAddrs map[string]bool,
authority string,
logger log.Logger,
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
) BaseKeeper {
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic(fmt.Errorf("invalid bank authority address: %w", err))
Expand All @@ -100,6 +103,7 @@ func NewBaseKeeper(
cdc: cdc,
storeKey: storeKey,
mintCoinsRestrictionFn: func(ctx sdk.Context, coins sdk.Coins) error { return nil },
logger: logger,
}
}

Expand Down Expand Up @@ -347,7 +351,7 @@ func (k BaseKeeper) UndelegateCoinsFromModuleToAccount(
func (k BaseKeeper) MintCoins(ctx sdk.Context, moduleName string, amounts sdk.Coins) error {
err := k.mintCoinsRestrictionFn(ctx, amounts)
if err != nil {
ctx.Logger().Error(fmt.Sprintf("Module %q attempted to mint coins %s it doesn't have permission for, error %v", moduleName, amounts, err))
k.logger.Error(fmt.Sprintf("Module %q attempted to mint coins %s it doesn't have permission for, error %v", moduleName, amounts, err))
return err
}
acc := k.ak.GetModuleAccount(ctx, moduleName)
Expand All @@ -370,8 +374,7 @@ func (k BaseKeeper) MintCoins(ctx sdk.Context, moduleName string, amounts sdk.Co
k.setSupply(ctx, supply)
}

logger := k.Logger(ctx)
logger.Debug("minted coins from module account", "amount", amounts.String(), "from", moduleName)
k.logger.Debug("minted coins from module account", "amount", amounts.String(), "from", moduleName)

// emit mint event
ctx.EventManager().EmitEvent(
Expand Down Expand Up @@ -404,8 +407,7 @@ func (k BaseKeeper) BurnCoins(ctx sdk.Context, moduleName string, amounts sdk.Co
k.setSupply(ctx, supply)
}

logger := k.Logger(ctx)
logger.Debug("burned tokens from module account", "amount", amounts.String(), "from", moduleName)
k.logger.Debug("burned tokens from module account", "amount", amounts.String(), "from", moduleName)

// emit burn event
ctx.EventManager().EmitEvent(
Expand Down
3 changes: 3 additions & 0 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"cosmossdk.io/log"
"cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand Down Expand Up @@ -139,6 +140,7 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.authKeeper,
map[string]bool{accAddrs[4].String(): true},
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
log.NewNopLogger(),
)

banktypes.RegisterInterfaces(encCfg.InterfaceRegistry)
Expand Down Expand Up @@ -240,6 +242,7 @@ func (suite *KeeperTestSuite) TestGetAuthority() {
nil,
nil,
authority,
log.NewNopLogger(),
)
}

Expand Down
6 changes: 0 additions & 6 deletions x/bank/keeper/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/cosmos/cosmos-sdk/runtime"

"cosmossdk.io/log"
"cosmossdk.io/math"

errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -95,11 +94,6 @@ func NewBaseViewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, ak t
return k
}

// Logger returns a module-specific logger.
func (k BaseViewKeeper) Logger(ctx sdk.Context) log.Logger {
facundomedica marked this conversation as resolved.
Show resolved Hide resolved
return ctx.Logger().With("module", "x/"+types.ModuleName)
}
facundomedica marked this conversation as resolved.
Show resolved Hide resolved

// HasBalance returns whether or not an account has at least amt balance.
func (k BaseViewKeeper) HasBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coin) bool {
return k.GetBalance(ctx, addr, amt.Denom).IsGTE(amt)
Expand Down
6 changes: 6 additions & 0 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
modulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -212,6 +213,7 @@ type ModuleInputs struct {
Config *modulev1.Module
Cdc codec.Codec
Key *store.KVStoreKey
Logger log.Logger

AccountKeeper types.AccountKeeper

Expand Down Expand Up @@ -248,12 +250,16 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
}

// add the module name to the logger
logger := in.Logger.With("module", "x/"+types.ModuleName)
facundomedica marked this conversation as resolved.
Show resolved Hide resolved

bankKeeper := keeper.NewBaseKeeper(
in.Cdc,
in.Key,
in.AccountKeeper,
blockedAddresses,
authority.String(),
logger,
)
m := NewAppModule(in.Cdc, bankKeeper, in.AccountKeeper, in.LegacySubspace)

Expand Down