diff --git a/runtime/header.go b/runtime/header.go new file mode 100644 index 000000000000..5016912d9a71 --- /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 bb31d24f8445..12daf0637215 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/internal/implementation/context.go b/x/accounts/internal/implementation/context.go index 38cc07521013..f6e340ada306 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 2c8db80ca2cf..7aa33575eaa6 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 4023ef608af4..024381613ddf 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 1aeb5ddd2025..696420f00924 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 f2561e5e60c2..d29b291f0a1f 100644 --- a/x/accounts/keeper.go +++ b/x/accounts/keeper.go @@ -15,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" @@ -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 68adc2d94c6e..bcef6b963f26 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 }