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): use account number as state prefix for account state #18664

Merged
merged 2 commits into from
Dec 11, 2023
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
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 @@
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;

Check failure on line 22 in proto/cosmos/accounts/v1/genesis.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "3" with name "account_number" on message "GenesisAccount" changed option "json_name" from "state" to "accountNumber".

Check failure on line 22 in proto/cosmos/accounts/v1/genesis.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "3" on message "GenesisAccount" changed label from "repeated" to "optional".

Check failure on line 22 in proto/cosmos/accounts/v1/genesis.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "3" on message "GenesisAccount" changed type from "message" to "uint64".

Check failure on line 22 in proto/cosmos/accounts/v1/genesis.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "3" on message "GenesisAccount" changed name from "state" to "account_number".
// 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
Loading