refactor(core,x/**): simplify core service api and embed environment in keepers (#20071)

This commit is contained in:
Julien Robert
2024-04-17 18:18:16 +00:00
committed by GitHub
parent a4ff821981
commit 5e7aae0db1
115 changed files with 546 additions and 651 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
anteDecorators := []sdk.AnteDecorator{
NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
NewValidateBasicDecorator(options.AccountKeeper.Environment()),
NewValidateBasicDecorator(options.AccountKeeper.GetEnvironment()),
NewTxTimeoutHeightDecorator(),
NewValidateMemoDecorator(options.AccountKeeper),
NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
+1 -1
View File
@@ -108,7 +108,7 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ b
ctx.GasMeter().ConsumeGas(params.TxSizeCostPerByte*storetypes.Gas(len(ctx.TxBytes())), "txSize")
// simulate gas cost for signatures in simulate mode
txService := cgts.ak.Environment().TransactionService
txService := cgts.ak.GetEnvironment().TransactionService
if txService.ExecMode(ctx) == transaction.ExecModeSimulate {
// in simulate mode, each element should be a nil signature
sigs, err := sigTx.GetSignaturesV2()
+1 -1
View File
@@ -36,7 +36,7 @@ func TestValidateBasic(t *testing.T) {
invalidTx, err := suite.CreateTestTx(suite.ctx, privs, accNums, accSeqs, suite.ctx.ChainID(), signing.SignMode_SIGN_MODE_DIRECT)
require.NoError(t, err)
vbd := ante.NewValidateBasicDecorator(suite.accountKeeper.Environment())
vbd := ante.NewValidateBasicDecorator(suite.accountKeeper.GetEnvironment())
antehandler := sdk.ChainAnteDecorators(vbd)
_, err = antehandler(suite.ctx, invalidTx, false)
+1 -5
View File
@@ -10,10 +10,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
type HasEnvironment interface {
Environment() appmodule.Environment
}
// AccountKeeper defines the contract needed for AccountKeeper related APIs.
// Interface provides support to use non-sdk AccountKeeper for AnteHandler's decorators.
type AccountKeeper interface {
@@ -23,7 +19,7 @@ type AccountKeeper interface {
GetModuleAddress(moduleName string) sdk.AccAddress
NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
AddressCodec() address.Codec
Environment() appmodule.Environment
GetEnvironment() appmodule.Environment
}
// FeegrantKeeper defines the expected feegrant keeper.
+1 -1
View File
@@ -46,7 +46,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, _ bool, nex
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}
txService := dfd.accountKeeper.Environment().TransactionService
txService := dfd.accountKeeper.GetEnvironment().TransactionService
execMode := txService.ExecMode(ctx)
if execMode != transaction.ExecModeSimulate && ctx.BlockHeight() > 0 && feeTx.GetGas() == 0 {
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidGasLimit, "must provide positive gas")
+3 -3
View File
@@ -281,7 +281,7 @@ func (svd SigVerificationDecorator) consumeSignatureGas(
pubKey cryptotypes.PubKey,
signature signing.SignatureV2,
) error {
if svd.ak.Environment().TransactionService.ExecMode(ctx) == transaction.ExecModeSimulate && pubKey == nil {
if svd.ak.GetEnvironment().TransactionService.ExecMode(ctx) == transaction.ExecModeSimulate && pubKey == nil {
pubKey = simSecp256k1Pubkey
}
@@ -311,7 +311,7 @@ func (svd SigVerificationDecorator) verifySig(ctx sdk.Context, tx sdk.Tx, acc sd
// we're in simulation mode, or in ReCheckTx, or context is not
// on sig verify tx, then we do not need to verify the signatures
// in the tx.
if svd.ak.Environment().TransactionService.ExecMode(ctx) == transaction.ExecModeSimulate || ctx.IsReCheckTx() || !ctx.IsSigverifyTx() {
if svd.ak.GetEnvironment().TransactionService.ExecMode(ctx) == transaction.ExecModeSimulate || ctx.IsReCheckTx() || !ctx.IsSigverifyTx() {
return nil
}
@@ -385,7 +385,7 @@ func (svd SigVerificationDecorator) setPubKey(ctx sdk.Context, acc sdk.AccountI,
if txPubKey == nil {
// if we're not in simulation mode, and we do not have a valid pubkey
// for this signer, then we simply error.
if svd.ak.Environment().TransactionService.ExecMode(ctx) != transaction.ExecModeSimulate {
if svd.ak.GetEnvironment().TransactionService.ExecMode(ctx) != transaction.ExecModeSimulate {
return fmt.Errorf("the account %s is without a pubkey and did not provide a pubkey in the tx to set it", acc.GetAddress().String())
}
// if we're in simulation mode, then we can populate the pubkey with the
+1 -1
View File
@@ -20,7 +20,7 @@ type mockAccount struct {
ante.AccountKeeper
}
func (*mockAccount) Environment() appmodule.Environment {
func (*mockAccount) GetEnvironment() appmodule.Environment {
return appmodule.Environment{
TransactionService: &mockTransactionService{},
}
+6 -6
View File
@@ -25,7 +25,7 @@ func TestUnorderedTxDecorator_OrderedTx(t *testing.T) {
suite := SetupTestSuite(t, false)
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment()))
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.GetEnvironment()))
tx, txBz := genUnorderedTx(t, false, 0)
ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100)
@@ -44,7 +44,7 @@ func TestUnorderedTxDecorator_UnorderedTx_NoTTL(t *testing.T) {
suite := SetupTestSuite(t, false)
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment()))
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.GetEnvironment()))
tx, txBz := genUnorderedTx(t, true, 0)
ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100)
@@ -63,7 +63,7 @@ func TestUnorderedTxDecorator_UnorderedTx_InvalidTTL(t *testing.T) {
suite := SetupTestSuite(t, false)
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment()))
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.GetEnvironment()))
tx, txBz := genUnorderedTx(t, true, 100+unorderedtx.DefaultMaxUnOrderedTTL+1)
ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100)
@@ -82,7 +82,7 @@ func TestUnorderedTxDecorator_UnorderedTx_AlreadyExists(t *testing.T) {
suite := SetupTestSuite(t, false)
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment()))
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.GetEnvironment()))
tx, txBz := genUnorderedTx(t, true, 150)
ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100)
@@ -104,7 +104,7 @@ func TestUnorderedTxDecorator_UnorderedTx_ValidCheckTx(t *testing.T) {
suite := SetupTestSuite(t, false)
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment()))
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.GetEnvironment()))
tx, txBz := genUnorderedTx(t, true, 150)
ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100).WithExecMode(sdk.ExecModeCheck)
@@ -123,7 +123,7 @@ func TestUnorderedTxDecorator_UnorderedTx_ValidDeliverTx(t *testing.T) {
suite := SetupTestSuite(t, false)
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.Environment()))
chain := sdk.ChainAnteDecorators(ante.NewUnorderedTxDecorator(unorderedtx.DefaultMaxUnOrderedTTL, txm, suite.accountKeeper.GetEnvironment()))
tx, txBz := genUnorderedTx(t, true, 150)
ctx := sdk.Context{}.WithTxBytes(txBz).WithBlockHeight(100).WithExecMode(sdk.ExecModeFinalize)
+8 -17
View File
@@ -10,7 +10,6 @@ import (
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/x/auth/types"
"github.com/cosmos/cosmos-sdk/codec"
@@ -54,9 +53,6 @@ type AccountKeeperI interface {
// AddressCodec returns the account address codec.
AddressCodec() address.Codec
// Environment returns the module's environment.
Environment() appmodule.Environment
}
func NewAccountIndexes(sb *collections.SchemaBuilder) AccountsIndexes {
@@ -84,10 +80,11 @@ func (a AccountsIndexes) IndexesList() []collections.Index[sdk.AccAddress, sdk.A
// AccountKeeper encodes/decodes accounts using the go-amino (binary)
// encoding/decoding library.
type AccountKeeper struct {
appmodule.Environment
addressCodec address.Codec
AccountsModKeeper types.AccountsModKeeper
environment appmodule.Environment
cdc codec.BinaryCodec
permAddrs map[string]types.PermissionsForAddress
bech32Prefix string
@@ -127,9 +124,9 @@ func NewAccountKeeper(
sb := collections.NewSchemaBuilder(env.KVStoreService)
ak := AccountKeeper{
Environment: env,
addressCodec: ac,
bech32Prefix: bech32Prefix,
environment: env,
proto: proto,
cdc: cdc,
AccountsModKeeper: accountsModKeeper,
@@ -152,17 +149,16 @@ func (ak AccountKeeper) GetAuthority() string {
return ak.authority
}
func (ak AccountKeeper) GetEnvironment() appmodule.Environment {
return ak.Environment
}
// AddressCodec returns the x/auth account address codec.
// x/auth is tied to bech32 encoded user accounts
func (ak AccountKeeper) AddressCodec() address.Codec {
return ak.addressCodec
}
// Logger returns a module-specific logger.
func (ak AccountKeeper) Logger(ctx context.Context) log.Logger {
return ak.environment.Logger.With("module", "x/"+types.ModuleName)
}
// GetPubKey Returns the PubKey of the account at address
func (ak AccountKeeper) GetPubKey(ctx context.Context, addr sdk.AccAddress) (cryptotypes.PubKey, error) {
acc := ak.GetAccount(ctx, addr)
@@ -294,7 +290,7 @@ func (ak AccountKeeper) NonAtomicMsgsExec(ctx context.Context, signer sdk.AccAdd
}
}
if err := ak.environment.BranchService.Execute(ctx, func(ctx context.Context) error {
if err := ak.BranchService.Execute(ctx, func(ctx context.Context) error {
result, err := ak.AccountsModKeeper.SendModuleMessageUntyped(ctx, signer, msg)
if err != nil {
// If an error occurs during message execution, append error response
@@ -318,8 +314,3 @@ func (ak AccountKeeper) NonAtomicMsgsExec(ctx context.Context, signer sdk.AccAdd
return msgResponses, nil
}
// Environment returns the module's environment.
func (ak AccountKeeper) Environment() appmodule.Environment {
return ak.environment
}
+2 -2
View File
@@ -42,7 +42,7 @@ func (m Migrator) Migrate3to4(ctx context.Context) error {
// It migrates the GlobalAccountNumber from being a protobuf defined value to a
// big-endian encoded uint64, it also migrates it to use a more canonical prefix.
func (m Migrator) Migrate4To5(ctx context.Context) error {
return v5.Migrate(ctx, m.keeper.environment.KVStoreService, m.keeper.AccountNumber)
return v5.Migrate(ctx, m.keeper.KVStoreService, m.keeper.AccountNumber)
}
// V45_SetAccount implements V45_SetAccount
@@ -51,7 +51,7 @@ func (m Migrator) Migrate4To5(ctx context.Context) error {
// NOTE: This is used for testing purposes only.
func (m Migrator) V45SetAccount(ctx context.Context, acc sdk.AccountI) error {
addr := acc.GetAddress()
store := m.keeper.environment.KVStoreService.OpenKVStore(ctx)
store := m.keeper.KVStoreService.OpenKVStore(ctx)
bz, err := m.keeper.Accounts.ValueCodec().Encode(acc)
if err != nil {