From 30ba01dc91bf62fb0b8551968c59608177a74dfc Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Tue, 9 Jan 2024 23:48:10 +0100 Subject: [PATCH 1/2] add header service support to accounts --- x/accounts/internal/implementation/context.go | 14 +++++++++++--- .../internal/implementation/context_test.go | 2 +- .../internal/implementation/implementation.go | 11 +++++++++-- .../implementation/implementation_test.go | 2 +- x/accounts/keeper.go | 16 +++++++--------- x/accounts/utils_test.go | 2 +- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/x/accounts/internal/implementation/context.go b/x/accounts/internal/implementation/context.go index 38cc0752101..f6e340ada30 100644 --- a/x/accounts/internal/implementation/context.go +++ b/x/accounts/internal/implementation/context.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "cosmossdk.io/collections" + "cosmossdk.io/core/header" "cosmossdk.io/core/store" "cosmossdk.io/x/accounts/internal/prefixstore" ) @@ -51,8 +52,8 @@ func MakeAccountContext( sender: sender, whoami: accountAddr, originalContext: ctx, - moduleExecUntyped: moduleExecUntyped, moduleExec: moduleExec, + moduleExecUntyped: moduleExecUntyped, moduleQuery: moduleQuery, }) } @@ -107,8 +108,8 @@ func QueryModule[Resp any, RespProto ProtoMsgG[Resp], Req any, ReqProto ProtoMsg return resp, nil } -// OpenKVStore returns the prefixed store for the account given the context. -func OpenKVStore(ctx context.Context) store.KVStore { +// openKVStore returns the prefixed store for the account given the context. +func openKVStore(ctx context.Context) store.KVStore { return ctx.Value(contextKey{}).(contextValue).store } @@ -121,3 +122,10 @@ func Sender(ctx context.Context) []byte { func Whoami(ctx context.Context) []byte { return ctx.Value(contextKey{}).(contextValue).whoami } + +type headerService struct{ header.Service } + +func (h headerService) GetHeaderInfo(ctx context.Context) header.Info { + originalContext := ctx.Value(contextKey{}).(contextValue).originalContext + return h.Service.GetHeaderInfo(originalContext) +} diff --git a/x/accounts/internal/implementation/context_test.go b/x/accounts/internal/implementation/context_test.go index 2c8db80ca2c..7aa33575eaa 100644 --- a/x/accounts/internal/implementation/context_test.go +++ b/x/accounts/internal/implementation/context_test.go @@ -16,7 +16,7 @@ func TestMakeAccountContext(t *testing.T) { storeService, originalContext := colltest.MockStore() accountAddr := []byte("accountAddr") sender := []byte("sender") - sb := collections.NewSchemaBuilderFromAccessor(OpenKVStore) + sb := collections.NewSchemaBuilderFromAccessor(openKVStore) accountCtx := MakeAccountContext(originalContext, storeService, 1, accountAddr, sender, nil, nil, nil) diff --git a/x/accounts/internal/implementation/implementation.go b/x/accounts/internal/implementation/implementation.go index 4023ef608af..024381613dd 100644 --- a/x/accounts/internal/implementation/implementation.go +++ b/x/accounts/internal/implementation/implementation.go @@ -6,12 +6,14 @@ import ( "cosmossdk.io/collections" "cosmossdk.io/core/address" + "cosmossdk.io/core/header" ) // Dependencies are passed to the constructor of a smart account. type Dependencies struct { SchemaBuilder *collections.SchemaBuilder AddressCodec address.Codec + HeaderService header.Service } // AccountCreatorFunc is a function that creates an account. @@ -19,13 +21,18 @@ type AccountCreatorFunc = func(deps Dependencies) (string, Account, error) // MakeAccountsMap creates a map of account names to account implementations // from a list of account creator functions. -func MakeAccountsMap(addressCodec address.Codec, accounts []AccountCreatorFunc) (map[string]Implementation, error) { +func MakeAccountsMap( + addressCodec address.Codec, + hs header.Service, + accounts []AccountCreatorFunc, +) (map[string]Implementation, error) { accountsMap := make(map[string]Implementation, len(accounts)) for _, makeAccount := range accounts { - stateSchemaBuilder := collections.NewSchemaBuilderFromAccessor(OpenKVStore) + stateSchemaBuilder := collections.NewSchemaBuilderFromAccessor(openKVStore) deps := Dependencies{ SchemaBuilder: stateSchemaBuilder, AddressCodec: addressCodec, + HeaderService: headerService{hs}, } name, accountInterface, err := makeAccount(deps) if err != nil { diff --git a/x/accounts/internal/implementation/implementation_test.go b/x/accounts/internal/implementation/implementation_test.go index 1aeb5ddd202..696420f0092 100644 --- a/x/accounts/internal/implementation/implementation_test.go +++ b/x/accounts/internal/implementation/implementation_test.go @@ -11,7 +11,7 @@ import ( ) func TestImplementation(t *testing.T) { - impl, err := newImplementation(collections.NewSchemaBuilderFromAccessor(OpenKVStore), TestAccount{}) + impl, err := newImplementation(collections.NewSchemaBuilderFromAccessor(openKVStore), TestAccount{}) require.NoError(t, err) ctx := context.Background() diff --git a/x/accounts/keeper.go b/x/accounts/keeper.go index f2561e5e60c..b7ffe88f334 100644 --- a/x/accounts/keeper.go +++ b/x/accounts/keeper.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" + "cosmossdk.io/core/header" gogoproto "github.com/cosmos/gogoproto/proto" "google.golang.org/protobuf/proto" @@ -54,9 +55,6 @@ type SignerProvider interface { GetMsgV1Signers(msg gogoproto.Message) ([][]byte, proto.Message, error) } -// BranchExecutor defines an interface used to execute ops in a branch. -type BranchExecutor = branch.Service - type InterfaceRegistry interface { RegisterInterface(name string, iface any, impls ...gogoproto.Message) RegisterImplementations(iface any, impls ...gogoproto.Message) @@ -65,7 +63,8 @@ type InterfaceRegistry interface { func NewKeeper( ss store.KVStoreService, es event.Service, - bs BranchExecutor, + hs header.Service, + bs branch.Service, addressCodec address.Codec, signerProvider SignerProvider, execRouter MsgRouter, @@ -77,12 +76,11 @@ func NewKeeper( keeper := Keeper{ storeService: ss, eventService: es, - branchExecutor: bs, addressCodec: addressCodec, - signerProvider: signerProvider, + branchExecutor: bs, msgRouter: execRouter, + signerProvider: signerProvider, queryRouter: queryRouter, - Schema: collections.Schema{}, AccountNumber: collections.NewSequence(sb, AccountNumberKey, "account_number"), AccountsByType: collections.NewMap(sb, AccountTypeKeyPrefix, "accounts_by_type", collections.BytesKey, collections.StringValue), AccountByNumber: collections.NewMap(sb, AccountByNumber, "account_by_number", collections.BytesKey, collections.Uint64Value), @@ -94,7 +92,7 @@ func NewKeeper( return Keeper{}, err } keeper.Schema = schema - keeper.accounts, err = implementation.MakeAccountsMap(keeper.addressCodec, accounts) + keeper.accounts, err = implementation.MakeAccountsMap(keeper.addressCodec, hs, accounts) if err != nil { return Keeper{}, err } @@ -107,7 +105,7 @@ type Keeper struct { storeService store.KVStoreService eventService event.Service addressCodec address.Codec - branchExecutor BranchExecutor + branchExecutor branch.Service msgRouter MsgRouter signerProvider SignerProvider queryRouter QueryRouter diff --git a/x/accounts/utils_test.go b/x/accounts/utils_test.go index 68adc2d94c6..bcef6b963f2 100644 --- a/x/accounts/utils_test.go +++ b/x/accounts/utils_test.go @@ -47,7 +47,7 @@ func (i interfaceRegistry) RegisterImplementations(any, ...gogoproto.Message) {} func newKeeper(t *testing.T, accounts ...implementation.AccountCreatorFunc) (Keeper, context.Context) { t.Helper() ss, ctx := colltest.MockStore() - m, err := NewKeeper(ss, eventService{}, nil, addressCodec{}, nil, nil, nil, interfaceRegistry{}, accounts...) + m, err := NewKeeper(ss, eventService{}, nil, nil, addressCodec{}, nil, nil, nil, interfaceRegistry{}, accounts...) require.NoError(t, err) return m, ctx } From db63d57c27cf8fc18f3bbb84e4d1feeb7d3642aa Mon Sep 17 00:00:00 2001 From: unknown unknown Date: Wed, 10 Jan 2024 12:48:02 +0100 Subject: [PATCH 2/2] add header service support to accounts --- runtime/header.go | 17 +++++++++++++++++ simapp/app.go | 1 + x/accounts/keeper.go | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 runtime/header.go diff --git a/runtime/header.go b/runtime/header.go new file mode 100644 index 00000000000..5016912d9a7 --- /dev/null +++ b/runtime/header.go @@ -0,0 +1,17 @@ +package runtime + +import ( + "context" + + "cosmossdk.io/core/header" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var _ header.Service = (*HeaderService)(nil) + +type HeaderService struct{} + +func (h HeaderService) GetHeaderInfo(ctx context.Context) header.Info { + return sdk.UnwrapSDKContext(ctx).HeaderInfo() +} diff --git a/simapp/app.go b/simapp/app.go index bb31d24f844..12daf063721 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -288,6 +288,7 @@ func NewSimApp( accountsKeeper, err := accounts.NewKeeper( runtime.NewKVStoreService(keys[accounts.StoreKey]), runtime.EventService{}, + runtime.HeaderService{}, runtime.BranchService{}, app.AuthKeeper.AddressCodec(), appCodec, diff --git a/x/accounts/keeper.go b/x/accounts/keeper.go index b7ffe88f334..d29b291f0a1 100644 --- a/x/accounts/keeper.go +++ b/x/accounts/keeper.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" - "cosmossdk.io/core/header" gogoproto "github.com/cosmos/gogoproto/proto" "google.golang.org/protobuf/proto" @@ -16,6 +15,7 @@ import ( "cosmossdk.io/core/address" "cosmossdk.io/core/branch" "cosmossdk.io/core/event" + "cosmossdk.io/core/header" "cosmossdk.io/core/store" "cosmossdk.io/x/accounts/accountstd" "cosmossdk.io/x/accounts/internal/implementation"