Skip to content

Commit

Permalink
feat(accounts): use account number as state prefix for account state (#…
Browse files Browse the repository at this point in the history
…18664)

Co-authored-by: unknown unknown <unknown@unknown>
  • Loading branch information
testinginprod and unknown unknown authored Dec 11, 2023
1 parent 8b894f7 commit 39865d8
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 91 deletions.
154 changes: 107 additions & 47 deletions api/cosmos/accounts/v1/genesis.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion proto/cosmos/accounts/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ message GenesisAccount {
string address = 1;
// account_type is the account type of the account.
string account_type = 2;
// account_number is the account number of the account.
uint64 account_number = 3;
// state is the account state represented as a slice of raw key value byte pairs.
repeated KVPair state = 3;
repeated KVPair state = 4;
}

// KVPair defines a key value pair.
Expand Down
2 changes: 1 addition & 1 deletion store/root/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/commitment"
"cosmossdk.io/store/v2/commitment/iavl"
"cosmossdk.io/store/v2/storage"
"cosmossdk.io/store/v2/pruning"
"cosmossdk.io/store/v2/storage"
"cosmossdk.io/store/v2/storage/sqlite"
)

Expand Down
29 changes: 19 additions & 10 deletions x/accounts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ func (k Keeper) ExportState(ctx context.Context) (*v1.GenesisState, error) {

genState.AccountNumber = accountNumber

err = k.AccountsByType.Walk(ctx, nil, func(key []byte, value string) (stop bool, err error) {
accState, err := k.exportAccount(ctx, key, value)
err = k.AccountsByType.Walk(ctx, nil, func(accAddr []byte, accType string) (stop bool, err error) {
accNum, err := k.AccountByNumber.Get(ctx, accAddr)
if err != nil {
return true, err
}
accState, err := k.exportAccount(ctx, accAddr, accType, accNum)
if err != nil {
return true, err
}
Expand All @@ -34,20 +38,21 @@ func (k Keeper) ExportState(ctx context.Context) (*v1.GenesisState, error) {
return genState, nil
}

func (k Keeper) exportAccount(ctx context.Context, addr []byte, accType string) (*v1.GenesisAccount, error) {
func (k Keeper) exportAccount(ctx context.Context, addr []byte, accType string, accNum uint64) (*v1.GenesisAccount, error) {
addrString, err := k.addressCodec.BytesToString(addr)
if err != nil {
return nil, err
}
account := &v1.GenesisAccount{
Address: addrString,
AccountType: accType,
Address: addrString,
AccountType: accType,
AccountNumber: accNum,
State: nil,
}
rng := new(collections.Range[[]byte]).
Prefix(addr)
err = k.AccountsState.Walk(ctx, rng, func(key, value []byte) (stop bool, err error) {
rng := collections.NewPrefixedPairRange[uint64, []byte](accNum)
err = k.AccountsState.Walk(ctx, rng, func(key collections.Pair[uint64, []byte], value []byte) (stop bool, err error) {
account.State = append(account.State, &v1.KVPair{
Key: key,
Key: key.K2(),
Value: value,
})
return false, nil
Expand Down Expand Up @@ -84,8 +89,12 @@ func (k Keeper) importAccount(ctx context.Context, acc *v1.GenesisAccount) error
if err != nil {
return err
}
err = k.AccountByNumber.Set(ctx, addrBytes, acc.AccountNumber)
if err != nil {
return err
}
for _, kv := range acc.State {
err = k.AccountsState.Set(ctx, kv.Key, kv.Value)
err = k.AccountsState.Set(ctx, collections.Join(acc.AccountNumber, kv.Key), kv.Value)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 39865d8

Please sign in to comment.