* temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
327 lines
14 KiB
Go
327 lines
14 KiB
Go
package simulation_test
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
|
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/simulation"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
// TestWeightedOperations tests the weights of the operations.
|
|
func TestWeightedOperations(t *testing.T) {
|
|
|
|
app, ctx := createTestApp(false)
|
|
|
|
ctx.WithChainID("test-chain")
|
|
|
|
cdc := app.AppCodec()
|
|
appParams := make(simtypes.AppParams)
|
|
|
|
weightesOps := simulation.WeightedOperations(appParams, cdc, app.AccountKeeper,
|
|
app.BankKeeper, app.StakingKeeper,
|
|
)
|
|
|
|
s := rand.NewSource(1)
|
|
r := rand.New(s)
|
|
accs := simtypes.RandomAccounts(r, 3)
|
|
|
|
expected := []struct {
|
|
weight int
|
|
opMsgRoute string
|
|
opMsgName string
|
|
}{{simappparams.DefaultWeightMsgCreateValidator, types.ModuleName, types.TypeMsgCreateValidator},
|
|
{simappparams.DefaultWeightMsgEditValidator, types.ModuleName, types.TypeMsgEditValidator},
|
|
{simappparams.DefaultWeightMsgDelegate, types.ModuleName, types.TypeMsgDelegate},
|
|
{simappparams.DefaultWeightMsgUndelegate, types.ModuleName, types.TypeMsgUndelegate},
|
|
{simappparams.DefaultWeightMsgBeginRedelegate, types.ModuleName, types.TypeMsgBeginRedelegate},
|
|
}
|
|
|
|
for i, w := range weightesOps {
|
|
operationMsg, _, _ := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID())
|
|
// the following checks are very much dependent from the ordering of the output given
|
|
// by WeightedOperations. if the ordering in WeightedOperations changes some tests
|
|
// will fail
|
|
require.Equal(t, expected[i].weight, w.Weight(), "weight should be the same")
|
|
require.Equal(t, expected[i].opMsgRoute, operationMsg.Route, "route should be the same")
|
|
require.Equal(t, expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same")
|
|
}
|
|
}
|
|
|
|
// TestSimulateMsgCreateValidator tests the normal scenario of a valid message of type TypeMsgCreateValidator.
|
|
// Abonormal scenarios, where the message are created by an errors are not tested here.
|
|
func TestSimulateMsgCreateValidator(t *testing.T) {
|
|
app, ctx := createTestApp(false)
|
|
|
|
// setup 3 accounts
|
|
s := rand.NewSource(1)
|
|
r := rand.New(s)
|
|
accounts := getTestingAccounts(t, r, app, ctx, 3)
|
|
|
|
// begin a new block
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash}})
|
|
|
|
// execute operation
|
|
op := simulation.SimulateMsgCreateValidator(app.AccountKeeper, app.BankKeeper, app.StakingKeeper)
|
|
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
|
|
require.NoError(t, err)
|
|
|
|
var msg types.MsgCreateValidator
|
|
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
|
|
|
|
require.True(t, operationMsg.OK)
|
|
require.Equal(t, "0.080000000000000000", msg.Commission.MaxChangeRate.String())
|
|
require.Equal(t, "0.080000000000000000", msg.Commission.MaxRate.String())
|
|
require.Equal(t, "0.019527679037870745", msg.Commission.Rate.String())
|
|
require.Equal(t, types.TypeMsgCreateValidator, msg.Type())
|
|
require.Equal(t, []byte{0xa, 0x20, 0x51, 0xde, 0xbd, 0xe8, 0xfa, 0xdf, 0x4e, 0xfc, 0x33, 0xa5, 0x16, 0x94, 0xf6, 0xee, 0xd3, 0x69, 0x7a, 0x7a, 0x1c, 0x2d, 0x50, 0xb6, 0x2, 0xf7, 0x16, 0x4e, 0x66, 0x9f, 0xff, 0x38, 0x91, 0x9b}, msg.Pubkey.Value)
|
|
require.Equal(t, "cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.DelegatorAddress)
|
|
require.Equal(t, "cosmosvaloper1ghekyjucln7y67ntx7cf27m9dpuxxemnsvnaes", msg.ValidatorAddress)
|
|
require.Len(t, futureOperations, 0)
|
|
}
|
|
|
|
// TestSimulateMsgEditValidator tests the normal scenario of a valid message of type TypeMsgEditValidator.
|
|
// Abonormal scenarios, where the message is created by an errors are not tested here.
|
|
func TestSimulateMsgEditValidator(t *testing.T) {
|
|
app, ctx := createTestApp(false)
|
|
blockTime := time.Now().UTC()
|
|
ctx = ctx.WithBlockTime(blockTime)
|
|
|
|
// setup 3 accounts
|
|
s := rand.NewSource(1)
|
|
r := rand.New(s)
|
|
accounts := getTestingAccounts(t, r, app, ctx, 3)
|
|
|
|
// setup accounts[0] as validator
|
|
_ = getTestingValidator0(t, app, ctx, accounts)
|
|
|
|
// begin a new block
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}})
|
|
|
|
// execute operation
|
|
op := simulation.SimulateMsgEditValidator(app.AccountKeeper, app.BankKeeper, app.StakingKeeper)
|
|
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
|
|
require.NoError(t, err)
|
|
|
|
var msg types.MsgEditValidator
|
|
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
|
|
|
|
require.True(t, operationMsg.OK)
|
|
require.Equal(t, "0.280623462081924936", msg.CommissionRate.String())
|
|
require.Equal(t, "rBqDOTtGTO", msg.Description.Moniker)
|
|
require.Equal(t, "BSpYuLyYgg", msg.Description.Identity)
|
|
require.Equal(t, "wNbeHVIkPZ", msg.Description.Website)
|
|
require.Equal(t, "MOXcnQfyze", msg.Description.SecurityContact)
|
|
require.Equal(t, types.TypeMsgEditValidator, msg.Type())
|
|
require.Equal(t, "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", msg.ValidatorAddress)
|
|
require.Len(t, futureOperations, 0)
|
|
}
|
|
|
|
// TestSimulateMsgDelegate tests the normal scenario of a valid message of type TypeMsgDelegate.
|
|
// Abonormal scenarios, where the message is created by an errors are not tested here.
|
|
func TestSimulateMsgDelegate(t *testing.T) {
|
|
app, ctx := createTestApp(false)
|
|
blockTime := time.Now().UTC()
|
|
ctx = ctx.WithBlockTime(blockTime)
|
|
|
|
// setup 3 accounts
|
|
s := rand.NewSource(1)
|
|
r := rand.New(s)
|
|
accounts := getTestingAccounts(t, r, app, ctx, 3)
|
|
|
|
// setup accounts[0] as validator
|
|
validator0 := getTestingValidator0(t, app, ctx, accounts)
|
|
setupValidatorRewards(app, ctx, validator0.GetOperator())
|
|
|
|
// begin a new block
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}})
|
|
|
|
// execute operation
|
|
op := simulation.SimulateMsgDelegate(app.AccountKeeper, app.BankKeeper, app.StakingKeeper)
|
|
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
|
|
require.NoError(t, err)
|
|
|
|
var msg types.MsgDelegate
|
|
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
|
|
|
|
require.True(t, operationMsg.OK)
|
|
require.Equal(t, "cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.DelegatorAddress)
|
|
require.Equal(t, "98100858108421259236", msg.Amount.Amount.String())
|
|
require.Equal(t, "stake", msg.Amount.Denom)
|
|
require.Equal(t, types.TypeMsgDelegate, msg.Type())
|
|
require.Equal(t, "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", msg.ValidatorAddress)
|
|
require.Len(t, futureOperations, 0)
|
|
}
|
|
|
|
// TestSimulateMsgUndelegate tests the normal scenario of a valid message of type TypeMsgUndelegate.
|
|
// Abonormal scenarios, where the message is created by an errors are not tested here.
|
|
func TestSimulateMsgUndelegate(t *testing.T) {
|
|
app, ctx := createTestApp(false)
|
|
blockTime := time.Now().UTC()
|
|
ctx = ctx.WithBlockTime(blockTime)
|
|
|
|
// setup 3 accounts
|
|
s := rand.NewSource(1)
|
|
r := rand.New(s)
|
|
accounts := getTestingAccounts(t, r, app, ctx, 3)
|
|
|
|
// setup accounts[0] as validator
|
|
validator0 := getTestingValidator0(t, app, ctx, accounts)
|
|
|
|
// setup delegation
|
|
delTokens := sdk.TokensFromConsensusPower(2)
|
|
validator0, issuedShares := validator0.AddTokensFromDel(delTokens)
|
|
delegator := accounts[1]
|
|
delegation := types.NewDelegation(delegator.Address, validator0.GetOperator(), issuedShares)
|
|
app.StakingKeeper.SetDelegation(ctx, delegation)
|
|
app.DistrKeeper.SetDelegatorStartingInfo(ctx, validator0.GetOperator(), delegator.Address, distrtypes.NewDelegatorStartingInfo(2, sdk.OneDec(), 200))
|
|
|
|
setupValidatorRewards(app, ctx, validator0.GetOperator())
|
|
|
|
// begin a new block
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}})
|
|
|
|
// execute operation
|
|
op := simulation.SimulateMsgUndelegate(app.AccountKeeper, app.BankKeeper, app.StakingKeeper)
|
|
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
|
|
require.NoError(t, err)
|
|
|
|
var msg types.MsgUndelegate
|
|
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
|
|
|
|
require.True(t, operationMsg.OK)
|
|
require.Equal(t, "cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.DelegatorAddress)
|
|
require.Equal(t, "280623462081924937", msg.Amount.Amount.String())
|
|
require.Equal(t, "stake", msg.Amount.Denom)
|
|
require.Equal(t, types.TypeMsgUndelegate, msg.Type())
|
|
require.Equal(t, "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", msg.ValidatorAddress)
|
|
require.Len(t, futureOperations, 0)
|
|
|
|
}
|
|
|
|
// TestSimulateMsgBeginRedelegate tests the normal scenario of a valid message of type TypeMsgBeginRedelegate.
|
|
// Abonormal scenarios, where the message is created by an errors, are not tested here.
|
|
func TestSimulateMsgBeginRedelegate(t *testing.T) {
|
|
app, ctx := createTestApp(false)
|
|
blockTime := time.Now().UTC()
|
|
ctx = ctx.WithBlockTime(blockTime)
|
|
|
|
// setup 3 accounts
|
|
s := rand.NewSource(5)
|
|
r := rand.New(s)
|
|
accounts := getTestingAccounts(t, r, app, ctx, 3)
|
|
|
|
// setup accounts[0] as validator0 and accounts[1] as validator1
|
|
validator0 := getTestingValidator0(t, app, ctx, accounts)
|
|
validator1 := getTestingValidator1(t, app, ctx, accounts)
|
|
|
|
delTokens := sdk.TokensFromConsensusPower(2)
|
|
validator0, issuedShares := validator0.AddTokensFromDel(delTokens)
|
|
|
|
// setup accounts[2] as delegator
|
|
delegator := accounts[2]
|
|
delegation := types.NewDelegation(delegator.Address, validator1.GetOperator(), issuedShares)
|
|
app.StakingKeeper.SetDelegation(ctx, delegation)
|
|
app.DistrKeeper.SetDelegatorStartingInfo(ctx, validator1.GetOperator(), delegator.Address, distrtypes.NewDelegatorStartingInfo(2, sdk.OneDec(), 200))
|
|
|
|
setupValidatorRewards(app, ctx, validator0.GetOperator())
|
|
setupValidatorRewards(app, ctx, validator1.GetOperator())
|
|
|
|
// begin a new block
|
|
app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}})
|
|
|
|
// execute operation
|
|
op := simulation.SimulateMsgBeginRedelegate(app.AccountKeeper, app.BankKeeper, app.StakingKeeper)
|
|
operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
|
|
require.NoError(t, err)
|
|
|
|
var msg types.MsgBeginRedelegate
|
|
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
|
|
|
|
require.True(t, operationMsg.OK)
|
|
require.Equal(t, "cosmos12gwd9jchc69wck8dhstxgwz3z8qs8yv67ps8mu", msg.DelegatorAddress)
|
|
require.Equal(t, "489348507626016866", msg.Amount.Amount.String())
|
|
require.Equal(t, "stake", msg.Amount.Denom)
|
|
require.Equal(t, types.TypeMsgBeginRedelegate, msg.Type())
|
|
require.Equal(t, "cosmosvaloper1h6a7shta7jyc72hyznkys683z98z36e0zdk8g9", msg.ValidatorDstAddress)
|
|
require.Equal(t, "cosmosvaloper17s94pzwhsn4ah25tec27w70n65h5t2scgxzkv2", msg.ValidatorSrcAddress)
|
|
require.Len(t, futureOperations, 0)
|
|
|
|
}
|
|
|
|
// returns context and an app with updated mint keeper
|
|
func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) {
|
|
// sdk.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
|
|
app := simapp.Setup(isCheckTx)
|
|
|
|
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{})
|
|
app.MintKeeper.SetParams(ctx, minttypes.DefaultParams())
|
|
app.MintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter())
|
|
|
|
return app, ctx
|
|
}
|
|
|
|
func getTestingAccounts(t *testing.T, r *rand.Rand, app *simapp.SimApp, ctx sdk.Context, n int) []simtypes.Account {
|
|
accounts := simtypes.RandomAccounts(r, n)
|
|
|
|
initAmt := sdk.TokensFromConsensusPower(200)
|
|
initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
|
|
|
|
// add coins to the accounts
|
|
for _, account := range accounts {
|
|
acc := app.AccountKeeper.NewAccountWithAddress(ctx, account.Address)
|
|
app.AccountKeeper.SetAccount(ctx, acc)
|
|
require.NoError(t, simapp.FundAccount(app, ctx, account.Address, initCoins))
|
|
}
|
|
|
|
return accounts
|
|
}
|
|
|
|
func getTestingValidator0(t *testing.T, app *simapp.SimApp, ctx sdk.Context, accounts []simtypes.Account) types.Validator {
|
|
commission0 := types.NewCommission(sdk.ZeroDec(), sdk.OneDec(), sdk.OneDec())
|
|
return getTestingValidator(t, app, ctx, accounts, commission0, 0)
|
|
}
|
|
|
|
func getTestingValidator1(t *testing.T, app *simapp.SimApp, ctx sdk.Context, accounts []simtypes.Account) types.Validator {
|
|
commission1 := types.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
|
|
return getTestingValidator(t, app, ctx, accounts, commission1, 1)
|
|
}
|
|
|
|
func getTestingValidator(t *testing.T, app *simapp.SimApp, ctx sdk.Context, accounts []simtypes.Account, commission types.Commission, n int) types.Validator {
|
|
account := accounts[n]
|
|
valPubKey := account.PubKey
|
|
valAddr := sdk.ValAddress(account.PubKey.Address().Bytes())
|
|
validator := teststaking.NewValidator(t, valAddr, valPubKey)
|
|
validator, err := validator.SetInitialCommission(commission)
|
|
require.NoError(t, err)
|
|
|
|
validator.DelegatorShares = sdk.NewDec(100)
|
|
validator.Tokens = sdk.TokensFromConsensusPower(100)
|
|
|
|
app.StakingKeeper.SetValidator(ctx, validator)
|
|
|
|
return validator
|
|
}
|
|
|
|
func setupValidatorRewards(app *simapp.SimApp, ctx sdk.Context, valAddress sdk.ValAddress) {
|
|
decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, sdk.OneDec())}
|
|
historicalRewards := distrtypes.NewValidatorHistoricalRewards(decCoins, 2)
|
|
app.DistrKeeper.SetValidatorHistoricalRewards(ctx, valAddress, 2, historicalRewards)
|
|
// setup current revards
|
|
currentRewards := distrtypes.NewValidatorCurrentRewards(decCoins, 3)
|
|
app.DistrKeeper.SetValidatorCurrentRewards(ctx, valAddress, currentRewards)
|
|
|
|
}
|