Skip to content

Commit

Permalink
feat: icrease tests coverage and performance contextualizing more the…
Browse files Browse the repository at this point in the history
… index
  • Loading branch information
emidev98 committed Feb 27, 2024
1 parent a2e797c commit bd4162d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
107 changes: 107 additions & 0 deletions x/alliance/keeper/tests/unbonding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package tests_test

import (
"testing"
"time"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
test_helpers "github.com/terra-money/alliance/app"
"github.com/terra-money/alliance/x/alliance/types"
)

func TestUnbondingMethods(t *testing.T) {
// Setup the context with an alliance asset
app, ctx := createTestContext(t)
startTime := time.Now()
ctx = ctx.WithBlockTime(startTime).WithBlockHeight(1)
allianceAsset := types.AllianceAsset{
Denom: AllianceDenom,
RewardWeight: math.LegacyNewDec(1),
TakeRate: math.LegacyNewDec(0),
TotalTokens: math.ZeroInt(),
TotalValidatorShares: math.LegacyZeroDec(),
RewardStartTime: startTime,
RewardChangeRate: math.LegacyNewDec(0),
RewardChangeInterval: time.Duration(0),
LastRewardChangeTime: startTime,
RewardWeightRange: types.RewardWeightRange{Min: math.LegacyNewDec(0), Max: math.LegacyNewDec(1)},
IsInitialized: true,
}
app.AllianceKeeper.SetAsset(ctx, allianceAsset)

Check failure on line 33 in x/alliance/keeper/tests/unbonding_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `app.AllianceKeeper.SetAsset` is not checked (errcheck)

// Query staking module unbonding time to assert later on
unbondingTime, err := app.StakingKeeper.UnbondingTime(ctx)
require.NoError(t, err)

// Get the native delegations to have a validator address where to delegate
delegations, err := app.StakingKeeper.GetAllDelegations(ctx)
require.NoError(t, err)
valAddr, err := sdk.ValAddressFromBech32(delegations[0].ValidatorAddress)
require.NoError(t, err)

// Get a delegator address with funds
delAddrs := test_helpers.AddTestAddrsIncremental(app, ctx, 2, sdk.NewCoins(sdk.NewCoin(AllianceDenom, math.NewInt(1000_000_000))))
delAddr := delAddrs[0]
delAddr1 := delAddrs[1]

// Get an alliance validator
val, err := app.AllianceKeeper.GetAllianceValidator(ctx, valAddr)
require.NoError(t, err)

// Delegate the alliance asset with both accounts
res, err := app.AllianceKeeper.Delegate(ctx, delAddr, val, sdk.NewCoin(AllianceDenom, math.NewInt(1000_000_000)))
require.Nil(t, err)
require.Equal(t, math.LegacyNewDec(1000_000_000), *res)
res2, err := app.AllianceKeeper.Delegate(ctx, delAddr1, val, sdk.NewCoin(AllianceDenom, math.NewInt(1000_000_000)))
require.Nil(t, err)
require.Equal(t, math.LegacyNewDec(1000_000_000), *res2)

// Undelegate the alliance assets with both accounts
undelRes, err := app.AllianceKeeper.Undelegate(ctx, delAddr, val, sdk.NewCoin(AllianceDenom, math.NewInt(1000_000_000)))
require.Nil(t, err)
require.Equal(t, ctx.BlockHeader().Time.Add(unbondingTime), *undelRes)
undelRes2, err := app.AllianceKeeper.Undelegate(ctx, delAddr1, val, sdk.NewCoin(AllianceDenom, math.NewInt(1000_000_000)))
require.Nil(t, err)
require.Equal(t, ctx.BlockHeader().Time.Add(unbondingTime), *undelRes2)

// Validate that both user delegations executed the unbonding process
unbondings, err := app.AllianceKeeper.GetUnbondingsByDelegator(ctx, delAddr)
require.NoError(t, err)
require.Equal(t,
[]types.UnbondingDelegation{{
ValidatorAddress: valAddr.String(),
Amount: math.NewInt(1000_000_000),
CompletionTime: ctx.BlockHeader().Time.Add(unbondingTime),
Denom: AllianceDenom,
}},
unbondings,
)

// Validate that both user delegations executed the unbonding process
unbondings, err = app.AllianceKeeper.GetUnbondingsByDenomAndDelegator(ctx, AllianceDenom, delAddr1)
require.NoError(t, err)
require.Equal(t,
[]types.UnbondingDelegation{{
ValidatorAddress: valAddr.String(),
Amount: math.NewInt(1000_000_000),
CompletionTime: ctx.BlockHeader().Time.Add(unbondingTime),
Denom: AllianceDenom,
}},
unbondings,
)

unbondings, err = app.AllianceKeeper.GetUnbondings(ctx, AllianceDenom, delAddr1, valAddr)
require.NoError(t, err)
require.Equal(t,
[]types.UnbondingDelegation{{
ValidatorAddress: valAddr.String(),
Amount: math.NewInt(1000_000_000),
CompletionTime: ctx.BlockHeader().Time.Add(unbondingTime),
Denom: AllianceDenom,
}},
unbondings,
)
}
8 changes: 5 additions & 3 deletions x/alliance/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ func (k Keeper) GetUnbondings(
// Get the store
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
// create the iterator with the correct prefix
iter := storetypes.KVStorePrefixIterator(store, types.UndelegationByValidatorIndexKey)
prefix := types.GetUndelegationsIndexOrderedByValidatorKey(valAddr)
// Get the iterator
iter := storetypes.KVStorePrefixIterator(store, prefix)
defer iter.Close()
suffix := types.GetPartialUnbondingKeySuffix(denom, delAddr)

for ; iter.Valid(); iter.Next() {
key := iter.Key()
// Skip keys that are shorter than the suffix
if len(key) < len(suffix) {
// Skip keys that don't have the desired suffix
if bytes.HasSuffix(key, suffix) {
continue
}

Expand Down
5 changes: 4 additions & 1 deletion x/alliance/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ var (

// Indexes for querying
RedelegationByValidatorIndexKey = []byte{0x31}
UndelegationByValidatorIndexKey = []byte{0x32} // TOODO: Simplify this index
// The full index looks like:
// 0x32 + valAddr + completionTime + denom + delAddr
// TODO: Simplify this index
UndelegationByValidatorIndexKey = []byte{0x32}
)

func GetAssetKey(denom string) []byte {
Expand Down

0 comments on commit bd4162d

Please sign in to comment.