-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(accounts): add Genesis handling (#17802)
Co-authored-by: unknown unknown <unknown@unknown>
- Loading branch information
1 parent
5987d00
commit 5952ecf
Showing
10 changed files
with
3,078 additions
and
13 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
syntax = "proto3"; | ||
|
||
package cosmos.accounts.v1; | ||
|
||
option go_package = "cosmossdk.io/x/accounts/v1"; | ||
|
||
// GenesisState defines the accounts' module's genesis state. | ||
message GenesisState { | ||
// account_number is the latest account number. | ||
uint64 account_number = 1; | ||
// accounts are the genesis accounts. | ||
repeated GenesisAccount accounts = 2; | ||
} | ||
|
||
// GenesisAccount defines an account to be initialized in the genesis state. | ||
message GenesisAccount { | ||
// address is the address of the account. | ||
string address = 1; | ||
// account_type is the account type of the account. | ||
string account_type = 2; | ||
// state is the account state represented as a slice of raw key value byte pairs. | ||
repeated KVPair state = 3; | ||
} | ||
|
||
// KVPair defines a key value pair. | ||
message KVPair { | ||
// key is the key of the pair. | ||
bytes key = 1; | ||
// value is the value of the pair. | ||
bytes value = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package accounts | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"cosmossdk.io/collections" | ||
v1 "cosmossdk.io/x/accounts/v1" | ||
) | ||
|
||
func (k Keeper) ExportState(ctx context.Context) (*v1.GenesisState, error) { | ||
genState := &v1.GenesisState{} | ||
|
||
// get account number | ||
accountNumber, err := k.AccountNumber.Peek(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
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) | ||
if err != nil { | ||
return true, err | ||
} | ||
genState.Accounts = append(genState.Accounts, accState) | ||
return false, nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return genState, nil | ||
} | ||
|
||
func (k Keeper) exportAccount(ctx context.Context, addr []byte, accType string) (*v1.GenesisAccount, error) { | ||
addrString, err := k.addressCodec.BytesToString(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
account := &v1.GenesisAccount{ | ||
Address: addrString, | ||
AccountType: accType, | ||
} | ||
rng := new(collections.Range[[]byte]). | ||
Prefix(addr) | ||
err = k.AccountsState.Walk(ctx, rng, func(key, value []byte) (stop bool, err error) { | ||
account.State = append(account.State, &v1.KVPair{ | ||
Key: key, | ||
Value: value, | ||
}) | ||
return false, nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return account, nil | ||
} | ||
|
||
func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) error { | ||
err := k.AccountNumber.Set(ctx, genState.AccountNumber) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// import accounts | ||
for _, acc := range genState.Accounts { | ||
err = k.importAccount(ctx, acc) | ||
if err != nil { | ||
return fmt.Errorf("%w: %s", err, acc.Address) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (k Keeper) importAccount(ctx context.Context, acc *v1.GenesisAccount) error { | ||
// TODO: maybe check if impl exists? | ||
addrBytes, err := k.addressCodec.StringToBytes(acc.Address) | ||
if err != nil { | ||
return err | ||
} | ||
err = k.AccountsByType.Set(ctx, addrBytes, acc.AccountType) | ||
if err != nil { | ||
return err | ||
} | ||
for _, kv := range acc.State { | ||
err = k.AccountsState.Set(ctx, kv.Key, kv.Value) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package accounts | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
"google.golang.org/protobuf/types/known/wrapperspb" | ||
|
||
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" | ||
"cosmossdk.io/collections" | ||
"cosmossdk.io/collections/colltest" | ||
"cosmossdk.io/x/accounts/internal/implementation" | ||
) | ||
|
||
func TestGenesis(t *testing.T) { | ||
sb := collections.NewSchemaBuilderFromAccessor(implementation.OpenKVStore) | ||
acc := NewTestAccount(sb) | ||
|
||
k, ctx := newKeeper(t, map[string]implementation.Account{ | ||
"test": acc, | ||
}) | ||
k.queryModuleFunc = func(ctx context.Context, _ proto.Message) (proto.Message, error) { | ||
return &bankv1beta1.QueryBalanceResponse{}, nil | ||
} | ||
|
||
// we init two accounts of the same type | ||
|
||
// we set counter to 10 | ||
_, addr1, err := k.Init(ctx, "test", []byte("sender"), &emptypb.Empty{}) | ||
require.NoError(t, err) | ||
_, err = k.Execute(ctx, addr1, []byte("sender"), &wrapperspb.UInt64Value{Value: 10}) | ||
require.NoError(t, err) | ||
|
||
// we set counter to 20 | ||
_, addr2, err := k.Init(ctx, "test", []byte("sender"), &emptypb.Empty{}) | ||
require.NoError(t, err) | ||
_, err = k.Execute(ctx, addr2, []byte("sender"), &wrapperspb.UInt64Value{Value: 20}) | ||
require.NoError(t, err) | ||
|
||
// export state | ||
state, err := k.ExportState(ctx) | ||
require.NoError(t, err) | ||
|
||
// reset state | ||
_, ctx = colltest.MockStore() | ||
err = k.ImportState(ctx, state) | ||
require.NoError(t, err) | ||
|
||
// if genesis import went fine, we should be able to query the accounts | ||
// and get the expected values. | ||
resp, err := k.Query(ctx, addr1, &wrapperspb.DoubleValue{}) | ||
require.NoError(t, err) | ||
require.Equal(t, &wrapperspb.UInt64Value{Value: 10}, resp) | ||
|
||
resp, err = k.Query(ctx, addr2, &wrapperspb.DoubleValue{}) | ||
require.NoError(t, err) | ||
require.Equal(t, &wrapperspb.UInt64Value{Value: 20}, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.