Skip to content

Commit 1e7611f

Browse files
testinginprodunknown unknownalexanderbez
authored
feat(accounts): Add header.Service support (#19004)
Co-authored-by: unknown unknown <unknown@unknown> Co-authored-by: Aleksandr Bezobchuk <[email protected]>
1 parent 1aaad3e commit 1e7611f

File tree

8 files changed

+48
-17
lines changed

8 files changed

+48
-17
lines changed

runtime/header.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package runtime
2+
3+
import (
4+
"context"
5+
6+
"cosmossdk.io/core/header"
7+
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
)
10+
11+
var _ header.Service = (*HeaderService)(nil)
12+
13+
type HeaderService struct{}
14+
15+
func (h HeaderService) GetHeaderInfo(ctx context.Context) header.Info {
16+
return sdk.UnwrapSDKContext(ctx).HeaderInfo()
17+
}

simapp/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ func NewSimApp(
288288
accountsKeeper, err := accounts.NewKeeper(
289289
runtime.NewKVStoreService(keys[accounts.StoreKey]),
290290
runtime.EventService{},
291+
runtime.HeaderService{},
291292
runtime.BranchService{},
292293
app.AuthKeeper.AddressCodec(),
293294
appCodec,

x/accounts/internal/implementation/context.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/binary"
66

77
"cosmossdk.io/collections"
8+
"cosmossdk.io/core/header"
89
"cosmossdk.io/core/store"
910
"cosmossdk.io/x/accounts/internal/prefixstore"
1011
)
@@ -51,8 +52,8 @@ func MakeAccountContext(
5152
sender: sender,
5253
whoami: accountAddr,
5354
originalContext: ctx,
54-
moduleExecUntyped: moduleExecUntyped,
5555
moduleExec: moduleExec,
56+
moduleExecUntyped: moduleExecUntyped,
5657
moduleQuery: moduleQuery,
5758
})
5859
}
@@ -107,8 +108,8 @@ func QueryModule[Resp any, RespProto ProtoMsgG[Resp], Req any, ReqProto ProtoMsg
107108
return resp, nil
108109
}
109110

110-
// OpenKVStore returns the prefixed store for the account given the context.
111-
func OpenKVStore(ctx context.Context) store.KVStore {
111+
// openKVStore returns the prefixed store for the account given the context.
112+
func openKVStore(ctx context.Context) store.KVStore {
112113
return ctx.Value(contextKey{}).(contextValue).store
113114
}
114115

@@ -121,3 +122,10 @@ func Sender(ctx context.Context) []byte {
121122
func Whoami(ctx context.Context) []byte {
122123
return ctx.Value(contextKey{}).(contextValue).whoami
123124
}
125+
126+
type headerService struct{ header.Service }
127+
128+
func (h headerService) GetHeaderInfo(ctx context.Context) header.Info {
129+
originalContext := ctx.Value(contextKey{}).(contextValue).originalContext
130+
return h.Service.GetHeaderInfo(originalContext)
131+
}

x/accounts/internal/implementation/context_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestMakeAccountContext(t *testing.T) {
1616
storeService, originalContext := colltest.MockStore()
1717
accountAddr := []byte("accountAddr")
1818
sender := []byte("sender")
19-
sb := collections.NewSchemaBuilderFromAccessor(OpenKVStore)
19+
sb := collections.NewSchemaBuilderFromAccessor(openKVStore)
2020

2121
accountCtx := MakeAccountContext(originalContext, storeService, 1, accountAddr, sender, nil, nil, nil)
2222

x/accounts/internal/implementation/implementation.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,33 @@ import (
66

77
"cosmossdk.io/collections"
88
"cosmossdk.io/core/address"
9+
"cosmossdk.io/core/header"
910
)
1011

1112
// Dependencies are passed to the constructor of a smart account.
1213
type Dependencies struct {
1314
SchemaBuilder *collections.SchemaBuilder
1415
AddressCodec address.Codec
16+
HeaderService header.Service
1517
}
1618

1719
// AccountCreatorFunc is a function that creates an account.
1820
type AccountCreatorFunc = func(deps Dependencies) (string, Account, error)
1921

2022
// MakeAccountsMap creates a map of account names to account implementations
2123
// from a list of account creator functions.
22-
func MakeAccountsMap(addressCodec address.Codec, accounts []AccountCreatorFunc) (map[string]Implementation, error) {
24+
func MakeAccountsMap(
25+
addressCodec address.Codec,
26+
hs header.Service,
27+
accounts []AccountCreatorFunc,
28+
) (map[string]Implementation, error) {
2329
accountsMap := make(map[string]Implementation, len(accounts))
2430
for _, makeAccount := range accounts {
25-
stateSchemaBuilder := collections.NewSchemaBuilderFromAccessor(OpenKVStore)
31+
stateSchemaBuilder := collections.NewSchemaBuilderFromAccessor(openKVStore)
2632
deps := Dependencies{
2733
SchemaBuilder: stateSchemaBuilder,
2834
AddressCodec: addressCodec,
35+
HeaderService: headerService{hs},
2936
}
3037
name, accountInterface, err := makeAccount(deps)
3138
if err != nil {

x/accounts/internal/implementation/implementation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestImplementation(t *testing.T) {
14-
impl, err := newImplementation(collections.NewSchemaBuilderFromAccessor(OpenKVStore), TestAccount{})
14+
impl, err := newImplementation(collections.NewSchemaBuilderFromAccessor(openKVStore), TestAccount{})
1515
require.NoError(t, err)
1616

1717
ctx := context.Background()

x/accounts/keeper.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"cosmossdk.io/core/address"
1616
"cosmossdk.io/core/branch"
1717
"cosmossdk.io/core/event"
18+
"cosmossdk.io/core/header"
1819
"cosmossdk.io/core/store"
1920
"cosmossdk.io/x/accounts/accountstd"
2021
"cosmossdk.io/x/accounts/internal/implementation"
@@ -54,9 +55,6 @@ type SignerProvider interface {
5455
GetMsgV1Signers(msg gogoproto.Message) ([][]byte, proto.Message, error)
5556
}
5657

57-
// BranchExecutor defines an interface used to execute ops in a branch.
58-
type BranchExecutor = branch.Service
59-
6058
type InterfaceRegistry interface {
6159
RegisterInterface(name string, iface any, impls ...gogoproto.Message)
6260
RegisterImplementations(iface any, impls ...gogoproto.Message)
@@ -65,7 +63,8 @@ type InterfaceRegistry interface {
6563
func NewKeeper(
6664
ss store.KVStoreService,
6765
es event.Service,
68-
bs BranchExecutor,
66+
hs header.Service,
67+
bs branch.Service,
6968
addressCodec address.Codec,
7069
signerProvider SignerProvider,
7170
execRouter MsgRouter,
@@ -77,12 +76,11 @@ func NewKeeper(
7776
keeper := Keeper{
7877
storeService: ss,
7978
eventService: es,
80-
branchExecutor: bs,
8179
addressCodec: addressCodec,
82-
signerProvider: signerProvider,
80+
branchExecutor: bs,
8381
msgRouter: execRouter,
82+
signerProvider: signerProvider,
8483
queryRouter: queryRouter,
85-
Schema: collections.Schema{},
8684
AccountNumber: collections.NewSequence(sb, AccountNumberKey, "account_number"),
8785
AccountsByType: collections.NewMap(sb, AccountTypeKeyPrefix, "accounts_by_type", collections.BytesKey, collections.StringValue),
8886
AccountByNumber: collections.NewMap(sb, AccountByNumber, "account_by_number", collections.BytesKey, collections.Uint64Value),
@@ -94,7 +92,7 @@ func NewKeeper(
9492
return Keeper{}, err
9593
}
9694
keeper.Schema = schema
97-
keeper.accounts, err = implementation.MakeAccountsMap(keeper.addressCodec, accounts)
95+
keeper.accounts, err = implementation.MakeAccountsMap(keeper.addressCodec, hs, accounts)
9896
if err != nil {
9997
return Keeper{}, err
10098
}
@@ -107,7 +105,7 @@ type Keeper struct {
107105
storeService store.KVStoreService
108106
eventService event.Service
109107
addressCodec address.Codec
110-
branchExecutor BranchExecutor
108+
branchExecutor branch.Service
111109
msgRouter MsgRouter
112110
signerProvider SignerProvider
113111
queryRouter QueryRouter

x/accounts/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (i interfaceRegistry) RegisterImplementations(any, ...gogoproto.Message) {}
4747
func newKeeper(t *testing.T, accounts ...implementation.AccountCreatorFunc) (Keeper, context.Context) {
4848
t.Helper()
4949
ss, ctx := colltest.MockStore()
50-
m, err := NewKeeper(ss, eventService{}, nil, addressCodec{}, nil, nil, nil, interfaceRegistry{}, accounts...)
50+
m, err := NewKeeper(ss, eventService{}, nil, nil, addressCodec{}, nil, nil, nil, interfaceRegistry{}, accounts...)
5151
require.NoError(t, err)
5252
return m, ctx
5353
}

0 commit comments

Comments
 (0)