Skip to content

Commit

Permalink
Merge pull request #7897 from sikkatech/fix_power_reduction_test_cast…
Browse files Browse the repository at this point in the history
…_int64

Fix power reduction test cast int64
  • Loading branch information
sunnya97 authored Nov 16, 2020
2 parents be10bcb + 479ad6b commit 10d5ce3
Show file tree
Hide file tree
Showing 24 changed files with 244 additions and 186 deletions.
12 changes: 7 additions & 5 deletions x/bank/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
)
}
}
}()

Expand Down
12 changes: 6 additions & 6 deletions x/distribution/keeper/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions x/distribution/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions x/distribution/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
)
}
}
}()

Expand Down Expand Up @@ -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)},
)
}
}
}()

Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions x/slashing/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions x/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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++
Expand All @@ -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))
Expand All @@ -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()))
Expand Down
4 changes: 2 additions & 2 deletions x/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions x/staking/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
},
Expand Down Expand Up @@ -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{},
},
Expand Down Expand Up @@ -891,16 +891,16 @@ 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",
[]string{
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 {
Expand Down
5 changes: 3 additions & 2 deletions x/staking/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions x/staking/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions x/staking/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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},
},
Expand Down Expand Up @@ -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},
},
Expand Down Expand Up @@ -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)),
},
},
},
Expand Down
Loading

0 comments on commit 10d5ce3

Please sign in to comment.