diff --git a/precompiles/distribution/integration_test.go b/precompiles/distribution/integration_test.go index f816ee5c..f9b6d035 100644 --- a/precompiles/distribution/integration_test.go +++ b/precompiles/distribution/integration_test.go @@ -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" @@ -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" @@ -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 @@ -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), @@ -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, @@ -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() @@ -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() { @@ -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() { @@ -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() { diff --git a/precompiles/distribution/setup_test.go b/precompiles/distribution/setup_test.go index 3e1668c8..f4da1f04 100644 --- a/precompiles/distribution/setup_test.go +++ b/precompiles/distribution/setup_test.go @@ -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" @@ -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 diff --git a/precompiles/distribution/utils_test.go b/precompiles/distribution/utils_test.go index a38fb0ef..69d0b152 100644 --- a/precompiles/distribution/utils_test.go +++ b/precompiles/distribution/utils_test.go @@ -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 diff --git a/testutil/integration/os/network/clients.go b/testutil/integration/os/network/clients.go index a00dc198..9aa66893 100644 --- a/testutil/integration/os/network/clients.go +++ b/testutil/integration/os/network/clients.go @@ -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 ( @@ -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" @@ -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) +} diff --git a/testutil/integration/os/network/network.go b/testutil/integration/os/network/network.go index 50d05de9..9f92553a 100644 --- a/testutil/integration/os/network/network.go +++ b/testutil/integration/os/network/network.go @@ -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" @@ -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" @@ -47,6 +47,7 @@ type Network interface { GetEvmClient() evmtypes.QueryClient GetGovClient() govtypes.QueryClient GetFeeMarketClient() feemarkettypes.QueryClient + GetMintClient() minttypes.QueryClient } var _ Network = (*IntegrationNetwork)(nil) diff --git a/testutil/integration/os/utils/staking.go b/testutil/integration/os/utils/staking.go index 787a3f16..74d6a253 100644 --- a/testutil/integration/os/utils/staking.go +++ b/testutil/integration/os/utils/staking.go @@ -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" ) @@ -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) @@ -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 { @@ -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) @@ -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 +}