stake keeper renaming/refactor

This commit is contained in:
rigelrozanski 2018-05-09 23:47:58 -04:00
parent 06cf8b2934
commit aff7d28bb0
4 changed files with 118 additions and 156 deletions

View File

@ -2,14 +2,12 @@ package stake
import ( import (
"bytes" "bytes"
"sort"
"github.com/cosmos/cosmos-sdk/store" "github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/bank"
abci "github.com/tendermint/abci/types" abci "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
) )
// keeper of the staking store // keeper of the staking store
@ -36,25 +34,6 @@ func NewKeeper(cdc *wire.Codec, key sdk.StoreKey, ck bank.Keeper, codespace sdk.
return keeper return keeper
} }
// get the current in-block validator operation counter
func (k Keeper) getIntraTxCounter(ctx sdk.Context) int16 {
store := ctx.KVStore(k.storeKey)
b := store.Get(IntraTxCounterKey)
if b == nil {
return 0
}
var counter int16
k.cdc.MustUnmarshalBinary(b, &counter)
return counter
}
// set the current in-block validator operation counter
func (k Keeper) setIntraTxCounter(ctx sdk.Context, counter int16) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinary(counter)
store.Set(IntraTxCounterKey, bz)
}
//_________________________________________________________________________ //_________________________________________________________________________
// get a single candidate // get a single candidate
@ -121,7 +100,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
} }
// delete the old record in the power ordered list // delete the old record in the power ordered list
store.Delete(GetValidatorsByPowerKey(oldCandidate.validator())) store.Delete(GetValidatorsBondedByPowerKey(oldCandidate.validator()))
} }
// set the new candidate record // set the new candidate record
@ -131,15 +110,15 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) {
// update the list ordered by voting power // update the list ordered by voting power
validator := candidate.validator() validator := candidate.validator()
bzVal := k.cdc.MustMarshalBinary(validator) bzVal := k.cdc.MustMarshalBinary(validator)
store.Set(GetValidatorsByPowerKey(validator), bzVal) store.Set(GetValidatorsBondedByPowerKey(validator), bzVal)
// add to the validators to update list if is already a validator // add to the validators to update list if is already a validator
if store.Get(GetValidatorsBondedKey(candidate.PubKey)) != nil { if store.Get(GetValidatorsBondedBondedKey(candidate.PubKey)) != nil {
bzAbci := k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc)) bzAbci := k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc))
store.Set(GetAccUpdateValidatorKey(address), bzAbci) store.Set(GetAccUpdateValidatorKey(address), bzAbci)
// also update the current validator store // also update the current validator store
store.Set(GetValidatorsBondedKey(validator.PubKey), bzVal) store.Set(GetValidatorsBondedBondedKey(validator.PubKey), bzVal)
return return
} }
@ -159,22 +138,22 @@ func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) {
// delete the old candidate record // delete the old candidate record
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
store.Delete(GetCandidateKey(address)) store.Delete(GetCandidateKey(address))
store.Delete(GetValidatorsByPowerKey(candidate.validator())) store.Delete(GetValidatorsBondedByPowerKey(candidate.validator()))
// delete from current and power weighted validator groups if the validator // delete from current and power weighted validator groups if the validator
// exists and add validator with zero power to the validator updates // exists and add validator with zero power to the validator updates
if store.Get(GetValidatorsBondedKey(candidate.PubKey)) == nil { if store.Get(GetValidatorsBondedBondedKey(candidate.PubKey)) == nil {
return return
} }
bz := k.cdc.MustMarshalBinary(candidate.validator().abciValidatorZero(k.cdc)) bz := k.cdc.MustMarshalBinary(candidate.validator().abciValidatorZero(k.cdc))
store.Set(GetAccUpdateValidatorKey(address), bz) store.Set(GetAccUpdateValidatorKey(address), bz)
store.Delete(GetValidatorsBondedKey(candidate.PubKey)) store.Delete(GetValidatorsBondedBondedKey(candidate.PubKey))
} }
//___________________________________________________________________________ //___________________________________________________________________________
// get the group of the most current validators // get the group of the bonded validators
func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) { func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []Validator) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
// add the actual validator power sorted store // add the actual validator power sorted store
@ -194,21 +173,23 @@ func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) {
return validators[:i] // trim return validators[:i] // trim
} }
// Only used for testing // get the group of bonded validators sorted by power-rank
// get the group of the most current validators func (k Keeper) GetValidatorsBondedByPower(ctx sdk.Context) []Validator {
func (k Keeper) getValidatorsOrdered(ctx sdk.Context) []Validator { maxValidators := k.GetParams(ctx).MaxValidators
vals := k.GetValidators(ctx) validators = make([]Validator, maxValidators)
sort.Sort(sort.Reverse(validators(vals))) iterator = store.ReverseSubspaceIterator(ValidatorsByPowerKey) // largest to smallest
return vals i := 0
} for ; ; i++ {
if !iterator.Valid() || i > int(maxValidators-1) {
// Is the address provided a part of the current validator set? iterator.Close()
func (k Keeper) IsValidator(ctx sdk.Context, pk crypto.PubKey) bool { break
store := ctx.KVStore(k.storeKey) }
if store.Get(GetValidatorsBondedKey(pk)) == nil { bz := iterator.Value()
return false var validator Validator
k.cdc.MustUnmarshalBinary(bz, &validator)
validators[i] = validator
iterator.Next()
} }
return true
} }
// This function add's (or doesn't add) a candidate record to the validator group // This function add's (or doesn't add) a candidate record to the validator group
@ -219,7 +200,7 @@ func (k Keeper) IsValidator(ctx sdk.Context, pk crypto.PubKey) bool {
// the current validator records are updated in store with the // the current validator records are updated in store with the
// ValidatorsBondedKey. This store is used to determine if a candidate is a // ValidatorsBondedKey. This store is used to determine if a candidate is a
// validator without needing to iterate over the subspace as we do in // validator without needing to iterate over the subspace as we do in
// GetValidators // GetValidatorsBonded
func (k Keeper) addNewValidatorOrNot(ctx sdk.Context, store sdk.KVStore, address sdk.Address) { func (k Keeper) addNewValidatorOrNot(ctx sdk.Context, store sdk.KVStore, address sdk.Address) {
// clear the current validators store, add to the ToKickOut temp store // clear the current validators store, add to the ToKickOut temp store
@ -256,7 +237,7 @@ func (k Keeper) addNewValidatorOrNot(ctx sdk.Context, store sdk.KVStore, address
toKickOut[GetToKickOutValidatorKey(validator.Address)] = nil toKickOut[GetToKickOutValidatorKey(validator.Address)] = nil
// also add to the current validators group // also add to the current validators group
store.Set(GetValidatorsBondedKey(validator.PubKey), bz) store.Set(GetValidatorsBondedBondedKey(validator.PubKey), bz)
// MOST IMPORTANTLY, add to the accumulated changes if this is the modified candidate // MOST IMPORTANTLY, add to the accumulated changes if this is the modified candidate
if bytes.Equal(address, validator.Address) { if bytes.Equal(address, validator.Address) {
@ -318,10 +299,10 @@ func (k Keeper) GetTotalPrecommitVotingPower(ctx sdk.Context) sdk.Rat {
} }
//_________________________________________________________________________ //_________________________________________________________________________
// Accumulated updates to the validator set // Accumulated updates to the active/bonded validator set for tendermint
// get the most recently updated validators // get the most recently updated validators
func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []abci.Validator) { func (k Keeper) getValidatorsTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
iterator := store.SubspaceIterator(ValidatorsTendermintUpdatesKey) //smallest to largest iterator := store.SubspaceIterator(ValidatorsTendermintUpdatesKey) //smallest to largest
@ -336,7 +317,7 @@ func (k Keeper) getAccUpdateValidators(ctx sdk.Context) (updates []abci.Validato
} }
// remove all validator update entries after applied to Tendermint // remove all validator update entries after applied to Tendermint
func (k Keeper) clearAccUpdateValidators(ctx sdk.Context) { func (k Keeper) clearValidatorsTendermintUpdates(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey) store := ctx.KVStore(k.storeKey)
// delete subspace // delete subspace
@ -561,3 +542,24 @@ func (k Keeper) Iterate(delAddr sdk.Address, fn func(index int64, delegator sdk.
} }
iterator.Close() iterator.Close()
} }
//__________________________________________________________________________
// get the current in-block validator operation counter
func (k Keeper) getIntraTxCounter(ctx sdk.Context) int16 {
store := ctx.KVStore(k.storeKey)
b := store.Get(IntraTxCounterKey)
if b == nil {
return 0
}
var counter int16
k.cdc.MustUnmarshalBinary(b, &counter)
return counter
}
// set the current in-block validator operation counter
func (k Keeper) setIntraTxCounter(ctx sdk.Context, counter int16) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinary(counter)
store.Set(IntraTxCounterKey, bz)
}

View File

@ -31,7 +31,7 @@ func GetCandidateKey(addr sdk.Address) []byte {
} }
// get the key for the validator used in the power-store // get the key for the validator used in the power-store
func GetValidatorsByPowerKey(validator Validator) []byte { func GetValidatorsBondedByPowerKey(validator Validator) []byte {
powerBytes := []byte(validator.Power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first) powerBytes := []byte(validator.Power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)
// TODO ensure that the key will be a readable string.. probably should add seperators and have // TODO ensure that the key will be a readable string.. probably should add seperators and have
@ -47,12 +47,12 @@ func GetValidatorsByPowerKey(validator Validator) []byte {
} }
// get the key for the accumulated update validators // get the key for the accumulated update validators
func GetValidatorsTendermintUpdatesKey(addr sdk.Address) []byte { func GetValidatorsBondedTendermintUpdatesKey(addr sdk.Address) []byte {
return append(ValidatorsTendermintUpdatesKey, addr.Bytes()...) return append(ValidatorsTendermintUpdatesKey, addr.Bytes()...)
} }
// get the key for the current validator group, ordered like tendermint // get the key for the current validator group, ordered like tendermint
func GetValidatorsBondedKey(pk crypto.PubKey) []byte { func GetValidatorsBondedBondedKey(pk crypto.PubKey) []byte {
addr := pk.Address() addr := pk.Address()
return append(ValidatorsBondedKey, addr.Bytes()...) return append(ValidatorsBondedKey, addr.Bytes()...)
} }

View File

@ -179,7 +179,7 @@ func TestBond(t *testing.T) {
} }
// TODO seperate out into multiple tests // TODO seperate out into multiple tests
func TestGetValidators(t *testing.T) { func TestGetValidatorsBonded(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0) ctx, _, keeper := createTestInput(t, false, 0)
// initialize some candidates into the state // initialize some candidates into the state
@ -194,7 +194,7 @@ func TestGetValidators(t *testing.T) {
} }
// first make sure everything made it in to the validator group // first make sure everything made it in to the validator group
validators := keeper.getValidatorsOrdered(ctx) validators := keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
assert.Equal(t, sdk.NewRat(400), validators[0].Power, "%v", validators) assert.Equal(t, sdk.NewRat(400), validators[0].Power, "%v", validators)
assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators) assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators)
@ -210,7 +210,7 @@ func TestGetValidators(t *testing.T) {
// test a basic increase in voting power // test a basic increase in voting power
candidates[3].BondedShares = sdk.NewRat(500) candidates[3].BondedShares = sdk.NewRat(500)
keeper.setCandidate(ctx, candidates[3]) keeper.setCandidate(ctx, candidates[3])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
assert.Equal(t, sdk.NewRat(500), validators[0].Power, "%v", validators) assert.Equal(t, sdk.NewRat(500), validators[0].Power, "%v", validators)
assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators)
@ -218,7 +218,7 @@ func TestGetValidators(t *testing.T) {
// test a decrease in voting power // test a decrease in voting power
candidates[3].BondedShares = sdk.NewRat(300) candidates[3].BondedShares = sdk.NewRat(300)
keeper.setCandidate(ctx, candidates[3]) keeper.setCandidate(ctx, candidates[3])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
assert.Equal(t, sdk.NewRat(300), validators[0].Power, "%v", validators) assert.Equal(t, sdk.NewRat(300), validators[0].Power, "%v", validators)
assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators)
@ -228,7 +228,7 @@ func TestGetValidators(t *testing.T) {
candidates[3].BondedShares = sdk.NewRat(200) candidates[3].BondedShares = sdk.NewRat(200)
ctx = ctx.WithBlockHeight(10) ctx = ctx.WithBlockHeight(10)
keeper.setCandidate(ctx, candidates[3]) keeper.setCandidate(ctx, candidates[3])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
//assert.Equal(t, sdk.NewRat(200), validators[0].Power, "%v", validators) //assert.Equal(t, sdk.NewRat(200), validators[0].Power, "%v", validators)
//assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators) //assert.Equal(t, sdk.NewRat(200), validators[1].Power, "%v", validators)
@ -241,7 +241,7 @@ func TestGetValidators(t *testing.T) {
// no change in voting power - no change in sort // no change in voting power - no change in sort
ctx = ctx.WithBlockHeight(20) ctx = ctx.WithBlockHeight(20)
keeper.setCandidate(ctx, candidates[4]) keeper.setCandidate(ctx, candidates[4])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
//assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) //assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators)
//assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) //assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators)
@ -251,11 +251,11 @@ func TestGetValidators(t *testing.T) {
candidates[3].BondedShares = sdk.NewRat(300) candidates[3].BondedShares = sdk.NewRat(300)
candidates[4].BondedShares = sdk.NewRat(300) candidates[4].BondedShares = sdk.NewRat(300)
keeper.setCandidate(ctx, candidates[3]) keeper.setCandidate(ctx, candidates[3])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
ctx = ctx.WithBlockHeight(30) ctx = ctx.WithBlockHeight(30)
keeper.setCandidate(ctx, candidates[4]) keeper.setCandidate(ctx, candidates[4])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n, "%v", validators) require.Equal(t, len(validators), n, "%v", validators)
//assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) //assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators)
//assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) //assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators)
@ -264,7 +264,7 @@ func TestGetValidators(t *testing.T) {
// TODO seperate out into multiple tests // TODO seperate out into multiple tests
/* XXX FIX THESE TESTS /* XXX FIX THESE TESTS
func TestGetValidatorsEdgeCases(t *testing.T) { func TestGetValidatorsBondedEdgeCases(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0) ctx, _, keeper := createTestInput(t, false, 0)
// now 2 max validators // now 2 max validators
@ -285,7 +285,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
candidates[0].BondedShares = sdk.NewRat(500) candidates[0].BondedShares = sdk.NewRat(500)
keeper.setCandidate(ctx, candidates[0]) keeper.setCandidate(ctx, candidates[0])
validators := keeper.getValidatorsOrdered(ctx) validators := keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, uint16(len(validators)), params.MaxValidators) require.Equal(t, uint16(len(validators)), params.MaxValidators)
require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
// candidate 3 was set before candidate 4 // candidate 3 was set before candidate 4
@ -298,7 +298,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
//ref https://github.com/cosmos/cosmos-sdk/issues/582#issuecomment-380757108 //ref https://github.com/cosmos/cosmos-sdk/issues/582#issuecomment-380757108
candidates[4].BondedShares = sdk.NewRat(301) candidates[4].BondedShares = sdk.NewRat(301)
keeper.setCandidate(ctx, candidates[4]) keeper.setCandidate(ctx, candidates[4])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, uint16(len(validators)), params.MaxValidators) require.Equal(t, uint16(len(validators)), params.MaxValidators)
require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
require.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) require.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators)
@ -306,14 +306,14 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
// candidate 4 kicked out temporarily // candidate 4 kicked out temporarily
candidates[4].BondedShares = sdk.NewRat(200) candidates[4].BondedShares = sdk.NewRat(200)
keeper.setCandidate(ctx, candidates[4]) keeper.setCandidate(ctx, candidates[4])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, uint16(len(validators)), params.MaxValidators) require.Equal(t, uint16(len(validators)), params.MaxValidators)
require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators)
// candidate 4 does not get spot back // candidate 4 does not get spot back
candidates[4].BondedShares = sdk.NewRat(300) candidates[4].BondedShares = sdk.NewRat(300)
keeper.setCandidate(ctx, candidates[4]) keeper.setCandidate(ctx, candidates[4])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, uint16(len(validators)), params.MaxValidators) require.Equal(t, uint16(len(validators)), params.MaxValidators)
require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators)
@ -330,7 +330,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
candidates[2].BondedShares = sdk.NewRat(1000) candidates[2].BondedShares = sdk.NewRat(1000)
keeper.setCandidate(ctx, candidates[1]) keeper.setCandidate(ctx, candidates[1])
keeper.setCandidate(ctx, candidates[2]) keeper.setCandidate(ctx, candidates[2])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, uint16(len(validators)), params.MaxValidators) require.Equal(t, uint16(len(validators)), params.MaxValidators)
require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
require.Equal(t, candidates[1].Address, validators[1].Address, "%v", validators) require.Equal(t, candidates[1].Address, validators[1].Address, "%v", validators)
@ -338,7 +338,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
candidates[2].BondedShares = sdk.NewRat(1100) candidates[2].BondedShares = sdk.NewRat(1100)
keeper.setCandidate(ctx, candidates[2]) keeper.setCandidate(ctx, candidates[2])
keeper.setCandidate(ctx, candidates[1]) keeper.setCandidate(ctx, candidates[1])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, uint16(len(validators)), params.MaxValidators) require.Equal(t, uint16(len(validators)), params.MaxValidators)
require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
require.Equal(t, candidates[2].Address, validators[1].Address, "%v", validators) require.Equal(t, candidates[2].Address, validators[1].Address, "%v", validators)
@ -361,7 +361,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
// test a swap in voting power // test a swap in voting power
candidates[0].BondedShares = sdk.NewRat(600) candidates[0].BondedShares = sdk.NewRat(600)
keeper.setCandidate(ctx, candidates[0]) keeper.setCandidate(ctx, candidates[0])
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators) assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators)
assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
@ -373,7 +373,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
n = 2 n = 2
params.MaxValidators = uint16(n) params.MaxValidators = uint16(n)
keeper.setParams(ctx, params) keeper.setParams(ctx, params)
validators = keeper.getValidatorsOrdered(ctx) validators = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, len(validators), n) require.Equal(t, len(validators), n)
assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators) assert.Equal(t, sdk.NewRat(600), validators[0].Power, "%v", validators)
assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) assert.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators)
@ -383,7 +383,7 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
*/ */
// clear the tracked changes to the validator set // clear the tracked changes to the validator set
func TestClearAccUpdateValidators(t *testing.T) { func TestClearValidatorsTendermintUpdates(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0) ctx, _, keeper := createTestInput(t, false, 0)
amts := []int64{100, 400, 200} amts := []int64{100, 400, 200}
@ -395,15 +395,15 @@ func TestClearAccUpdateValidators(t *testing.T) {
keeper.setCandidate(ctx, candidates[i]) keeper.setCandidate(ctx, candidates[i])
} }
acc := keeper.getAccUpdateValidators(ctx) acc := keeper.getValidatorsTendermintUpdates(ctx)
assert.Equal(t, len(amts), len(acc)) assert.Equal(t, len(amts), len(acc))
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
acc = keeper.getAccUpdateValidators(ctx) acc = keeper.getValidatorsTendermintUpdates(ctx)
assert.Equal(t, 0, len(acc)) assert.Equal(t, 0, len(acc))
} }
// test the mechanism which keeps track of a validator set change // test the mechanism which keeps track of a validator set change
func TestGetAccUpdateValidators(t *testing.T) { func TestGetValidatorsTendermintUpdates(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0) ctx, _, keeper := createTestInput(t, false, 0)
params := defaultParams() params := defaultParams()
params.MaxValidators = 4 params.MaxValidators = 4
@ -427,15 +427,15 @@ func TestGetAccUpdateValidators(t *testing.T) {
// validator set: {} -> {c1, c3} // validator set: {} -> {c1, c3}
// accUpdate set: {} -> {c1, c3} // accUpdate set: {} -> {c1, c3}
assert.Equal(t, 0, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 0, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 0, len(keeper.GetValidators(ctx))) assert.Equal(t, 0, len(keeper.GetValidatorsBonded(ctx)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
keeper.setCandidate(ctx, candidatesIn[1]) keeper.setCandidate(ctx, candidatesIn[1])
keeper.setCandidate(ctx, candidatesIn[3]) keeper.setCandidate(ctx, candidatesIn[3])
vals := keeper.getValidatorsOrdered(ctx) // to init recent validator set vals := keeper.GetValidatorsBondedByPower(ctx) // to init recent validator set
require.Equal(t, 2, len(vals)) require.Equal(t, 2, len(vals))
acc := keeper.getAccUpdateValidators(ctx) acc := keeper.getValidatorsTendermintUpdates(ctx)
require.Equal(t, 2, len(acc)) require.Equal(t, 2, len(acc))
candidates := keeper.GetCandidates(ctx, 5) candidates := keeper.GetCandidates(ctx, 5)
require.Equal(t, 2, len(candidates)) require.Equal(t, 2, len(candidates))
@ -447,22 +447,22 @@ func TestGetAccUpdateValidators(t *testing.T) {
// test identical, // test identical,
// candidate set: {c1, c3} -> {c1, c3} // candidate set: {c1, c3} -> {c1, c3}
// accUpdate set: {} -> {} // accUpdate set: {} -> {}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
keeper.setCandidate(ctx, candidates[0]) keeper.setCandidate(ctx, candidates[0])
keeper.setCandidate(ctx, candidates[1]) keeper.setCandidate(ctx, candidates[1])
require.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) require.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
// test single value change // test single value change
// candidate set: {c1, c3} -> {c1', c3} // candidate set: {c1, c3} -> {c1', c3}
// accUpdate set: {} -> {c1'} // accUpdate set: {} -> {c1'}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
candidates[0].BondedShares = sdk.NewRat(600) candidates[0].BondedShares = sdk.NewRat(600)
keeper.setCandidate(ctx, candidates[0]) keeper.setCandidate(ctx, candidates[0])
@ -470,23 +470,23 @@ func TestGetAccUpdateValidators(t *testing.T) {
candidates = keeper.GetCandidates(ctx, 5) candidates = keeper.GetCandidates(ctx, 5)
require.Equal(t, 2, len(candidates)) require.Equal(t, 2, len(candidates))
assert.True(t, candidates[0].BondedShares.Equal(sdk.NewRat(600))) assert.True(t, candidates[0].BondedShares.Equal(sdk.NewRat(600)))
acc = keeper.getAccUpdateValidators(ctx) acc = keeper.getValidatorsTendermintUpdates(ctx)
require.Equal(t, 1, len(acc)) require.Equal(t, 1, len(acc))
assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0])
// test multiple value change // test multiple value change
// candidate set: {c1, c3} -> {c1', c3'} // candidate set: {c1, c3} -> {c1', c3'}
// accUpdate set: {c1, c3} -> {c1', c3'} // accUpdate set: {c1, c3} -> {c1', c3'}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
candidates[0].BondedShares = sdk.NewRat(200) candidates[0].BondedShares = sdk.NewRat(200)
candidates[1].BondedShares = sdk.NewRat(100) candidates[1].BondedShares = sdk.NewRat(100)
keeper.setCandidate(ctx, candidates[0]) keeper.setCandidate(ctx, candidates[0])
keeper.setCandidate(ctx, candidates[1]) keeper.setCandidate(ctx, candidates[1])
acc = keeper.getAccUpdateValidators(ctx) acc = keeper.getValidatorsTendermintUpdates(ctx)
require.Equal(t, 2, len(acc)) require.Equal(t, 2, len(acc))
candidates = keeper.GetCandidates(ctx, 5) candidates = keeper.GetCandidates(ctx, 5)
require.Equal(t, 2, len(candidates)) require.Equal(t, 2, len(candidates))
@ -496,12 +496,12 @@ func TestGetAccUpdateValidators(t *testing.T) {
// test validtor added at the beginning // test validtor added at the beginning
// candidate set: {c1, c3} -> {c0, c1, c3} // candidate set: {c1, c3} -> {c0, c1, c3}
// accUpdate set: {} -> {c0} // accUpdate set: {} -> {c0}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
keeper.setCandidate(ctx, candidatesIn[0]) keeper.setCandidate(ctx, candidatesIn[0])
acc = keeper.getAccUpdateValidators(ctx) acc = keeper.getValidatorsTendermintUpdates(ctx)
require.Equal(t, 1, len(acc)) require.Equal(t, 1, len(acc))
candidates = keeper.GetCandidates(ctx, 5) candidates = keeper.GetCandidates(ctx, 5)
require.Equal(t, 3, len(candidates)) require.Equal(t, 3, len(candidates))
@ -510,12 +510,12 @@ func TestGetAccUpdateValidators(t *testing.T) {
// test validator added at the middle // test validator added at the middle
// candidate set: {c0, c1, c3} -> {c0, c1, c2, c3] // candidate set: {c0, c1, c3} -> {c0, c1, c2, c3]
// accUpdate set: {} -> {c2} // accUpdate set: {} -> {c2}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 3, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 3, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
keeper.setCandidate(ctx, candidatesIn[2]) keeper.setCandidate(ctx, candidatesIn[2])
acc = keeper.getAccUpdateValidators(ctx) acc = keeper.getValidatorsTendermintUpdates(ctx)
require.Equal(t, 1, len(acc)) require.Equal(t, 1, len(acc))
candidates = keeper.GetCandidates(ctx, 5) candidates = keeper.GetCandidates(ctx, 5)
require.Equal(t, 4, len(candidates)) require.Equal(t, 4, len(candidates))
@ -525,55 +525,55 @@ func TestGetAccUpdateValidators(t *testing.T) {
// candidate set: {c0, c1, c2, c3} -> {c0, c1, c2, c3, c4} // candidate set: {c0, c1, c2, c3} -> {c0, c1, c2, c3, c4}
// validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} // validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3}
// accUpdate set: {} -> {} // accUpdate set: {} -> {}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 4, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 4, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 4, len(keeper.GetValidators(ctx))) assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
keeper.setCandidate(ctx, candidatesIn[4]) keeper.setCandidate(ctx, candidatesIn[4])
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 4, len(keeper.GetValidators(ctx))) assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx)))
require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4 require.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx))) // max validator number is 4
// test candidate change its power but still not in the valset // test candidate change its power but still not in the valset
// candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4} // candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4}
// validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} // validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3}
// accUpdate set: {} -> {} // accUpdate set: {} -> {}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 4, len(keeper.GetValidators(ctx))) assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
candidatesIn[4].BondedShares = sdk.NewRat(1) candidatesIn[4].BondedShares = sdk.NewRat(1)
keeper.setCandidate(ctx, candidatesIn[4]) keeper.setCandidate(ctx, candidatesIn[4])
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 4, len(keeper.GetValidators(ctx))) assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx)))
require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4 require.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx))) // max validator number is 4
// test candidate change its power and become a validator (pushing out an existing) // test candidate change its power and become a validator (pushing out an existing)
// candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4} // candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4}
// validator set: {c0, c1, c2, c3} -> {c1, c2, c3, c4} // validator set: {c0, c1, c2, c3} -> {c1, c2, c3, c4}
// accUpdate set: {} -> {c0, c4} // accUpdate set: {} -> {c0, c4}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 4, len(keeper.GetValidators(ctx))) assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
candidatesIn[4].BondedShares = sdk.NewRat(1000) candidatesIn[4].BondedShares = sdk.NewRat(1000)
keeper.setCandidate(ctx, candidatesIn[4]) keeper.setCandidate(ctx, candidatesIn[4])
candidates = keeper.GetCandidates(ctx, 5) candidates = keeper.GetCandidates(ctx, 5)
require.Equal(t, 5, len(candidates)) require.Equal(t, 5, len(candidates))
vals = keeper.getValidatorsOrdered(ctx) vals = keeper.GetValidatorsBondedByPower(ctx)
require.Equal(t, 4, len(vals)) require.Equal(t, 4, len(vals))
assert.Equal(t, candidatesIn[1].Address, vals[1].Address) assert.Equal(t, candidatesIn[1].Address, vals[1].Address)
assert.Equal(t, candidatesIn[2].Address, vals[3].Address) assert.Equal(t, candidatesIn[2].Address, vals[3].Address)
assert.Equal(t, candidatesIn[3].Address, vals[2].Address) assert.Equal(t, candidatesIn[3].Address, vals[2].Address)
assert.Equal(t, candidatesIn[4].Address, vals[0].Address) assert.Equal(t, candidatesIn[4].Address, vals[0].Address)
acc = keeper.getAccUpdateValidators(ctx) acc = keeper.getValidatorsTendermintUpdates(ctx)
require.Equal(t, 2, len(acc), "%v", acc) require.Equal(t, 2, len(acc), "%v", acc)
assert.Equal(t, candidatesIn[0].PubKey.Bytes(), acc[0].PubKey) assert.Equal(t, candidatesIn[0].PubKey.Bytes(), acc[0].PubKey)
@ -584,10 +584,10 @@ func TestGetAccUpdateValidators(t *testing.T) {
// candidate set: {c0, c1, c2, c3, c4} -> {} // candidate set: {c0, c1, c2, c3, c4} -> {}
// validator set: {c1, c2, c3, c4} -> {} // validator set: {c1, c2, c3, c4} -> {}
// accUpdate set: {} -> {c1, c2, c3, c4} // accUpdate set: {} -> {c1, c2, c3, c4}
keeper.clearAccUpdateValidators(ctx) keeper.clearValidatorsTendermintUpdates(ctx)
assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5)))
assert.Equal(t, 4, len(keeper.GetValidators(ctx))) assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx)))
assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) assert.Equal(t, 0, len(keeper.getValidatorsTendermintUpdates(ctx)))
keeper.removeCandidate(ctx, candidatesIn[0].Address) keeper.removeCandidate(ctx, candidatesIn[0].Address)
keeper.removeCandidate(ctx, candidatesIn[1].Address) keeper.removeCandidate(ctx, candidatesIn[1].Address)
@ -595,11 +595,11 @@ func TestGetAccUpdateValidators(t *testing.T) {
keeper.removeCandidate(ctx, candidatesIn[3].Address) keeper.removeCandidate(ctx, candidatesIn[3].Address)
keeper.removeCandidate(ctx, candidatesIn[4].Address) keeper.removeCandidate(ctx, candidatesIn[4].Address)
vals = keeper.getValidatorsOrdered(ctx) vals = keeper.GetValidatorsBondedByPower(ctx)
assert.Equal(t, 0, len(vals), "%v", vals) assert.Equal(t, 0, len(vals), "%v", vals)
candidates = keeper.GetCandidates(ctx, 5) candidates = keeper.GetCandidates(ctx, 5)
require.Equal(t, 0, len(candidates)) require.Equal(t, 0, len(candidates))
acc = keeper.getAccUpdateValidators(ctx) acc = keeper.getValidatorsTendermintUpdates(ctx)
require.Equal(t, 4, len(acc)) require.Equal(t, 4, len(acc))
assert.Equal(t, candidatesIn[1].PubKey.Bytes(), acc[0].PubKey) assert.Equal(t, candidatesIn[1].PubKey.Bytes(), acc[0].PubKey)
assert.Equal(t, candidatesIn[2].PubKey.Bytes(), acc[1].PubKey) assert.Equal(t, candidatesIn[2].PubKey.Bytes(), acc[1].PubKey)
@ -611,46 +611,6 @@ func TestGetAccUpdateValidators(t *testing.T) {
assert.Equal(t, int64(0), acc[3].Power) assert.Equal(t, int64(0), acc[3].Power)
} }
// test if is a validator from the last update
func TestIsValidator(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0)
amts := []int64{9, 8, 7, 10, 6}
var candidatesIn [5]Candidate
for i, amt := range amts {
candidatesIn[i] = NewCandidate(addrVals[i], pks[i], Description{})
candidatesIn[i].BondedShares = sdk.NewRat(amt)
candidatesIn[i].DelegatorShares = sdk.NewRat(amt)
}
// test that an empty validator set doesn't have any validators
validators := keeper.getValidatorsOrdered(ctx)
assert.Equal(t, 0, len(validators))
// get the validators for the first time
keeper.setCandidate(ctx, candidatesIn[0])
keeper.setCandidate(ctx, candidatesIn[1])
validators = keeper.getValidatorsOrdered(ctx)
require.Equal(t, 2, len(validators))
assert.True(t, candidatesIn[0].validator().equal(validators[0]))
c1ValWithCounter := candidatesIn[1].validator()
c1ValWithCounter.Counter = int16(1)
assert.True(t, c1ValWithCounter.equal(validators[1]))
// test a basic retrieve of something that should be a recent validator
assert.True(t, keeper.IsValidator(ctx, candidatesIn[0].PubKey))
assert.True(t, keeper.IsValidator(ctx, candidatesIn[1].PubKey))
// test a basic retrieve of something that should not be a recent validator
assert.False(t, keeper.IsValidator(ctx, candidatesIn[2].PubKey))
// remove that validator, but don't retrieve the recent validator group
keeper.removeCandidate(ctx, candidatesIn[0].Address)
// test that removed validator is not considered a recent validator
assert.False(t, keeper.IsValidator(ctx, candidatesIn[0].PubKey))
}
// test if is a validator from the last update // test if is a validator from the last update
func TestGetTotalPrecommitVotingPower(t *testing.T) { func TestGetTotalPrecommitVotingPower(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0) ctx, _, keeper := createTestInput(t, false, 0)
@ -665,7 +625,7 @@ func TestGetTotalPrecommitVotingPower(t *testing.T) {
} }
// test that an empty validator set doesn't have any validators // test that an empty validator set doesn't have any validators
validators := keeper.GetValidators(ctx) validators := keeper.GetValidatorsBonded(ctx)
assert.Equal(t, 5, len(validators)) assert.Equal(t, 5, len(validators))
totPow := keeper.GetTotalPrecommitVotingPower(ctx) totPow := keeper.GetTotalPrecommitVotingPower(ctx)

View File

@ -30,8 +30,8 @@ func (k Keeper) Tick(ctx sdk.Context) (change []abci.Validator) {
k.setIntraTxCounter(ctx, 0) k.setIntraTxCounter(ctx, 0)
// calculate validator set changes // calculate validator set changes
change = k.getAccUpdateValidators(ctx) change = k.getValidatorsTendermintUpdates(ctx)
k.clearAccUpdateValidators(ctx) k.clearValidatorsTendermintUpdates(ctx)
// XXX get the total validator of the previous validator set // XXX get the total validator of the previous validator set
// XXX get the total validator of the current validator set // XXX get the total validator of the current validator set