This commit is contained in:
rigelrozanski 2018-05-22 15:13:03 -04:00
parent 92c9d3b047
commit db9fd51d1c
10 changed files with 187 additions and 179 deletions

View File

@ -408,7 +408,7 @@ func TestStakeMsgs(t *testing.T) {
validator, found := gapp.stakeKeeper.GetValidator(ctxDeliver, addr1)
require.True(t, found)
require.Equal(t, addr1, validator.Owner)
require.Equal(t, sdk.Bonded, validator.Status)
require.Equal(t, sdk.Bonded, validator.Status())
require.True(sdk.RatEq(t, sdk.NewRat(10), validator.PoolShares.Bonded()))
// check the bond that should have been created as well

View File

@ -32,7 +32,7 @@ func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
k.setPool(ctx, data.Pool)
k.setNewParams(ctx, data.Params)
for _, validator := range data.Validators {
k.setValidator(ctx, validator)
k.updateValidator(ctx, validator)
}
for _, bond := range data.Bonds {
k.setDelegation(ctx, bond)

View File

@ -54,7 +54,7 @@ func handleMsgDeclareCandidacy(ctx sdk.Context, msg MsgDeclareCandidacy, k Keepe
}
validator := NewValidator(msg.ValidatorAddr, msg.PubKey, msg.Description)
validator = k.setValidator(ctx, validator)
k.setValidator(ctx, validator)
tags := sdk.NewTags(
"action", []byte("declareCandidacy"),
"validator", msg.ValidatorAddr.Bytes(),
@ -92,7 +92,7 @@ func handleMsgEditCandidacy(ctx sdk.Context, msg MsgEditCandidacy, k Keeper) sdk
validator.Description.Website = msg.Description.Website
validator.Description.Details = msg.Description.Details
k.setValidator(ctx, validator)
k.updateValidator(ctx, validator)
tags := sdk.NewTags(
"action", []byte("editCandidacy"),
"validator", msg.ValidatorAddr.Bytes(),
@ -156,7 +156,7 @@ func delegate(ctx sdk.Context, k Keeper, delegatorAddr sdk.Address,
k.setPool(ctx, pool)
k.setDelegation(ctx, bond)
k.setValidator(ctx, validator)
k.updateValidator(ctx, validator)
tags := sdk.NewTags("action", []byte("delegate"), "delegator", delegatorAddr.Bytes(), "validator", validator.Owner.Bytes())
return tags, nil
}
@ -246,7 +246,7 @@ func handleMsgUnbond(ctx sdk.Context, msg MsgUnbond, k Keeper) sdk.Result {
if validator.DelegatorShares.IsZero() {
k.removeValidator(ctx, validator.Owner)
} else {
k.setValidator(ctx, validator)
k.updateValidator(ctx, validator)
}
k.setPool(ctx, pool)
tags := sdk.NewTags("action", []byte("unbond"), "delegator", msg.DelegatorAddr.Bytes(), "validator", msg.ValidatorAddr.Bytes())

View File

@ -1,6 +1,7 @@
package stake
import (
"fmt"
"strconv"
"testing"
@ -43,7 +44,7 @@ func TestDuplicatesMsgDeclareCandidacy(t *testing.T) {
assert.True(t, got.IsOK(), "%v", got)
validator, found := keeper.GetValidator(ctx, validatorAddr)
require.True(t, found)
assert.Equal(t, sdk.Bonded, validator.Status)
assert.Equal(t, sdk.Bonded, validator.Status())
assert.Equal(t, validatorAddr, validator.Owner)
assert.Equal(t, pk, validator.PubKey)
assert.Equal(t, sdk.NewRat(10), validator.PoolShares.Bonded())
@ -71,7 +72,7 @@ func TestIncrementsMsgDelegate(t *testing.T) {
validator, found := keeper.GetValidator(ctx, validatorAddr)
require.True(t, found)
require.Equal(t, sdk.Bonded, validator.Status)
require.Equal(t, sdk.Bonded, validator.Status())
assert.Equal(t, bondAmount, validator.DelegatorShares.Evaluate())
assert.Equal(t, bondAmount, validator.PoolShares.Bonded().Evaluate(), "validator: %v", validator)
@ -224,12 +225,14 @@ func TestMultipleMsgDeclareCandidacy(t *testing.T) {
// bond them all
for i, validatorAddr := range validatorAddrs {
fmt.Printf("debug i: %v\n", i)
msgDeclareCandidacy := newTestMsgDeclareCandidacy(validatorAddr, pks[i], 10)
got := handleMsgDeclareCandidacy(ctx, msgDeclareCandidacy, keeper)
require.True(t, got.IsOK(), "expected msg %d to be ok, got %v", i, got)
//Check that the account is bonded
validators := keeper.GetValidators(ctx, 100)
fmt.Printf("debug validators: %v\n", validators)
require.Equal(t, (i + 1), len(validators))
val := validators[i]
balanceExpd := initBond - 10

View File

@ -89,19 +89,25 @@ func (k Keeper) GetValidators(ctx sdk.Context, maxRetrieve int16) (validators Va
return validators[:i] // trim
}
func (k Keeper) setValidator(ctx sdk.Context, validator Validator) Validator {
func (k Keeper) setValidator(ctx sdk.Context, validator Validator) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinary(validator)
store.Set(GetValidatorKey(validator.Owner), bz)
}
func (k Keeper) updateValidator(ctx sdk.Context, validator Validator) Validator {
store := ctx.KVStore(k.storeKey)
pool := k.getPool(store)
address := validator.Owner
ownerAddr := validator.Owner
// update the main list ordered by address before exiting
// always update the main list ordered by owner address before exiting
defer func() {
bz := k.cdc.MustMarshalBinary(validator)
store.Set(GetValidatorKey(address), bz)
store.Set(GetValidatorKey(ownerAddr), bz)
}()
// retreive the old validator record
oldValidator, oldFound := k.GetValidator(ctx, address)
oldValidator, oldFound := k.GetValidator(ctx, ownerAddr)
powerIncreasing := false
if oldFound {
@ -114,10 +120,14 @@ func (k Keeper) setValidator(ctx sdk.Context, validator Validator) Validator {
} else if oldValidator.PoolShares.Bonded().LT(validator.PoolShares.Bonded()) {
powerIncreasing = true
}
// delete the old record in the power ordered list
store.Delete(GetValidatorsBondedByPowerKey(oldValidator, pool))
}
// update the list ordered by voting power
if oldFound {
store.Delete(GetValidatorsByPowerKey(oldValidator, pool))
}
store.Set(GetValidatorsByPowerKey(validator, pool), validator.Owner)
// if already a validator, copy the old block height and counter, else set them
if oldFound && oldValidator.Status() == sdk.Bonded {
validator.BondHeight = oldValidator.BondHeight
@ -129,28 +139,22 @@ func (k Keeper) setValidator(ctx sdk.Context, validator Validator) Validator {
k.setIntraTxCounter(ctx, counter+1)
}
// update the list ordered by voting power
store.Set(GetValidatorsBondedByPowerKey(validator, pool), validator.Owner)
// efficiency case:
// add to the validators and return to update list if is already a validator and power is increasing
if powerIncreasing && oldFound && oldValidator.Status() == sdk.Bonded {
// update the store for bonded validators
store.Set(GetValidatorsBondedKey(validator.PubKey), validator.Owner)
// and the Tendermint updates
// if already bonded and power increasing only need to update tendermint
if powerIncreasing && oldValidator.Status() == sdk.Bonded {
bz := k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc))
store.Set(GetTendermintUpdatesKey(address), bz)
store.Set(GetTendermintUpdatesKey(ownerAddr), bz)
return validator
}
// update the validator set for this validator
nowBonded, retrieve := k.updateBondedValidators(ctx, store, pool, validator.Owner)
if nowBonded {
validator = retrieve
}
// TODO efficiency case:
// if was unbonded/or is a new validator - and the new power is less than the cliff validator
// update the validator set for this validator
updatedVal := k.updateBondedValidatorsNew(ctx, store, validator)
if updatedVal.Owner != nil { // updates to validator occured to be updated
validator = updatedVal
}
return validator
}
@ -166,14 +170,15 @@ func (k Keeper) removeValidator(ctx sdk.Context, address sdk.Address) {
store := ctx.KVStore(k.storeKey)
pool := k.getPool(store)
store.Delete(GetValidatorKey(address))
store.Delete(GetValidatorsBondedByPowerKey(validator, pool))
store.Delete(GetValidatorsBondedKey(validator.PubKey))
store.Delete(GetValidatorsByPowerKey(validator, pool))
// delete from the current and power weighted validator groups if the validator
// is bonded - and add validator with zero power to the validator updates
if store.Get(GetValidatorsBondedKey(validator.PubKey)) == nil {
return
}
store.Delete(GetValidatorsBondedKey(validator.PubKey))
bz := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc))
store.Set(GetTendermintUpdatesKey(address), bz)
}
@ -210,7 +215,7 @@ func (k Keeper) GetValidatorsBonded(ctx sdk.Context) (validators []Validator) {
}
// get the group of bonded validators sorted by power-rank
func (k Keeper) GetValidatorsBondedByPower(ctx sdk.Context) []Validator {
func (k Keeper) GetValidatorsByPower(ctx sdk.Context) []Validator {
store := ctx.KVStore(k.storeKey)
maxValidators := k.GetParams(ctx).MaxValidators
validators := make([]Validator, maxValidators)
@ -250,22 +255,18 @@ func (k Keeper) GetValidatorsBondedByPower(ctx sdk.Context) []Validator {
// GetValidators.
//
// Optionally also return the validator from a retrieve address if the validator has been bonded
func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore, pool Pool,
OptionalRetrieve sdk.Address) (retrieveBonded bool, retrieve Validator) {
func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore) {
k.updateBondedValidatorsNew(ctx, store, Validator{})
}
func (k Keeper) updateBondedValidatorsNew(ctx sdk.Context, store sdk.KVStore,
newValidator Validator) (updatedVal Validator) {
// clear the current validators store, add to the ToKickOut temp store
toKickOut := make(map[string]Validator) // map[key]value
toKickOut := make(map[string]int8) // map[key]value
iterator := store.SubspaceIterator(ValidatorsBondedKey)
for ; iterator.Valid(); iterator.Next() {
address := iterator.Value()
validator, found := k.getValidator(store, address)
if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", address))
}
toKickOut[string(validator.Owner)] = validator
// XXX store.Delete(iterator.Key())
ownerAddr := iterator.Value()
toKickOut[string(ownerAddr)] = 0 // dummy set, only need key
}
iterator.Close()
@ -279,17 +280,27 @@ func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore, pool
break
}
address := iterator.Value()
validator, found := k.getValidator(store, address)
// either retrieve the original validator from the store,
// or under the situation that this is the "new validator" just
// use the validator provided because it has not yet been updated
// in the main validator store
ownerAddr := iterator.Value()
var validator Validator
if bytes.Equal(ownerAddr, newValidator.Owner) {
validator = newValidator
} else {
var found bool
validator, found = k.getValidator(store, ownerAddr)
if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", address))
panic(fmt.Sprintf("validator record not found for address: %v\n", ownerAddr))
}
}
_, found = toKickOut[string(validator.Owner)]
_, found := toKickOut[string(ownerAddr)]
if found {
// remove from ToKickOut group
delete(toKickOut, string(validator.Owner))
delete(toKickOut, string(ownerAddr))
// XXX also add to the current validators group
//store.Set(GetValidatorsBondedKey(validator.PubKey), validator.Owner)
@ -299,9 +310,8 @@ func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore, pool
// this wasn't a previously a validator, therefor
// update the validator to enter the validator group
validator = k.bondValidator(ctx, store, validator)
if bytes.Equal(validator.Owner, OptionalRetrieve) {
retrieveBonded = true
retrieve = validator
if bytes.Equal(ownerAddr, newValidator.Owner) {
updatedVal = validator
}
}
@ -309,12 +319,15 @@ func (k Keeper) updateBondedValidators(ctx sdk.Context, store sdk.KVStore, pool
}
// perform the actual kicks
for _, validator := range toKickOut {
for key := range toKickOut {
ownerAddr := []byte(key)
validator, found := k.getValidator(store, ownerAddr)
if !found {
panic(fmt.Sprintf("validator record not found for address: %v\n", ownerAddr))
}
k.unbondValidator(ctx, store, validator)
}
// save the pool as well before exiting
k.setPool(ctx, pool)
return
}
@ -328,7 +341,7 @@ func (k Keeper) unbondValidator(ctx sdk.Context, store sdk.KVStore, validator Va
}
// XXX first delete the old record in the pool
//store.Delete(GetValidatorsBondedByPowerKey(validator, pool))
//store.Delete(GetValidatorsByPowerKey(validator, pool))
// set the status
validator, pool = validator.UpdateStatus(pool, sdk.Unbonded)
@ -337,7 +350,7 @@ func (k Keeper) unbondValidator(ctx sdk.Context, store sdk.KVStore, validator Va
// save the now unbonded validator record
bzVal := k.cdc.MustMarshalBinary(validator)
store.Set(GetValidatorKey(validator.Owner), bzVal)
// XXX store.Set(GetValidatorsBondedByPowerKey(validator, pool), validator.Owner)
// XXX store.Set(GetValidatorsByPowerKey(validator, pool), validator.Owner)
// add to accumulated changes for tendermint
bzABCI := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc))
@ -357,7 +370,7 @@ func (k Keeper) bondValidator(ctx sdk.Context, store sdk.KVStore, validator Vali
}
// XXX first delete the old record in the pool
//store.Delete(GetValidatorsBondedByPowerKey(validator, pool))
//store.Delete(GetValidatorsByPowerKey(validator, pool))
// set the status
validator, pool = validator.UpdateStatus(pool, sdk.Bonded)
@ -366,7 +379,7 @@ func (k Keeper) bondValidator(ctx sdk.Context, store sdk.KVStore, validator Vali
// save the now bonded validator record to the three referenced stores
bzVal := k.cdc.MustMarshalBinary(validator)
store.Set(GetValidatorKey(validator.Owner), bzVal)
// XXX store.Set(GetValidatorsBondedByPowerKey(validator, pool), validator.Owner)
// XXX store.Set(GetValidatorsByPowerKey(validator, pool), validator.Owner)
store.Set(GetValidatorsBondedKey(validator.PubKey), validator.Owner)
// add to accumulated changes for tendermint
@ -508,8 +521,7 @@ func (k Keeper) setParams(ctx sdk.Context, params Params) {
// if max validator count changes, must recalculate validator set
if exParams.MaxValidators != params.MaxValidators {
pool := k.GetPool(ctx)
k.updateBondedValidators(ctx, store, pool, nil)
k.updateBondedValidators(ctx, store)
}
b := k.cdc.MustMarshalBinary(params)
store.Set(ParamKey, b)

View File

@ -13,11 +13,11 @@ import (
//nolint
var (
// Keys for store prefixes
ParamKey = []byte{0x00} // key for global parameters relating to staking
PoolKey = []byte{0x01} // key for global parameters relating to staking
ParamKey = []byte{0x00} // key for parameters relating to staking
PoolKey = []byte{0x01} // key for the staking pools
ValidatorsKey = []byte{0x02} // prefix for each key to a validator
ValidatorsByPowerKey = []byte{0x03} // prefix for each key to a validator sorted by power
ValidatorsBondedKey = []byte{0x04} // prefix for each key to bonded/actively validating validators
ValidatorsBondedKey = []byte{0x03} // prefix for each key to bonded/actively validating validators
ValidatorsByPowerKey = []byte{0x04} // prefix for each key to a validator sorted by power
TendermintUpdatesKey = []byte{0x05} // prefix for each key to a validator which is being updated
DelegationKey = []byte{0x06} // prefix for each key to a delegator's bond
IntraTxCounterKey = []byte{0x07} // key for block-local tx index
@ -26,12 +26,18 @@ var (
const maxDigitsForAccount = 12 // ~220,000,000 atoms created at launch
// get the key for the validator with address
func GetValidatorKey(addr sdk.Address) []byte {
return append(ValidatorsKey, addr.Bytes()...)
func GetValidatorKey(ownerAddr sdk.Address) []byte {
return append(ValidatorsKey, ownerAddr.Bytes()...)
}
// get the key for the current validator group, ordered like tendermint
func GetValidatorsBondedKey(pk crypto.PubKey) []byte {
addr := pk.Address()
return append(ValidatorsBondedKey, addr.Bytes()...)
}
// get the key for the validator used in the power-store
func GetValidatorsBondedByPowerKey(validator Validator, pool Pool) []byte {
func GetValidatorsByPowerKey(validator Validator, pool Pool) []byte {
power := validator.EquivalentBondedShares(pool)
powerBytes := []byte(power.ToLeftPadded(maxDigitsForAccount)) // power big-endian (more powerful validators first)
@ -49,14 +55,8 @@ func GetValidatorsBondedByPowerKey(validator Validator, pool Pool) []byte {
}
// get the key for the accumulated update validators
func GetTendermintUpdatesKey(addr sdk.Address) []byte {
return append(TendermintUpdatesKey, addr.Bytes()...)
}
// get the key for the current validator group, ordered like tendermint
func GetValidatorsBondedKey(pk crypto.PubKey) []byte {
addr := pk.Address()
return append(ValidatorsBondedKey, addr.Bytes()...)
func GetTendermintUpdatesKey(ownerAddr sdk.Address) []byte {
return append(TendermintUpdatesKey, ownerAddr.Bytes()...)
}
// get the key for delegator bond with validator
@ -72,10 +72,3 @@ func GetDelegationsKey(delegatorAddr sdk.Address, cdc *wire.Codec) []byte {
}
return append(DelegationKey, res...)
}
//______________________________________________________________
// remove the prefix byte from a key, possibly revealing and address
func AddrFromKey(key []byte) sdk.Address {
return key[1:]
}

View File

@ -30,16 +30,16 @@ func TestSetValidator(t *testing.T) {
// test how the validator is set from a purely unbonbed pool
validator := NewValidator(addrVals[0], pks[0], Description{})
validator, pool, _ = validator.addTokensFromDel(pool, 10)
require.Equal(t, sdk.Unbonded, validator.Status)
require.Equal(t, sdk.Unbonded, validator.Status())
assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.PoolShares.Unbonded()))
assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.DelegatorShares))
keeper.setPool(ctx, pool)
keeper.setValidator(ctx, validator)
keeper.updateValidator(ctx, validator)
// after the save the validator should be bonded
validator, found := keeper.GetValidator(ctx, addrVals[0])
require.True(t, found)
require.Equal(t, sdk.Bonded, validator.Status)
require.Equal(t, sdk.Bonded, validator.Status())
assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.PoolShares.Bonded()))
assert.True(sdk.RatEq(t, sdk.NewRat(10), validator.DelegatorShares))
@ -51,7 +51,7 @@ func TestSetValidator(t *testing.T) {
require.Equal(t, 1, len(resVals))
assert.True(ValEq(t, validator, resVals[0]))
resVals = keeper.GetValidatorsBondedByPower(ctx)
resVals = keeper.GetValidatorsByPower(ctx)
require.Equal(t, 1, len(resVals))
assert.True(ValEq(t, validator, resVals[0]))
@ -61,7 +61,7 @@ func TestSetValidator(t *testing.T) {
}
// This function tests setValidator, GetValidator, GetValidatorsBonded, removeValidator
// This function tests updateValidator, GetValidator, GetValidatorsBonded, removeValidator
func TestValidatorBasics(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0)
pool := keeper.GetPool(ctx)
@ -82,7 +82,7 @@ func TestValidatorBasics(t *testing.T) {
assert.Zero(t, len(resVals))
// set and retrieve a record
validators[0] = keeper.setValidator(ctx, validators[0])
validators[0] = keeper.updateValidator(ctx, validators[0])
resVal, found := keeper.GetValidator(ctx, addrVals[0])
require.True(t, found)
assert.True(ValEq(t, validators[0], resVal))
@ -94,7 +94,7 @@ func TestValidatorBasics(t *testing.T) {
// modify a records, save, and retrieve
validators[0].PoolShares = NewBondedShares(sdk.NewRat(10))
validators[0].DelegatorShares = sdk.NewRat(10)
validators[0] = keeper.setValidator(ctx, validators[0])
validators[0] = keeper.updateValidator(ctx, validators[0])
resVal, found = keeper.GetValidator(ctx, addrVals[0])
require.True(t, found)
assert.True(ValEq(t, validators[0], resVal))
@ -104,8 +104,8 @@ func TestValidatorBasics(t *testing.T) {
assert.True(ValEq(t, validators[0], resVals[0]))
// add other validators
validators[1] = keeper.setValidator(ctx, validators[1])
validators[2] = keeper.setValidator(ctx, validators[2])
validators[1] = keeper.updateValidator(ctx, validators[1])
validators[2] = keeper.updateValidator(ctx, validators[2])
resVal, found = keeper.GetValidator(ctx, addrVals[1])
require.True(t, found)
assert.True(ValEq(t, validators[1], resVal))
@ -125,7 +125,7 @@ func TestValidatorBasics(t *testing.T) {
assert.False(t, found)
}
// test how the validators are sorted, tests GetValidatorsBondedByPower
// test how the validators are sorted, tests GetValidatorsByPower
func GetValidatorSortingUnmixed(t *testing.T) {
ctx, _, keeper := createTestInput(t, false, 0)
@ -137,11 +137,11 @@ func GetValidatorSortingUnmixed(t *testing.T) {
validators[i] = NewValidator(addrs[i], pks[i], Description{})
validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
keeper.setValidator(ctx, validators[i])
keeper.updateValidator(ctx, validators[i])
}
// first make sure everything made it in to the gotValidator group
resValidators := keeper.GetValidatorsBondedByPower(ctx)
resValidators := keeper.GetValidatorsByPower(ctx)
require.Equal(t, n, len(resValidators))
assert.Equal(t, sdk.NewRat(400), resValidators[0].PoolShares.Bonded(), "%v", resValidators)
assert.Equal(t, sdk.NewRat(200), resValidators[1].PoolShares.Bonded(), "%v", resValidators)
@ -156,15 +156,15 @@ func GetValidatorSortingUnmixed(t *testing.T) {
// test a basic increase in voting power
validators[3].PoolShares = NewBondedShares(sdk.NewRat(500))
keeper.setValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
keeper.updateValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, len(resValidators), n)
assert.True(ValEq(t, validators[3], resValidators[0]))
// test a decrease in voting power
validators[3].PoolShares = NewBondedShares(sdk.NewRat(300))
keeper.setValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
keeper.updateValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, len(resValidators), n)
assert.True(ValEq(t, validators[3], resValidators[0]))
assert.True(ValEq(t, validators[4], resValidators[1]))
@ -172,8 +172,8 @@ func GetValidatorSortingUnmixed(t *testing.T) {
// test equal voting power, different age
validators[3].PoolShares = NewBondedShares(sdk.NewRat(200))
ctx = ctx.WithBlockHeight(10)
keeper.setValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
keeper.updateValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, len(resValidators), n)
assert.True(ValEq(t, validators[3], resValidators[0]))
assert.True(ValEq(t, validators[4], resValidators[1]))
@ -182,8 +182,8 @@ func GetValidatorSortingUnmixed(t *testing.T) {
// no change in voting power - no change in sort
ctx = ctx.WithBlockHeight(20)
keeper.setValidator(ctx, validators[4])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
keeper.updateValidator(ctx, validators[4])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, len(resValidators), n)
assert.True(ValEq(t, validators[3], resValidators[0]))
assert.True(ValEq(t, validators[4], resValidators[1]))
@ -191,12 +191,12 @@ func GetValidatorSortingUnmixed(t *testing.T) {
// change in voting power of both validators, both still in v-set, no age change
validators[3].PoolShares = NewBondedShares(sdk.NewRat(300))
validators[4].PoolShares = NewBondedShares(sdk.NewRat(300))
keeper.setValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
keeper.updateValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, len(resValidators), n)
ctx = ctx.WithBlockHeight(30)
keeper.setValidator(ctx, validators[4])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
keeper.updateValidator(ctx, validators[4])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, len(resValidators), n, "%v", resValidators)
assert.True(ValEq(t, validators[3], resValidators[0]))
assert.True(ValEq(t, validators[4], resValidators[1]))
@ -225,7 +225,7 @@ func GetValidatorSortingMixed(t *testing.T) {
validators[3].PoolShares = NewBondedShares(sdk.NewRat(amts[3]))
validators[4].PoolShares = NewBondedShares(sdk.NewRat(amts[4]))
for i := range amts {
keeper.setValidator(ctx, validators[i])
keeper.updateValidator(ctx, validators[i])
}
val0, found := keeper.GetValidator(ctx, addrs[0])
require.True(t, found)
@ -237,14 +237,14 @@ func GetValidatorSortingMixed(t *testing.T) {
require.True(t, found)
val4, found := keeper.GetValidator(ctx, addrs[4])
require.True(t, found)
assert.Equal(t, sdk.Unbonded, val0.Status)
assert.Equal(t, sdk.Unbonded, val1.Status)
assert.Equal(t, sdk.Unbonded, val2.Status)
assert.Equal(t, sdk.Bonded, val3.Status)
assert.Equal(t, sdk.Bonded, val4.Status)
assert.Equal(t, sdk.Unbonded, val0.Status())
assert.Equal(t, sdk.Unbonded, val1.Status())
assert.Equal(t, sdk.Unbonded, val2.Status())
assert.Equal(t, sdk.Bonded, val3.Status())
assert.Equal(t, sdk.Bonded, val4.Status())
// first make sure everything made it in to the gotValidator group
resValidators := keeper.GetValidatorsBondedByPower(ctx)
resValidators := keeper.GetValidatorsByPower(ctx)
require.Equal(t, n, len(resValidators))
assert.Equal(t, sdk.NewRat(400), resValidators[0].PoolShares.Bonded(), "%v", resValidators)
assert.Equal(t, sdk.NewRat(200), resValidators[1].PoolShares.Bonded(), "%v", resValidators)
@ -276,19 +276,19 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
validators[i] = NewValidator(addrs[i], pks[i], Description{})
validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
validators[i] = keeper.setValidator(ctx, validators[i])
validators[i] = keeper.updateValidator(ctx, validators[i])
}
for i := range amts {
validators[i], found = keeper.GetValidator(ctx, validators[i].Owner)
require.True(t, found)
}
resValidators := keeper.GetValidatorsBondedByPower(ctx)
resValidators := keeper.GetValidatorsByPower(ctx)
assert.True(ValEq(t, validators[2], resValidators[0]))
assert.True(ValEq(t, validators[3], resValidators[1]))
validators[0].PoolShares = NewUnbondedShares(sdk.NewRat(500))
validators[0] = keeper.setValidator(ctx, validators[0])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
validators[0] = keeper.updateValidator(ctx, validators[0])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, nMax, uint16(len(resValidators)))
assert.True(ValEq(t, validators[0], resValidators[0]))
assert.True(ValEq(t, validators[2], resValidators[1]))
@ -303,24 +303,24 @@ func TestGetValidatorsEdgeCases(t *testing.T) {
validators[3], found = keeper.GetValidator(ctx, validators[3].Owner)
require.True(t, found)
validators[3].PoolShares = NewUnbondedShares(sdk.NewRat(401))
validators[3] = keeper.setValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
validators[3] = keeper.updateValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, nMax, uint16(len(resValidators)))
assert.True(ValEq(t, validators[0], resValidators[0]))
assert.True(ValEq(t, validators[3], resValidators[1]))
// validator 3 kicked out temporarily
validators[3].PoolShares = NewBondedShares(sdk.NewRat(200))
validators[3] = keeper.setValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
validators[3] = keeper.updateValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, nMax, uint16(len(resValidators)))
assert.True(ValEq(t, validators[0], resValidators[0]))
assert.True(ValEq(t, validators[2], resValidators[1]))
// validator 4 does not get spot back
validators[3].PoolShares = NewBondedShares(sdk.NewRat(400))
validators[3] = keeper.setValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
validators[3] = keeper.updateValidator(ctx, validators[3])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, nMax, uint16(len(resValidators)))
assert.True(ValEq(t, validators[0], resValidators[0]))
assert.True(ValEq(t, validators[2], resValidators[1]))
@ -349,23 +349,23 @@ func TestValidatorBondHeight(t *testing.T) {
validators[2].PoolShares = NewUnbondedShares(sdk.NewRat(100))
validators[2].DelegatorShares = sdk.NewRat(100)
validators[0] = keeper.setValidator(ctx, validators[0])
validators[0] = keeper.updateValidator(ctx, validators[0])
////////////////////////////////////////
// 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] = keeper.setValidator(ctx, validators[1])
validators[2] = keeper.setValidator(ctx, validators[2])
resValidators := keeper.GetValidatorsBondedByPower(ctx)
validators[1] = keeper.updateValidator(ctx, validators[1])
validators[2] = keeper.updateValidator(ctx, validators[2])
resValidators := keeper.GetValidatorsByPower(ctx)
require.Equal(t, uint16(len(resValidators)), params.MaxValidators)
assert.True(ValEq(t, validators[0], resValidators[0]))
assert.True(ValEq(t, validators[1], resValidators[1]))
validators[1].PoolShares = NewUnbondedShares(sdk.NewRat(150))
validators[2].PoolShares = NewUnbondedShares(sdk.NewRat(150))
validators[2] = keeper.setValidator(ctx, validators[2])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
validators[2] = keeper.updateValidator(ctx, validators[2])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, params.MaxValidators, uint16(len(resValidators)))
validators[1] = keeper.setValidator(ctx, validators[1])
validators[1] = keeper.updateValidator(ctx, validators[1])
assert.True(ValEq(t, validators[0], resValidators[0]))
assert.True(ValEq(t, validators[2], resValidators[1]))
}
@ -384,27 +384,27 @@ func TestFullValidatorSetPowerChange(t *testing.T) {
validators[i] = NewValidator(addrs[i], pks[i], Description{})
validators[i].PoolShares = NewUnbondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
keeper.setValidator(ctx, validators[i])
keeper.updateValidator(ctx, validators[i])
}
for i := range amts {
var found bool
validators[i], found = keeper.GetValidator(ctx, validators[i].Owner)
require.True(t, found)
}
assert.Equal(t, sdk.Unbonded, validators[0].Status)
assert.Equal(t, sdk.Unbonded, 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.GetValidatorsBondedByPower(ctx)
assert.Equal(t, sdk.Unbonded, validators[0].Status())
assert.Equal(t, sdk.Unbonded, 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.GetValidatorsByPower(ctx)
require.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
validators[0].PoolShares = NewBondedShares(sdk.NewRat(600))
validators[0] = keeper.setValidator(ctx, validators[0])
resValidators = keeper.GetValidatorsBondedByPower(ctx)
validators[0] = keeper.updateValidator(ctx, validators[0])
resValidators = keeper.GetValidatorsByPower(ctx)
require.Equal(t, max, len(resValidators))
assert.True(ValEq(t, validators[0], resValidators[0]))
assert.True(ValEq(t, validators[2], resValidators[1]))
@ -420,7 +420,7 @@ func TestClearTendermintUpdates(t *testing.T) {
validators[i] = NewValidator(addrs[i], pks[i], Description{})
validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
keeper.setValidator(ctx, validators[i])
keeper.updateValidator(ctx, validators[i])
}
updates := keeper.getTendermintUpdates(ctx)
@ -444,8 +444,8 @@ func TestGetTendermintUpdatesAllNone(t *testing.T) {
// test from nothing to something
// tendermintUpdate set: {} -> {c1, c3}
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
updates := keeper.getTendermintUpdates(ctx)
require.Equal(t, 2, len(updates))
@ -478,15 +478,15 @@ func TestGetTendermintUpdatesIdentical(t *testing.T) {
validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
}
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
keeper.clearTendermintUpdates(ctx)
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
// test identical,
// tendermintUpdate set: {} -> {}
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
}
@ -500,15 +500,15 @@ func TestGetTendermintUpdatesSingleValueChange(t *testing.T) {
validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
}
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
keeper.clearTendermintUpdates(ctx)
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
// test single value change
// tendermintUpdate set: {} -> {c1'}
validators[0].PoolShares = NewBondedShares(sdk.NewRat(600))
validators[0] = keeper.setValidator(ctx, validators[0])
validators[0] = keeper.updateValidator(ctx, validators[0])
updates := keeper.getTendermintUpdates(ctx)
@ -526,8 +526,8 @@ func TestGetTendermintUpdatesMultipleValueChange(t *testing.T) {
validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
}
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
keeper.clearTendermintUpdates(ctx)
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
@ -535,8 +535,8 @@ func TestGetTendermintUpdatesMultipleValueChange(t *testing.T) {
// tendermintUpdate set: {c1, c3} -> {c1', c3'}
validators[0].PoolShares = NewBondedShares(sdk.NewRat(200))
validators[1].PoolShares = NewBondedShares(sdk.NewRat(100))
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
updates := keeper.getTendermintUpdates(ctx)
require.Equal(t, 2, len(updates))
@ -554,14 +554,14 @@ func TestGetTendermintUpdatesInserted(t *testing.T) {
validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
}
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
keeper.clearTendermintUpdates(ctx)
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
// test validtor added at the beginning
// tendermintUpdate set: {} -> {c0}
keeper.setValidator(ctx, validators[2])
keeper.updateValidator(ctx, validators[2])
updates := keeper.getTendermintUpdates(ctx)
require.Equal(t, 1, len(updates))
require.Equal(t, validators[2].abciValidator(keeper.cdc), updates[0])
@ -569,7 +569,7 @@ func TestGetTendermintUpdatesInserted(t *testing.T) {
// test validtor added at the beginning
// tendermintUpdate set: {} -> {c0}
keeper.clearTendermintUpdates(ctx)
keeper.setValidator(ctx, validators[3])
keeper.updateValidator(ctx, validators[3])
updates = keeper.getTendermintUpdates(ctx)
require.Equal(t, 1, len(updates))
require.Equal(t, validators[3].abciValidator(keeper.cdc), updates[0])
@ -577,7 +577,7 @@ func TestGetTendermintUpdatesInserted(t *testing.T) {
// test validtor added at the end
// tendermintUpdate set: {} -> {c0}
keeper.clearTendermintUpdates(ctx)
keeper.setValidator(ctx, validators[4])
keeper.updateValidator(ctx, validators[4])
updates = keeper.getTendermintUpdates(ctx)
require.Equal(t, 1, len(updates))
require.Equal(t, validators[4].abciValidator(keeper.cdc), updates[0])
@ -596,14 +596,14 @@ func TestGetTendermintUpdatesNotValidatorCliff(t *testing.T) {
validators[i].PoolShares = NewBondedShares(sdk.NewRat(amt))
validators[i].DelegatorShares = sdk.NewRat(amt)
}
validators[0] = keeper.setValidator(ctx, validators[0])
validators[1] = keeper.setValidator(ctx, validators[1])
validators[0] = keeper.updateValidator(ctx, validators[0])
validators[1] = keeper.updateValidator(ctx, validators[1])
keeper.clearTendermintUpdates(ctx)
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
// test validator added at the end but not inserted in the valset
// tendermintUpdate set: {} -> {}
keeper.setValidator(ctx, validators[2])
keeper.updateValidator(ctx, validators[2])
updates := keeper.getTendermintUpdates(ctx)
require.Equal(t, 0, len(updates))
@ -613,7 +613,7 @@ func TestGetTendermintUpdatesNotValidatorCliff(t *testing.T) {
assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx)))
validators[2].PoolShares = NewBondedShares(sdk.NewRat(15))
validators[2] = keeper.setValidator(ctx, validators[2])
validators[2] = keeper.updateValidator(ctx, validators[2])
updates = keeper.getTendermintUpdates(ctx)
require.Equal(t, 2, len(updates), "%v", updates)
@ -635,7 +635,7 @@ func TestBond(t *testing.T) {
}
// first add a validators[0] to delegate too
validators[0] = keeper.setValidator(ctx, validators[0])
validators[0] = keeper.updateValidator(ctx, validators[0])
bond1to1 := Delegation{
DelegatorAddr: addrDels[0],
@ -661,8 +661,8 @@ func TestBond(t *testing.T) {
assert.True(t, bond1to1.equal(resBond))
// add some more records
validators[1] = keeper.setValidator(ctx, validators[1])
validators[2] = keeper.setValidator(ctx, validators[2])
validators[1] = keeper.updateValidator(ctx, validators[1])
validators[2] = keeper.updateValidator(ctx, validators[2])
bond1to2 := Delegation{addrDels[0], addrVals[1], sdk.NewRat(9), 0}
bond1to3 := Delegation{addrDels[0], addrVals[2], sdk.NewRat(9), 1}
bond2to1 := Delegation{addrDels[1], addrVals[0], sdk.NewRat(9), 2}

View File

@ -92,10 +92,10 @@ func paramsNoInflation() Params {
// hogpodge of all sorts of input required for testing
func createTestInput(t *testing.T, isCheckTx bool, initCoins int64) (sdk.Context, sdk.AccountMapper, Keeper) {
db := dbm.NewMemDB()
keyStake := sdk.NewKVStoreKey("stake")
keyAcc := sdk.NewKVStoreKey("acc")
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(keyStake, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)

View File

@ -75,26 +75,26 @@ func TestProcessProvisions(t *testing.T) {
var validators [5]Validator
validators[0] = NewValidator(addrs[0], pks[0], Description{})
validators[0], pool, _ = validators[0].addTokensFromDel(pool, 150000000)
validators[0] = keeper.setValidator(ctx, validators[0])
validators[0] = keeper.updateValidator(ctx, validators[0])
keeper.setPool(ctx, pool)
assert.Equal(t, bondedShares, pool.BondedTokens, "%v", pool)
fmt.Printf("debug pool: %v validator: %v\n", pool, validators[0])
validators[1] = NewValidator(addrs[1], pks[1], Description{})
validators[1], pool, _ = validators[1].addTokensFromDel(pool, 100000000)
keeper.setPool(ctx, pool)
validators[1] = keeper.setValidator(ctx, validators[1])
validators[1] = keeper.updateValidator(ctx, validators[1])
validators[2] = NewValidator(addrs[2], pks[2], Description{})
validators[2], pool, _ = validators[2].addTokensFromDel(pool, 100000000)
keeper.setPool(ctx, pool)
validators[2] = keeper.setValidator(ctx, validators[2])
validators[2] = keeper.updateValidator(ctx, validators[2])
validators[3] = NewValidator(addrs[3], pks[3], Description{})
validators[3], pool, _ = validators[3].addTokensFromDel(pool, 100000000)
keeper.setPool(ctx, pool)
validators[3] = keeper.setValidator(ctx, validators[3])
validators[3] = keeper.updateValidator(ctx, validators[3])
validators[4] = NewValidator(addrs[4], pks[4], Description{})
validators[4], pool, _ = validators[4].addTokensFromDel(pool, 100000000)
keeper.setPool(ctx, pool)
validators[4] = keeper.setValidator(ctx, validators[4])
validators[4] = keeper.updateValidator(ctx, validators[4])
validator, _ := keeper.GetValidator(ctx, addrs[0])
fmt.Printf("debug validators[0]: %v\n", validator)

View File

@ -26,7 +26,7 @@ func TestViewSlashBond(t *testing.T) {
}
// first add a validators[0] to delegate too
keeper.setValidator(ctx, validators[0])
keeper.updateValidator(ctx, validators[0])
bond1to1 := Delegation{
DelegatorAddr: addrDels[0],
@ -54,8 +54,8 @@ func TestViewSlashBond(t *testing.T) {
assert.True(t, bond1to1.equal(resBond))
// add some more records
keeper.setValidator(ctx, validators[1])
keeper.setValidator(ctx, validators[2])
keeper.updateValidator(ctx, validators[1])
keeper.updateValidator(ctx, validators[2])
bond1to2 := Delegation{addrDels[0], addrVals[1], sdk.NewRat(9), 0}
bond1to3 := Delegation{addrDels[0], addrVals[2], sdk.NewRat(9), 1}
bond2to1 := Delegation{addrDels[1], addrVals[0], sdk.NewRat(9), 2}