Skip to content

Commit

Permalink
fix: parse redelegation pagination key time
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Feb 26, 2024
1 parent c5a2fdd commit 3c2453b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/terra-money/alliance

go 1.21

toolchain go1.21.5
toolchain go1.21.4

require (
cosmossdk.io/core v0.11.0
Expand Down
37 changes: 35 additions & 2 deletions x/alliance/keeper/tests/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/terra-money/alliance/x/alliance/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
teststaking "github.com/cosmos/cosmos-sdk/x/staking/testutil"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -332,6 +333,26 @@ func TestSuccessfulRedelegation(t *testing.T) {
// Check total bonded amount
require.Equal(t, math.NewInt(3_000_000), totalBonded)

// Query redelegations by the delegator address
redelegationsByDelegator, err := queryServer.AllianceRedelegationsByDelegator(ctx, &types.QueryAllianceRedelegationsByDelegatorRequest{
DelegatorAddr: delAddr1.String(),
})
require.NoError(t, err)
require.Equal(t, &types.QueryAllianceRedelegationsByDelegatorResponse{
Redelegations: []types.RedelegationEntry{
{
DelegatorAddress: delAddr1.String(),
SrcValidatorAddress: valAddr1.String(),
DstValidatorAddress: valAddr2.String(),
Balance: sdk.NewCoin(AllianceDenom, math.NewInt(500_000)),
CompletionTime: time.Date(1, time.January, 22, 0, 0, 0, 0, time.UTC),
},
},
Pagination: &query.PageResponse{
Total: 1,
},
}, redelegationsByDelegator)

// Check if there is a re-delegation event stored
iter := app.AllianceKeeper.IterateRedelegationsByDelegator(ctx, delAddr1)
defer iter.Close()
Expand Down Expand Up @@ -388,9 +409,21 @@ func TestSuccessfulRedelegation(t *testing.T) {
DelegatorAddr: delAddr1.String(),
Pagination: nil,
})

require.NoError(t, err)
require.Len(t, redelegationsRes.Redelegations, 1)
require.Equal(t, &types.QueryAllianceRedelegationsResponse{
Redelegations: []types.RedelegationEntry{
{
DelegatorAddress: delAddr1.String(),
SrcValidatorAddress: valAddr1.String(),
DstValidatorAddress: valAddr2.String(),
Balance: sdk.NewCoin(AllianceDenom, math.NewInt(500_000)),
CompletionTime: time.Date(1, time.January, 22, 0, 0, 0, 0, time.UTC),
},
},
Pagination: &query.PageResponse{
Total: 1,
},
}, redelegationsRes)

unbondingPeriod, err := app.StakingKeeper.UnbondingTime(ctx)
require.NoError(t, err)
Expand Down
7 changes: 3 additions & 4 deletions x/alliance/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,9 @@ func ParseRedelegationKeyForCompletionTime(key []byte) time.Time {
}

func ParseRedelegationPaginationKeyTime(key []byte) time.Time {
offset := 0
offset += int(key[offset]) + 1
b := key[offset:]
t, err := sdk.ParseTimeBytes(b)
timestampLength := len(sdk.SortableTimeFormat)
timestampStart := len(key) - timestampLength
t, err := sdk.ParseTimeBytes(key[timestampStart:])
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 3c2453b

Please sign in to comment.