feat(accounts): Add header.Service support (#19004)
Co-authored-by: unknown unknown <unknown@unknown> Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
parent
1aaad3e817
commit
1e7611faab
17
runtime/header.go
Normal file
17
runtime/header.go
Normal file
@ -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()
|
||||
}
|
||||
@ -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,
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user