Skip to content

Commit

Permalink
test(halo/app): adds delegation rewards test (#2970)
Browse files Browse the repository at this point in the history
Adds a new test ensuring that delegation rewards are distributed.

issue: #2892
  • Loading branch information
chmllr authored Feb 5, 2025
1 parent 1b676b0 commit d153d97
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
65 changes: 55 additions & 10 deletions e2e/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
dtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -141,22 +142,22 @@ func TestCLIOperator(t *testing.T) {
}, valChangeWait, 500*time.Millisecond, "failed to self-delegate")
})

// delegator's keys
privKey, pubKey := anvil.DevPrivateKey5(), anvil.DevAccount5()
delegatorCosmosAddr := sdk.AccAddress(pubKey.Bytes()).String()
delegatorPrivKeyFile := filepath.Join(tmpDir, "delegator_privkey")
require.NoError(
t,
ethcrypto.SaveECDSA(delegatorPrivKeyFile, privKey),
"failed to save new validator private key to temp file",
)

// delegate from a new account
t.Run("delegation", func(t *testing.T) {
if !feature.FlagEVMStakingModule.Enabled(ctx) {
t.Skip("Skipping delegation tests")
}

// delegator's keys
privKey, pubKey := anvil.DevPrivateKey5(), anvil.DevAccount5()
delegatorCosmosAddr := sdk.AccAddress(pubKey.Bytes()).String()
delegatorPrivKeyFile := filepath.Join(tmpDir, "delegator_privkey")
require.NoError(
t,
ethcrypto.SaveECDSA(delegatorPrivKeyFile, privKey),
"failed to save new validator private key to temp file",
)

// delegator delegate test
const delegatorDelegation = uint64(700)
stdOut, _, err := execCLI(
Expand Down Expand Up @@ -220,6 +221,50 @@ func TestCLIOperator(t *testing.T) {
return val.GetMoniker() == moniker && val.MinSelfDelegation.Equal(minSelfWei)
}, valChangeWait, 500*time.Millisecond, "failed to edit validator")
})

// test rewards distribution
t.Run("distribution", func(t *testing.T) {
if !feature.FlagEVMStakingModule.Enabled(ctx) {
t.Skip("Skipping evmstaking2 tests")
}

val, ok, _ := cprov.SDKValidator(ctx, validatorAddr)
require.True(t, ok)

// fetch rewards and make sure they are positive
resp, err := cprov.QueryClients().Distribution.DelegationRewards(ctx, &dtypes.QueryDelegationRewardsRequest{
DelegatorAddress: delegatorCosmosAddr,
ValidatorAddress: val.OperatorAddress,
})
require.NoError(t, err)
require.NotEmpty(t, resp.Rewards)

for _, coin := range resp.Rewards {
require.Equal(t, "stake", coin.Denom)
require.True(t, coin.Amount.IsPositive())
}

// fetch again and make sure they increased
wait := time.Second * 2
require.Eventuallyf(t, func() bool {
resp2, err := cprov.QueryClients().Distribution.DelegationRewards(ctx, &dtypes.QueryDelegationRewardsRequest{
DelegatorAddress: delegatorCosmosAddr,
ValidatorAddress: val.OperatorAddress,
})
require.NoError(t, err)
require.NotEmpty(t, resp2.Rewards)

// all fetched values should be strictly larger
for i, coin2 := range resp2.Rewards {
coin := resp.Rewards[i]
if !coin2.Amount.GT(coin.Amount) {
return false
}
}

return true
}, wait, 500*time.Millisecond, "no rewards increase")
})
})
}

Expand Down
2 changes: 1 addition & 1 deletion halo/app/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func testCProvider(t *testing.T, ctx context.Context, cprov cprovider.Provider)
if feature.FlagEVMStakingModule.Enabled(ctx) {
resp, err := cprov.QueryClients().Mint.Inflation(ctx, &minttypes.QueryInflationRequest{})
require.NoError(t, err)
require.EqualValues(t, "11.000000000000000000", resp.Inflation.String())
require.EqualValues(t, "0.110000000000000000", resp.Inflation.String())
}
}

Expand Down

0 comments on commit d153d97

Please sign in to comment.