Merge pull request #7897 from sikkatech/fix_power_reduction_test_cast_int64

Fix power reduction test cast int64
This commit is contained in:
Sunny Aggarwal 2020-11-16 18:57:59 -04:00 committed by GitHub
commit 10d5ce39ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 244 additions and 186 deletions

View File

@ -50,11 +50,13 @@ func (k msgServer) Send(goCtx context.Context, msg *types.MsgSend) (*types.MsgSe
defer func() {
for _, a := range msg.Amount {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "send"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
if a.Amount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "send"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
}
}
}()

View File

@ -24,7 +24,7 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
// create validator with 50% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(sdk.ValAddress(addrs[0]), valConsPk1, 100, true)
tstaking.CreateValidator(sdk.ValAddress(addrs[0]), valConsPk1, sdk.NewInt(100), true)
val := app.StakingKeeper.Validator(ctx, valAddrs[0])
// allocate tokens
@ -53,11 +53,11 @@ func TestAllocateTokensToManyValidators(t *testing.T) {
// create validator with 50% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
// create second validator with 0% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[1], valConsPk2, 100, true)
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), true)
abciValA := abci.Validator{
Address: valConsPk1.Address(),
@ -123,15 +123,15 @@ func TestAllocateTokensTruncation(t *testing.T) {
// create validator with 10% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, 110, true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(110), true)
// create second validator with 10% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[1], valConsPk2, 100, true)
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), true)
// create third validator with 10% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[2], valConsPk3, 100, true)
tstaking.CreateValidator(valAddrs[2], valConsPk3, sdk.NewInt(100), true)
abciValA := abci.Validator{
Address: valConsPk1.Address(),

View File

@ -23,7 +23,7 @@ func TestCalculateRewardsBasic(t *testing.T) {
// create validator with 50% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
// end block to bond validator and start new block
staking.EndBlocker(ctx, app.StakingKeeper)
@ -215,7 +215,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) {
// create validator with 50% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
@ -234,7 +234,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) {
// second delegation
tstaking.Ctx = ctx
tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], sdk.NewInt(100))
del2 := app.StakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])
// fetch updated validator
@ -500,7 +500,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
// create validator with 50% commission
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
// end block to bond validator
staking.EndBlocker(ctx, app.StakingKeeper)
@ -519,7 +519,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
require.Equal(t, uint64(2), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx))
// second delegation
tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], sdk.NewInt(100))
// historical count should be 3 (second delegation init)
require.Equal(t, uint64(3), app.DistrKeeper.GetValidatorHistoricalReferenceCount(ctx))

View File

@ -343,7 +343,7 @@ func (suite *KeeperTestSuite) TestGRPCDelegationRewards() {
tstaking := teststaking.NewHelper(suite.T(), ctx, app.StakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valAddrs[0], valConsPk1, 100, true)
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
staking.EndBlocker(ctx, app.StakingKeeper)
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)

View File

@ -66,11 +66,13 @@ func (k msgServer) WithdrawDelegatorReward(goCtx context.Context, msg *types.Msg
defer func() {
for _, a := range amount {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "withdraw_reward"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
if a.Amount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "withdraw_reward"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
}
}
}()
@ -98,11 +100,13 @@ func (k msgServer) WithdrawValidatorCommission(goCtx context.Context, msg *types
defer func() {
for _, a := range amount {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "withdraw_commission"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
if a.Amount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "withdraw_commission"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
}
}
}()

View File

@ -172,7 +172,7 @@ func TestQueries(t *testing.T) {
// test delegation rewards query
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
tstaking.CreateValidator(valOpAddr1, valConsPk1, 100, true)
tstaking.CreateValidator(valOpAddr1, valConsPk1, sdk.NewInt(100), true)
staking.EndBlocker(ctx, app.StakingKeeper)

View File

@ -26,7 +26,8 @@ func TestBeginBlocker(t *testing.T) {
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
// bond the validator
amt := tstaking.CreateValidatorWithValPower(addr, pk, 100, true)
power := int64(100)
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
staking.EndBlocker(ctx, app.StakingKeeper)
require.Equal(
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
@ -36,7 +37,7 @@ func TestBeginBlocker(t *testing.T) {
val := abci.Validator{
Address: pk.Address(),
Power: amt.Int64(),
Power: power,
}
// mark the validator as having signed

View File

@ -58,7 +58,7 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) {
slh := slashing.NewHandler(app.SlashingKeeper)
addr, val := sdk.ValAddress(pks[0].Address()), pks[0]
amt := sdk.TokensFromConsensusPower(100)
msg := tstaking.CreateValidatorMsg(addr, val, amt.Int64())
msg := tstaking.CreateValidatorMsg(addr, val, amt)
msg.MinSelfDelegation = amt
tstaking.Handle(msg, true)
@ -101,7 +101,7 @@ func TestJailedValidatorDelegations(t *testing.T) {
// delegate tokens to the validator
delAddr := sdk.AccAddress(pks[2].Address())
tstaking.Delegate(delAddr, valAddr, amt.Int64())
tstaking.Delegate(delAddr, valAddr, amt)
// unbond validator total self-delegations (which should jail the validator)
valAcc := sdk.AccAddress(valAddr)
@ -120,7 +120,7 @@ func TestJailedValidatorDelegations(t *testing.T) {
require.Nil(t, res)
// self-delegate to validator
tstaking.Delegate(valAcc, valAddr, amt.Int64())
tstaking.Delegate(valAcc, valAddr, amt)
// verify the validator can now unjail itself
res, err = slashing.NewHandler(app.SlashingKeeper)(ctx, types.NewMsgUnjail(valAddr))
@ -214,10 +214,10 @@ func TestHandleAbsentValidator(t *testing.T) {
validator, _ = app.StakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val))
require.Equal(t, stakingtypes.Unbonding, validator.GetStatus())
slashAmt := amt.ToDec().Mul(app.SlashingKeeper.SlashFractionDowntime(ctx)).RoundInt64()
slashAmt := amt.ToDec().Mul(app.SlashingKeeper.SlashFractionDowntime(ctx)).RoundInt()
// validator should have been slashed
require.Equal(t, amt.Int64()-slashAmt, validator.GetTokens().Int64())
require.True(t, amt.Sub(slashAmt).Equal(validator.GetTokens()))
// 502nd block *also* missed (since the LastCommit would have still included the just-unbonded validator)
height++
@ -233,7 +233,7 @@ func TestHandleAbsentValidator(t *testing.T) {
// validator should not have been slashed any more, since it was already jailed
validator, _ = app.StakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(val))
require.Equal(t, amt.Int64()-slashAmt, validator.GetTokens().Int64())
require.True(t, amt.Sub(slashAmt).Equal(validator.GetTokens()))
// unrevocation should fail prior to jail expiration
res, err := slh(ctx, types.NewMsgUnjail(addr))
@ -254,7 +254,7 @@ func TestHandleAbsentValidator(t *testing.T) {
require.Equal(t, stakingtypes.Bonded, validator.GetStatus())
// validator should have been slashed
require.Equal(t, amt.Int64()-slashAmt, app.BankKeeper.GetBalance(ctx, bondPool.GetAddress(), app.StakingKeeper.BondDenom(ctx)).Amount.Int64())
require.True(t, amt.Sub(slashAmt).Equal(app.BankKeeper.GetBalance(ctx, bondPool.GetAddress(), app.StakingKeeper.BondDenom(ctx)).Amount))
// Validator start height should not have been changed
info, found = app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(val.Address()))

View File

@ -40,7 +40,7 @@ func TestUnJailNotBonded(t *testing.T) {
// create a 6th validator with less power than the cliff validator (won't be bonded)
addr, val := valAddrs[5], pks[5]
amt := sdk.TokensFromConsensusPower(50)
msg := tstaking.CreateValidatorMsg(addr, val, amt.Int64())
msg := tstaking.CreateValidatorMsg(addr, val, amt)
msg.MinSelfDelegation = amt
tstaking.Handle(msg, true)
@ -117,7 +117,7 @@ func TestHandleNewValidator(t *testing.T) {
require.Equal(t, stakingtypes.Bonded, validator.GetStatus())
bondPool := app.StakingKeeper.GetBondedPool(ctx)
expTokens := sdk.TokensFromConsensusPower(100)
require.Equal(t, expTokens.Int64(), app.BankKeeper.GetBalance(ctx, bondPool.GetAddress(), app.StakingKeeper.BondDenom(ctx)).Amount.Int64())
require.True(t, expTokens.Equal(app.BankKeeper.GetBalance(ctx, bondPool.GetAddress(), app.StakingKeeper.BondDenom(ctx)).Amount))
}
// Test a jailed validator being "down" twice

View File

@ -381,7 +381,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegations() {
&types.QueryDelegatorDelegationsResponse{},
&types.QueryDelegatorDelegationsResponse{
DelegationResponses: types.DelegationResponses{
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDec(100000000), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000))),
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDecFromInt(cli.DefaultTokens), sdk.NewCoin(sdk.DefaultBondDenom, cli.DefaultTokens)),
},
Pagination: &query.PageResponse{},
},
@ -437,7 +437,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegationsTo() {
&types.QueryValidatorDelegationsResponse{},
&types.QueryValidatorDelegationsResponse{
DelegationResponses: types.DelegationResponses{
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDec(100000000), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000))),
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDecFromInt(cli.DefaultTokens), sdk.NewCoin(sdk.DefaultBondDenom, cli.DefaultTokens)),
},
Pagination: &query.PageResponse{},
},
@ -891,8 +891,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryPool() {
fmt.Sprintf("--%s=text", tmcli.OutputFlag),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
`bonded_tokens: "200000000"
not_bonded_tokens: "0"`,
fmt.Sprintf(`bonded_tokens: "%s"
not_bonded_tokens: "0"`, cli.DefaultTokens.Mul(sdk.NewInt(2)).String()),
},
{
"with json",
@ -900,7 +900,7 @@ not_bonded_tokens: "0"`,
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
fmt.Sprintf("--%s=1", flags.FlagHeight),
},
`{"not_bonded_tokens":"0","bonded_tokens":"200000000"}`,
fmt.Sprintf(`{"not_bonded_tokens":"0","bonded_tokens":"%s"}`, cli.DefaultTokens.Mul(sdk.NewInt(2)).String()),
},
}
for _, tc := range testCases {

View File

@ -17,9 +17,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// default values
var (
defaultTokens = sdk.TokensFromConsensusPower(100)
defaultAmount = defaultTokens.String() + sdk.DefaultBondDenom
DefaultTokens = sdk.TokensFromConsensusPower(100)
defaultAmount = DefaultTokens.String() + sdk.DefaultBondDenom
defaultCommissionRate = "0.1"
defaultCommissionMaxRate = "0.2"
defaultCommissionMaxChangeRate = "0.01"

View File

@ -33,7 +33,7 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Moniker: moniker,
Amount: "100000000stake",
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",
@ -69,7 +69,7 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: "100000000stake",
Amount: defaultAmount,
CommissionRate: "0.54",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",
@ -87,7 +87,7 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: "100000000stake",
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.89",
CommissionMaxChangeRate: "0.01",
@ -105,7 +105,7 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: "100000000stake",
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.55",
@ -123,7 +123,7 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) {
ChainID: chainID,
NodeID: nodeID,
PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey),
Amount: "100000000stake",
Amount: defaultAmount,
CommissionRate: "0.1",
CommissionMaxRate: "0.2",
CommissionMaxChangeRate: "0.01",

View File

@ -15,6 +15,7 @@ import (
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/client/testutil"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
@ -194,7 +195,7 @@ func (s *IntegrationTestSuite) TestQueryValidatorDelegationsGRPC() {
&types.QueryValidatorDelegationsResponse{},
&types.QueryValidatorDelegationsResponse{
DelegationResponses: types.DelegationResponses{
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDec(100000000), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000))),
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDecFromInt(cli.DefaultTokens), sdk.NewCoin(sdk.DefaultBondDenom, cli.DefaultTokens)),
},
Pagination: &query.PageResponse{Total: 1},
},
@ -438,7 +439,7 @@ func (s *IntegrationTestSuite) TestQueryDelegatorDelegationsGRPC() {
&types.QueryDelegatorDelegationsResponse{},
&types.QueryDelegatorDelegationsResponse{
DelegationResponses: types.DelegationResponses{
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDec(100000000), sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000))),
types.NewDelegationResp(val.Address, val.ValAddress, sdk.NewDecFromInt(cli.DefaultTokens), sdk.NewCoin(sdk.DefaultBondDenom, cli.DefaultTokens)),
},
Pagination: &query.PageResponse{Total: 1},
},
@ -775,7 +776,7 @@ func (s *IntegrationTestSuite) TestQueryPoolGRPC() {
&types.QueryPoolResponse{
Pool: types.Pool{
NotBondedTokens: sdk.NewInt(10),
BondedTokens: sdk.NewInt(199999990),
BondedTokens: cli.DefaultTokens.Mul(sdk.NewInt(2)).Sub(sdk.NewInt(10)),
},
},
},

View File

@ -1,6 +1,8 @@
package staking_test
import (
"math/big"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
@ -12,6 +14,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
func init() {
sdk.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
}
// nolint:deadcode,unused,varcheck
var (
priv1 = secp256k1.GenPrivKey()
@ -48,8 +54,8 @@ func getBaseSimappWithCustomKeeper() (*codec.LegacyAmino, *simapp.SimApp, sdk.Co
}
// generateAddresses generates numAddrs of normal AccAddrs and ValAddrs
func generateAddresses(app *simapp.SimApp, ctx sdk.Context, numAddrs int, accAmount int64) ([]sdk.AccAddress, []sdk.ValAddress) {
addrDels := simapp.AddTestAddrsIncremental(app, ctx, numAddrs, sdk.NewInt(accAmount))
func generateAddresses(app *simapp.SimApp, ctx sdk.Context, numAddrs int, accAmount sdk.Int) ([]sdk.AccAddress, []sdk.ValAddress) {
addrDels := simapp.AddTestAddrsIncremental(app, ctx, numAddrs, accAmount)
addrVals := simapp.ConvertAddrsToValAddrs(addrDels)
return addrDels, addrVals

View File

@ -21,7 +21,7 @@ import (
func bootstrapGenesisTest(t *testing.T, power int64, numAddrs int) (*simapp.SimApp, sdk.Context, []sdk.AccAddress) {
_, app, ctx := getBaseSimappWithCustomKeeper()
addrDels, _ := generateAddresses(app, ctx, numAddrs, 10000)
addrDels, _ := generateAddresses(app, ctx, numAddrs, sdk.NewInt(10000))
amt := sdk.TokensFromConsensusPower(power)
totalSupply := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), amt.MulRaw(int64(len(addrDels)))))

View File

@ -26,7 +26,7 @@ import (
"github.com/golang/protobuf/proto"
)
func bootstrapHandlerGenesisTest(t *testing.T, power int64, numAddrs int, accAmount int64) (*simapp.SimApp, sdk.Context, []sdk.AccAddress, []sdk.ValAddress) {
func bootstrapHandlerGenesisTest(t *testing.T, power int64, numAddrs int, accAmount sdk.Int) (*simapp.SimApp, sdk.Context, []sdk.AccAddress, []sdk.ValAddress) {
_, app, ctx := getBaseSimappWithCustomKeeper()
addrDels, addrVals := generateAddresses(app, ctx, numAddrs, accAmount)
@ -46,7 +46,7 @@ func bootstrapHandlerGenesisTest(t *testing.T, power int64, numAddrs int, accAmo
func TestValidatorByPowerIndex(t *testing.T) {
initPower := int64(1000000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 10, 10000000000000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 10, sdk.TokensFromConsensusPower(initPower))
validatorAddr, validatorAddr3 := valAddrs[0], valAddrs[1]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -123,7 +123,7 @@ func TestValidatorByPowerIndex(t *testing.T) {
func TestDuplicatesMsgCreateValidator(t *testing.T) {
initPower := int64(1000000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 10, 10000000000000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 10, sdk.TokensFromConsensusPower(initPower))
addr1, addr2 := valAddrs[0], valAddrs[1]
pk1, pk2 := PKs[0], PKs[1]
@ -144,13 +144,13 @@ func TestDuplicatesMsgCreateValidator(t *testing.T) {
assert.Equal(t, types.Description{}, validator.Description)
// two validators can't have the same operator address
tstaking.CreateValidator(addr1, pk2, valTokens.Int64(), false)
tstaking.CreateValidator(addr1, pk2, valTokens, false)
// two validators can't have the same pubkey
tstaking.CreateValidator(addr2, pk1, valTokens.Int64(), false)
tstaking.CreateValidator(addr2, pk1, valTokens, false)
// must have different pubkey and operator
tstaking.CreateValidator(addr2, pk2, valTokens.Int64(), true)
tstaking.CreateValidator(addr2, pk2, valTokens, true)
// must end-block
updates, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
@ -170,7 +170,8 @@ func TestDuplicatesMsgCreateValidator(t *testing.T) {
}
func TestInvalidPubKeyTypeMsgCreateValidator(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 1, 1000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 1, sdk.TokensFromConsensusPower(initPower))
ctx = ctx.WithConsensusParams(&abci.ConsensusParams{
Validator: &tmproto.ValidatorParams{PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519}},
})
@ -180,11 +181,11 @@ func TestInvalidPubKeyTypeMsgCreateValidator(t *testing.T) {
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
// invalid pukKey type should not be allowed
tstaking.CreateValidator(addr, invalidPk, 10, false)
tstaking.CreateValidator(addr, invalidPk, sdk.NewInt(10), false)
}
func TestBothPubKeyTypesMsgCreateValidator(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, sdk.NewInt(1000))
ctx = ctx.WithConsensusParams(&abci.ConsensusParams{
Validator: &tmproto.ValidatorParams{PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519, tmtypes.ABCIPubKeyTypeSecp256k1}},
})
@ -209,13 +210,14 @@ func TestBothPubKeyTypesMsgCreateValidator(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(*testing.T) {
tstaking.CreateValidator(tc.addr, tc.pk, 10, true)
tstaking.CreateValidator(tc.addr, tc.pk, sdk.NewInt(10), true)
})
}
}
func TestLegacyValidatorDelegations(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 100000000)
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
valAddr := valAddrs[0]
@ -236,7 +238,7 @@ func TestLegacyValidatorDelegations(t *testing.T) {
require.Equal(t, bondAmount, validator.BondedTokens())
// delegate tokens to the validator
tstaking.Delegate(delAddr, valAddr, bondAmount.Int64())
tstaking.Delegate(delAddr, valAddr, bondAmount)
// verify validator bonded shares
validator = tstaking.CheckValidator(valAddr, types.Bonded, false)
@ -265,7 +267,7 @@ func TestLegacyValidatorDelegations(t *testing.T) {
require.Equal(t, bondAmount, validator.DelegatorShares.RoundInt())
// verify the validator can still self-delegate
tstaking.Delegate(sdk.AccAddress(valAddr), valAddr, bondAmount.Int64())
tstaking.Delegate(sdk.AccAddress(valAddr), valAddr, bondAmount)
// verify validator bonded shares
validator, found = app.StakingKeeper.GetValidator(ctx, valAddr)
@ -277,7 +279,7 @@ func TestLegacyValidatorDelegations(t *testing.T) {
app.StakingKeeper.Unjail(ctx, valConsAddr)
// verify the validator can now accept delegations
tstaking.Delegate(delAddr, valAddr, bondAmount.Int64())
tstaking.Delegate(delAddr, valAddr, bondAmount)
// verify validator bonded shares
validator, found = app.StakingKeeper.GetValidator(ctx, valAddr)
@ -295,7 +297,7 @@ func TestLegacyValidatorDelegations(t *testing.T) {
func TestIncrementsMsgDelegate(t *testing.T) {
initPower := int64(1000)
initBond := sdk.TokensFromConsensusPower(initPower)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, 1000000000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
params := app.StakingKeeper.GetParams(ctx)
validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1]
@ -318,12 +320,12 @@ func TestIncrementsMsgDelegate(t *testing.T) {
require.Equal(t, bondAmount, bond.Shares.RoundInt())
bondedTokens := app.StakingKeeper.TotalBondedTokens(ctx)
require.Equal(t, bondAmount.Int64(), bondedTokens.Int64())
require.Equal(t, bondAmount, bondedTokens)
for i := int64(0); i < 5; i++ {
ctx = ctx.WithBlockHeight(i)
tstaking.Ctx = ctx
tstaking.Delegate(delegatorAddr, validatorAddr, bondAmount.Int64())
tstaking.Delegate(delegatorAddr, validatorAddr, bondAmount)
//Check that the accounts and the bond account have the appropriate values
validator, found := app.StakingKeeper.GetValidator(ctx, validatorAddr)
@ -354,13 +356,13 @@ func TestIncrementsMsgDelegate(t *testing.T) {
func TestEditValidatorDecreaseMinSelfDelegation(t *testing.T) {
initPower := int64(100)
initBond := sdk.TokensFromConsensusPower(100)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 1, 1000000000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 1, sdk.TokensFromConsensusPower(initPower))
validatorAddr := valAddrs[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
// create validator
msgCreateValidator := tstaking.CreateValidatorMsg(validatorAddr, PKs[0], initBond.Int64())
msgCreateValidator := tstaking.CreateValidatorMsg(validatorAddr, PKs[0], initBond)
msgCreateValidator.MinSelfDelegation = sdk.NewInt(2)
tstaking.Handle(msgCreateValidator, true)
@ -386,12 +388,12 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) {
initPower := int64(100)
initBond := sdk.TokensFromConsensusPower(100)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, 1000000000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
validatorAddr := valAddrs[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
// create validator
msgCreateValidator := tstaking.CreateValidatorMsg(validatorAddr, PKs[0], initBond.Int64())
msgCreateValidator := tstaking.CreateValidatorMsg(validatorAddr, PKs[0], initBond)
msgCreateValidator.MinSelfDelegation = sdk.NewInt(2)
tstaking.Handle(msgCreateValidator, true)
@ -416,7 +418,7 @@ func TestEditValidatorIncreaseMinSelfDelegationBeyondCurrentBond(t *testing.T) {
func TestIncrementsMsgUnbond(t *testing.T) {
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, 1000000000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
params := app.StakingKeeper.GetParams(ctx)
denom := params.BondDenom
@ -428,7 +430,7 @@ func TestIncrementsMsgUnbond(t *testing.T) {
// initial balance
amt1 := app.BankKeeper.GetBalance(ctx, delegatorAddr, denom).Amount
tstaking.Delegate(delegatorAddr, validatorAddr, initBond.Int64())
tstaking.Delegate(delegatorAddr, validatorAddr, initBond)
// balance should have been subtracted after delegation
amt2 := app.BankKeeper.GetBalance(ctx, delegatorAddr, denom).Amount
@ -473,13 +475,13 @@ func TestIncrementsMsgUnbond(t *testing.T) {
gotDelegatorShares := validator.DelegatorShares.RoundInt()
gotDelegatorAcc := app.BankKeeper.GetBalance(ctx, delegatorAddr, params.BondDenom).Amount
require.Equal(t, expBond.Int64(), gotBond.Int64(),
require.Equal(t, expBond, gotBond,
"i: %v\nexpBond: %v\ngotBond: %v\nvalidator: %v\nbond: %v\n",
i, expBond, gotBond, validator, bond)
require.Equal(t, expDelegatorShares.Int64(), gotDelegatorShares.Int64(),
require.Equal(t, expDelegatorShares, gotDelegatorShares,
"i: %v\nexpDelegatorShares: %v\ngotDelegatorShares: %v\nvalidator: %v\nbond: %v\n",
i, expDelegatorShares, gotDelegatorShares, validator, bond)
require.Equal(t, expDelegatorAcc.Int64(), gotDelegatorAcc.Int64(),
require.Equal(t, expDelegatorAcc, gotDelegatorAcc,
"i: %v\nexpDelegatorAcc: %v\ngotDelegatorAcc: %v\nvalidator: %v\nbond: %v\n",
i, expDelegatorAcc, gotDelegatorAcc, validator, bond)
}
@ -505,7 +507,7 @@ func TestIncrementsMsgUnbond(t *testing.T) {
func TestMultipleMsgCreateValidator(t *testing.T) {
initPower := int64(1000)
initTokens := sdk.TokensFromConsensusPower(initPower)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 3, 1000000000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 3, sdk.TokensFromConsensusPower(initPower))
params := app.StakingKeeper.GetParams(ctx)
blockTime := time.Now().UTC()
@ -526,7 +528,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) {
// bond them all
amt := sdk.TokensFromConsensusPower(10)
for i, validatorAddr := range validatorAddrs {
tstaking.CreateValidator(validatorAddr, PKs[i], amt.Int64(), true)
tstaking.CreateValidator(validatorAddr, PKs[i], amt, true)
// verify that the account is bonded
validators := app.StakingKeeper.GetValidators(ctx, 100)
require.Equal(t, (i + 1), len(validators))
@ -536,7 +538,7 @@ func TestMultipleMsgCreateValidator(t *testing.T) {
balanceGot := app.BankKeeper.GetBalance(ctx, delegatorAddrs[i], params.BondDenom).Amount
require.Equal(t, i+1, len(validators), "expected %d validators got %d, validators: %v", i+1, len(validators), validators)
require.Equal(t, amt, val.DelegatorShares.RoundInt(), "expected %d shares, got %d", 10, val.DelegatorShares)
require.Equal(t, amt, val.DelegatorShares.RoundInt(), "expected %d shares, got %d", amt, val.DelegatorShares)
require.Equal(t, balanceExpd, balanceGot, "expected account to have %d, got %d", balanceExpd, balanceGot)
}
@ -573,17 +575,18 @@ func TestMultipleMsgCreateValidator(t *testing.T) {
}
func TestMultipleMsgDelegate(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 50, 1000000000)
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 50, sdk.TokensFromConsensusPower(initPower))
validatorAddr, delegatorAddrs := valAddrs[0], delAddrs[1:]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
var amount int64 = 10
// first make a validator
tstaking.CreateValidator(validatorAddr, PKs[0], amount, true)
tstaking.CreateValidator(validatorAddr, PKs[0], sdk.NewInt(amount), true)
// delegate multiple parties
for _, delegatorAddr := range delegatorAddrs {
tstaking.Delegate(delegatorAddr, validatorAddr, 10)
tstaking.Delegate(delegatorAddr, validatorAddr, sdk.NewInt(10))
tstaking.CheckDelegator(delegatorAddr, validatorAddr, true)
}
@ -606,14 +609,15 @@ func TestMultipleMsgDelegate(t *testing.T) {
}
func TestJailValidator(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000)
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
var amt int64 = 10
// create the validator and delegate
tstaking.CreateValidator(validatorAddr, PKs[0], amt, true)
tstaking.Delegate(delegatorAddr, validatorAddr, amt)
tstaking.CreateValidator(validatorAddr, PKs[0], sdk.NewInt(amt), true)
tstaking.Delegate(delegatorAddr, validatorAddr, sdk.NewInt(amt))
// unbond the validators bond portion
unamt := sdk.NewInt(amt)
@ -640,11 +644,12 @@ func TestJailValidator(t *testing.T) {
tstaking.Ctx = ctx
// verify that the pubkey can now be reused
tstaking.CreateValidator(validatorAddr, PKs[0], amt, true)
tstaking.CreateValidator(validatorAddr, PKs[0], sdk.NewInt(amt), true)
}
func TestValidatorQueue(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000)
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -655,7 +660,7 @@ func TestValidatorQueue(t *testing.T) {
// create the validator and make a bond
amt := tstaking.CreateValidatorWithValPower(validatorAddr, PKs[0], 10, true)
tstaking.Delegate(delegatorAddr, validatorAddr, amt.Int64())
tstaking.Delegate(delegatorAddr, validatorAddr, amt)
staking.EndBlocker(ctx, app.StakingKeeper)
// unbond the all self-delegation to put validator in unbonding state
@ -690,7 +695,8 @@ func TestValidatorQueue(t *testing.T) {
}
func TestUnbondingPeriod(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 1, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 1, sdk.TokensFromConsensusPower(initPower))
validatorAddr := valAddrs[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -728,13 +734,14 @@ func TestUnbondingPeriod(t *testing.T) {
}
func TestUnbondingFromUnbondingValidator(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000)
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
validatorAddr, delegatorAddr := valAddrs[0], delAddrs[1]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
// create the validator and delegate
tstaking.CreateValidator(validatorAddr, PKs[0], 10, true)
tstaking.Delegate(delegatorAddr, validatorAddr, 10)
tstaking.CreateValidator(validatorAddr, PKs[0], sdk.NewInt(10), true)
tstaking.Delegate(delegatorAddr, validatorAddr, sdk.NewInt(10))
// unbond the validators bond portion
unbondAmt := sdk.NewInt(10)
@ -760,7 +767,8 @@ func TestUnbondingFromUnbondingValidator(t *testing.T) {
}
func TestRedelegationPeriod(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
validatorAddr, validatorAddr2 := valAddrs[0], valAddrs[1]
denom := app.StakingKeeper.GetParams(ctx).BondDenom
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -773,13 +781,13 @@ func TestRedelegationPeriod(t *testing.T) {
amt1 := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount
// create the validators
tstaking.CreateValidator(validatorAddr, PKs[0], 10, true)
tstaking.CreateValidator(validatorAddr, PKs[0], sdk.NewInt(10), true)
// balance should have been subtracted after creation
amt2 := app.BankKeeper.GetBalance(ctx, sdk.AccAddress(validatorAddr), denom).Amount
require.Equal(t, amt1.Sub(sdk.NewInt(10)).Int64(), amt2.Int64(), "expected coins to be subtracted")
require.Equal(t, amt1.Sub(sdk.NewInt(10)), amt2, "expected coins to be subtracted")
tstaking.CreateValidator(validatorAddr2, PKs[1], 10, true)
tstaking.CreateValidator(validatorAddr2, PKs[1], sdk.NewInt(10), true)
bal1 := app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(validatorAddr))
// begin redelegate
@ -810,7 +818,8 @@ func TestRedelegationPeriod(t *testing.T) {
}
func TestTransitiveRedelegation(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 3, sdk.TokensFromConsensusPower(initPower))
val1, val2, val3 := valAddrs[0], valAddrs[1], valAddrs[2]
blockTime := time.Now().UTC()
@ -818,9 +827,9 @@ func TestTransitiveRedelegation(t *testing.T) {
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
// create the validators
tstaking.CreateValidator(val1, PKs[0], 10, true)
tstaking.CreateValidator(val2, PKs[1], 10, true)
tstaking.CreateValidator(val3, PKs[2], 10, true)
tstaking.CreateValidator(val1, PKs[0], sdk.NewInt(10), true)
tstaking.CreateValidator(val2, PKs[1], sdk.NewInt(10), true)
tstaking.CreateValidator(val3, PKs[2], sdk.NewInt(10), true)
// begin redelegate
redAmt := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))
@ -843,7 +852,8 @@ func TestTransitiveRedelegation(t *testing.T) {
}
func TestMultipleRedelegationAtSameTime(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
valAddr := valAddrs[0]
valAddr2 := valAddrs[1]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -855,7 +865,7 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) {
// create the validators
valTokens := tstaking.CreateValidatorWithValPower(valAddr, PKs[0], 10, true)
tstaking.CreateValidator(valAddr2, PKs[1], valTokens.Int64(), true)
tstaking.CreateValidator(valAddr2, PKs[1], valTokens, true)
// end block to bond them
staking.EndBlocker(ctx, app.StakingKeeper)
@ -886,7 +896,8 @@ func TestMultipleRedelegationAtSameTime(t *testing.T) {
}
func TestMultipleRedelegationAtUniqueTimes(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 2, sdk.TokensFromConsensusPower(initPower))
valAddr := valAddrs[0]
valAddr2 := valAddrs[1]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -898,7 +909,7 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) {
// create the validators
valTokens := tstaking.CreateValidatorWithValPower(valAddr, PKs[0], 10, true)
tstaking.CreateValidator(valAddr2, PKs[1], valTokens.Int64(), true)
tstaking.CreateValidator(valAddr2, PKs[1], valTokens, true)
// end block to bond them
staking.EndBlocker(ctx, app.StakingKeeper)
@ -932,7 +943,8 @@ func TestMultipleRedelegationAtUniqueTimes(t *testing.T) {
}
func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 1, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 1, sdk.TokensFromConsensusPower(initPower))
valAddr := valAddrs[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -971,7 +983,8 @@ func TestMultipleUnbondingDelegationAtSameTime(t *testing.T) {
}
func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 1, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 1, sdk.TokensFromConsensusPower(initPower))
valAddr := valAddrs[0]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
@ -1018,7 +1031,8 @@ func TestMultipleUnbondingDelegationAtUniqueTimes(t *testing.T) {
}
func TestUnbondingWhenExcessValidators(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000)
initPower := int64(1000)
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 3, sdk.TokensFromConsensusPower(initPower))
val1 := valAddrs[0]
val2 := valAddrs[1]
val3 := valAddrs[2]
@ -1057,16 +1071,17 @@ func TestUnbondingWhenExcessValidators(t *testing.T) {
}
func TestBondUnbondRedelegateSlashTwice(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000)
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 3, sdk.TokensFromConsensusPower(initPower))
valA, valB, del := valAddrs[0], valAddrs[1], delAddrs[2]
consAddr0 := sdk.ConsAddress(PKs[0].Address())
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
valTokens := tstaking.CreateValidatorWithValPower(valA, PKs[0], 10, true)
tstaking.CreateValidator(valB, PKs[1], valTokens.Int64(), true)
tstaking.CreateValidator(valB, PKs[1], valTokens, true)
// delegate 10 stake
tstaking.Delegate(del, valA, valTokens.Int64())
tstaking.Delegate(del, valA, valTokens)
// apply Tendermint updates
updates, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
@ -1161,7 +1176,8 @@ func TestInvalidMsg(t *testing.T) {
}
func TestInvalidCoinDenom(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 3, 1000000000)
initPower := int64(1000)
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, initPower, 3, sdk.TokensFromConsensusPower(initPower))
valA, valB, delAddr := valAddrs[0], valAddrs[1], delAddrs[2]
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)

View File

@ -1,6 +1,7 @@
package keeper_test
import (
"math/big"
"testing"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@ -16,6 +17,10 @@ var (
PKs = simapp.CreateTestPubKeys(500)
)
func init() {
sdk.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
}
// createTestInput Returns a simapp with custom StakingKeeper
// to avoid messing with the hooks.
func createTestInput() (*codec.LegacyAmino, *simapp.SimApp, sdk.Context) {

View File

@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
"github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
)
func (suite *KeeperTestSuite) TestGRPCQueryValidators() {
@ -727,7 +728,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryValidatorUnbondingDelegations() {
}
func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers []int64) ([]sdk.AccAddress, []sdk.ValAddress, []types.Validator) {
addrs := simapp.AddTestAddrsIncremental(app, ctx, 5, sdk.NewInt(300000000))
addrs := simapp.AddTestAddrsIncremental(app, ctx, 5, sdk.TokensFromConsensusPower(300))
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
pks := simapp.CreateTestPubKeys(5)
@ -751,9 +752,12 @@ func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers
app.StakingKeeper.SetNewValidatorByPowerIndex(ctx, val1)
app.StakingKeeper.SetNewValidatorByPowerIndex(ctx, val2)
_, _ = app.StakingKeeper.Delegate(ctx, addrs[0], sdk.TokensFromConsensusPower(powers[0]), types.Unbonded, val1, true)
_, _ = app.StakingKeeper.Delegate(ctx, addrs[1], sdk.TokensFromConsensusPower(powers[1]), types.Unbonded, val2, true)
_, _ = app.StakingKeeper.Delegate(ctx, addrs[0], sdk.TokensFromConsensusPower(powers[2]), types.Unbonded, val2, true)
_, err := app.StakingKeeper.Delegate(ctx, addrs[0], sdk.TokensFromConsensusPower(powers[0]), types.Unbonded, val1, true)
require.NoError(t, err)
_, err = app.StakingKeeper.Delegate(ctx, addrs[1], sdk.TokensFromConsensusPower(powers[1]), types.Unbonded, val2, true)
require.NoError(t, err)
_, err = app.StakingKeeper.Delegate(ctx, addrs[0], sdk.TokensFromConsensusPower(powers[2]), types.Unbonded, val2, true)
require.NoError(t, err)
applyValidatorSetUpdates(t, ctx, app.StakingKeeper, -1)
return addrs, valAddrs, vals

View File

@ -209,14 +209,16 @@ func (k msgServer) Delegate(goCtx context.Context, msg *types.MsgDelegate) (*typ
return nil, err
}
defer func() {
telemetry.IncrCounter(1, types.ModuleName, "delegate")
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", msg.Type()},
float32(msg.Amount.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
)
}()
if msg.Amount.Amount.IsInt64() {
defer func() {
telemetry.IncrCounter(1, types.ModuleName, "delegate")
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", msg.Type()},
float32(msg.Amount.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
)
}()
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
@ -268,14 +270,16 @@ func (k msgServer) BeginRedelegate(goCtx context.Context, msg *types.MsgBeginRed
return nil, err
}
defer func() {
telemetry.IncrCounter(1, types.ModuleName, "redelegate")
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", msg.Type()},
float32(msg.Amount.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
)
}()
if msg.Amount.Amount.IsInt64() {
defer func() {
telemetry.IncrCounter(1, types.ModuleName, "redelegate")
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", msg.Type()},
float32(msg.Amount.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
)
}()
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
@ -325,14 +329,16 @@ func (k msgServer) Undelegate(goCtx context.Context, msg *types.MsgUndelegate) (
return nil, err
}
defer func() {
telemetry.IncrCounter(1, types.ModuleName, "undelegate")
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", msg.Type()},
float32(msg.Amount.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
)
}()
if msg.Amount.Amount.IsInt64() {
defer func() {
telemetry.IncrCounter(1, types.ModuleName, "undelegate")
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", msg.Type()},
float32(msg.Amount.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", msg.Amount.Denom)},
)
}()
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(

View File

@ -90,13 +90,13 @@ func TestSlashUnbondingDelegation(t *testing.T) {
// unbonding started prior to the infraction height, stakw didn't contribute
slashAmount := app.StakingKeeper.SlashUnbondingDelegation(ctx, ubd, 1, fraction)
require.Equal(t, int64(0), slashAmount.Int64())
require.True(t, slashAmount.Equal(sdk.NewInt(0)))
// after the expiration time, no longer eligible for slashing
ctx = ctx.WithBlockHeader(tmproto.Header{Time: time.Unix(10, 0)})
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
slashAmount = app.StakingKeeper.SlashUnbondingDelegation(ctx, ubd, 0, fraction)
require.Equal(t, int64(0), slashAmount.Int64())
require.True(t, slashAmount.Equal(sdk.NewInt(0)))
// test valid slash, before expiration timestamp and to which stake contributed
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx)
@ -104,7 +104,7 @@ func TestSlashUnbondingDelegation(t *testing.T) {
ctx = ctx.WithBlockHeader(tmproto.Header{Time: time.Unix(0, 0)})
app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
slashAmount = app.StakingKeeper.SlashUnbondingDelegation(ctx, ubd, 0, fraction)
require.Equal(t, int64(5), slashAmount.Int64())
require.True(t, slashAmount.Equal(sdk.NewInt(5)))
ubd, found := app.StakingKeeper.GetUnbondingDelegation(ctx, addrDels[0], addrVals[0])
require.True(t, found)
require.Len(t, ubd.Entries, 1)
@ -116,7 +116,7 @@ func TestSlashUnbondingDelegation(t *testing.T) {
require.Equal(t, sdk.NewInt(5), ubd.Entries[0].Balance)
newUnbondedPoolBalances := app.BankKeeper.GetAllBalances(ctx, notBondedPool.GetAddress())
diffTokens := oldUnbondedPoolBalances.Sub(newUnbondedPoolBalances)
require.Equal(t, int64(5), diffTokens.AmountOf(app.StakingKeeper.BondDenom(ctx)).Int64())
require.True(t, diffTokens.AmountOf(app.StakingKeeper.BondDenom(ctx)).Equal(sdk.NewInt(5)))
}
// tests slashRedelegation
@ -147,7 +147,7 @@ func TestSlashRedelegation(t *testing.T) {
validator, found := app.StakingKeeper.GetValidator(ctx, addrVals[1])
require.True(t, found)
slashAmount := app.StakingKeeper.SlashRedelegation(ctx, validator, rd, 1, fraction)
require.Equal(t, int64(0), slashAmount.Int64())
require.True(t, slashAmount.Equal(sdk.NewInt(0)))
// after the expiration time, no longer eligible for slashing
ctx = ctx.WithBlockHeader(tmproto.Header{Time: time.Unix(10, 0)})
@ -155,7 +155,7 @@ func TestSlashRedelegation(t *testing.T) {
validator, found = app.StakingKeeper.GetValidator(ctx, addrVals[1])
require.True(t, found)
slashAmount = app.StakingKeeper.SlashRedelegation(ctx, validator, rd, 0, fraction)
require.Equal(t, int64(0), slashAmount.Int64())
require.True(t, slashAmount.Equal(sdk.NewInt(0)))
balances = app.BankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())
@ -165,7 +165,7 @@ func TestSlashRedelegation(t *testing.T) {
validator, found = app.StakingKeeper.GetValidator(ctx, addrVals[1])
require.True(t, found)
slashAmount = app.StakingKeeper.SlashRedelegation(ctx, validator, rd, 0, fraction)
require.Equal(t, int64(5), slashAmount.Int64())
require.True(t, slashAmount.Equal(sdk.NewInt(5)))
rd, found = app.StakingKeeper.GetRedelegation(ctx, addrDels[0], addrVals[0], addrVals[1])
require.True(t, found)
require.Len(t, rd.Entries, 1)

View File

@ -349,19 +349,19 @@ func TestGetValidatorSortingUnmixed(t *testing.T) {
app, ctx, addrs, _ := bootstrapValidatorTest(t, 1000, 20)
// initialize some validators into the state
amts := []int64{
0,
100 * sdk.PowerReduction.Int64(),
1 * sdk.PowerReduction.Int64(),
400 * sdk.PowerReduction.Int64(),
200 * sdk.PowerReduction.Int64()}
amts := []sdk.Int{
sdk.NewIntFromUint64(0),
sdk.PowerReduction.MulRaw(100),
sdk.PowerReduction,
sdk.PowerReduction.MulRaw(400),
sdk.PowerReduction.MulRaw(200)}
n := len(amts)
var validators [5]types.Validator
for i, amt := range amts {
validators[i] = teststaking.NewValidator(t, sdk.ValAddress(addrs[i]), PKs[i])
validators[i].Status = types.Bonded
validators[i].Tokens = sdk.NewInt(amt)
validators[i].DelegatorShares = sdk.NewDec(amt)
validators[i].Tokens = amt
validators[i].DelegatorShares = sdk.NewDecFromInt(amt)
keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[i], true)
}
@ -445,19 +445,19 @@ func TestGetValidatorSortingMixed(t *testing.T) {
app.StakingKeeper.SetParams(ctx, params)
// initialize some validators into the state
amts := []int64{
0,
100 * sdk.PowerReduction.Int64(),
1 * sdk.PowerReduction.Int64(),
400 * sdk.PowerReduction.Int64(),
200 * sdk.PowerReduction.Int64()}
amts := []sdk.Int{
sdk.NewIntFromUint64(0),
sdk.PowerReduction.MulRaw(100),
sdk.PowerReduction,
sdk.PowerReduction.MulRaw(400),
sdk.PowerReduction.MulRaw(200)}
var validators [5]types.Validator
for i, amt := range amts {
validators[i] = teststaking.NewValidator(t, sdk.ValAddress(addrs[i]), PKs[i])
validators[i].DelegatorShares = sdk.NewDec(amt)
validators[i].DelegatorShares = sdk.NewDecFromInt(amt)
validators[i].Status = types.Bonded
validators[i].Tokens = sdk.NewInt(amt)
validators[i].Tokens = amt
keeper.TestingUpdateValidator(app.StakingKeeper, ctx, validators[i], true)
}

View File

@ -0,0 +1,11 @@
package simulation_test
import (
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func init() {
sdk.PowerReduction = sdk.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil))
}

View File

@ -82,9 +82,9 @@ func TestSimulateMsgCreateValidator(t *testing.T) {
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
require.True(t, operationMsg.OK)
require.Equal(t, "0.170063593193511020", msg.Commission.MaxChangeRate.String())
require.Equal(t, "0.660000000000000000", msg.Commission.MaxRate.String())
require.Equal(t, "0.047464127245687382", msg.Commission.Rate.String())
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)
@ -120,10 +120,10 @@ func TestSimulateMsgEditValidator(t *testing.T) {
require.True(t, operationMsg.OK)
require.Equal(t, "0.280623462081924936", msg.CommissionRate.String())
require.Equal(t, "jLxzIivHSl", msg.Description.Moniker)
require.Equal(t, "rBqDOTtGTO", msg.Description.Identity)
require.Equal(t, "BSpYuLyYgg", msg.Description.Website)
require.Equal(t, "wNbeHVIkPZ", msg.Description.SecurityContact)
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)
@ -158,7 +158,7 @@ func TestSimulateMsgDelegate(t *testing.T) {
require.True(t, operationMsg.OK)
require.Equal(t, "cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r", msg.DelegatorAddress)
require.Equal(t, "4896096", msg.Amount.Amount.String())
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)
@ -203,7 +203,7 @@ func TestSimulateMsgUndelegate(t *testing.T) {
require.True(t, operationMsg.OK)
require.Equal(t, "cosmos1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7u4x9a0", msg.DelegatorAddress)
require.Equal(t, "560969", msg.Amount.Amount.String())
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)
@ -252,7 +252,7 @@ func TestSimulateMsgBeginRedelegate(t *testing.T) {
require.True(t, operationMsg.OK)
require.Equal(t, "cosmos12gwd9jchc69wck8dhstxgwz3z8qs8yv67ps8mu", msg.DelegatorAddress)
require.Equal(t, "692322", msg.Amount.Amount.String())
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)
@ -263,6 +263,7 @@ func TestSimulateMsgBeginRedelegate(t *testing.T) {
// 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{})
@ -308,7 +309,7 @@ func getTestingValidator(t *testing.T, app *simapp.SimApp, ctx sdk.Context, acco
require.NoError(t, err)
validator.DelegatorShares = sdk.NewDec(100)
validator.Tokens = sdk.NewInt(1000000)
validator.Tokens = sdk.TokensFromConsensusPower(100)
app.StakingKeeper.SetValidator(ctx, validator)

View File

@ -32,8 +32,8 @@ func NewHelper(t *testing.T, ctx sdk.Context, k keeper.Keeper) *Helper {
}
// CreateValidator calls handler to create a new staking validator
func (sh *Helper) CreateValidator(addr sdk.ValAddress, pk cryptotypes.PubKey, stakeAmount int64, ok bool) {
coin := sdk.NewCoin(sh.Denom, sdk.NewInt(stakeAmount))
func (sh *Helper) CreateValidator(addr sdk.ValAddress, pk cryptotypes.PubKey, stakeAmount sdk.Int, ok bool) {
coin := sdk.NewCoin(sh.Denom, stakeAmount)
sh.createValidator(addr, pk, coin, ok)
}
@ -47,8 +47,8 @@ func (sh *Helper) CreateValidatorWithValPower(addr sdk.ValAddress, pk cryptotype
}
// CreateValidatorMsg returns a message used to create validator in this service.
func (sh *Helper) CreateValidatorMsg(addr sdk.ValAddress, pk cryptotypes.PubKey, stakeAmount int64) *stakingtypes.MsgCreateValidator {
coin := sdk.NewCoin(sh.Denom, sdk.NewInt(stakeAmount))
func (sh *Helper) CreateValidatorMsg(addr sdk.ValAddress, pk cryptotypes.PubKey, stakeAmount sdk.Int) *stakingtypes.MsgCreateValidator {
coin := sdk.NewCoin(sh.Denom, stakeAmount)
msg, err := stakingtypes.NewMsgCreateValidator(addr, pk, coin, stakingtypes.Description{}, sh.Commission, sdk.OneInt())
require.NoError(sh.t, err)
return msg
@ -61,8 +61,8 @@ func (sh *Helper) createValidator(addr sdk.ValAddress, pk cryptotypes.PubKey, co
}
// Delegate calls handler to delegate stake for a validator
func (sh *Helper) Delegate(delegator sdk.AccAddress, val sdk.ValAddress, amount int64) {
coin := sdk.NewCoin(sh.Denom, sdk.NewInt(amount))
func (sh *Helper) Delegate(delegator sdk.AccAddress, val sdk.ValAddress, amount sdk.Int) {
coin := sdk.NewCoin(sh.Denom, amount)
msg := stakingtypes.NewMsgDelegate(delegator, val, coin)
sh.Handle(msg, true)
}