Skip to content

Commit

Permalink
chore: address the review comments++
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Aug 24, 2022
1 parent 62498a3 commit c6654a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions x/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {

// should all pass
var completionTime time.Time
for i := uint32(0); i < maxEntries; i++ {
for i := int64(0); i < int64(maxEntries); i++ {
var err error
ctx = ctx.WithBlockHeight(int64(i))
ctx = ctx.WithBlockHeight(i)
completionTime, err = app.StakingKeeper.Undelegate(ctx, addrDels[0], addrVals[0], math.LegacyNewDec(1))
require.NoError(t, err)
}
Expand Down
27 changes: 14 additions & 13 deletions x/staking/migrations/v4/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestMigrate(t *testing.T) {
valAddrs := sims.ConvertAddrsToValAddrs(accAddrs)
valAddr := valAddrs[0]

err := createOldStateUnbondind(t, duplicateCreationHeight, valAddr, accAddr, cdc, store)
// creating 10 ubdEntries with same height and 10 ubdEntries with different creation height
err := createOldStateUnbonding(t, duplicateCreationHeight, valAddr, accAddr, cdc, store)
require.NoError(t, err)

legacySubspace := newMockSubspace(types.DefaultParams())
Expand Down Expand Up @@ -77,7 +78,7 @@ func TestMigrate(t *testing.T) {
require.NoError(t, cdc.Unmarshal(bz, &res))
require.Equal(t, legacySubspace.ps, res)

// checking the updated balnace for duplicateCreationHeight
// checking the updated balance for duplicateCreationHeight
for _, ubdEntry := range ubd.Entries {
if ubdEntry.CreationHeight == duplicateCreationHeight {
require.Equal(t, sdk.NewInt(100*10), ubdEntry.Balance)
Expand All @@ -93,12 +94,12 @@ func TestMigrate(t *testing.T) {
}
}

// createOldStateUnbondind will create the ubd entries with duplicate heights
// 10 duplicate heights and 10 unique ubd creation height
func createOldStateUnbondind(t *testing.T, creationHeight int64, valAddr sdk.ValAddress, accAddr sdk.AccAddress, cdc codec.BinaryCodec, store storetypes.KVStore) error {
// createOldStateUnbonding will create the ubd entries with duplicate heights
// 10 duplicate heights and 10 unique ubd with creation height
func createOldStateUnbonding(t *testing.T, creationHeight int64, valAddr sdk.ValAddress, accAddr sdk.AccAddress, cdc codec.BinaryCodec, store storetypes.KVStore) error {
unbondBalance := sdk.NewInt(100)
completionTime := time.Now()
udbEntries := make([]types.UnbondingDelegationEntry, 0, 10)
ubdEntries := make([]types.UnbondingDelegationEntry, 0, 10)

for i := int64(0); i < 10; i++ {
ubdEntry := types.UnbondingDelegationEntry{
Expand All @@ -107,17 +108,17 @@ func createOldStateUnbondind(t *testing.T, creationHeight int64, valAddr sdk.Val
InitialBalance: unbondBalance,
CompletionTime: completionTime,
}
udbEntries = append(udbEntries, ubdEntry)
ubdEntries = append(ubdEntries, ubdEntry)
// creating more entries for testing the creation_heights
ubdEntry.CreationHeight = i + 2
ubdEntry.CompletionTime = completionTime.Add(time.Minute * 10)
udbEntries = append(udbEntries, ubdEntry)
ubdEntries = append(ubdEntries, ubdEntry)
}

ubd := types.UnbondingDelegation{
ValidatorAddress: valAddr.String(),
DelegatorAddress: accAddr.String(),
Entries: udbEntries,
Entries: ubdEntries,
}

// set the unbond delegation with validator and delegator
Expand All @@ -128,11 +129,11 @@ func createOldStateUnbondind(t *testing.T, creationHeight int64, valAddr sdk.Val
}

func getUBD(t *testing.T, accAddr sdk.AccAddress, valAddr sdk.ValAddress, store storetypes.KVStore, cdc codec.BinaryCodec) types.UnbondingDelegation {
// get the unbondind delegations
var udbRes types.UnbondingDelegation
// get the unbonding delegations
var ubdRes types.UnbondingDelegation
ubdbz := store.Get(getUBDKey(accAddr, valAddr))
require.NoError(t, cdc.Unmarshal(ubdbz, &udbRes))
return udbRes
require.NoError(t, cdc.Unmarshal(ubdbz, &ubdRes))
return ubdRes
}

func getUBDKey(accAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte {
Expand Down
8 changes: 4 additions & 4 deletions x/staking/migrations/v4/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Binar
return err
}

// migrate unbondig delegations
if err := migrateUBD(ctx, store, cdc, legacySubspace); err != nil {
// migrate unbonding delegations
if err := migrateUBDEntries(ctx, store, cdc, legacySubspace); err != nil {
return err
}

Expand All @@ -41,9 +41,9 @@ func migrateParams(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCo
return nil
}

// migrateUBD will remove the ubdEntries with same creation_height
// migrateUBDEntries will remove the ubdEntries with same creation_height
// and create a new ubdEntry with updated balance and initial_balance
func migrateUBD(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, legacySubspace exported.Subspace) error {
func migrateUBDEntries(ctx sdk.Context, store storetypes.KVStore, cdc codec.BinaryCodec, legacySubspace exported.Subspace) error {
iterator := sdk.KVStorePrefixIterator(store, types.UnbondingDelegationKey)
defer iterator.Close()

Expand Down

0 comments on commit c6654a8

Please sign in to comment.