736 lines
30 KiB
Go
736 lines
30 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
func TestGetValidatorSortingMixed(t *testing.T) {
|
|
ctx, _, bk, keeper, _ := CreateTestInput(t, false, 1000)
|
|
bondedPool := keeper.GetBondedPool(ctx)
|
|
notBondedPool := keeper.GetNotBondedPool(ctx)
|
|
|
|
bk.SetBalances(ctx, bondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(501))))
|
|
bk.SetBalances(ctx, notBondedPool.GetAddress(), sdk.NewCoins(sdk.NewCoin(keeper.BondDenom(ctx), sdk.TokensFromConsensusPower(0))))
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool)
|
|
|
|
// now 2 max resValidators
|
|
params := keeper.GetParams(ctx)
|
|
params.MaxValidators = 2
|
|
keeper.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()}
|
|
|
|
var validators [5]types.Validator
|
|
for i, amt := range amts {
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
validators[i].DelegatorShares = sdk.NewDec(amt)
|
|
validators[i].Status = sdk.Bonded
|
|
validators[i].Tokens = sdk.NewInt(amt)
|
|
TestingUpdateValidator(keeper, ctx, validators[i], true)
|
|
}
|
|
|
|
val0, found := keeper.GetValidator(ctx, sdk.ValAddress(Addrs[0]))
|
|
require.True(t, found)
|
|
val1, found := keeper.GetValidator(ctx, sdk.ValAddress(Addrs[1]))
|
|
require.True(t, found)
|
|
val2, found := keeper.GetValidator(ctx, sdk.ValAddress(Addrs[2]))
|
|
require.True(t, found)
|
|
val3, found := keeper.GetValidator(ctx, sdk.ValAddress(Addrs[3]))
|
|
require.True(t, found)
|
|
val4, found := keeper.GetValidator(ctx, sdk.ValAddress(Addrs[4]))
|
|
require.True(t, found)
|
|
require.Equal(t, sdk.Bonded, val0.Status)
|
|
require.Equal(t, sdk.Unbonding, val1.Status)
|
|
require.Equal(t, sdk.Unbonding, val2.Status)
|
|
require.Equal(t, sdk.Bonded, val3.Status)
|
|
require.Equal(t, sdk.Bonded, val4.Status)
|
|
|
|
// first make sure everything made it in to the gotValidator group
|
|
resValidators := keeper.GetBondedValidatorsByPower(ctx)
|
|
// The validators returned should match the max validators
|
|
assert.Equal(t, 2, len(resValidators))
|
|
assert.Equal(t, sdk.NewInt(400).Mul(sdk.PowerReduction), resValidators[0].BondedTokens(), "%v", resValidators)
|
|
assert.Equal(t, sdk.NewInt(200).Mul(sdk.PowerReduction), resValidators[1].BondedTokens(), "%v", resValidators)
|
|
assert.Equal(t, validators[3].OperatorAddress, resValidators[0].OperatorAddress, "%v", resValidators)
|
|
assert.Equal(t, validators[4].OperatorAddress, resValidators[1].OperatorAddress, "%v", resValidators)
|
|
}
|
|
|
|
// TODO separate out into multiple tests
|
|
func TestGetValidatorsEdgeCases(t *testing.T) {
|
|
ctx, _, bk, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
// set max validators to 2
|
|
params := keeper.GetParams(ctx)
|
|
nMax := uint32(2)
|
|
params.MaxValidators = nMax
|
|
keeper.SetParams(ctx, params)
|
|
|
|
// initialize some validators into the state
|
|
powers := []int64{0, 100, 400, 400}
|
|
var validators [4]types.Validator
|
|
for i, power := range powers {
|
|
moniker := fmt.Sprintf("val#%d", int64(i))
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{Moniker: moniker})
|
|
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
notBondedPool := keeper.GetNotBondedPool(ctx)
|
|
balances := bk.GetAllBalances(ctx, notBondedPool.GetAddress())
|
|
require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(sdk.NewCoin(params.BondDenom, tokens))))
|
|
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
|
validators[i] = TestingUpdateValidator(keeper, ctx, validators[i], true)
|
|
}
|
|
|
|
// ensure that the first two bonded validators are the largest validators
|
|
resValidators := keeper.GetBondedValidatorsByPower(ctx)
|
|
require.Equal(t, nMax, uint32(len(resValidators)))
|
|
assert.True(ValEq(t, validators[2], resValidators[0]))
|
|
assert.True(ValEq(t, validators[3], resValidators[1]))
|
|
|
|
// delegate 500 tokens to validator 0
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[0])
|
|
delTokens := sdk.TokensFromConsensusPower(500)
|
|
validators[0], _ = validators[0].AddTokensFromDel(delTokens)
|
|
notBondedPool := keeper.GetNotBondedPool(ctx)
|
|
|
|
newTokens := sdk.NewCoins()
|
|
balances := bk.GetAllBalances(ctx, notBondedPool.GetAddress())
|
|
require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(newTokens...)))
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
|
|
|
// test that the two largest validators are
|
|
// a) validator 0 with 500 tokens
|
|
// b) validator 2 with 400 tokens (delegated before validator 3)
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], true)
|
|
resValidators = keeper.GetBondedValidatorsByPower(ctx)
|
|
require.Equal(t, nMax, uint32(len(resValidators)))
|
|
assert.True(ValEq(t, validators[0], resValidators[0]))
|
|
assert.True(ValEq(t, validators[2], resValidators[1]))
|
|
|
|
// A validator which leaves the bonded validator set due to a decrease in voting power,
|
|
// then increases to the original voting power, does not get its spot back in the
|
|
// case of a tie.
|
|
//
|
|
// Order of operations for this test:
|
|
// - validator 3 enter validator set with 1 new token
|
|
// - validator 3 removed validator set by removing 201 tokens (validator 2 enters)
|
|
// - validator 3 adds 200 tokens (equal to validator 2 now) and does not get its spot back
|
|
|
|
// validator 3 enters bonded validator set
|
|
ctx = ctx.WithBlockHeight(40)
|
|
|
|
validators[3] = keeper.mustGetValidator(ctx, validators[3].OperatorAddress)
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[3])
|
|
validators[3], _ = validators[3].AddTokensFromDel(sdk.TokensFromConsensusPower(1))
|
|
|
|
notBondedPool = keeper.GetNotBondedPool(ctx)
|
|
newTokens = sdk.NewCoins(sdk.NewCoin(params.BondDenom, sdk.TokensFromConsensusPower(1)))
|
|
balances = bk.GetAllBalances(ctx, notBondedPool.GetAddress())
|
|
require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(newTokens...)))
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
|
|
|
validators[3] = TestingUpdateValidator(keeper, ctx, validators[3], true)
|
|
resValidators = keeper.GetBondedValidatorsByPower(ctx)
|
|
require.Equal(t, nMax, uint32(len(resValidators)))
|
|
assert.True(ValEq(t, validators[0], resValidators[0]))
|
|
assert.True(ValEq(t, validators[3], resValidators[1]))
|
|
|
|
// validator 3 kicked out temporarily
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[3])
|
|
rmTokens := validators[3].TokensFromShares(sdk.NewDec(201)).TruncateInt()
|
|
validators[3], _ = validators[3].RemoveDelShares(sdk.NewDec(201))
|
|
|
|
bondedPool := keeper.GetBondedPool(ctx)
|
|
balances = bk.GetAllBalances(ctx, bondedPool.GetAddress())
|
|
require.NoError(t, bk.SetBalances(ctx, bondedPool.GetAddress(), balances.Add(sdk.NewCoin(params.BondDenom, rmTokens))))
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, bondedPool)
|
|
|
|
validators[3] = TestingUpdateValidator(keeper, ctx, validators[3], true)
|
|
resValidators = keeper.GetBondedValidatorsByPower(ctx)
|
|
require.Equal(t, nMax, uint32(len(resValidators)))
|
|
assert.True(ValEq(t, validators[0], resValidators[0]))
|
|
assert.True(ValEq(t, validators[2], resValidators[1]))
|
|
|
|
// validator 3 does not get spot back
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[3])
|
|
validators[3], _ = validators[3].AddTokensFromDel(sdk.NewInt(200))
|
|
|
|
notBondedPool = keeper.GetNotBondedPool(ctx)
|
|
balances = bk.GetAllBalances(ctx, notBondedPool.GetAddress())
|
|
require.NoError(t, bk.SetBalances(ctx, notBondedPool.GetAddress(), balances.Add(sdk.NewCoin(params.BondDenom, sdk.NewInt(200)))))
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, notBondedPool)
|
|
|
|
validators[3] = TestingUpdateValidator(keeper, ctx, validators[3], true)
|
|
resValidators = keeper.GetBondedValidatorsByPower(ctx)
|
|
require.Equal(t, nMax, uint32(len(resValidators)))
|
|
assert.True(ValEq(t, validators[0], resValidators[0]))
|
|
assert.True(ValEq(t, validators[2], resValidators[1]))
|
|
_, exists := keeper.GetValidator(ctx, validators[3].OperatorAddress)
|
|
require.True(t, exists)
|
|
}
|
|
|
|
func TestValidatorBondHeight(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
// now 2 max resValidators
|
|
params := keeper.GetParams(ctx)
|
|
params.MaxValidators = 2
|
|
keeper.SetParams(ctx, params)
|
|
|
|
// initialize some validators into the state
|
|
var validators [3]types.Validator
|
|
validators[0] = types.NewValidator(sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0], types.Description{})
|
|
validators[1] = types.NewValidator(sdk.ValAddress(Addrs[1]), PKs[1], types.Description{})
|
|
validators[2] = types.NewValidator(sdk.ValAddress(Addrs[2]), PKs[2], types.Description{})
|
|
|
|
tokens0 := sdk.TokensFromConsensusPower(200)
|
|
tokens1 := sdk.TokensFromConsensusPower(100)
|
|
tokens2 := sdk.TokensFromConsensusPower(100)
|
|
validators[0], _ = validators[0].AddTokensFromDel(tokens0)
|
|
validators[1], _ = validators[1].AddTokensFromDel(tokens1)
|
|
validators[2], _ = validators[2].AddTokensFromDel(tokens2)
|
|
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], true)
|
|
|
|
////////////////////////////////////////
|
|
// If two validators both increase to the same voting power in the same block,
|
|
// the one with the first transaction should become bonded
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], true)
|
|
validators[2] = TestingUpdateValidator(keeper, ctx, validators[2], true)
|
|
|
|
resValidators := keeper.GetBondedValidatorsByPower(ctx)
|
|
require.Equal(t, uint32(len(resValidators)), params.MaxValidators)
|
|
|
|
assert.True(ValEq(t, validators[0], resValidators[0]))
|
|
assert.True(ValEq(t, validators[1], resValidators[1]))
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[1])
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[2])
|
|
delTokens := sdk.TokensFromConsensusPower(50)
|
|
validators[1], _ = validators[1].AddTokensFromDel(delTokens)
|
|
validators[2], _ = validators[2].AddTokensFromDel(delTokens)
|
|
validators[2] = TestingUpdateValidator(keeper, ctx, validators[2], true)
|
|
resValidators = keeper.GetBondedValidatorsByPower(ctx)
|
|
require.Equal(t, params.MaxValidators, uint32(len(resValidators)))
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], true)
|
|
assert.True(ValEq(t, validators[0], resValidators[0]))
|
|
assert.True(ValEq(t, validators[2], resValidators[1]))
|
|
}
|
|
|
|
func TestFullValidatorSetPowerChange(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
params := keeper.GetParams(ctx)
|
|
max := 2
|
|
params.MaxValidators = uint32(2)
|
|
keeper.SetParams(ctx, params)
|
|
|
|
// initialize some validators into the state
|
|
powers := []int64{0, 100, 400, 400, 200}
|
|
var validators [5]types.Validator
|
|
for i, power := range powers {
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
TestingUpdateValidator(keeper, ctx, validators[i], true)
|
|
}
|
|
for i := range powers {
|
|
var found bool
|
|
validators[i], found = keeper.GetValidator(ctx, validators[i].OperatorAddress)
|
|
require.True(t, found)
|
|
}
|
|
assert.Equal(t, sdk.Unbonded, validators[0].Status)
|
|
assert.Equal(t, sdk.Unbonding, validators[1].Status)
|
|
assert.Equal(t, sdk.Bonded, validators[2].Status)
|
|
assert.Equal(t, sdk.Bonded, validators[3].Status)
|
|
assert.Equal(t, sdk.Unbonded, validators[4].Status)
|
|
resValidators := keeper.GetBondedValidatorsByPower(ctx)
|
|
assert.Equal(t, max, len(resValidators))
|
|
assert.True(ValEq(t, validators[2], resValidators[0])) // in the order of txs
|
|
assert.True(ValEq(t, validators[3], resValidators[1]))
|
|
|
|
// test a swap in voting power
|
|
|
|
tokens := sdk.TokensFromConsensusPower(600)
|
|
validators[0], _ = validators[0].AddTokensFromDel(tokens)
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], true)
|
|
resValidators = keeper.GetBondedValidatorsByPower(ctx)
|
|
assert.Equal(t, max, len(resValidators))
|
|
assert.True(ValEq(t, validators[0], resValidators[0]))
|
|
assert.True(ValEq(t, validators[2], resValidators[1]))
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesAllNone(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
powers := []int64{10, 20}
|
|
var validators [2]types.Validator
|
|
for i, power := range powers {
|
|
valPubKey := PKs[i+1]
|
|
valAddr := sdk.ValAddress(valPubKey.Address().Bytes())
|
|
|
|
validators[i] = types.NewValidator(valAddr, valPubKey, types.Description{})
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
}
|
|
|
|
// test from nothing to something
|
|
// tendermintUpdate set: {} -> {c1, c3}
|
|
require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
keeper.SetValidator(ctx, validators[0])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[0])
|
|
keeper.SetValidator(ctx, validators[1])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[1])
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
assert.Equal(t, 2, len(updates))
|
|
validators[0], _ = keeper.GetValidator(ctx, validators[0].OperatorAddress)
|
|
validators[1], _ = keeper.GetValidator(ctx, validators[1].OperatorAddress)
|
|
assert.Equal(t, validators[0].ABCIValidatorUpdate(), updates[1])
|
|
assert.Equal(t, validators[1].ABCIValidatorUpdate(), updates[0])
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesIdentical(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
powers := []int64{10, 20}
|
|
var validators [2]types.Validator
|
|
for i, power := range powers {
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
}
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
require.Equal(t, 2, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// test identical,
|
|
// tendermintUpdate set: {} -> {}
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesSingleValueChange(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
powers := []int64{10, 20}
|
|
var validators [2]types.Validator
|
|
for i, power := range powers {
|
|
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
}
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
require.Equal(t, 2, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// test single value change
|
|
// tendermintUpdate set: {} -> {c1'}
|
|
validators[0].Status = sdk.Bonded
|
|
validators[0].Tokens = sdk.TokensFromConsensusPower(600)
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
require.Equal(t, 1, len(updates))
|
|
require.Equal(t, validators[0].ABCIValidatorUpdate(), updates[0])
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesMultipleValueChange(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
powers := []int64{10, 20}
|
|
var validators [2]types.Validator
|
|
for i, power := range powers {
|
|
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
}
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
require.Equal(t, 2, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// test multiple value change
|
|
// tendermintUpdate set: {c1, c3} -> {c1', c3'}
|
|
delTokens1 := sdk.TokensFromConsensusPower(190)
|
|
delTokens2 := sdk.TokensFromConsensusPower(80)
|
|
validators[0], _ = validators[0].AddTokensFromDel(delTokens1)
|
|
validators[1], _ = validators[1].AddTokensFromDel(delTokens2)
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
require.Equal(t, 2, len(updates))
|
|
require.Equal(t, validators[0].ABCIValidatorUpdate(), updates[0])
|
|
require.Equal(t, validators[1].ABCIValidatorUpdate(), updates[1])
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesInserted(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
powers := []int64{10, 20, 5, 15, 25}
|
|
var validators [5]types.Validator
|
|
for i, power := range powers {
|
|
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
}
|
|
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
require.Equal(t, 2, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// test validtor added at the beginning
|
|
// tendermintUpdate set: {} -> {c0}
|
|
keeper.SetValidator(ctx, validators[2])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[2])
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
validators[2], _ = keeper.GetValidator(ctx, validators[2].OperatorAddress)
|
|
require.Equal(t, 1, len(updates))
|
|
require.Equal(t, validators[2].ABCIValidatorUpdate(), updates[0])
|
|
|
|
// test validtor added at the beginning
|
|
// tendermintUpdate set: {} -> {c0}
|
|
keeper.SetValidator(ctx, validators[3])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[3])
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
validators[3], _ = keeper.GetValidator(ctx, validators[3].OperatorAddress)
|
|
require.Equal(t, 1, len(updates))
|
|
require.Equal(t, validators[3].ABCIValidatorUpdate(), updates[0])
|
|
|
|
// test validtor added at the end
|
|
// tendermintUpdate set: {} -> {c0}
|
|
keeper.SetValidator(ctx, validators[4])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[4])
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
validators[4], _ = keeper.GetValidator(ctx, validators[4].OperatorAddress)
|
|
require.Equal(t, 1, len(updates))
|
|
require.Equal(t, validators[4].ABCIValidatorUpdate(), updates[0])
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesWithCliffValidator(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
params := types.DefaultParams()
|
|
params.MaxValidators = 2
|
|
keeper.SetParams(ctx, params)
|
|
|
|
powers := []int64{10, 20, 5}
|
|
var validators [5]types.Validator
|
|
for i, power := range powers {
|
|
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
}
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
require.Equal(t, 2, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// test validator added at the end but not inserted in the valset
|
|
// tendermintUpdate set: {} -> {}
|
|
TestingUpdateValidator(keeper, ctx, validators[2], false)
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
require.Equal(t, 0, len(updates))
|
|
|
|
// test validator change its power and become a gotValidator (pushing out an existing)
|
|
// tendermintUpdate set: {} -> {c0, c4}
|
|
require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
tokens := sdk.TokensFromConsensusPower(10)
|
|
validators[2], _ = validators[2].AddTokensFromDel(tokens)
|
|
keeper.SetValidator(ctx, validators[2])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[2])
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
validators[2], _ = keeper.GetValidator(ctx, validators[2].OperatorAddress)
|
|
require.Equal(t, 2, len(updates), "%v", updates)
|
|
require.Equal(t, validators[0].ABCIValidatorUpdateZero(), updates[1])
|
|
require.Equal(t, validators[2].ABCIValidatorUpdate(), updates[0])
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesPowerDecrease(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
|
|
powers := []int64{100, 100}
|
|
var validators [2]types.Validator
|
|
for i, power := range powers {
|
|
|
|
validators[i] = types.NewValidator(sdk.ValAddress(Addrs[i]), PKs[i], types.Description{})
|
|
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
}
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
require.Equal(t, 2, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// check initial power
|
|
require.Equal(t, int64(100), validators[0].GetConsensusPower())
|
|
require.Equal(t, int64(100), validators[1].GetConsensusPower())
|
|
|
|
// test multiple value change
|
|
// tendermintUpdate set: {c1, c3} -> {c1', c3'}
|
|
delTokens1 := sdk.TokensFromConsensusPower(20)
|
|
delTokens2 := sdk.TokensFromConsensusPower(30)
|
|
validators[0], _ = validators[0].RemoveDelShares(delTokens1.ToDec())
|
|
validators[1], _ = validators[1].RemoveDelShares(delTokens2.ToDec())
|
|
validators[0] = TestingUpdateValidator(keeper, ctx, validators[0], false)
|
|
validators[1] = TestingUpdateValidator(keeper, ctx, validators[1], false)
|
|
|
|
// power has changed
|
|
require.Equal(t, int64(80), validators[0].GetConsensusPower())
|
|
require.Equal(t, int64(70), validators[1].GetConsensusPower())
|
|
|
|
// Tendermint updates should reflect power change
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
require.Equal(t, 2, len(updates))
|
|
require.Equal(t, validators[0].ABCIValidatorUpdate(), updates[0])
|
|
require.Equal(t, validators[1].ABCIValidatorUpdate(), updates[1])
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesNewValidator(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
params := keeper.GetParams(ctx)
|
|
params.MaxValidators = uint32(3)
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
powers := []int64{100, 100}
|
|
var validators [2]types.Validator
|
|
|
|
// initialize some validators into the state
|
|
for i, power := range powers {
|
|
|
|
valPubKey := PKs[i+1]
|
|
valAddr := sdk.ValAddress(valPubKey.Address().Bytes())
|
|
|
|
validators[i] = types.NewValidator(valAddr, valPubKey, types.Description{})
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
keeper.SetValidator(ctx, validators[i])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[i])
|
|
}
|
|
|
|
// verify initial Tendermint updates are correct
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
require.Equal(t, len(validators), len(updates))
|
|
validators[0], _ = keeper.GetValidator(ctx, validators[0].OperatorAddress)
|
|
validators[1], _ = keeper.GetValidator(ctx, validators[1].OperatorAddress)
|
|
require.Equal(t, validators[0].ABCIValidatorUpdate(), updates[0])
|
|
require.Equal(t, validators[1].ABCIValidatorUpdate(), updates[1])
|
|
|
|
require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// update initial validator set
|
|
for i, power := range powers {
|
|
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[i])
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
|
|
keeper.SetValidator(ctx, validators[i])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[i])
|
|
}
|
|
|
|
// add a new validator that goes from zero power, to non-zero power, back to
|
|
// zero power
|
|
valPubKey := PKs[len(validators)+1]
|
|
valAddr := sdk.ValAddress(valPubKey.Address().Bytes())
|
|
amt := sdk.NewInt(100)
|
|
|
|
validator := types.NewValidator(valAddr, valPubKey, types.Description{})
|
|
validator, _ = validator.AddTokensFromDel(amt)
|
|
|
|
keeper.SetValidator(ctx, validator)
|
|
|
|
validator, _ = validator.RemoveDelShares(amt.ToDec())
|
|
keeper.SetValidator(ctx, validator)
|
|
keeper.SetValidatorByPowerIndex(ctx, validator)
|
|
|
|
// add a new validator that increases in power
|
|
valPubKey = PKs[len(validators)+2]
|
|
valAddr = sdk.ValAddress(valPubKey.Address().Bytes())
|
|
|
|
validator = types.NewValidator(valAddr, valPubKey, types.Description{})
|
|
tokens := sdk.TokensFromConsensusPower(500)
|
|
validator, _ = validator.AddTokensFromDel(tokens)
|
|
keeper.SetValidator(ctx, validator)
|
|
keeper.SetValidatorByPowerIndex(ctx, validator)
|
|
|
|
// verify initial Tendermint updates are correct
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
validator, _ = keeper.GetValidator(ctx, validator.OperatorAddress)
|
|
validators[0], _ = keeper.GetValidator(ctx, validators[0].OperatorAddress)
|
|
validators[1], _ = keeper.GetValidator(ctx, validators[1].OperatorAddress)
|
|
require.Equal(t, len(validators)+1, len(updates))
|
|
require.Equal(t, validator.ABCIValidatorUpdate(), updates[0])
|
|
require.Equal(t, validators[0].ABCIValidatorUpdate(), updates[1])
|
|
require.Equal(t, validators[1].ABCIValidatorUpdate(), updates[2])
|
|
}
|
|
|
|
func TestApplyAndReturnValidatorSetUpdatesBondTransition(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
params := keeper.GetParams(ctx)
|
|
params.MaxValidators = uint32(2)
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
powers := []int64{100, 200, 300}
|
|
var validators [3]types.Validator
|
|
|
|
// initialize some validators into the state
|
|
for i, power := range powers {
|
|
moniker := fmt.Sprintf("%d", i)
|
|
valPubKey := PKs[i+1]
|
|
valAddr := sdk.ValAddress(valPubKey.Address().Bytes())
|
|
|
|
validators[i] = types.NewValidator(valAddr, valPubKey, types.Description{Moniker: moniker})
|
|
tokens := sdk.TokensFromConsensusPower(power)
|
|
validators[i], _ = validators[i].AddTokensFromDel(tokens)
|
|
keeper.SetValidator(ctx, validators[i])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[i])
|
|
}
|
|
|
|
// verify initial Tendermint updates are correct
|
|
updates := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
require.Equal(t, 2, len(updates))
|
|
validators[2], _ = keeper.GetValidator(ctx, validators[2].OperatorAddress)
|
|
validators[1], _ = keeper.GetValidator(ctx, validators[1].OperatorAddress)
|
|
require.Equal(t, validators[2].ABCIValidatorUpdate(), updates[0])
|
|
require.Equal(t, validators[1].ABCIValidatorUpdate(), updates[1])
|
|
|
|
require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// delegate to validator with lowest power but not enough to bond
|
|
ctx = ctx.WithBlockHeight(1)
|
|
|
|
var found bool
|
|
validators[0], found = keeper.GetValidator(ctx, validators[0].OperatorAddress)
|
|
require.True(t, found)
|
|
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[0])
|
|
tokens := sdk.TokensFromConsensusPower(1)
|
|
validators[0], _ = validators[0].AddTokensFromDel(tokens)
|
|
keeper.SetValidator(ctx, validators[0])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[0])
|
|
|
|
// verify initial Tendermint updates are correct
|
|
require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
|
|
// create a series of events that will bond and unbond the validator with
|
|
// lowest power in a single block context (height)
|
|
ctx = ctx.WithBlockHeight(2)
|
|
|
|
validators[1], found = keeper.GetValidator(ctx, validators[1].OperatorAddress)
|
|
require.True(t, found)
|
|
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[0])
|
|
validators[0], _ = validators[0].RemoveDelShares(validators[0].DelegatorShares)
|
|
keeper.SetValidator(ctx, validators[0])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[0])
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
require.Equal(t, 0, len(updates))
|
|
|
|
keeper.DeleteValidatorByPowerIndex(ctx, validators[1])
|
|
tokens = sdk.TokensFromConsensusPower(250)
|
|
validators[1], _ = validators[1].AddTokensFromDel(tokens)
|
|
keeper.SetValidator(ctx, validators[1])
|
|
keeper.SetValidatorByPowerIndex(ctx, validators[1])
|
|
|
|
// verify initial Tendermint updates are correct
|
|
updates = keeper.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
require.Equal(t, 1, len(updates))
|
|
require.Equal(t, validators[1].ABCIValidatorUpdate(), updates[0])
|
|
|
|
require.Equal(t, 0, len(keeper.ApplyAndReturnValidatorSetUpdates(ctx)))
|
|
}
|
|
|
|
func TestUpdateValidatorCommission(t *testing.T) {
|
|
ctx, _, _, keeper, _ := CreateTestInput(t, false, 1000)
|
|
ctx = ctx.WithBlockHeader(abci.Header{Time: time.Now().UTC()})
|
|
|
|
commission1 := types.NewCommissionWithTime(
|
|
sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(3, 1),
|
|
sdk.NewDecWithPrec(1, 1), time.Now().UTC().Add(time.Duration(-1)*time.Hour),
|
|
)
|
|
commission2 := types.NewCommission(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(3, 1), sdk.NewDecWithPrec(1, 1))
|
|
|
|
val1 := types.NewValidator(addrVals[0], PKs[0], types.Description{})
|
|
val2 := types.NewValidator(addrVals[1], PKs[1], types.Description{})
|
|
|
|
val1, _ = val1.SetInitialCommission(commission1)
|
|
val2, _ = val2.SetInitialCommission(commission2)
|
|
|
|
keeper.SetValidator(ctx, val1)
|
|
keeper.SetValidator(ctx, val2)
|
|
|
|
testCases := []struct {
|
|
validator types.Validator
|
|
newRate sdk.Dec
|
|
expectedErr bool
|
|
}{
|
|
{val1, sdk.ZeroDec(), true},
|
|
{val2, sdk.NewDecWithPrec(-1, 1), true},
|
|
{val2, sdk.NewDecWithPrec(4, 1), true},
|
|
{val2, sdk.NewDecWithPrec(3, 1), true},
|
|
{val2, sdk.NewDecWithPrec(2, 1), false},
|
|
}
|
|
|
|
for i, tc := range testCases {
|
|
commission, err := keeper.UpdateValidatorCommission(ctx, tc.validator, tc.newRate)
|
|
|
|
if tc.expectedErr {
|
|
require.Error(t, err, "expected error for test case #%d with rate: %s", i, tc.newRate)
|
|
} else {
|
|
tc.validator.Commission = commission
|
|
keeper.SetValidator(ctx, tc.validator)
|
|
val, found := keeper.GetValidator(ctx, tc.validator.OperatorAddress)
|
|
|
|
require.True(t, found,
|
|
"expected to find validator for test case #%d with rate: %s", i, tc.newRate,
|
|
)
|
|
require.NoError(t, err,
|
|
"unexpected error for test case #%d with rate: %s", i, tc.newRate,
|
|
)
|
|
require.Equal(t, tc.newRate, val.Commission.Rate,
|
|
"expected new validator commission rate for test case #%d with rate: %s", i, tc.newRate,
|
|
)
|
|
require.Equal(t, ctx.BlockHeader().Time, val.Commission.UpdateTime,
|
|
"expected new validator commission update time for test case #%d with rate: %s", i, tc.newRate,
|
|
)
|
|
}
|
|
}
|
|
}
|