Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(accounts): Add header.Service support #19004

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions runtime/header.go
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 11 additions & 3 deletions x/accounts/internal/implementation/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"

"cosmossdk.io/collections"
"cosmossdk.io/core/header"
"cosmossdk.io/core/store"
"cosmossdk.io/x/accounts/internal/prefixstore"
)
Expand Down Expand Up @@ -51,8 +52,8 @@ func MakeAccountContext(
sender: sender,
whoami: accountAddr,
originalContext: ctx,
moduleExecUntyped: moduleExecUntyped,
moduleExec: moduleExec,
moduleExecUntyped: moduleExecUntyped,
moduleQuery: moduleQuery,
})
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion x/accounts/internal/implementation/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 9 additions & 2 deletions x/accounts/internal/implementation/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@ 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.
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 7 additions & 9 deletions x/accounts/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/accounts/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading