From 59562cd86493e4e718d000c8386d28a5a81bdc71 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Fri, 23 Mar 2018 16:55:50 +0100 Subject: [PATCH] remove term voting power from candidates --- x/stake/keeper.go | 10 ++--- x/stake/keeper_test.go | 86 +++++++++++++++++++++++------------------- x/stake/test_common.go | 33 +--------------- x/stake/types.go | 16 ++++---- 4 files changed, 60 insertions(+), 85 deletions(-) diff --git a/x/stake/keeper.go b/x/stake/keeper.go index 7f82d747a8..1caaeb63bc 100644 --- a/x/stake/keeper.go +++ b/x/stake/keeper.go @@ -81,7 +81,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { store.Set(GetCandidateKey(candidate.Address), bz) // mashal the new validator record - validator := Validator{address, candidate.VotingPower} + validator := Validator{address, candidate.Assets} bz, err = k.cdc.MarshalBinary(validator) if err != nil { panic(err) @@ -89,7 +89,7 @@ func (k Keeper) setCandidate(ctx sdk.Context, candidate Candidate) { // update the list ordered by voting power if oldFound { - store.Delete(GetValidatorKey(address, oldCandidate.VotingPower, k.cdc)) + store.Delete(GetValidatorKey(address, oldCandidate.Assets, k.cdc)) } store.Set(GetValidatorKey(address, validator.VotingPower, k.cdc), bz) @@ -124,14 +124,14 @@ func (k Keeper) removeCandidate(ctx sdk.Context, address sdk.Address) { } store.Set(GetAccUpdateValidatorKey(address), bz) store.Delete(GetRecentValidatorKey(address)) - store.Delete(GetValidatorKey(address, oldCandidate.VotingPower, k.cdc)) + store.Delete(GetValidatorKey(address, oldCandidate.Assets, k.cdc)) } //___________________________________________________________________________ // get the most recent updated validator set from the Candidates. These bonds -// are already sorted by VotingPower from the UpdateVotingPower function which -// is the only function which is to modify the VotingPower +// are already sorted by Assets from the UpdateVotingPower function which +// is the only function which is to modify the Assets // this function also updaates the most recent validators saved in store func (k Keeper) GetValidators(ctx sdk.Context) (validators []Validator) { store := ctx.KVStore(k.storeKey) diff --git a/x/stake/keeper_test.go b/x/stake/keeper_test.go index 77a214df82..911b7036c3 100644 --- a/x/stake/keeper_test.go +++ b/x/stake/keeper_test.go @@ -11,19 +11,60 @@ import ( "github.com/stretchr/testify/require" ) -// XXX XXX XXX -// XXX revive these tests but for the store update proceedure -// XXX XXX XXX +var ( + addrDel1 = addrs[0] + addrDel2 = addrs[1] + addrVal1 = addrs[2] + addrVal2 = addrs[3] + addrVal3 = addrs[4] + pk1 = crypto.GenPrivKeyEd25519().PubKey() + pk2 = crypto.GenPrivKeyEd25519().PubKey() + pk3 = crypto.GenPrivKeyEd25519().PubKey() + + candidate1 = Candidate{ + Address: addrVal1, + PubKey: pk1, + Assets: sdk.NewRat(9), + Liabilities: sdk.NewRat(9), + } + candidate2 = Candidate{ + Address: addrVal2, + PubKey: pk2, + Assets: sdk.NewRat(9), + Liabilities: sdk.NewRat(9), + } + candidate3 = Candidate{ + Address: addrVal3, + PubKey: pk3, + Assets: sdk.NewRat(9), + Liabilities: sdk.NewRat(9), + } +) func TestUpdateVotingPower(t *testing.T) { ctx, _, keeper := createTestInput(t, nil, false, 0) - candidates := candidatesFromAddrs(ctx, keeper, addrs, []int64{400, 200, 100, 10, 1}) + // initialize some candidates into the state + amts := []int64{400, 200, 100, 10, 1} + candidates := make([]Candidate, 5) + for i := 0; i < len(5); i++ { + c := Candidate{ + Status: Unbonded, + PubKey: pks[i], + Address: addrs[i], + Assets: sdk.NewRat(amts[i]), + Liabilities: sdk.NewRat(amts[i]), + VotingPower: sdk.NewRat(amts[i]), + } + keeper.setCandidate(ctx, c) + candidate[i] = c + } // test a basic change in voting power candidates[0].Assets = sdk.NewRat(500) - candidates.updateVotingPower(store, p, params) - keeper.updaate + keeper.setCandidate(ctx, candidate[0]) + validators + assert.Equal(int64(500), candidates[0].VotingPower.Evaluate(), "%v", candidates[0]) // test a swap in voting power @@ -170,39 +211,6 @@ func TestGetValidators(t *testing.T) { assert.Equal(t, addrs[1], validators[1].Address, "%v", validators) } -var ( - addrDel1 = addrs[0] - addrDel2 = addrs[1] - addrVal1 = addrs[2] - addrVal2 = addrs[3] - addrVal3 = addrs[4] - pk1 = crypto.GenPrivKeyEd25519().PubKey() - pk2 = crypto.GenPrivKeyEd25519().PubKey() - pk3 = crypto.GenPrivKeyEd25519().PubKey() - - candidate1 = Candidate{ - Address: addrVal1, - PubKey: pk1, - Assets: sdk.NewRat(9), - Liabilities: sdk.NewRat(9), - VotingPower: sdk.ZeroRat, - } - candidate2 = Candidate{ - Address: addrVal2, - PubKey: pk2, - Assets: sdk.NewRat(9), - Liabilities: sdk.NewRat(9), - VotingPower: sdk.ZeroRat, - } - candidate3 = Candidate{ - Address: addrVal3, - PubKey: pk3, - Assets: sdk.NewRat(9), - Liabilities: sdk.NewRat(9), - VotingPower: sdk.ZeroRat, - } -) - // XXX expand to include both liabilities and assets use/test all candidate1 fields func TestCandidate(t *testing.T) { ctx, _, keeper := createTestInput(t, nil, false, 0) diff --git a/x/stake/test_common.go b/x/stake/test_common.go index 96923872cd..b836194c93 100644 --- a/x/stake/test_common.go +++ b/x/stake/test_common.go @@ -148,8 +148,7 @@ var addrs = []sdk.Address{ testAddr("A58856F0FD53BF058B4909A21AEC019107BA6169"), } -// NOTE: PubKey is supposed to be the binaryBytes of the crypto.PubKey -// instead this is just being set the address here for testing purposes +// XXX TODO remove this dep func candidatesFromAddrs(ctx sdk.Context, keeper Keeper, addrs []crypto.Address, amts []int64) { for i := 0; i < len(amts); i++ { c := Candidate{ @@ -158,37 +157,7 @@ func candidatesFromAddrs(ctx sdk.Context, keeper Keeper, addrs []crypto.Address, Address: addrs[i], Assets: sdk.NewRat(amts[i]), Liabilities: sdk.NewRat(amts[i]), - VotingPower: sdk.NewRat(amts[i]), } keeper.setCandidate(ctx, c) } } - -func candidatesFromAddrsEmpty(addrs []crypto.Address) (candidates Candidates) { - for i := 0; i < len(addrs); i++ { - c := Candidate{ - Status: Unbonded, - PubKey: pks[i], - Address: addrs[i], - Assets: sdk.ZeroRat, - Liabilities: sdk.ZeroRat, - VotingPower: sdk.ZeroRat, - } - candidates = append(candidates, c) - } - return -} - -//// helper function test if Candidate is changed asabci.Validator -//func testChange(t *testing.T, val Validator, chg *abci.Validator) { -//assert := assert.New(t) -//assert.Equal(val.PubKey.Bytes(), chg.PubKey) -//assert.Equal(val.VotingPower.Evaluate(), chg.Power) -//} - -//// helper function test if Candidate is removed as abci.Validator -//func testRemove(t *testing.T, val Validator, chg *abci.Validator) { -//assert := assert.New(t) -//assert.Equal(val.PubKey.Bytes(), chg.PubKey) -//assert.Equal(int64(0), chg.Power) -//} diff --git a/x/stake/types.go b/x/stake/types.go index cdf8ece066..0521bece53 100644 --- a/x/stake/types.go +++ b/x/stake/types.go @@ -98,13 +98,12 @@ const ( // exchange rate. Voting power can be calculated as total bonds multiplied by // exchange rate. type Candidate struct { - Status CandidateStatus `json:"status"` // Bonded status - Address sdk.Address `json:"owner"` // Sender of BondTx - UnbondTx returns here - PubKey crypto.PubKey `json:"pub_key"` // Pubkey of candidate - Assets sdk.Rat `json:"assets"` // total shares of a global hold pools - Liabilities sdk.Rat `json:"liabilities"` // total shares issued to a candidate's delegators - VotingPower sdk.Rat `json:"voting_power"` // Voting power if considered a validator - Description Description `json:"description"` // Description terms for the candidate + Status CandidateStatus `json:"status"` // Bonded status + Address sdk.Address `json:"owner"` // Sender of BondTx - UnbondTx returns here + PubKey crypto.PubKey `json:"pub_key"` // Pubkey of candidate + Assets sdk.Rat `json:"assets"` // total shares of a global hold pools + Liabilities sdk.Rat `json:"liabilities"` // total shares issued to a candidate's delegators + Description Description `json:"description"` // Description terms for the candidate } // Description - description fields for a candidate @@ -123,7 +122,6 @@ func NewCandidate(address sdk.Address, pubKey crypto.PubKey, description Descrip PubKey: pubKey, Assets: sdk.ZeroRat, Liabilities: sdk.ZeroRat, - VotingPower: sdk.ZeroRat, Description: description, } } @@ -141,7 +139,7 @@ func (c Candidate) delegatorShareExRate() sdk.Rat { func (c Candidate) validator() Validator { return Validator{ Address: c.Address, // XXX !!! - VotingPower: c.VotingPower, + VotingPower: c.Assets, } }