From a0c73372be860c54cb4a54e9f147314f551363fd Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 10 May 2018 21:38:57 -0400 Subject: [PATCH] stake refactor, tests compiling --- cmd/gaia/app/app_test.go | 4 +- x/fee_distribution/keeper_test.go | 31 ++ x/stake/handler_test.go | 4 +- x/stake/keeper.go | 16 +- x/stake/keeper_keys.go | 6 +- x/stake/keeper_test.go | 871 +++++++++++++----------------- x/stake/pool_test.go | 80 +-- x/stake/tick.go | 4 +- x/stake/tick_test.go | 10 +- 9 files changed, 470 insertions(+), 556 deletions(-) create mode 100644 x/fee_distribution/keeper_test.go diff --git a/cmd/gaia/app/app_test.go b/cmd/gaia/app/app_test.go index 73c7022d19..6f030a315a 100644 --- a/cmd/gaia/app/app_test.go +++ b/cmd/gaia/app/app_test.go @@ -405,7 +405,7 @@ func TestStakeMsgs(t *testing.T) { ctxDeliver := gapp.BaseApp.NewContext(false, abci.Header{}) res1 = gapp.accountMapper.GetAccount(ctxDeliver, addr1) require.Equal(t, genCoins.Minus(sdk.Coins{bondCoin}), res1.GetCoins()) - candidate, found := gapp.stakeKeeper.GetCandidate(ctxDeliver, addr1) + candidate, found := gapp.stakeKeeper.GetValidator(ctxDeliver, addr1) require.True(t, found) require.Equal(t, candidate.Address, addr1) @@ -417,7 +417,7 @@ func TestStakeMsgs(t *testing.T) { ) SignDeliver(t, gapp, editCandidacyMsg, []int64{1}, true, priv1) - candidate, found = gapp.stakeKeeper.GetCandidate(ctxDeliver, addr1) + candidate, found = gapp.stakeKeeper.GetValidator(ctxDeliver, addr1) require.True(t, found) require.Equal(t, candidate.Description, description) diff --git a/x/fee_distribution/keeper_test.go b/x/fee_distribution/keeper_test.go new file mode 100644 index 0000000000..4ad8180e17 --- /dev/null +++ b/x/fee_distribution/keeper_test.go @@ -0,0 +1,31 @@ +package stake + +//// test if is a gotValidator from the last update +//func TestGetTotalPrecommitVotingPower(t *testing.T) { +//ctx, _, keeper := createTestInput(t, false, 0) + +//amts := []int64{10000, 1000, 100, 10, 1} +//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) +//keeper.setCandidate(ctx, candidatesIn[i]) +//} + +//// test that an empty gotValidator set doesn't have any gotValidators +//gotValidators := keeper.GetValidators(ctx) +//assert.Equal(t, 5, len(gotValidators)) + +//totPow := keeper.GetTotalPrecommitVotingPower(ctx) +//exp := sdk.NewRat(11111) +//assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow) + +//// set absent gotValidators to be the 1st and 3rd record sorted by pubKey address +//ctx = ctx.WithAbsentValidators([]int32{1, 3}) +//totPow = keeper.GetTotalPrecommitVotingPower(ctx) + +//// XXX verify that this order should infact exclude these two records +//exp = sdk.NewRat(11100) +//assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow) +//} diff --git a/x/stake/handler_test.go b/x/stake/handler_test.go index b667f53e1b..00f9b2b35a 100644 --- a/x/stake/handler_test.go +++ b/x/stake/handler_test.go @@ -43,7 +43,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, Unbonded, validator.Status) + assert.Equal(t, sdk.Unbonded, validator.Status) assert.Equal(t, validatorAddr, validator.Address) assert.Equal(t, pk, validator.PubKey) assert.Equal(t, sdk.NewRat(10), validator.BondedShares) @@ -296,7 +296,7 @@ func TestVoidCandidacy(t *testing.T) { require.True(t, got.IsOK(), "expected no error on runMsgDeclareCandidacy") validator, found := keeper.GetValidator(ctx, validatorAddr) require.True(t, found) - require.Equal(t, Revoked, validator.Status) + require.Equal(t, sdk.Revoked, validator.Status) // test that this address cannot yet be bonded too because is revoked got = handleMsgDelegate(ctx, msgDelegate, keeper) diff --git a/x/stake/keeper.go b/x/stake/keeper.go index c194f3a4a7..2dc2dae85c 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -108,7 +108,7 @@ func (k Keeper) setValidator(ctx sdk.Context, validator Validator) { // add to the validators and return to update list if is already a validator and power is increasing if powerIncreasing && oldValidator.Status == sdk.Bonded { bzABCI := k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc)) - store.Set(GetValidatorsTendermintUpdatesKey(address), bzABCI) + store.Set(GetTendermintUpdatesKey(address), bzABCI) // also update the recent validator store store.Set(GetValidatorsBondedKey(validator.PubKey), bzVal) @@ -139,7 +139,7 @@ func (k Keeper) removeValidator(ctx sdk.Context, address sdk.Address) { return } bz := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc)) - store.Set(GetValidatorsTendermintUpdatesKey(address), bz) + store.Set(GetTendermintUpdatesKey(address), bz) store.Delete(GetValidatorsBondedKey(validator.PubKey)) } @@ -239,7 +239,7 @@ func (k Keeper) updateValidators(ctx sdk.Context, store sdk.KVStore, updatedVali // MOST IMPORTANTLY, add to the accumulated changes if this is the modified validator if bytes.Equal(updatedValidatorAddr, validator.Address) { bz = k.cdc.MustMarshalBinary(validator.abciValidator(k.cdc)) - store.Set(GetValidatorsTendermintUpdatesKey(updatedValidatorAddr), bz) + store.Set(GetTendermintUpdatesKey(updatedValidatorAddr), bz) } iterator.Next() @@ -252,7 +252,7 @@ func (k Keeper) updateValidators(ctx sdk.Context, store sdk.KVStore, updatedVali var validator Validator k.cdc.MustUnmarshalBinary(value, &validator) bz := k.cdc.MustMarshalBinary(validator.abciValidatorZero(k.cdc)) - store.Set(GetValidatorsTendermintUpdatesKey(addr), bz) + store.Set(GetTendermintUpdatesKey(addr), bz) } } @@ -260,10 +260,10 @@ func (k Keeper) updateValidators(ctx sdk.Context, store sdk.KVStore, updatedVali // Accumulated updates to the active/bonded validator set for tendermint // get the most recently updated validators -func (k Keeper) getValidatorsTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) { +func (k Keeper) getTendermintUpdates(ctx sdk.Context) (updates []abci.Validator) { store := ctx.KVStore(k.storeKey) - iterator := store.SubspaceIterator(ValidatorsTendermintUpdatesKey) //smallest to largest + iterator := store.SubspaceIterator(TendermintUpdatesKey) //smallest to largest for ; iterator.Valid(); iterator.Next() { valBytes := iterator.Value() var val abci.Validator @@ -275,11 +275,11 @@ func (k Keeper) getValidatorsTendermintUpdates(ctx sdk.Context) (updates []abci. } // remove all validator update entries after applied to Tendermint -func (k Keeper) clearValidatorsTendermintUpdates(ctx sdk.Context) { +func (k Keeper) clearTendermintUpdates(ctx sdk.Context) { store := ctx.KVStore(k.storeKey) // delete subspace - iterator := store.SubspaceIterator(ValidatorsTendermintUpdatesKey) + iterator := store.SubspaceIterator(TendermintUpdatesKey) for ; iterator.Valid(); iterator.Next() { store.Delete(iterator.Key()) } diff --git a/x/stake/keeper_keys.go b/x/stake/keeper_keys.go index 6d828b37e8..f449b6f022 100644 --- a/x/stake/keeper_keys.go +++ b/x/stake/keeper_keys.go @@ -17,7 +17,7 @@ var ( PoolKey = []byte{0x01} // key for global parameters relating to staking ValidatorsKey = []byte{0x02} // prefix for each key to a validator ValidatorsByPowerKey = []byte{0x03} // prefix for each key to a validator - ValidatorsTendermintUpdatesKey = []byte{0x04} // prefix for each key to a validator which is being updated + TendermintUpdatesKey = []byte{0x04} // prefix for each key to a validator which is being updated ValidatorsBondedKey = []byte{0x05} // prefix for each key to bonded/actively validating validators DelegationKey = []byte{0x06} // prefix for each key to a delegator's bond IntraTxCounterKey = []byte{0x07} // key for block-local tx index @@ -47,8 +47,8 @@ func GetValidatorsBondedByPowerKey(validator Validator) []byte { } // get the key for the accumulated update validators -func GetValidatorsTendermintUpdatesKey(addr sdk.Address) []byte { - return append(ValidatorsTendermintUpdatesKey, addr.Bytes()...) +func GetTendermintUpdatesKey(addr sdk.Address) []byte { + return append(TendermintUpdatesKey, addr.Bytes()...) } // get the key for the current validator group, ordered like tendermint diff --git a/x/stake/keeper_test.go b/x/stake/keeper_test.go index a66fc031bd..172633d33b 100644 --- a/x/stake/keeper_test.go +++ b/x/stake/keeper_test.go @@ -23,128 +23,128 @@ var ( } ) -// This function tests GetCandidate, GetCandidates, setCandidate, removeCandidate -func TestCandidate(t *testing.T) { +// This function tests GetValidator, GetValidatorsBonded, setValidator, removeValidator +func TestValidator(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - //construct the candidates - var candidates [3]Candidate + //construct the validators + var validators [3]Validator amts := []int64{9, 8, 7} for i, amt := range amts { - candidates[i] = NewCandidate(addrVals[i], pks[i], Description{}) - candidates[i].BondedShares = sdk.NewRat(amt) - candidates[i].DelegatorShares = sdk.NewRat(amt) + validators[i] = NewValidator(addrVals[i], pks[i], Description{}) + validators[i].BondedShares = sdk.NewRat(amt) + validators[i].DelegatorShares = sdk.NewRat(amt) } // check the empty keeper first - _, found := keeper.GetCandidate(ctx, addrVals[0]) + _, found := keeper.GetValidator(ctx, addrVals[0]) assert.False(t, found) - resCands := keeper.GetCandidates(ctx, 100) + resCands := keeper.GetValidatorsBonded(ctx) assert.Zero(t, len(resCands)) // set and retrieve a record - keeper.setCandidate(ctx, candidates[0]) - resCand, found := keeper.GetCandidate(ctx, addrVals[0]) + keeper.setValidator(ctx, validators[0]) + resCand, found := keeper.GetValidator(ctx, addrVals[0]) require.True(t, found) - assert.True(t, candidates[0].equal(resCand), "%v \n %v", resCand, candidates[0]) + assert.True(t, validators[0].equal(resCand), "%v \n %v", resCand, validators[0]) // modify a records, save, and retrieve - candidates[0].DelegatorShares = sdk.NewRat(99) - keeper.setCandidate(ctx, candidates[0]) - resCand, found = keeper.GetCandidate(ctx, addrVals[0]) + validators[0].DelegatorShares = sdk.NewRat(99) + keeper.setValidator(ctx, validators[0]) + resCand, found = keeper.GetValidator(ctx, addrVals[0]) require.True(t, found) - assert.True(t, candidates[0].equal(resCand)) + assert.True(t, validators[0].equal(resCand)) // also test that the address has been added to address list - resCands = keeper.GetCandidates(ctx, 100) + resCands = keeper.GetValidatorsBonded(ctx) require.Equal(t, 1, len(resCands)) assert.Equal(t, addrVals[0], resCands[0].Address) - // add other candidates - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - resCand, found = keeper.GetCandidate(ctx, addrVals[1]) + // add other validators + keeper.setValidator(ctx, validators[1]) + keeper.setValidator(ctx, validators[2]) + resCand, found = keeper.GetValidator(ctx, addrVals[1]) require.True(t, found) - assert.True(t, candidates[1].equal(resCand), "%v \n %v", resCand, candidates[1]) - resCand, found = keeper.GetCandidate(ctx, addrVals[2]) + assert.True(t, validators[1].equal(resCand), "%v \n %v", resCand, validators[1]) + resCand, found = keeper.GetValidator(ctx, addrVals[2]) require.True(t, found) - assert.True(t, candidates[2].equal(resCand), "%v \n %v", resCand, candidates[2]) - resCands = keeper.GetCandidates(ctx, 100) + assert.True(t, validators[2].equal(resCand), "%v \n %v", resCand, validators[2]) + resCands = keeper.GetValidatorsBonded(ctx) require.Equal(t, 3, len(resCands)) - assert.True(t, candidates[0].equal(resCands[0]), "%v \n %v", resCands[0], candidates[0]) - assert.True(t, candidates[1].equal(resCands[1]), "%v \n %v", resCands[1], candidates[1]) - assert.True(t, candidates[2].equal(resCands[2]), "%v \n %v", resCands[2], candidates[2]) + assert.True(t, validators[0].equal(resCands[0]), "%v \n %v", resCands[0], validators[0]) + assert.True(t, validators[1].equal(resCands[1]), "%v \n %v", resCands[1], validators[1]) + assert.True(t, validators[2].equal(resCands[2]), "%v \n %v", resCands[2], validators[2]) // remove a record - keeper.removeCandidate(ctx, candidates[1].Address) - _, found = keeper.GetCandidate(ctx, addrVals[1]) + keeper.removeValidator(ctx, validators[1].Address) + _, found = keeper.GetValidator(ctx, addrVals[1]) assert.False(t, found) } -// tests GetDelegatorBond, GetDelegatorBonds, SetDelegatorBond, removeDelegatorBond, GetBonds +// tests GetDelegation, GetDelegations, SetDelegation, removeDelegation, GetBonds func TestBond(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - //construct the candidates + //construct the validators amts := []int64{9, 8, 7} - var candidates [3]Candidate + var validators [3]Validator for i, amt := range amts { - candidates[i] = NewCandidate(addrVals[i], pks[i], Description{}) - candidates[i].BondedShares = sdk.NewRat(amt) - candidates[i].DelegatorShares = sdk.NewRat(amt) + validators[i] = NewValidator(addrVals[i], pks[i], Description{}) + validators[i].BondedShares = sdk.NewRat(amt) + validators[i].DelegatorShares = sdk.NewRat(amt) } - // first add a candidates[0] to delegate too - keeper.setCandidate(ctx, candidates[0]) + // first add a validators[0] to delegate too + keeper.setValidator(ctx, validators[0]) - bond1to1 := DelegatorBond{ + bond1to1 := Delegation{ DelegatorAddr: addrDels[0], - CandidateAddr: addrVals[0], + ValidatorAddr: addrVals[0], Shares: sdk.NewRat(9), } // check the empty keeper first - _, found := keeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + _, found := keeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.False(t, found) // set and retrieve a record - keeper.setDelegatorBond(ctx, bond1to1) - resBond, found := keeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + keeper.setDelegation(ctx, bond1to1) + resBond, found := keeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.True(t, found) assert.True(t, bond1to1.equal(resBond)) // modify a records, save, and retrieve bond1to1.Shares = sdk.NewRat(99) - keeper.setDelegatorBond(ctx, bond1to1) - resBond, found = keeper.GetDelegatorBond(ctx, addrDels[0], addrVals[0]) + keeper.setDelegation(ctx, bond1to1) + resBond, found = keeper.GetDelegation(ctx, addrDels[0], addrVals[0]) assert.True(t, found) assert.True(t, bond1to1.equal(resBond)) // add some more records - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - bond1to2 := DelegatorBond{addrDels[0], addrVals[1], sdk.NewRat(9), 0} - bond1to3 := DelegatorBond{addrDels[0], addrVals[2], sdk.NewRat(9), 1} - bond2to1 := DelegatorBond{addrDels[1], addrVals[0], sdk.NewRat(9), 2} - bond2to2 := DelegatorBond{addrDels[1], addrVals[1], sdk.NewRat(9), 3} - bond2to3 := DelegatorBond{addrDels[1], addrVals[2], sdk.NewRat(9), 4} - keeper.setDelegatorBond(ctx, bond1to2) - keeper.setDelegatorBond(ctx, bond1to3) - keeper.setDelegatorBond(ctx, bond2to1) - keeper.setDelegatorBond(ctx, bond2to2) - keeper.setDelegatorBond(ctx, bond2to3) + keeper.setValidator(ctx, validators[1]) + keeper.setValidator(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} + bond2to2 := Delegation{addrDels[1], addrVals[1], sdk.NewRat(9), 3} + bond2to3 := Delegation{addrDels[1], addrVals[2], sdk.NewRat(9), 4} + keeper.setDelegation(ctx, bond1to2) + keeper.setDelegation(ctx, bond1to3) + keeper.setDelegation(ctx, bond2to1) + keeper.setDelegation(ctx, bond2to2) + keeper.setDelegation(ctx, bond2to3) // test all bond retrieve capabilities - resBonds := keeper.GetDelegatorBonds(ctx, addrDels[0], 5) + resBonds := keeper.GetDelegations(ctx, addrDels[0], 5) require.Equal(t, 3, len(resBonds)) assert.True(t, bond1to1.equal(resBonds[0])) assert.True(t, bond1to2.equal(resBonds[1])) assert.True(t, bond1to3.equal(resBonds[2])) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[0], 3) + resBonds = keeper.GetDelegations(ctx, addrDels[0], 3) require.Equal(t, 3, len(resBonds)) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[0], 2) + resBonds = keeper.GetDelegations(ctx, addrDels[0], 2) require.Equal(t, 2, len(resBonds)) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[1], 5) + resBonds = keeper.GetDelegations(ctx, addrDels[1], 5) require.Equal(t, 3, len(resBonds)) assert.True(t, bond2to1.equal(resBonds[0])) assert.True(t, bond2to2.equal(resBonds[1])) @@ -159,22 +159,22 @@ func TestBond(t *testing.T) { assert.True(t, bond2to3.equal(allBonds[5])) // delete a record - keeper.removeDelegatorBond(ctx, bond2to3) - _, found = keeper.GetDelegatorBond(ctx, addrDels[1], addrVals[2]) + keeper.removeDelegation(ctx, bond2to3) + _, found = keeper.GetDelegation(ctx, addrDels[1], addrVals[2]) assert.False(t, found) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[1], 5) + resBonds = keeper.GetDelegations(ctx, addrDels[1], 5) require.Equal(t, 2, len(resBonds)) assert.True(t, bond2to1.equal(resBonds[0])) assert.True(t, bond2to2.equal(resBonds[1])) // delete all the records from delegator 2 - keeper.removeDelegatorBond(ctx, bond2to1) - keeper.removeDelegatorBond(ctx, bond2to2) - _, found = keeper.GetDelegatorBond(ctx, addrDels[1], addrVals[0]) + keeper.removeDelegation(ctx, bond2to1) + keeper.removeDelegation(ctx, bond2to2) + _, found = keeper.GetDelegation(ctx, addrDels[1], addrVals[0]) assert.False(t, found) - _, found = keeper.GetDelegatorBond(ctx, addrDels[1], addrVals[1]) + _, found = keeper.GetDelegation(ctx, addrDels[1], addrVals[1]) assert.False(t, found) - resBonds = keeper.GetDelegatorBonds(ctx, addrDels[1], 5) + resBonds = keeper.GetDelegations(ctx, addrDels[1], 5) require.Equal(t, 0, len(resBonds)) } @@ -182,497 +182,427 @@ func TestBond(t *testing.T) { func TestGetValidators(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - // initialize some candidates into the state + // initialize some validators into the state amts := []int64{0, 100, 1, 400, 200} n := len(amts) - var candidates [5]Candidate + var validators [5]Validator for i, amt := range amts { - candidates[i] = NewCandidate(addrs[i], pks[i], Description{}) - candidates[i].BondedShares = sdk.NewRat(amt) - candidates[i].DelegatorShares = sdk.NewRat(amt) - keeper.setCandidate(ctx, candidates[i]) + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].BondedShares = sdk.NewRat(amt) + validators[i].DelegatorShares = sdk.NewRat(amt) + keeper.setValidator(ctx, validators[i]) } - // first make sure everything made it in to the validator group - validators := keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) - 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(100), validators[2].Power, "%v", validators) - assert.Equal(t, sdk.NewRat(1), validators[3].Power, "%v", validators) - assert.Equal(t, sdk.NewRat(0), validators[4].Power, "%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[1].Address, validators[2].Address, "%v", validators) - assert.Equal(t, candidates[2].Address, validators[3].Address, "%v", validators) - assert.Equal(t, candidates[0].Address, validators[4].Address, "%v", validators) + // first make sure everything made it in to the gotValidator group + gotValidators := keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) + assert.Equal(t, sdk.NewRat(400), gotValidators[0].BondedShares, "%v", gotValidators) + assert.Equal(t, sdk.NewRat(200), gotValidators[1].BondedShares, "%v", gotValidators) + assert.Equal(t, sdk.NewRat(100), gotValidators[2].BondedShares, "%v", gotValidators) + assert.Equal(t, sdk.NewRat(1), gotValidators[3].BondedShares, "%v", gotValidators) + assert.Equal(t, sdk.NewRat(0), gotValidators[4].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[3].Address, gotValidators[0].Address, "%v", gotValidators) + assert.Equal(t, validators[4].Address, gotValidators[1].Address, "%v", gotValidators) + assert.Equal(t, validators[1].Address, gotValidators[2].Address, "%v", gotValidators) + assert.Equal(t, validators[2].Address, gotValidators[3].Address, "%v", gotValidators) + assert.Equal(t, validators[0].Address, gotValidators[4].Address, "%v", gotValidators) // test a basic increase in voting power - candidates[3].BondedShares = sdk.NewRat(500) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, sdk.NewRat(500), validators[0].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) + validators[3].BondedShares = sdk.NewRat(500) + keeper.setValidator(ctx, validators[3]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) + assert.Equal(t, sdk.NewRat(500), gotValidators[0].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[3].Address, gotValidators[0].Address, "%v", gotValidators) // test a decrease in voting power - candidates[3].BondedShares = sdk.NewRat(300) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) - 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[4].Address, validators[1].Address, "%v", validators) + validators[3].BondedShares = sdk.NewRat(300) + keeper.setValidator(ctx, validators[3]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) + assert.Equal(t, sdk.NewRat(300), gotValidators[0].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[3].Address, gotValidators[0].Address, "%v", gotValidators) + assert.Equal(t, validators[4].Address, gotValidators[1].Address, "%v", gotValidators) // test equal voting power, different age - candidates[3].BondedShares = sdk.NewRat(200) + validators[3].BondedShares = sdk.NewRat(200) ctx = ctx.WithBlockHeight(10) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) - 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, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) - assert.Equal(t, int64(0), validators[0].Height, "%v", validators) - assert.Equal(t, int64(0), validators[1].Height, "%v", validators) + keeper.setValidator(ctx, validators[3]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) + assert.Equal(t, sdk.NewRat(200), gotValidators[0].BondedShares, "%v", gotValidators) + assert.Equal(t, sdk.NewRat(200), gotValidators[1].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[3].Address, gotValidators[0].Address, "%v", gotValidators) + assert.Equal(t, validators[4].Address, gotValidators[1].Address, "%v", gotValidators) + assert.Equal(t, int64(0), gotValidators[0].BondHeight, "%v", gotValidators) + assert.Equal(t, int64(0), gotValidators[1].BondHeight, "%v", gotValidators) // no change in voting power - no change in sort ctx = ctx.WithBlockHeight(20) - keeper.setCandidate(ctx, candidates[4]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) + keeper.setValidator(ctx, validators[4]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) + assert.Equal(t, validators[3].Address, gotValidators[0].Address, "%v", gotValidators) + assert.Equal(t, validators[4].Address, gotValidators[1].Address, "%v", gotValidators) - // change in voting power of both candidates, both still in v-set, no age change - candidates[3].BondedShares = sdk.NewRat(300) - candidates[4].BondedShares = sdk.NewRat(300) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) + // change in voting power of both validators, both still in v-set, no age change + validators[3].BondedShares = sdk.NewRat(300) + validators[4].BondedShares = sdk.NewRat(300) + keeper.setValidator(ctx, validators[3]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) ctx = ctx.WithBlockHeight(30) - keeper.setCandidate(ctx, candidates[4]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[0].Address, "%v", validators) - assert.Equal(t, candidates[4].Address, validators[1].Address, "%v", validators) + keeper.setValidator(ctx, validators[4]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n, "%v", gotValidators) + assert.Equal(t, validators[3].Address, gotValidators[0].Address, "%v", gotValidators) + assert.Equal(t, validators[4].Address, gotValidators[1].Address, "%v", gotValidators) } // TODO seperate out into multiple tests func TestGetValidatorsEdgeCases(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) - // now 2 max validators + // now 2 max gotValidators params := keeper.GetParams(ctx) params.MaxValidators = 2 keeper.setParams(ctx, params) - // initialize some candidates into the state + // initialize some validators into the state amts := []int64{0, 100, 400, 400, 200} n := len(amts) - var candidates [5]Candidate + var validators [5]Validator for i, amt := range amts { - candidates[i] = NewCandidate(addrs[i], pks[i], Description{}) - candidates[i].BondedShares = sdk.NewRat(amt) - candidates[i].DelegatorShares = sdk.NewRat(amt) - keeper.setCandidate(ctx, candidates[i]) + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].BondedShares = sdk.NewRat(amt) + validators[i].DelegatorShares = sdk.NewRat(amt) + keeper.setValidator(ctx, validators[i]) } - candidates[0].BondedShares = sdk.NewRat(500) - keeper.setCandidate(ctx, candidates[0]) - validators := keeper.getValidatorsOrdered(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - // candidate 3 was set before candidate 4 - require.Equal(t, candidates[2].Address, validators[1].Address, "%v", validators) + validators[0].BondedShares = sdk.NewRat(500) + keeper.setValidator(ctx, validators[0]) + gotValidators := keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, uint16(len(gotValidators)), params.MaxValidators) + require.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + // validator 3 was set before validator 4 + require.Equal(t, validators[2].Address, gotValidators[1].Address, "%v", gotValidators) - // A candidate which leaves the validator set due to a decrease in voting power, + // A validator which leaves the gotValidator 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. // ref https://github.com/cosmos/cosmos-sdk/issues/582#issuecomment-380757108 - candidates[3].BondedShares = sdk.NewRat(401) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) + validators[3].BondedShares = sdk.NewRat(401) + keeper.setValidator(ctx, validators[3]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, uint16(len(gotValidators)), params.MaxValidators) + require.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + require.Equal(t, validators[3].Address, gotValidators[1].Address, "%v", gotValidators) ctx = ctx.WithBlockHeight(40) - // candidate 3 kicked out temporarily - candidates[3].BondedShares = sdk.NewRat(200) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[2].Address, validators[1].Address, "%v", validators) - // candidate 4 does not get spot back - candidates[3].BondedShares = sdk.NewRat(400) - keeper.setCandidate(ctx, candidates[3]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[2].Address, validators[1].Address, "%v", validators) - candidate, exists := keeper.GetCandidate(ctx, candidates[3].Address) + // validator 3 kicked out temporarily + validators[3].BondedShares = sdk.NewRat(200) + keeper.setValidator(ctx, validators[3]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, uint16(len(gotValidators)), params.MaxValidators) + require.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + require.Equal(t, validators[2].Address, gotValidators[1].Address, "%v", gotValidators) + // validator 4 does not get spot back + validators[3].BondedShares = sdk.NewRat(400) + keeper.setValidator(ctx, validators[3]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, uint16(len(gotValidators)), params.MaxValidators) + require.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + require.Equal(t, validators[2].Address, gotValidators[1].Address, "%v", gotValidators) + validator, exists := keeper.GetValidator(ctx, validators[3].Address) require.Equal(t, exists, true) - require.Equal(t, candidate.ValidatorBondHeight, int64(40)) + require.Equal(t, validator.BondHeight, int64(40)) - // If two candidates both increase to the same voting power in the same block, - // the one with the first transaction should take precedence (become a validator). + // If two validators both increase to the same voting power in the same block, + // the one with the first transaction should take precedence (become a gotValidator). // ref https://github.com/cosmos/cosmos-sdk/issues/582#issuecomment-381250392 - candidates[0].BondedShares = sdk.NewRat(2000) - keeper.setCandidate(ctx, candidates[0]) - candidates[1].BondedShares = sdk.NewRat(1000) - candidates[2].BondedShares = sdk.NewRat(1000) - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[1].Address, validators[1].Address, "%v", validators) - candidates[1].BondedShares = sdk.NewRat(1100) - candidates[2].BondedShares = sdk.NewRat(1100) - keeper.setCandidate(ctx, candidates[2]) - keeper.setCandidate(ctx, candidates[1]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, uint16(len(validators)), params.MaxValidators) - require.Equal(t, candidates[0].Address, validators[0].Address, "%v", validators) - require.Equal(t, candidates[2].Address, validators[1].Address, "%v", validators) + validators[0].BondedShares = sdk.NewRat(2000) + keeper.setValidator(ctx, validators[0]) + validators[1].BondedShares = sdk.NewRat(1000) + validators[2].BondedShares = sdk.NewRat(1000) + keeper.setValidator(ctx, validators[1]) + keeper.setValidator(ctx, validators[2]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, uint16(len(gotValidators)), params.MaxValidators) + require.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + require.Equal(t, validators[1].Address, gotValidators[1].Address, "%v", gotValidators) + validators[1].BondedShares = sdk.NewRat(1100) + validators[2].BondedShares = sdk.NewRat(1100) + keeper.setValidator(ctx, validators[2]) + keeper.setValidator(ctx, validators[1]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, uint16(len(gotValidators)), params.MaxValidators) + require.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + require.Equal(t, validators[2].Address, gotValidators[1].Address, "%v", gotValidators) // reset assets / heights params.MaxValidators = 100 keeper.setParams(ctx, params) - candidates[0].BondedShares = sdk.NewRat(0) - candidates[1].BondedShares = sdk.NewRat(100) - candidates[2].BondedShares = sdk.NewRat(1) - candidates[3].BondedShares = sdk.NewRat(300) - candidates[4].BondedShares = sdk.NewRat(200) + validators[0].BondedShares = sdk.NewRat(0) + validators[1].BondedShares = sdk.NewRat(100) + validators[2].BondedShares = sdk.NewRat(1) + validators[3].BondedShares = sdk.NewRat(300) + validators[4].BondedShares = sdk.NewRat(200) ctx = ctx.WithBlockHeight(0) - keeper.setCandidate(ctx, candidates[0]) - keeper.setCandidate(ctx, candidates[1]) - keeper.setCandidate(ctx, candidates[2]) - keeper.setCandidate(ctx, candidates[3]) - keeper.setCandidate(ctx, candidates[4]) + keeper.setValidator(ctx, validators[0]) + keeper.setValidator(ctx, validators[1]) + keeper.setValidator(ctx, validators[2]) + keeper.setValidator(ctx, validators[3]) + keeper.setValidator(ctx, validators[4]) // test a swap in voting power - candidates[0].BondedShares = sdk.NewRat(600) - keeper.setCandidate(ctx, candidates[0]) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) - 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, sdk.NewRat(300), validators[1].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) + validators[0].BondedShares = sdk.NewRat(600) + keeper.setValidator(ctx, validators[0]) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) + assert.Equal(t, sdk.NewRat(600), gotValidators[0].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + assert.Equal(t, sdk.NewRat(300), gotValidators[1].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[3].Address, gotValidators[1].Address, "%v", gotValidators) - // test the max validators term + // test the max gotValidators term params = keeper.GetParams(ctx) n = 2 params.MaxValidators = uint16(n) keeper.setParams(ctx, params) - validators = keeper.getValidatorsOrdered(ctx) - require.Equal(t, len(validators), n) - 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, sdk.NewRat(300), validators[1].Power, "%v", validators) - assert.Equal(t, candidates[3].Address, validators[1].Address, "%v", validators) + gotValidators = keeper.GetValidatorsBondedByPower(ctx) + require.Equal(t, len(gotValidators), n) + assert.Equal(t, sdk.NewRat(600), gotValidators[0].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[0].Address, gotValidators[0].Address, "%v", gotValidators) + assert.Equal(t, sdk.NewRat(300), gotValidators[1].BondedShares, "%v", gotValidators) + assert.Equal(t, validators[3].Address, gotValidators[1].Address, "%v", gotValidators) } -// clear the tracked changes to the validator set -func TestClearAccUpdateValidators(t *testing.T) { +// clear the tracked changes to the gotValidator set +func TestClearTendermintUpdates(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) amts := []int64{100, 400, 200} - candidates := make([]Candidate, len(amts)) + validators := make([]Validator, len(amts)) for i, amt := range amts { - candidates[i] = NewCandidate(addrs[i], pks[i], Description{}) - candidates[i].BondedShares = sdk.NewRat(amt) - candidates[i].DelegatorShares = sdk.NewRat(amt) - keeper.setCandidate(ctx, candidates[i]) + validators[i] = NewValidator(addrs[i], pks[i], Description{}) + validators[i].BondedShares = sdk.NewRat(amt) + validators[i].DelegatorShares = sdk.NewRat(amt) + keeper.setValidator(ctx, validators[i]) } - acc := keeper.getAccUpdateValidators(ctx) - assert.Equal(t, len(amts), len(acc)) - keeper.clearAccUpdateValidators(ctx) - acc = keeper.getAccUpdateValidators(ctx) - assert.Equal(t, 0, len(acc)) + updates := keeper.getTendermintUpdates(ctx) + assert.Equal(t, len(amts), len(updates)) + keeper.clearTendermintUpdates(ctx) + updates = keeper.getTendermintUpdates(ctx) + assert.Equal(t, 0, len(updates)) } -// test the mechanism which keeps track of a validator set change -func TestGetAccUpdateValidators(t *testing.T) { +// test the mechanism which keeps track of a gotValidator set change +func TestGetTendermintUpdates(t *testing.T) { ctx, _, keeper := createTestInput(t, false, 0) params := defaultParams() params.MaxValidators = 4 keeper.setParams(ctx, params) - // TODO eliminate use of candidatesIn here + // TODO eliminate use of validatorsIn here // tests could be clearer if they just - // created the candidate at time of use + // created the validator at time of use // and were labelled by power in the comments // outlining in each test amts := []int64{10, 11, 12, 13, 1} - var candidatesIn [5]Candidate + var validatorsIn [5]Validator for i, amt := range amts { - candidatesIn[i] = NewCandidate(addrs[i], pks[i], Description{}) - candidatesIn[i].BondedShares = sdk.NewRat(amt) - candidatesIn[i].DelegatorShares = sdk.NewRat(amt) + validatorsIn[i] = NewValidator(addrs[i], pks[i], Description{}) + validatorsIn[i].BondedShares = sdk.NewRat(amt) + validatorsIn[i].DelegatorShares = sdk.NewRat(amt) } // test from nothing to something - // candidate set: {} -> {c1, c3} // validator set: {} -> {c1, c3} - // accUpdate set: {} -> {c1, c3} - assert.Equal(t, 0, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // gotValidator set: {} -> {c1, c3} + // tendermintUpdate set: {} -> {c1, c3} + assert.Equal(t, 0, len(keeper.GetValidatorsBonded(ctx))) // GetValidatorsBonded(ctx, 5 + assert.Equal(t, 0, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - keeper.setCandidate(ctx, candidatesIn[1]) - keeper.setCandidate(ctx, candidatesIn[3]) + keeper.setValidator(ctx, validatorsIn[1]) + keeper.setValidator(ctx, validatorsIn[3]) - vals := keeper.getValidatorsOrdered(ctx) // to init recent validator set + vals := keeper.GetValidatorsBondedByPower(ctx) // to init recent gotValidator set require.Equal(t, 2, len(vals)) - acc := keeper.getAccUpdateValidators(ctx) - require.Equal(t, 2, len(acc)) - candidates := keeper.GetCandidates(ctx, 5) - require.Equal(t, 2, len(candidates)) - assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) - assert.Equal(t, candidates[1].validator().abciValidator(keeper.cdc), acc[1]) - assert.True(t, candidates[0].validator().equal(vals[1])) - assert.True(t, candidates[1].validator().equal(vals[0])) + updates := keeper.getTendermintUpdates(ctx) + require.Equal(t, 2, len(updates)) + validators := keeper.GetValidatorsBonded(ctx) //GetValidatorsBonded(ctx, 5 + require.Equal(t, 2, len(validators)) + assert.Equal(t, validators[0].abciValidator(keeper.cdc), updates[0]) + assert.Equal(t, validators[1].abciValidator(keeper.cdc), updates[1]) + assert.True(t, validators[0].equal(vals[1])) + assert.True(t, validators[1].equal(vals[0])) // test identical, - // candidate set: {c1, c3} -> {c1, c3} - // accUpdate set: {} -> {} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // validator set: {c1, c3} -> {c1, c3} + // tendermintUpdate set: {} -> {} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 2, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - keeper.setCandidate(ctx, candidates[0]) - keeper.setCandidate(ctx, candidates[1]) + keeper.setValidator(ctx, validators[0]) + keeper.setValidator(ctx, validators[1]) - require.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + require.Equal(t, 2, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) // test single value change - // candidate set: {c1, c3} -> {c1', c3} - // accUpdate set: {} -> {c1'} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // validator set: {c1, c3} -> {c1', c3} + // tendermintUpdate set: {} -> {c1'} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 2, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - candidates[0].BondedShares = sdk.NewRat(600) - keeper.setCandidate(ctx, candidates[0]) + validators[0].BondedShares = sdk.NewRat(600) + keeper.setValidator(ctx, validators[0]) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 2, len(candidates)) - assert.True(t, candidates[0].BondedShares.Equal(sdk.NewRat(600))) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 1, len(acc)) - assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) + validators = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 2, len(validators)) + assert.True(t, validators[0].BondedShares.Equal(sdk.NewRat(600))) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 1, len(updates)) + assert.Equal(t, validators[0].abciValidator(keeper.cdc), updates[0]) // test multiple value change - // candidate set: {c1, c3} -> {c1', c3'} - // accUpdate set: {c1, c3} -> {c1', c3'} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // validator set: {c1, c3} -> {c1', c3'} + // tendermintUpdate set: {c1, c3} -> {c1', c3'} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 2, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - candidates[0].BondedShares = sdk.NewRat(200) - candidates[1].BondedShares = sdk.NewRat(100) - keeper.setCandidate(ctx, candidates[0]) - keeper.setCandidate(ctx, candidates[1]) + validators[0].BondedShares = sdk.NewRat(200) + validators[1].BondedShares = sdk.NewRat(100) + keeper.setValidator(ctx, validators[0]) + keeper.setValidator(ctx, validators[1]) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 2, len(acc)) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 2, len(candidates)) - require.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) - require.Equal(t, candidates[1].validator().abciValidator(keeper.cdc), acc[1]) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 2, len(updates)) + validators = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 2, len(validators)) + require.Equal(t, validators[0].abciValidator(keeper.cdc), updates[0]) + require.Equal(t, validators[1].abciValidator(keeper.cdc), updates[1]) // test validtor added at the beginning - // candidate set: {c1, c3} -> {c0, c1, c3} - // accUpdate set: {} -> {c0} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 2, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // validator set: {c1, c3} -> {c0, c1, c3} + // tendermintUpdate set: {} -> {c0} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 2, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - keeper.setCandidate(ctx, candidatesIn[0]) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 1, len(acc)) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 3, len(candidates)) - assert.Equal(t, candidates[0].validator().abciValidator(keeper.cdc), acc[0]) + keeper.setValidator(ctx, validatorsIn[0]) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 1, len(updates)) + validators = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 3, len(validators)) + assert.Equal(t, validators[0].abciValidator(keeper.cdc), updates[0]) - // test validator added at the middle - // candidate set: {c0, c1, c3} -> {c0, c1, c2, c3] - // accUpdate set: {} -> {c2} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 3, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // test gotValidator added at the middle + // validator set: {c0, c1, c3} -> {c0, c1, c2, c3] + // tendermintUpdate set: {} -> {c2} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 3, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - keeper.setCandidate(ctx, candidatesIn[2]) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 1, len(acc)) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 4, len(candidates)) - assert.Equal(t, candidates[2].validator().abciValidator(keeper.cdc), acc[0]) + keeper.setValidator(ctx, validatorsIn[2]) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 1, len(updates)) + validators = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 4, len(validators)) + assert.Equal(t, validators[2].abciValidator(keeper.cdc), updates[0]) - // test candidate added at the end but not inserted in the valset - // candidate set: {c0, c1, c2, c3} -> {c0, c1, c2, c3, c4} - // validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} - // accUpdate set: {} -> {} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 4, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // test validator added at the end but not inserted in the valset + // validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3, c4} + // gotValidator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} + // tendermintUpdate set: {} -> {} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - keeper.setCandidate(ctx, candidatesIn[4]) + keeper.setValidator(ctx, validatorsIn[4]) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4 + assert.Equal(t, 5, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx))) + require.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) // max gotValidator number is 4 - // test candidate change its power but still not in the valset - // candidate set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4} - // validator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} - // accUpdate set: {} -> {} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // test validator change its power but still not in the valset + // validator set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4} + // gotValidator set: {c0, c1, c2, c3} -> {c0, c1, c2, c3} + // tendermintUpdate set: {} -> {} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 5, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - candidatesIn[4].BondedShares = sdk.NewRat(1) - keeper.setCandidate(ctx, candidatesIn[4]) + validatorsIn[4].BondedShares = sdk.NewRat(1) + keeper.setValidator(ctx, validatorsIn[4]) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - require.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) // max validator number is 4 + assert.Equal(t, 5, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx))) + require.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) // max gotValidator number is 4 - // 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} - // validator set: {c0, c1, c2, c3} -> {c1, c2, c3, c4} - // accUpdate set: {} -> {c0, c4} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // test validator change its power and become a gotValidator (pushing out an existing) + // validator set: {c0, c1, c2, c3, c4} -> {c0, c1, c2, c3, c4} + // gotValidator set: {c0, c1, c2, c3} -> {c1, c2, c3, c4} + // tendermintUpdate set: {} -> {c0, c4} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 5, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - candidatesIn[4].BondedShares = sdk.NewRat(1000) - keeper.setCandidate(ctx, candidatesIn[4]) + validatorsIn[4].BondedShares = sdk.NewRat(1000) + keeper.setValidator(ctx, validatorsIn[4]) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 5, len(candidates)) - vals = keeper.getValidatorsOrdered(ctx) + validators = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 5, len(validators)) + vals = keeper.GetValidatorsBondedByPower(ctx) require.Equal(t, 4, len(vals)) - assert.Equal(t, candidatesIn[1].Address, vals[1].Address) - assert.Equal(t, candidatesIn[2].Address, vals[3].Address) - assert.Equal(t, candidatesIn[3].Address, vals[2].Address) - assert.Equal(t, candidatesIn[4].Address, vals[0].Address) + assert.Equal(t, validatorsIn[1].Address, vals[1].Address) + assert.Equal(t, validatorsIn[2].Address, vals[3].Address) + assert.Equal(t, validatorsIn[3].Address, vals[2].Address) + assert.Equal(t, validatorsIn[4].Address, vals[0].Address) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 2, len(acc), "%v", acc) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 2, len(updates), "%v", updates) - assert.Equal(t, candidatesIn[0].PubKey.Bytes(), acc[0].PubKey) - assert.Equal(t, int64(0), acc[0].Power) - assert.Equal(t, vals[0].abciValidator(keeper.cdc), acc[1]) + assert.Equal(t, validatorsIn[0].PubKey.Bytes(), updates[0].PubKey) + assert.Equal(t, int64(0), updates[0].Power) + assert.Equal(t, vals[0].abciValidator(keeper.cdc), updates[1]) // test from something to nothing - // candidate set: {c0, c1, c2, c3, c4} -> {} - // validator set: {c1, c2, c3, c4} -> {} - // accUpdate set: {} -> {c1, c2, c3, c4} - keeper.clearAccUpdateValidators(ctx) - assert.Equal(t, 5, len(keeper.GetCandidates(ctx, 5))) - assert.Equal(t, 4, len(keeper.GetValidators(ctx))) - assert.Equal(t, 0, len(keeper.getAccUpdateValidators(ctx))) + // validator set: {c0, c1, c2, c3, c4} -> {} + // gotValidator set: {c1, c2, c3, c4} -> {} + // tendermintUpdate set: {} -> {c1, c2, c3, c4} + keeper.clearTendermintUpdates(ctx) + assert.Equal(t, 5, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 4, len(keeper.GetValidatorsBonded(ctx))) + assert.Equal(t, 0, len(keeper.getTendermintUpdates(ctx))) - keeper.removeCandidate(ctx, candidatesIn[0].Address) - keeper.removeCandidate(ctx, candidatesIn[1].Address) - keeper.removeCandidate(ctx, candidatesIn[2].Address) - keeper.removeCandidate(ctx, candidatesIn[3].Address) - keeper.removeCandidate(ctx, candidatesIn[4].Address) + keeper.removeValidator(ctx, validatorsIn[0].Address) + keeper.removeValidator(ctx, validatorsIn[1].Address) + keeper.removeValidator(ctx, validatorsIn[2].Address) + keeper.removeValidator(ctx, validatorsIn[3].Address) + keeper.removeValidator(ctx, validatorsIn[4].Address) - vals = keeper.getValidatorsOrdered(ctx) + vals = keeper.GetValidatorsBondedByPower(ctx) assert.Equal(t, 0, len(vals), "%v", vals) - candidates = keeper.GetCandidates(ctx, 5) - require.Equal(t, 0, len(candidates)) - acc = keeper.getAccUpdateValidators(ctx) - require.Equal(t, 4, len(acc)) - assert.Equal(t, candidatesIn[1].PubKey.Bytes(), acc[0].PubKey) - assert.Equal(t, candidatesIn[2].PubKey.Bytes(), acc[1].PubKey) - assert.Equal(t, candidatesIn[3].PubKey.Bytes(), acc[2].PubKey) - assert.Equal(t, candidatesIn[4].PubKey.Bytes(), acc[3].PubKey) - assert.Equal(t, int64(0), acc[0].Power) - assert.Equal(t, int64(0), acc[1].Power) - assert.Equal(t, int64(0), acc[2].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 -func TestGetTotalPrecommitVotingPower(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - amts := []int64{10000, 1000, 100, 10, 1} - 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) - keeper.setCandidate(ctx, candidatesIn[i]) - } - - // test that an empty validator set doesn't have any validators - validators := keeper.GetValidators(ctx) - assert.Equal(t, 5, len(validators)) - - totPow := keeper.GetTotalPrecommitVotingPower(ctx) - exp := sdk.NewRat(11111) - assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow) - - // set absent validators to be the 1st and 3rd record sorted by pubKey address - ctx = ctx.WithAbsentValidators([]int32{1, 3}) - totPow = keeper.GetTotalPrecommitVotingPower(ctx) - - // XXX verify that this order should infact exclude these two records - exp = sdk.NewRat(11100) - assert.True(t, exp.Equal(totPow), "exp %v, got %v", exp, totPow) + validators = keeper.GetValidatorsBonded(ctx) + require.Equal(t, 0, len(validators)) + updates = keeper.getTendermintUpdates(ctx) + require.Equal(t, 4, len(updates)) + assert.Equal(t, validatorsIn[1].PubKey.Bytes(), updates[0].PubKey) + assert.Equal(t, validatorsIn[2].PubKey.Bytes(), updates[1].PubKey) + assert.Equal(t, validatorsIn[3].PubKey.Bytes(), updates[2].PubKey) + assert.Equal(t, validatorsIn[4].PubKey.Bytes(), updates[3].PubKey) + assert.Equal(t, int64(0), updates[0].Power) + assert.Equal(t, int64(0), updates[1].Power) + assert.Equal(t, int64(0), updates[2].Power) + assert.Equal(t, int64(0), updates[3].Power) } func TestParams(t *testing.T) { @@ -704,50 +634,3 @@ func TestPool(t *testing.T) { resPool = keeper.GetPool(ctx) assert.True(t, expPool.equal(resPool)) } - -func TestValidatorsetKeeper(t *testing.T) { - ctx, _, keeper := createTestInput(t, false, 0) - - total := int64(0) - amts := []int64{9, 8, 7} - var validators [3]Validator - for i, amt := range amts { - candidates[i] = Candidate{ - Address: addrVals[i], - PubKey: pks[i], - Assets: sdk.NewRat(amt), - Liabilities: sdk.NewRat(amt), - } - - keeper.setValidator(ctx, validators[i]) - - total += amt - } - - assert.Equal(t, 3, keeper.Size(ctx)) - - for _, addr := range addrVals[:3] { - assert.True(t, keeper.IsValidator(ctx, addr)) - } - for _, addr := range addrVals[3:] { - assert.False(t, keeper.IsValidator(ctx, addr)) - } - - for i, addr := range addrVals[:3] { - index, val := keeper.GetByAddress(ctx, addr) - assert.Equal(t, i, index) - assert.Equal(t, candidates[i].validator().abciValidator(keeper.cdc), *val) - } - - for _, addr := range addrVals[3:] { - index, val := keeper.GetByAddress(ctx, addr) - assert.Equal(t, -1, index) - assert.Nil(t, val) - } - - for i, can := range candidates { - assert.Equal(t, can.validator().abciValidator(keeper.cdc), *keeper.GetByIndex(ctx, i)) - } - - assert.Equal(t, total, keeper.TotalPower(ctx).Evaluate()) -} diff --git a/x/stake/pool_test.go b/x/stake/pool_test.go index 1c4b8d48a9..417a455475 100644 --- a/x/stake/pool_test.go +++ b/x/stake/pool_test.go @@ -60,16 +60,16 @@ func TestBondedToUnbondedPool(t *testing.T) { assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat()) assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat()) candA := Validator{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - BondedShares: sdk.OneRat(), + Status: sdk.Bonded, + Address: addrs[0], + PubKey: pks[0], + BondedShares: sdk.OneRat(), DelegatorShares: sdk.OneRat(), } poolB, candB := poolA.bondedToUnbondedPool(candA) // status unbonded - assert.Equal(t, candB.Status, Unbonded) + assert.Equal(t, candB.Status, sdk.Unbonded) // same exchange rate, assets unchanged assert.Equal(t, candB.BondedShares, candA.BondedShares) // bonded pool decreased @@ -87,17 +87,17 @@ func TestUnbonbedtoBondedPool(t *testing.T) { assert.Equal(t, poolA.bondedShareExRate(), sdk.OneRat()) assert.Equal(t, poolA.unbondedShareExRate(), sdk.OneRat()) candA := Validator{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - BondedShares: sdk.OneRat(), + Status: sdk.Bonded, + Address: addrs[0], + PubKey: pks[0], + BondedShares: sdk.OneRat(), DelegatorShares: sdk.OneRat(), } - candA.Status = Unbonded + candA.Status = sdk.Unbonded poolB, candB := poolA.unbondedToBondedPool(candA) // status bonded - assert.Equal(t, candB.Status, Bonded) + assert.Equal(t, candB.Status, sdk.Bonded) // same exchange rate, assets unchanged assert.Equal(t, candB.BondedShares, candA.BondedShares) // bonded pool increased @@ -177,10 +177,10 @@ func TestValidatorAddTokens(t *testing.T) { poolA := keeper.GetPool(ctx) candA := Validator{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - BondedShares: sdk.NewRat(9), + Status: sdk.Bonded, + Address: addrs[0], + PubKey: pks[0], + BondedShares: sdk.NewRat(9), DelegatorShares: sdk.NewRat(9), } poolA.BondedPool = candA.BondedShares.Evaluate() @@ -203,10 +203,10 @@ func TestValidatorRemoveShares(t *testing.T) { poolA := keeper.GetPool(ctx) candA := Validator{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - BondedShares: sdk.NewRat(9), + Status: sdk.Bonded, + Address: addrs[0], + PubKey: pks[0], + BondedShares: sdk.NewRat(9), DelegatorShares: sdk.NewRat(9), } poolA.BondedPool = candA.BondedShares.Evaluate() @@ -227,10 +227,10 @@ func TestValidatorRemoveShares(t *testing.T) { assets := sdk.NewRat(5102) liabilities := sdk.NewRat(115) cand := Validator{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - BondedShares: assets, + Status: sdk.Bonded, + Address: addrs[0], + PubKey: pks[0], + BondedShares: assets, DelegatorShares: liabilities, } pool := Pool{ @@ -258,19 +258,19 @@ func TestValidatorRemoveShares(t *testing.T) { // generate a random validator func randomValidator(r *rand.Rand) Validator { - var status ValidatorStatus + var status sdk.ValidatorStatus if r.Float64() < float64(0.5) { - status = Bonded + status = sdk.Bonded } else { - status = Unbonded + status = sdk.Unbonded } assets := sdk.NewRat(int64(r.Int31n(10000))) liabilities := sdk.NewRat(int64(r.Int31n(10000))) return Validator{ - Status: status, - Address: addrs[0], - PubKey: pks[0], - BondedShares: assets, + Status: status, + Address: addrs[0], + PubKey: pks[0], + BondedShares: assets, DelegatorShares: liabilities, } } @@ -290,10 +290,10 @@ func randomSetup(r *rand.Rand, numValidators int) (Pool, Validators) { validators := make([]Validator, numValidators) for i := 0; i < numValidators; i++ { validator := randomValidator(r) - if validator.Status == Bonded { + if validator.Status == sdk.Bonded { pool.BondedShares = pool.BondedShares.Add(validator.BondedShares) pool.BondedPool += validator.BondedShares.Evaluate() - } else if validator.Status == Unbonded { + } else if validator.Status == sdk.Unbonded { pool.UnbondedShares = pool.UnbondedShares.Add(validator.BondedShares) pool.UnbondedPool += validator.BondedShares.Evaluate() } @@ -310,13 +310,13 @@ type Operation func(r *rand.Rand, p Pool, c Validator) (Pool, Validator, int64, // operation: bond or unbond a validator depending on current status func OpBondOrUnbond(r *rand.Rand, p Pool, cand Validator) (Pool, Validator, int64, string) { var msg string - if cand.Status == Bonded { - msg = fmt.Sprintf("Unbonded previously bonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)", + if cand.Status == sdk.Bonded { + msg = fmt.Sprintf("sdk.Unbonded previously bonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)", cand.Address, cand.BondedShares, cand.DelegatorShares, cand.delegatorShareExRate()) p, cand = p.bondedToUnbondedPool(cand) - } else if cand.Status == Unbonded { - msg = fmt.Sprintf("Bonded previously unbonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)", + } else if cand.Status == sdk.Unbonded { + msg = fmt.Sprintf("sdk.Bonded previously unbonded validator %s (assets: %v, liabilities: %v, delegatorShareExRate: %v)", cand.Address, cand.BondedShares, cand.DelegatorShares, cand.delegatorShareExRate()) p, cand = p.unbondedToBondedPool(cand) } @@ -436,10 +436,10 @@ func TestPossibleOverflow(t *testing.T) { assets := sdk.NewRat(2159) liabilities := sdk.NewRat(391432570689183511).Quo(sdk.NewRat(40113011844664)) cand := Validator{ - Status: Bonded, - Address: addrs[0], - PubKey: pks[0], - BondedShares: assets, + Status: sdk.Bonded, + Address: addrs[0], + PubKey: pks[0], + BondedShares: assets, DelegatorShares: liabilities, } pool := Pool{ diff --git a/x/stake/tick.go b/x/stake/tick.go index 1c8878fad5..b712ff9c92 100644 --- a/x/stake/tick.go +++ b/x/stake/tick.go @@ -30,8 +30,8 @@ func (k Keeper) Tick(ctx sdk.Context) (change []abci.Validator) { k.setIntraTxCounter(ctx, 0) // calculate validator set changes - change = k.getValidatorsTendermintUpdates(ctx) - k.clearValidatorsTendermintUpdates(ctx) + change = k.getTendermintUpdates(ctx) + k.clearTendermintUpdates(ctx) // XXX get the total validator of the previous validator set // XXX get the total validator of the current validator set diff --git a/x/stake/tick_test.go b/x/stake/tick_test.go index 12fc303037..93efe00885 100644 --- a/x/stake/tick_test.go +++ b/x/stake/tick_test.go @@ -69,14 +69,14 @@ func TestProcessProvisions(t *testing.T) { validators := make([]Validator, 10) for i := 0; i < 10; i++ { c := Validator{ - Status: Unbonded, - PubKey: pks[i], - Address: addrs[i], - BondedShares: sdk.NewRat(0), + Status: sdk.Unbonded, + PubKey: pks[i], + Address: addrs[i], + BondedShares: sdk.NewRat(0), DelegatorShares: sdk.NewRat(0), } if i < 5 { - c.Status = Bonded + c.Status = sdk.Bonded } mintedTokens := int64((i + 1) * 10000000) pool.TotalSupply += mintedTokens