Example of more functional proposer API

This commit is contained in:
Zarko Milosevic
2019-01-10 16:30:24 +01:00
parent d57a82840f
commit c795ec7642
4 changed files with 21 additions and 41 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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{

View File

@@ -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.