From 0d3f4a82d4445cb68b2b2b4c36a39d7fd55d0837 Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 5 Mar 2024 12:23:15 +0100 Subject: [PATCH] feat(x/accounts): add event emission to accounts module (#19636) Co-authored-by: sontrinh16 --- go.work.example | 2 +- x/accounts/defaults/base/account.go | 1 - .../implementation/api_builder_test.go | 8 +++- x/accounts/internal/implementation/context.go | 38 ------------------- .../internal/implementation/implementation.go | 12 ++---- x/accounts/keeper.go | 2 +- .../testing/account_abstraction/minimal.go | 9 +++++ x/accounts/testing/counter/counter.go | 2 - 8 files changed, 21 insertions(+), 53 deletions(-) diff --git a/go.work.example b/go.work.example index 89c48b57ce..11cf4a9864 100644 --- a/go.work.example +++ b/go.work.example @@ -1,6 +1,6 @@ go 1.22 -toolchain go1.22.0 +toolchain go1.22 use ( . diff --git a/x/accounts/defaults/base/account.go b/x/accounts/defaults/base/account.go index 36f858e85d..76f7612499 100644 --- a/x/accounts/defaults/base/account.go +++ b/x/accounts/defaults/base/account.go @@ -37,7 +37,6 @@ func NewAccount(name string, handlerMap *signing.HandlerMap) accountstd.AccountC PubKey: collections.NewItem(deps.SchemaBuilder, PubKeyPrefix, "pub_key", codec.CollValue[secp256k1.PubKey](deps.LegacyStateCodec)), Sequence: collections.NewSequence(deps.SchemaBuilder, SequencePrefix, "sequence"), addrCodec: deps.AddressCodec, - hs: deps.HeaderService, signingHandlers: handlerMap, }, nil } diff --git a/x/accounts/internal/implementation/api_builder_test.go b/x/accounts/internal/implementation/api_builder_test.go index acad2af651..ab41ede899 100644 --- a/x/accounts/internal/implementation/api_builder_test.go +++ b/x/accounts/internal/implementation/api_builder_test.go @@ -10,8 +10,12 @@ import ( func TestRouterDoubleRegistration(t *testing.T) { router := NewExecuteBuilder() - RegisterExecuteHandler(router, func(_ context.Context, req *types.StringValue) (*types.StringValue, error) { return nil, nil }) - RegisterExecuteHandler(router, func(_ context.Context, req *types.StringValue) (*types.StringValue, error) { return nil, nil }) + RegisterExecuteHandler(router, func(_ context.Context, req *types.StringValue) (*types.StringValue, error) { + return nil, nil + }) + RegisterExecuteHandler(router, func(_ context.Context, req *types.StringValue) (*types.StringValue, error) { + return nil, nil + }) _, err := router.makeHandler() require.ErrorContains(t, err, "already registered") diff --git a/x/accounts/internal/implementation/context.go b/x/accounts/internal/implementation/context.go index 01ce89a5fb..4ab8d2d7cc 100644 --- a/x/accounts/internal/implementation/context.go +++ b/x/accounts/internal/implementation/context.go @@ -5,8 +5,6 @@ import ( "encoding/binary" "cosmossdk.io/collections" - "cosmossdk.io/core/gas" - "cosmossdk.io/core/header" "cosmossdk.io/core/store" "cosmossdk.io/x/accounts/internal/prefixstore" @@ -137,39 +135,3 @@ func Whoami(ctx context.Context) []byte { // Funds returns the funds associated with the execution context. func Funds(ctx context.Context) sdk.Coins { return getCtx(ctx).funds } - -type headerService struct{ hs header.Service } - -func (h headerService) GetHeaderInfo(ctx context.Context) header.Info { - return h.hs.GetHeaderInfo(getParentContext(ctx)) -} - -var _ gas.Service = (*gasService)(nil) - -type gasService struct{ gs gas.Service } - -func (g gasService) GetGasMeter(ctx context.Context) gas.Meter { - return g.gs.GetGasMeter(getParentContext(ctx)) -} - -func (g gasService) GetBlockGasMeter(ctx context.Context) gas.Meter { - return g.gs.GetBlockGasMeter(getParentContext(ctx)) -} - -func (g gasService) GetGasConfig(ctx context.Context) gas.GasConfig { - return g.gs.GetGasConfig(getParentContext(ctx)) -} - -func (g gasService) WithGasMeter(ctx context.Context, meter gas.Meter) context.Context { - v := getCtx(ctx) - v.parentContext = g.gs.WithGasMeter(v.parentContext, meter) - return context.WithValue(v.parentContext, contextKey{}, v) -} - -func (g gasService) WithBlockGasMeter(ctx context.Context, meter gas.Meter) context.Context { - v := getCtx(ctx) - v.parentContext = g.gs.WithBlockGasMeter(v.parentContext, meter) - return addCtx(v.parentContext, v) -} - -func getParentContext(ctx context.Context) context.Context { return getCtx(ctx).parentContext } diff --git a/x/accounts/internal/implementation/implementation.go b/x/accounts/internal/implementation/implementation.go index 57bf0f144b..cb55359d04 100644 --- a/x/accounts/internal/implementation/implementation.go +++ b/x/accounts/internal/implementation/implementation.go @@ -8,8 +8,7 @@ import ( "cosmossdk.io/collections" "cosmossdk.io/core/address" - "cosmossdk.io/core/gas" - "cosmossdk.io/core/header" + "cosmossdk.io/core/appmodule" "github.com/cosmos/cosmos-sdk/codec" ) @@ -18,8 +17,7 @@ import ( type Dependencies struct { SchemaBuilder *collections.SchemaBuilder AddressCodec address.Codec - HeaderService header.Service - GasService gas.Service + Environment appmodule.Environment LegacyStateCodec interface { Marshal(gogoproto.Message) ([]byte, error) Unmarshal([]byte, gogoproto.Message) error @@ -34,8 +32,7 @@ type AccountCreatorFunc = func(deps Dependencies) (string, Account, error) func MakeAccountsMap( cdc codec.BinaryCodec, addressCodec address.Codec, - hs header.Service, - gs gas.Service, + env appmodule.Environment, accounts []AccountCreatorFunc, ) (map[string]Implementation, error) { accountsMap := make(map[string]Implementation, len(accounts)) @@ -44,8 +41,7 @@ func MakeAccountsMap( deps := Dependencies{ SchemaBuilder: stateSchemaBuilder, AddressCodec: addressCodec, - HeaderService: headerService{hs}, - GasService: gasService{gs}, + Environment: env, LegacyStateCodec: cdc, } name, accountInterface, err := makeAccount(deps) diff --git a/x/accounts/keeper.go b/x/accounts/keeper.go index 870bce8760..010f5b4951 100644 --- a/x/accounts/keeper.go +++ b/x/accounts/keeper.go @@ -90,7 +90,7 @@ func NewKeeper( return Keeper{}, err } keeper.Schema = schema - keeper.accounts, err = implementation.MakeAccountsMap(cdc, keeper.addressCodec, env.HeaderService, env.GasService, accounts) + keeper.accounts, err = implementation.MakeAccountsMap(cdc, keeper.addressCodec, env, accounts) if err != nil { return Keeper{}, err } diff --git a/x/accounts/testing/account_abstraction/minimal.go b/x/accounts/testing/account_abstraction/minimal.go index 3f0e69a43e..30775e9842 100644 --- a/x/accounts/testing/account_abstraction/minimal.go +++ b/x/accounts/testing/account_abstraction/minimal.go @@ -6,6 +6,8 @@ import ( "cosmossdk.io/api/cosmos/crypto/secp256k1" "cosmossdk.io/collections" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/event" "cosmossdk.io/x/accounts/accountstd" account_abstractionv1 "cosmossdk.io/x/accounts/interfaces/account_abstraction/v1" rotationv1 "cosmossdk.io/x/accounts/testing/rotation/v1" @@ -24,6 +26,7 @@ func NewMinimalAbstractedAccount(d accountstd.Dependencies) (MinimalAbstractedAc return MinimalAbstractedAccount{ PubKey: collections.NewItem(d.SchemaBuilder, PubKeyPrefix, "pubkey", codec.CollValueV2[secp256k1.PubKey]()), Sequence: collections.NewSequence(d.SchemaBuilder, SequencePrefix, "sequence"), + Env: d.Environment, }, nil } @@ -32,6 +35,7 @@ func NewMinimalAbstractedAccount(d accountstd.Dependencies) (MinimalAbstractedAc type MinimalAbstractedAccount struct { PubKey collections.Item[*secp256k1.PubKey] Sequence collections.Sequence + Env appmodule.Environment } func (a MinimalAbstractedAccount) Init(ctx context.Context, msg *rotationv1.MsgInit) (*rotationv1.MsgInitResponse, error) { @@ -45,6 +49,11 @@ func (a MinimalAbstractedAccount) RotatePubKey(ctx context.Context, msg *rotatio // Authenticate authenticates the account, auth always passess. func (a MinimalAbstractedAccount) Authenticate(ctx context.Context, msg *account_abstractionv1.MsgAuthenticate) (*account_abstractionv1.MsgAuthenticateResponse, error) { _, err := a.Sequence.Next(ctx) + if err != nil { + return nil, err + } + err = a.Env.EventService.EventManager(ctx).EmitKV("account_bundler_authentication", event.NewAttribute("address", msg.Bundler)) + return &account_abstractionv1.MsgAuthenticateResponse{}, err } diff --git a/x/accounts/testing/counter/counter.go b/x/accounts/testing/counter/counter.go index 8bcdf38262..0e1ab9d783 100644 --- a/x/accounts/testing/counter/counter.go +++ b/x/accounts/testing/counter/counter.go @@ -29,9 +29,7 @@ func NewAccount(d accountstd.Dependencies) (Account, error) { Owner: collections.NewItem(d.SchemaBuilder, OwnerPrefix, "owner", collections.BytesValue), Counter: collections.NewItem(d.SchemaBuilder, CounterPrefix, "counter", collections.Uint64Value), TestStateCodec: collections.NewItem(d.SchemaBuilder, TestStateCodecPrefix, "test_state_codec", codec.CollValue[counterv1.MsgTestDependencies](d.LegacyStateCodec)), - hs: d.HeaderService, addressCodec: d.AddressCodec, - gs: d.GasService, }, nil }