From c795ec76423e0704bfe452c16d59989f5cec1bab Mon Sep 17 00:00:00 2001 From: Zarko Milosevic Date: Thu, 10 Jan 2019 16:30:24 +0100 Subject: [PATCH] Example of more functional proposer API --- consensus/state.go | 10 +++++++--- state/execution.go | 16 ++++++++-------- state/state.go | 3 ++- types/validator_set.go | 33 ++++----------------------------- 4 files changed, 21 insertions(+), 41 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 81bdce7d..5b2c5cf3 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -743,9 +743,13 @@ func (cs *ConsensusState) enterNewRound(height int64, round int) { // Increment validators if necessary validators := cs.Validators - if cs.Round < round { - validators = validators.Copy() - validators.IncrementProposerPriority(round - cs.Round) + //if cs.Round < round { + // validators = validators.Copy() + // validators.IncrementProposerPriority(round - cs.Round) + //} + + for i := cs.Round; i < round; i++ { + validators = validators.UpdateProposerPriority() } // Setup new round diff --git a/state/execution.go b/state/execution.go index b3d7268e..e3473c54 100644 --- a/state/execution.go +++ b/state/execution.go @@ -344,11 +344,7 @@ func validateValidatorUpdates(abciUpdates []abci.ValidatorUpdate, } func NextValidators(currentSet *types.ValidatorSet, updates []*types.Validator) (*types.ValidatorSet, error) { - nValSet := currentSet.Copy() - // update proposer priority. Increase proposer priority for every process for ammount - // equal to their voting power and decrease proposer priority of the initial proposer - // equal to total voting power - nValSet.UpdateProposerPriority() + nValSet := currentSet.UpdateProposerPriority() // apply updates for _, valUpdate := range updates { @@ -488,12 +484,13 @@ func updateState( // Copy the valset so we can apply changes from EndBlock // and update s.LastValidators and s.Validators. - nValSet := state.NextValidators.Copy() + nValSet, err := state.NextValidators, error(nil) // Update the validator set with the latest abciResponses. lastHeightValsChanged := state.LastHeightValidatorsChanged if len(validatorUpdates) > 0 { - err := updateValidators(nValSet, validatorUpdates) + //err := updateValidators(nValSet, validatorUpdates) + nValSet, err = NextValidators(nValSet, validatorUpdates) if err != nil { return state, fmt.Errorf("Error changing validator set: %v", err) } @@ -501,8 +498,11 @@ func updateState( lastHeightValsChanged = header.Height + 1 + 1 } + //TODO: Given validator set at height h, when we move to height h+1, should we increment + //proposer priority with the voting power they have at height h, or at height h+1? + // Update validator proposer priority and set state variables. - nValSet.IncrementProposerPriority(1) + //nValSet.IncrementProposerPriority(1) // Update the params with the latest abciResponses. nextParams := state.ConsensusParams diff --git a/state/state.go b/state/state.go index b6253b64..fd8320e1 100644 --- a/state/state.go +++ b/state/state.go @@ -226,7 +226,8 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) { validators[i] = types.NewValidator(val.PubKey, val.Power) } validatorSet = types.NewValidatorSet(validators) - nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1) + //nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1) + nextValidatorSet = validatorSet.UpdateProposerPriority() } return State{ diff --git a/types/validator_set.go b/types/validator_set.go index effb7e07..5c22f94c 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -37,8 +37,6 @@ type ValidatorSet struct { // cached (unexported) totalVotingPower int64 - initProposerPriorities []int64 - round int } // NewValidatorSet initializes a ValidatorSet by copying over the @@ -58,8 +56,6 @@ func NewValidatorSet(valz []*Validator) *ValidatorSet { vals := &ValidatorSet{ Validators: validators, - initProposerPriorities: propPriorities, - round: 0, } //if len(valz) > 0 { @@ -81,29 +77,6 @@ func (vals *ValidatorSet) CopyIncrementProposerPriority(times int) *ValidatorSet return copy } -// FindProposer computes the proposer of the given round for the validator set. -// The function for a given validator set and round number always return the same validator -// as a proposer, i.e., it is purely functional. -func (vals *ValidatorSet) FindProposer(round int) *Validator { - var proposer *Validator - if round < vals.round { - initialValSet := vals.Copy() - initialValSet.round = 0 - for i, val := range initialValSet.Validators { - val.ProposerPriority = initialValSet.initProposerPriorities[i] - } - for i := initialValSet.round; i <= round; i++ { - proposer = vals.UpdateProposerPriority() - } - } else { - for i := vals.round; i <= round; i++ { - proposer = vals.UpdateProposerPriority() - } - vals.round = round - } - return proposer -} - // IncrementProposerPriority increments ProposerPriority of each validator and updates the // proposer. Panics if validator set is empty. // `times` must be positive. @@ -127,13 +100,15 @@ func (vals *ValidatorSet) IncrementProposerPriority(times int) { vals.Proposer = proposer } +// UpdateProposerPriority update proposer priority between rounds. Proposer priority of +// every proposer is augmented with its voting power; proposer priority of the coordinator is +// decreased by the total voting power. func (vals *ValidatorSet) UpdateProposerPriority() *ValidatorSet { nValSet := vals.Copy() // just pick the proposer with the highest proposer priority // without any modification to it - proposer := vals.FindProposer(0) // maybe use getProposer here - + proposer := vals.GetProposer() // update proposer priority for _, val := range nValSet.Validators { // Check for overflow for sum.