Skip to content

Commit

Permalink
refactor(core,x/**): simplify core service api and embed environment …
Browse files Browse the repository at this point in the history
…in keepers (#20071)
  • Loading branch information
julienrbrt authored Apr 17, 2024
1 parent a4ff821 commit 5e7aae0
Show file tree
Hide file tree
Showing 115 changed files with 532 additions and 637 deletions.
28 changes: 14 additions & 14 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,35 +154,25 @@ The `codectypes.Any` has moved to `github.com/cosmos/gogoproto/types/any`. Modul

#### `**all**`

##### Simulation

`MsgSimulatorFn` has been updated to return an error. Its context argument has been removed, and an address.Codec has
been added to avoid the use of the Accounts.String() method.

```diff
-type MsgSimulatorFn func(r *rand.Rand, ctx sdk.Context, accs []Account) sdk.Msg
+type MsgSimulatorFn func(r *rand.Rand, accs []Account, cdc address.Codec) (sdk.Msg, error)
```

##### Core API

Core API has been introduced for modules since v0.47. With the deprecation of `sdk.Context`, we strongly recommend to use the `cosmossdk.io/core/appmodule` interfaces for the modules. This will allow the modules to work out of the box with server/v2 and baseapp, as well as limit their dependencies on the SDK.

Additionally, the `appmodule.Environment` interface is introduced to fetch different services from the application.
Additionally, the `appmodule.Environment` struct is introduced to fetch different services from the application.
This should be used as an alternative to using `sdk.UnwrapContext(ctx)` to fetch the services.
It needs to be passed into a module at instantiation.
It needs to be passed into a module at instantiation (or depinject will inject the correct environment).

`x/circuit` is used as an example:

```go
app.CircuitKeeper = circuitkeeper.NewKeeper(runtime.NewEnvironment((keys[circuittypes.StoreKey])), appCodec, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AuthKeeper.AddressCodec())
app.CircuitKeeper = circuitkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[circuittypes.StoreKey]), logger.With(log.ModuleKey, "x/circuit")), appCodec, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AuthKeeper.AddressCodec())
```

If your module requires a message server or query server, it should be passed in the environment as well.

```diff
-govKeeper := govkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[govtypes.StoreKey]), app.AuthKeeper, app.BankKeeper,app.StakingKeeper, app.PoolKeeper, app.MsgServiceRouter(), govConfig, authtypes.NewModuleAddress(govtypes.ModuleName).String())
+govKeeper := govkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[govtypes.StoreKey]), logger, runtime.EnvWithRouterService(app.GRPCQueryRouter(), app.MsgServiceRouter())), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.PoolKeeper, govConfig, authtypes.NewModuleAddress(govtypes.ModuleName).String())
+govKeeper := govkeeper.NewKeeper(appCodec, runtime.NewEnvironment(runtime.NewKVStoreService(keys[govtypes.StoreKey]), logger.With(log.ModuleKey, "x/circuit"), runtime.EnvWithRouterService(app.GRPCQueryRouter(), app.MsgServiceRouter())), app.AuthKeeper, app.BankKeeper, app.StakingKeeper, app.PoolKeeper, govConfig, authtypes.NewModuleAddress(govtypes.ModuleName).String())
```

The signature of the extension interface `HasRegisterInterfaces` has been changed to accept a `cosmossdk.io/core/registry.InterfaceRegistrar` instead of a `codec.InterfaceRegistry`. `HasRegisterInterfaces` is now a part of `cosmossdk.io/core/appmodule`. Modules should update their `HasRegisterInterfaces` implementation to accept a `cosmossdk.io/core/registry.InterfaceRegistrar` interface.
Expand All @@ -192,6 +182,16 @@ The signature of the extension interface `HasRegisterInterfaces` has been change
+func (AppModule) RegisterInterfaces(registry registry.InterfaceRegistrar) {
```

##### Simulation

`MsgSimulatorFn` has been updated to return an error. Its context argument has been removed, and an address.Codec has
been added to avoid the use of the Accounts.String() method.

```diff
-type MsgSimulatorFn func(r *rand.Rand, ctx sdk.Context, accs []Account) sdk.Msg
+type MsgSimulatorFn func(r *rand.Rand, accs []Account, cdc address.Codec) (sdk.Msg, error)
```

##### Dependency Injection

Previously `cosmossdk.io/core` held functions `Invoke`, `Provide` and `Register` were moved to `cosmossdk.io/depinject/appconfig`.
Expand Down
6 changes: 3 additions & 3 deletions core/comet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"time"
)

// CometInfoService is an interface that can be used to get information specific to Comet
type CometInfoService interface {
GetCometInfo(context.Context) Info
// Service is an interface that can be used to get information specific to Comet
type Service interface {
CometInfo(context.Context) Info
}

// Info is the information comet provides apps in ABCI
Expand Down
17 changes: 9 additions & 8 deletions core/gas/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@ const NoGasLimit Gas = math.MaxUint64
// gas.Service is a core API type that should be provided by the runtime module being used to
// build an app via depinject.
type Service interface {
// GetGasMeter returns the current transaction-level gas meter. A non-nil meter
// GasMeter returns the current transaction-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context an infinite gas meter
// will be returned.
GetGasMeter(context.Context) Meter

// GetBlockGasMeter returns the current block-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context an infinite gas meter
// will be returned.
GetBlockGasMeter(context.Context) Meter
GasMeter(context.Context) Meter

// WithGasMeter returns a new context with the provided transaction-level gas meter.
WithGasMeter(ctx context.Context, meter Meter) context.Context

// BlockGasMeter returns the current block-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context an infinite gas meter
// will be returned.
BlockGasMeter(context.Context) Meter

// WithBlockGasMeter returns a new context with the provided block-level gas meter.
WithBlockGasMeter(ctx context.Context, meter Meter) context.Context

GetGasConfig(ctx context.Context) GasConfig
// GasConfig returns the gas costs.
GasConfig(ctx context.Context) GasConfig
}

// Meter represents a gas meter for modules consumption
Expand Down
2 changes: 1 addition & 1 deletion core/header/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Service defines the interface in which you can get header information
type Service interface {
GetHeaderInfo(context.Context) Info
HeaderInfo(context.Context) Info
}

// Info defines a struct that contains information about the header
Expand Down
2 changes: 1 addition & 1 deletion core/store/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package store
// DatabaseService provides access to the underlying database for CRUD operations of non-consensus data.
// WARNING: using this api will make your module unprovable for fraud and validity proofs
type DatabaseService interface {
GetDatabase() NonConsensusStore
Database() NonConsensusStore
}

// NonConsensusStore is a simple key-value store that is used to store non-consensus data.
Expand Down
3 changes: 2 additions & 1 deletion docs/architecture/adr-063-core-module-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ Header Service defines a way to get header information about a block. This infor
```go

type Service interface {
GetHeaderInfo(context.Context) Info
HeaderInfo(context.Context) Info
}

type Info struct {
Expand Down Expand Up @@ -463,6 +463,7 @@ module manager and follow the Cosmos SDK's existing [0-based versioning](https:/
versioning as well as runtime modularity, new officially supported runtime modules will be created under the
`cosmossdk.io/runtime` prefix. For each supported consensus engine a semantically-versioned go module should be created
with a runtime implementation for that consensus engine. For example:

* `cosmossdk.io/runtime/comet`
* `cosmossdk.io/runtime/comet/v2`
* `cosmossdk.io/runtime/rollkit`
Expand Down
16 changes: 6 additions & 10 deletions runtime/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,26 @@ var _ gas.Service = GasService{}

type GasService struct{}

func (g GasService) GetGasMeter(ctx context.Context) gas.Meter {
func (g GasService) GasMeter(ctx context.Context) gas.Meter {
return CoreGasmeter{gm: sdk.UnwrapSDKContext(ctx).GasMeter()}
}

func (g GasService) GetBlockGasMeter(ctx context.Context) gas.Meter {
return CoreGasmeter{gm: sdk.UnwrapSDKContext(ctx).BlockGasMeter()}
}

func (g GasService) WithGasMeter(ctx context.Context, meter gas.Meter) context.Context {
return sdk.UnwrapSDKContext(ctx).WithGasMeter(SDKGasMeter{gm: meter})
}

func (g GasService) BlockGasMeter(ctx context.Context) gas.Meter {
return CoreGasmeter{gm: sdk.UnwrapSDKContext(ctx).BlockGasMeter()}
}

func (g GasService) WithBlockGasMeter(ctx context.Context, meter gas.Meter) context.Context {
return sdk.UnwrapSDKContext(ctx).WithGasMeter(SDKGasMeter{gm: meter})
}

func (g GasService) GetGasConfig(ctx context.Context) gas.GasConfig {
func (g GasService) GasConfig(ctx context.Context) gas.GasConfig {
return gas.GasConfig(sdk.UnwrapSDKContext(ctx).KVGasConfig())
}

// ______________________________________________________________________________________________
// Gas Meter Wrappers
// ______________________________________________________________________________________________

// SDKGasMeter is a wrapper around the SDK's GasMeter that implements the GasMeter interface.
type SDKGasMeter struct {
gm gas.Meter
Expand Down
2 changes: 1 addition & 1 deletion runtime/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ var _ header.Service = (*HeaderService)(nil)

type HeaderService struct{}

func (h HeaderService) GetHeaderInfo(ctx context.Context) header.Info {
func (h HeaderService) HeaderInfo(ctx context.Context) header.Info {
return sdk.UnwrapSDKContext(ctx).HeaderInfo()
}
2 changes: 1 addition & 1 deletion runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func ProvideEnvironment(

return kvService, memStoreService, NewEnvironment(
kvService,
logger,
logger.With(log.ModuleKey, fmt.Sprintf("x/%s", key.Name())),
EnvWithRouterService(queryServiceRouter, msgServiceRouter),
EnvWithMemStoreService(memStoreService),
)
Expand Down
4 changes: 2 additions & 2 deletions simapp/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
circuitante.NewCircuitBreakerDecorator(options.CircuitKeeper),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(options.AccountKeeper.Environment()),
ante.NewValidateBasicDecorator(options.AccountKeeper.GetEnvironment()),
ante.NewTxTimeoutHeightDecorator(),
ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, options.TxManager, options.AccountKeeper.Environment()),
ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, options.TxManager, options.AccountKeeper.GetEnvironment()),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
Expand Down
Loading

0 comments on commit 5e7aae0

Please sign in to comment.