From abeb7eee3b644d8e5551089e625a3dacd79a0e89 Mon Sep 17 00:00:00 2001 From: son trinh Date: Wed, 12 Jun 2024 23:25:02 +0700 Subject: [PATCH] fix (x/accounts): Fix genesis condition check (#20645) --- x/accounts/genesis.go | 2 +- x/accounts/genesis_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/x/accounts/genesis.go b/x/accounts/genesis.go index 047345789c..7279a25cf4 100644 --- a/x/accounts/genesis.go +++ b/x/accounts/genesis.go @@ -74,7 +74,7 @@ func (k Keeper) ImportState(ctx context.Context, genState *v1.GenesisState) erro // we set the latest account number only if there were any genesis accounts, otherwise // we leave it unset. - if genState.Accounts != nil { + if len(genState.Accounts) != 0 { // due to sequence semantics, we store the next account number. err = k.AccountNumber.Set(ctx, lastAccountNumber+1) if err != nil { diff --git a/x/accounts/genesis_test.go b/x/accounts/genesis_test.go index b9056827eb..14c2470d84 100644 --- a/x/accounts/genesis_test.go +++ b/x/accounts/genesis_test.go @@ -84,6 +84,23 @@ func TestGenesis(t *testing.T) { // AccountNumber should be set to the highest account number in the genesis state + 2 // (one is the sequence offset, the other is the genesis account being added through init msg) require.Equal(t, state.Accounts[0].AccountNumber+2, currentAccNum) + + // Test when init with empty accounts list, account number is not modified + // make genesis state accounts empty + state.Accounts = []*v1.GenesisAccount{} + + // set another value for account number + err = k.AccountNumber.Set(ctx, uint64(10)) + require.NoError(t, err) + + err = k.ImportState(ctx, state) + require.NoError(t, err) + + currentAccNum, err = k.AccountNumber.Peek(ctx) + require.NoError(t, err) + // AccountNumber should be 10 + 1 + // (one is the genesis account being added through init msg) + require.Equal(t, uint64(11), currentAccNum) } func TestImportAccountError(t *testing.T) {