Skip to content

Commit

Permalink
add test for upgrade logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sontrinh16 committed Jun 10, 2024
1 parent 610ef6f commit 7388cbd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
19 changes: 13 additions & 6 deletions simapp/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ func (app SimApp) RegisterUpgradeHandlers() {
UpgradeName,
func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) {
// sync accounts and auth module account number
currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx)
if err != nil {
return nil, err
}

err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum)
err := app.syncAccoutnNumber(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -61,3 +56,15 @@ func (app SimApp) RegisterUpgradeHandlers() {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}
}

func (app SimApp) syncAccoutnNumber(ctx context.Context) error {
// sync accounts and auth module account number
currentAccNum, err := app.AuthKeeper.RemoveLegacyAccountNumber(ctx)
if err != nil {
return err
}

err = app.AccountsKeeper.InitAccountNumberSeqUnsafe(ctx, currentAccNum)

return err
}
51 changes: 51 additions & 0 deletions simapp/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package simapp

import (
"testing"

"cosmossdk.io/collections"
authtypes "cosmossdk.io/x/auth/types"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
"github.com/stretchr/testify/require"
)

// TestSyncAccountNumber tests if accounts module account number is set correctly with the value get from auth.
// Also check if the store entry for auth GlobalAccountNumberKey is successfully deleted.
func TestSyncAccountNumber(t *testing.T) {
app := Setup(t, true)
ctx := app.NewUncachedContext(true, cmtproto.Header{})

bytesKey := authtypes.GlobalAccountNumberKey
store := app.AuthKeeper.KVStoreService.OpenKVStore(ctx)

// initially there is no value set yet
v, err := store.Get(bytesKey)
require.NoError(t, err)
require.Nil(t, v)

// set value for legacy account number
v, err = collections.Uint64Value.Encode(10)
require.NoError(t, err)
err = store.Set(bytesKey, v)
require.NoError(t, err)

// make sure value are updated
v, err = store.Get(bytesKey)
require.NoError(t, err)
require.NotEmpty(t, v)
num, err := collections.Uint64Value.Decode(v)
require.NoError(t, err)
require.Equal(t, uint64(10), num)

app.syncAccoutnNumber(ctx)

// make sure the DB entry for this key is deleted
v, err = store.Get(bytesKey)
require.NoError(t, err)
require.Nil(t, v)

// check if accounts's account number is updated
currentNum, err := app.AccountsKeeper.AccountNumber.Peek(ctx)
require.NoError(t, err)
require.Equal(t, uint64(10), currentNum)
}

0 comments on commit 7388cbd

Please sign in to comment.