Skip to content

Commit

Permalink
fix tests by implementing minting for distribution tests and expose m…
Browse files Browse the repository at this point in the history
…int client on test network
  • Loading branch information
MalteHerrmann committed Sep 23, 2024
1 parent f7c9d38 commit 595014f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
22 changes: 14 additions & 8 deletions precompiles/distribution/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"math/big"
"testing"

evmostestutil "github.com/evmos/os/testutil/constants"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -18,6 +16,7 @@ import (
"github.com/evmos/os/precompiles/staking"
"github.com/evmos/os/precompiles/testutil"
"github.com/evmos/os/precompiles/testutil/contracts"
evmostestutil "github.com/evmos/os/testutil/constants"
"github.com/evmos/os/testutil/integration/os/factory"
testutils "github.com/evmos/os/testutil/integration/os/utils"
testutiltx "github.com/evmos/os/testutil/tx"
Expand Down Expand Up @@ -170,6 +169,7 @@ var _ = Describe("Calling distribution precompile from EOA", func() {

Describe("Execute WithdrawDelegatorRewards transaction", func() {
var accruedRewards sdk.DecCoins

BeforeEach(func() {
var err error
// set the default call arguments
Expand All @@ -185,7 +185,11 @@ var _ = Describe("Calling distribution precompile from EOA", func() {
s.network.GetValidators()[0].OperatorAddress,
}

withdrawalCheck := defaultLogCheck.WithErrContains(cmn.ErrDelegatorDifferentOrigin, s.keyring.GetAddr(0).String(), differentAddr.String())
withdrawalCheck := defaultLogCheck.WithErrContains(
cmn.ErrDelegatorDifferentOrigin,
s.keyring.GetAddr(0).String(),
differentAddr.String(),
)

_, _, err := s.factory.CallContractAndCheckLogs(
s.keyring.GetPrivKey(0),
Expand All @@ -203,6 +207,8 @@ var _ = Describe("Calling distribution precompile from EOA", func() {
initialBalance := queryRes.Balance

txArgs.GasPrice = gasPrice.BigInt()
txArgs.GasLimit = 100_000

callArgs.Args = []interface{}{
s.keyring.GetAddr(0),
s.network.GetValidators()[0].OperatorAddress,
Expand Down Expand Up @@ -605,6 +611,7 @@ var _ = Describe("Calling distribution precompile from EOA", func() {
callArgs.Args = []interface{}{
s.keyring.GetAddr(0), uint32(valCount),
}
txArgs.GasLimit = 250_000

// get base fee to use in tx to then calculate fee paid
bfQuery, err := s.grpcHandler.GetBaseFee()
Expand Down Expand Up @@ -676,8 +683,8 @@ var _ = Describe("Calling distribution precompile from EOA", func() {

expAddr := s.validatorsKeys[0].AccAddr.String()
Expect(expAddr).To(Equal(out.DistributionInfo.OperatorAddress))
Expect(0).To(Equal(len(out.DistributionInfo.Commission)))
Expect(0).To(Equal(len(out.DistributionInfo.SelfBondRewards)))
Expect(1).To(Equal(len(out.DistributionInfo.Commission)))
Expect(1).To(Equal(len(out.DistributionInfo.SelfBondRewards)))
})

It("should get validator outstanding rewards - validatorOutstandingRewards query", func() {
Expand Down Expand Up @@ -712,10 +719,9 @@ var _ = Describe("Calling distribution precompile from EOA", func() {
expRewardAmt := accruedRewards.AmountOf(s.bondDenom).
Quo(math.LegacyNewDec(3)). // divide by validators count
Quo(math.LegacyNewDecWithPrec(95, 2)). // add 5% commission
Ceil(). // round up to get the same value
TruncateInt()

Expect(rewards[0].Amount).To(Equal(expRewardAmt.BigInt()))
Expect(rewards[0].Amount.String()).To(Equal(expRewardAmt.BigInt().String()))
})

It("should get validator commission - validatorCommission query", func() {
Expand Down Expand Up @@ -2482,7 +2488,7 @@ var _ = Describe("Calling distribution precompile from another contract", Ordere

Expect(expAddr).To(Equal(out.DistributionInfo.OperatorAddress))
Expect(1).To(Equal(len(out.DistributionInfo.Commission)))
Expect(0).To(Equal(len(out.DistributionInfo.SelfBondRewards)))
Expect(1).To(Equal(len(out.DistributionInfo.SelfBondRewards)))
})

It("should get validator outstanding rewards", func() {
Expand Down
8 changes: 7 additions & 1 deletion precompiles/distribution/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/evmos/os/precompiles/distribution"
testconstants "github.com/evmos/os/testutil/constants"
"github.com/evmos/os/testutil/integration/os/factory"
"github.com/evmos/os/testutil/integration/os/grpc"
testkeyring "github.com/evmos/os/testutil/integration/os/keyring"
Expand Down Expand Up @@ -59,6 +60,11 @@ func (s *PrecompileTestSuite) SetupTest() {
}
customGen[distrtypes.ModuleName] = distrGen

// set non-zero inflation for rewards to accrue (use defaults from SDK for values)
mintGen := minttypes.DefaultGenesisState()
mintGen.Params.MintDenom = testconstants.ExampleAttoDenom
customGen[minttypes.ModuleName] = mintGen

operatorsAddr := make([]sdk.AccAddress, 3)
for i, k := range s.validatorsKeys {
operatorsAddr[i] = k.AccAddr
Expand Down
2 changes: 1 addition & 1 deletion precompiles/distribution/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type stakingRewards struct {
}

var (
testRewardsAmt, _ = math.NewIntFromString("1000000000000000000")
testRewardsAmt, _ = math.NewIntFromString("100000000000")
validatorCommPercentage = math.LegacyNewDecWithPrec(5, 2) // 5% commission
validatorCommAmt = math.LegacyNewDecFromInt(testRewardsAmt).Mul(validatorCommPercentage).TruncateInt()
expRewardsAmt = testRewardsAmt.Sub(validatorCommAmt) // testRewardsAmt - commission
Expand Down
9 changes: 9 additions & 0 deletions testutil/integration/os/network/clients.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package network

import (
Expand All @@ -14,6 +15,8 @@ import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
erc20types "github.com/evmos/os/x/erc20/types"
Expand Down Expand Up @@ -82,3 +85,9 @@ func (n *IntegrationNetwork) GetDistrClient() distrtypes.QueryClient {
distrtypes.RegisterQueryServer(queryHelper, distrkeeper.Querier{Keeper: n.app.DistrKeeper})
return distrtypes.NewQueryClient(queryHelper)
}

func (n *IntegrationNetwork) GetMintClient() minttypes.QueryClient {
queryHelper := getQueryHelper(n.GetContext(), n.GetEncodingConfig())
minttypes.RegisterQueryServer(queryHelper, mintkeeper.NewQueryServerImpl(n.app.MintKeeper))
return minttypes.NewQueryClient(queryHelper)
}
5 changes: 3 additions & 2 deletions testutil/integration/os/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"math/big"
"time"

chainutil "github.com/evmos/os/example_chain/testutil"

sdkmath "cosmossdk.io/math"
abcitypes "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
Expand All @@ -22,9 +20,11 @@ import (
sdktestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gethparams "github.com/ethereum/go-ethereum/params"
app "github.com/evmos/os/example_chain"
chainutil "github.com/evmos/os/example_chain/testutil"
commonnetwork "github.com/evmos/os/testutil/integration/common/network"
"github.com/evmos/os/types"
erc20types "github.com/evmos/os/x/erc20/types"
Expand All @@ -47,6 +47,7 @@ type Network interface {
GetEvmClient() evmtypes.QueryClient
GetGovClient() govtypes.QueryClient
GetFeeMarketClient() feemarkettypes.QueryClient
GetMintClient() minttypes.QueryClient
}

var _ Network = (*IntegrationNetwork)(nil)
Expand Down
28 changes: 27 additions & 1 deletion testutil/integration/os/utils/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package utils

import (
"errors"
"time"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/evmos/os/testutil/integration/os/grpc"
"github.com/evmos/os/testutil/integration/os/network"
)
Expand All @@ -21,6 +23,10 @@ func WaitToAccrueRewards(n network.Network, gh grpc.Handler, delegatorAddr strin
rewards = sdk.DecCoins{}
)

if err = checkNonZeroInflation(n); err != nil {
return nil, err
}

expAmt := expRewards.AmountOf(n.GetDenom())
for rewards.AmountOf(n.GetDenom()).LT(expAmt) {
rewards, err = checkRewardsAfter(n, gh, delegatorAddr, lapse)
Expand All @@ -33,7 +39,7 @@ func WaitToAccrueRewards(n network.Network, gh grpc.Handler, delegatorAddr strin
}

// checkRewardsAfter is a helper function that checks the accrued rewards
// after the provided time lapse
// after the provided timelapse
func checkRewardsAfter(n network.Network, gh grpc.Handler, delegatorAddr string, lapse time.Duration) (sdk.DecCoins, error) {
err := n.NextBlockAfter(lapse)
if err != nil {
Expand All @@ -57,6 +63,10 @@ func WaitToAccrueCommission(n network.Network, gh grpc.Handler, validatorAddr st
commission = sdk.DecCoins{}
)

if err := checkNonZeroInflation(n); err != nil {
return nil, err
}

expAmt := expCommission.AmountOf(n.GetDenom())
for commission.AmountOf(n.GetDenom()).LT(expAmt) {
commission, err = checkCommissionAfter(n, gh, validatorAddr, lapse)
Expand All @@ -83,3 +93,19 @@ func checkCommissionAfter(n network.Network, gh grpc.Handler, valAddr string, la

return res.Commission.Commission, nil
}

// checkNonZeroInflation is a helper function that checks if the network's
// inflation is non-zero.
// This is required to ensure that rewards and commission are accrued.
func checkNonZeroInflation(n network.Network) error {
res, err := n.GetMintClient().Inflation(n.GetContext(), &minttypes.QueryInflationRequest{})
if err != nil {
return errorsmod.Wrap(err, "failed to get inflation")
}

if res.Inflation.IsZero() {
return errors.New("inflation is zero; must be non-zero for rewards or commission to be distributed")
}

return nil
}

0 comments on commit 595014f

Please sign in to comment.