0g-chain/x/liquid/keeper/staking_test.go
drklee3 f757d7ab15
feat: upgrade to Cosmos v0.46 (#1477)
* Update cosmos-sdk to v0.45.10-kava

* Add RegisterNodeService to app

* Update cosmos proto files

* Update cosmos proto files

* Use tagged v0.45.10-kava-v0.19-0.21 cosmos version

* update x/auth/legacy to x/auth/migrations

* Delete rest packages and registration

* Remove rest from proposal handlers

* Remove legacy types referencing removed sdk types

* Remove legacy tx broadcast handler

* Update incentive staking hooks to return error

* Remove grpc replace directive, use new grpc version

* Fix storetypes import

* Update tally_handler with updated gov types

* Delete legacy types

* Use new gov default config

* Update RegisterTendermintService params

Signed-off-by: drklee3 <derrick@dlee.dev>

* Replace sdk.StoreKey with storetypes.StoreKey

* Replace sdk.Int#ToDec with sdk.NewDecFromInt

* Replace sdk.NewUintFromBigInt with sdkmath.NewUintFromBigInt

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update most intances of govtypes to govv1beta1

* Unpack coin slice for Coins#Sub and Coins#SafeSub

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update committee gov codec registration

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update migrate utils period_vesting Coins#Sub

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update Coin#Sub in community proposal handler

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update Coin#Sub, FundModuleAccount/FundAccount in banktestutil

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update community, earn, kavadist proposal gov registration

* Update evm cli client EthSecp256k1Type check

* AccAddressFromHex to AccAddressFromHexUnsafe

* Add mint DefaultInflationCalculationFn to earn test

* Update use of removed staking.NewHandler

* Rename FlagIAVLFastNode -> FlagDisableIAVLFastNode

* cmd: Update new snapshot app option

Signed-off-by: drklee3 <derrick@dlee.dev>

* cmd: Add tendermint default config, use cosmos rpc status command

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update ethermint import path

github.com/tharsis/ethermint -> github.com/evmos/ethermint

* Upgrade ibc-go to v6

* Update proto dependencies

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update Tally handler test with new gov types

* Update helpers.GenTx -> helpers.GenSignedMockTx

* Update evmkeeper.NewKeeper params

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update ante authz, tests

* Add feemarket transient key, pass subspaces to evm/feemarket keepers

* Update new ante decorators

* Add new addModuleInitFlags to server commands

* Pass codec to keyring.New in genaccounts

* Pass codec to client keys add

* Add SendCoins to evmutil bank_keeper

* Use github.com/cosmos/iavl@v0.19.5

* Add ante HandlerOptions

* Add unimplemented SendCoins to evmutil bank keeper

Ethermint x/evm does not use this method

* Update init-new-chain script to disable post-london blocks

* Modify test genesis states to append 1 validator

* Update tally handler test to use string values

* Prevent querying balance for empty sdk.AccAddress in auction bidding test

* Set default bond denom to ukava

* Remove overwritten bank genesis total supply in committee proposal test

Signed-off-by: drklee3 <derrick@dlee.dev>

* Use ukava for testing staked balance

* Disable minting in community proposal handler test

Previously stake denom is used, which resulted in 0 minted coins

* Update hard APYToSPY test expected value

Increased iterations in sdk.ApproxRoot, updated closer to real value

* Fix NewDecCoinsFromCoins bug in incentive collectDerivativeStakingRewards

* Allow bkava earn incentive test values to match within small margin for rounding

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update invalid denom in issuance message coin validation

Colons are now valid in denoms

Signed-off-by: drklee3 <derrick@dlee.dev>

* Remove genesis validator in incentive delegation tests

* Update pricefeed market test for invalid denom

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update incentive delegator rewards test without genesis validator

Signed-off-by: drklee3 <derrick@dlee.dev>

* Add validator to export test

* Clear bank state in minting tests

Signed-off-by: drklee3 <derrick@dlee.dev>

* Remove validator for no stake tally test

Signed-off-by: drklee3 <derrick@dlee.dev>

* Clear incentive state before InitGenesis in incentive genesis export test

* Update swagger

Signed-off-by: drklee3 <derrick@dlee.dev>

* Update ethermint version to match replaced version

* Remove legacy swagger

* Add NewEthEmitEventDecorator

* Remove redundant func for AddModuleInitFlags

* Remove unused addBankBalanceForAddress func

* Add SetIAVLLazyLoading option to app cmd

* Use legacy.RegisterAminoMsg for committee msg concrete registration

* Remove unnecessary Amino field

* Add evm_util bankkeeper SendCoins comment

* Update test method ResetBankState to DeleteGenesisValidatorCoins to be more clear

* Validate incentive params.RewardsPerSecond to be non-zero

* Validate swap pools to disallow colons in token denoms

* Register all legacy amino types on gov modulecdc

* Remove redundant Comittee interface registration

* Pin goleveldb to v1.0.1-0.20210819022825-2ae1ddf74ef7

Causes failed to load state at height errors

* Update ethermint to new pinned version with minGasPrices parse error fix

* Update cosmos fork dependcy commit to include reverted account constructor patch

* Update Cosmos v0.46.11 and cometbft v0.34.27

* Bump minimum go version to 1.19

* Update tendermint proto

* Update internal testnet genesis

* Move NewCanTransferDecorator before NewEthGasConsumeDecorator

* Add hard borrow store tests (#1514)

* add store tests for Borrow type

* refactor Deposit tests to match

* Fix old bep3 tests (#1515)

* Update Ethermint to 1b17445 to fix duplicate proto registration

* Add custom status command to use snake_case and stdout

* Add SetInflation helper

* Reduce ambiguity with evm CanSignEthTx error

* Remove init genesis validator claim in test

* Add disabled evmante.NewMinGasPriceDecorator with x/feemarket note

* chore: use tagged versions for Cosmos and Ethermint forks

* update kvtool & increase wait for ibc transfer test

---------

Signed-off-by: drklee3 <derrick@dlee.dev>
Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
Co-authored-by: Robert Pirtle <astropirtle@gmail.com>
2023-04-03 20:08:45 -04:00

379 lines
14 KiB
Go

package keeper_test
import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/liquid/types"
)
var (
// d is an alias for sdk.MustNewDecFromStr
d = sdk.MustNewDecFromStr
// i is an alias for sdk.NewInt
i = sdk.NewInt
// c is an alias for sdk.NewInt64Coin
c = sdk.NewInt64Coin
)
func (suite *KeeperTestSuite) TestTransferDelegation_ValidatorStates() {
_, addrs := app.GeneratePrivKeyAddressPairs(3)
valAccAddr, fromDelegator, toDelegator := addrs[0], addrs[1], addrs[2]
valAddr := sdk.ValAddress(valAccAddr)
initialBalance := i(1e9)
notBondedModAddr := authtypes.NewModuleAddress(stakingtypes.NotBondedPoolName)
bondedModAddr := authtypes.NewModuleAddress(stakingtypes.BondedPoolName)
testCases := []struct {
name string
createValidator func() (delegatorShares sdk.Dec, err error)
}{
{
name: "bonded validator",
createValidator: func() (sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, initialBalance)
delegatorShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
// Run end blocker to update validator state to bonded.
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return delegatorShares, nil
},
},
{
name: "unbonded validator",
createValidator: func() (sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, initialBalance)
delegatorShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
// Don't run end blocker, new validators are by default unbonded.
return delegatorShares, nil
},
},
{
name: "ubonding (jailed) validator",
createValidator: func() (sdk.Dec, error) {
val := suite.CreateNewUnbondedValidator(valAddr, initialBalance)
delegatorShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
// Run end blocker to update validator state to bonded.
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
// Jail and run end blocker to transition validator to unbonding.
consAddr, err := val.GetConsAddr()
if err != nil {
return sdk.Dec{}, err
}
suite.StakingKeeper.Jail(suite.Ctx, consAddr)
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return delegatorShares, nil
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
suite.CreateAccountWithAddress(valAccAddr, suite.NewBondCoins(i(1e9)))
suite.CreateAccountWithAddress(fromDelegator, suite.NewBondCoins(i(1e9)))
fromDelegationShares, err := tc.createValidator()
suite.Require().NoError(err)
validator, found := suite.StakingKeeper.GetValidator(suite.Ctx, valAddr)
suite.Require().True(found)
notBondedBalance := suite.BankKeeper.GetAllBalances(suite.Ctx, notBondedModAddr)
bondedBalance := suite.BankKeeper.GetAllBalances(suite.Ctx, bondedModAddr)
shares := d("1000")
_, err = suite.Keeper.TransferDelegation(suite.Ctx, valAddr, fromDelegator, toDelegator, shares)
suite.Require().NoError(err)
// Transferring a delegation should move shares, and leave the validator and pool balances the same.
suite.DelegationSharesEqual(valAddr, fromDelegator, fromDelegationShares.Sub(shares))
suite.DelegationSharesEqual(valAddr, toDelegator, shares) // also creates new delegation
validatorAfter, found := suite.StakingKeeper.GetValidator(suite.Ctx, valAddr)
suite.Require().True(found)
suite.Equal(validator.GetTokens(), validatorAfter.GetTokens())
suite.Equal(validator.GetDelegatorShares(), validatorAfter.GetDelegatorShares())
suite.Equal(validator.GetStatus(), validatorAfter.GetStatus())
suite.AccountBalanceEqual(notBondedModAddr, notBondedBalance)
suite.AccountBalanceEqual(bondedModAddr, bondedBalance)
})
}
}
func (suite *KeeperTestSuite) TestTransferDelegation_Shares() {
_, addrs := app.GeneratePrivKeyAddressPairs(5)
valAccAddr, fromDelegator, toDelegator := addrs[0], addrs[1], addrs[2]
valAddr := sdk.ValAddress(valAccAddr)
initialBalance := i(1e12)
testCases := []struct {
name string
createDelegations func() (fromDelegatorShares, toDelegatorShares sdk.Dec, err error)
shares sdk.Dec
expectReceived sdk.Dec
expectedErr error
}{
{
name: "negative shares cannot be transferred",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
// Run end blocker to update validator state to bonded.
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return fromDelegationShares, sdk.ZeroDec(), nil
},
shares: d("-1.0"),
expectedErr: types.ErrUntransferableShares,
},
{
name: "nil shares cannot be transferred",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return fromDelegationShares, sdk.ZeroDec(), nil
},
shares: sdk.Dec{},
expectedErr: types.ErrUntransferableShares,
},
{
name: "0 shares cannot be transferred",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
toDelegationShares := suite.CreateDelegation(valAddr, toDelegator, i(2e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return fromDelegationShares, toDelegationShares, nil
},
shares: sdk.ZeroDec(),
expectedErr: types.ErrUntransferableShares,
},
{
name: "all shares can be transferred",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
toDelegationShares := suite.CreateDelegation(valAddr, toDelegator, i(2e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return fromDelegationShares, toDelegationShares, nil
},
shares: d("1000000000.0"),
expectReceived: d("1000000000.0"),
},
{
name: "excess shares cannot be transferred",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return fromDelegationShares, sdk.ZeroDec(), nil
},
shares: d("1000000000.000000000000000001"),
expectedErr: stakingtypes.ErrNotEnoughDelegationShares,
},
{
name: "shares can be transferred to a non existent delegation",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return fromDelegationShares, sdk.ZeroDec(), nil
},
shares: d("500000000.0"),
expectReceived: d("500000000.0"),
},
{
name: "shares cannot be transferred from a non existent delegation",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
return sdk.ZeroDec(), sdk.ZeroDec(), nil
},
shares: d("500000000.0"),
expectedErr: types.ErrNoDelegatorForAddress,
},
{
name: "slashed validator shares can be transferred",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
suite.SlashValidator(valAddr, d("0.05"))
return fromDelegationShares, sdk.ZeroDec(), nil
},
shares: d("500000000.0"),
expectReceived: d("500000000.0"),
},
{
name: "zero shares received when transfer < 1 token",
createDelegations: func() (sdk.Dec, sdk.Dec, error) {
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(1e9))
toDelegationShares := suite.CreateDelegation(valAddr, toDelegator, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
// make 1 share worth more than 1 token
suite.SlashValidator(valAddr, d("0.05"))
return fromDelegationShares, toDelegationShares, nil
},
shares: d("1.0"), // send 1 share (truncates to zero tokens)
expectReceived: d("0.0"),
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
suite.CreateAccountWithAddress(valAccAddr, suite.NewBondCoins(initialBalance))
suite.CreateAccountWithAddress(fromDelegator, suite.NewBondCoins(initialBalance))
suite.CreateAccountWithAddress(toDelegator, suite.NewBondCoins(initialBalance))
fromDelegationShares, toDelegationShares, err := tc.createDelegations()
suite.Require().NoError(err)
validator, found := suite.StakingKeeper.GetValidator(suite.Ctx, valAddr)
suite.Require().True(found)
_, err = suite.Keeper.TransferDelegation(suite.Ctx, valAddr, fromDelegator, toDelegator, tc.shares)
if tc.expectedErr != nil {
suite.ErrorIs(err, tc.expectedErr)
return
}
suite.NoError(err)
suite.DelegationSharesEqual(valAddr, fromDelegator, fromDelegationShares.Sub(tc.shares))
suite.DelegationSharesEqual(valAddr, toDelegator, toDelegationShares.Add(tc.expectReceived))
validatorAfter, found := suite.StakingKeeper.GetValidator(suite.Ctx, valAddr)
suite.Require().True(found)
// total tokens should not change
suite.Equal(validator.GetTokens(), validatorAfter.GetTokens())
// but total shares can differ
suite.Equal(
validator.GetDelegatorShares().Sub(tc.shares).Add(tc.expectReceived),
validatorAfter.GetDelegatorShares(),
)
})
}
}
func (suite *KeeperTestSuite) TestTransferDelegation_RedelegationsForbidden() {
_, addrs := app.GeneratePrivKeyAddressPairs(4)
val1AccAddr, val2AccAddr, fromDelegator, toDelegator := addrs[0], addrs[1], addrs[2], addrs[3]
val1Addr := sdk.ValAddress(val1AccAddr)
val2Addr := sdk.ValAddress(val2AccAddr)
initialBalance := i(1e12)
suite.CreateAccountWithAddress(val1AccAddr, suite.NewBondCoins(initialBalance))
suite.CreateAccountWithAddress(val2AccAddr, suite.NewBondCoins(initialBalance))
suite.CreateAccountWithAddress(fromDelegator, suite.NewBondCoins(initialBalance))
// create bonded validator 1 with a delegation
suite.CreateNewUnbondedValidator(val1Addr, i(1e9))
fromDelegationShares := suite.CreateDelegation(val1Addr, fromDelegator, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
// create validator 2 and redelegate to it
suite.CreateNewUnbondedValidator(val2Addr, i(1e9))
suite.CreateRedelegation(fromDelegator, val1Addr, val2Addr, i(1e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
_, err := suite.Keeper.TransferDelegation(suite.Ctx, val2Addr, fromDelegator, toDelegator, fromDelegationShares)
suite.ErrorIs(err, types.ErrRedelegationsNotCompleted)
suite.DelegationSharesEqual(val2Addr, fromDelegator, fromDelegationShares)
suite.DelegationSharesEqual(val2Addr, toDelegator, sdk.ZeroDec())
}
func (suite *KeeperTestSuite) TestTransferDelegation_CompliesWithMinSelfDelegation() {
_, addrs := app.GeneratePrivKeyAddressPairs(4)
valAccAddr, toDelegator := addrs[0], addrs[1]
valAddr := sdk.ValAddress(valAccAddr)
suite.CreateAccountWithAddress(valAccAddr, suite.NewBondCoins(i(1e12)))
// create bonded validator with minimum delegated
minSelfDelegation := i(1e9)
delegation := suite.NewBondCoin(i(1e9))
msg, err := stakingtypes.NewMsgCreateValidator(
valAddr,
ed25519.GenPrivKey().PubKey(),
delegation,
stakingtypes.Description{},
stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
minSelfDelegation,
)
suite.Require().NoError(err)
msgServer := stakingkeeper.NewMsgServerImpl(suite.StakingKeeper)
_, err = msgServer.CreateValidator(sdk.WrapSDKContext(suite.Ctx), msg)
suite.Require().NoError(err)
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
_, err = suite.Keeper.TransferDelegation(suite.Ctx, valAddr, valAccAddr, toDelegator, d("0.000000000000000001"))
suite.ErrorIs(err, types.ErrSelfDelegationBelowMinimum)
suite.DelegationSharesEqual(valAddr, valAccAddr, sdk.NewDecFromInt(delegation.Amount))
}
func (suite *KeeperTestSuite) TestTransferDelegation_CanTransferVested() {
_, addrs := app.GeneratePrivKeyAddressPairs(4)
valAccAddr, fromDelegator, toDelegator := addrs[0], addrs[1], addrs[2]
valAddr := sdk.ValAddress(valAccAddr)
suite.CreateAccountWithAddress(valAccAddr, suite.NewBondCoins(i(1e9)))
suite.CreateVestingAccountWithAddress(fromDelegator, suite.NewBondCoins(i(2e9)), suite.NewBondCoins(i(1e9)))
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
fromDelegationShares := suite.CreateDelegation(valAddr, fromDelegator, i(2e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
shares := d("1000000000.0")
_, err := suite.Keeper.TransferDelegation(suite.Ctx, valAddr, fromDelegator, toDelegator, shares)
suite.NoError(err)
suite.DelegationSharesEqual(valAddr, fromDelegator, fromDelegationShares.Sub(shares))
suite.DelegationSharesEqual(valAddr, toDelegator, shares)
}
func (suite *KeeperTestSuite) TestTransferDelegation_CannotTransferVesting() {
_, addrs := app.GeneratePrivKeyAddressPairs(4)
valAccAddr, fromDelegator, toDelegator := addrs[0], addrs[1], addrs[2]
valAddr := sdk.ValAddress(valAccAddr)
suite.CreateAccountWithAddress(valAccAddr, suite.NewBondCoins(i(1e9)))
suite.CreateVestingAccountWithAddress(fromDelegator, suite.NewBondCoins(i(2e9)), suite.NewBondCoins(i(1e9)))
suite.CreateNewUnbondedValidator(valAddr, i(1e9))
suite.CreateDelegation(valAddr, fromDelegator, i(2e9))
staking.EndBlocker(suite.Ctx, suite.StakingKeeper)
_, err := suite.Keeper.TransferDelegation(suite.Ctx, valAddr, fromDelegator, toDelegator, d("1000000001.0"))
suite.ErrorIs(err, sdkerrors.ErrInsufficientFunds)
}