mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-11 14:33:52 +00:00
dev: rename CompareProposerPriority to Cmp, add nil check (2.27)
This commit is contained in:
parent
48d6d76d18
commit
bfab1d932d
2 changed files with 10 additions and 6 deletions
|
|
@ -29,18 +29,23 @@ func NewValidator(address common.Address, votingPower int64) *Validator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new copy of the validator so we can mutate ProposerPriority.
|
// Copy creates a new copy of the validator so we can mutate ProposerPriority.
|
||||||
// Panics if the validator is nil.
|
// Panics if the validator is nil.
|
||||||
func (v *Validator) Copy() *Validator {
|
func (v *Validator) Copy() *Validator {
|
||||||
vCopy := *v
|
vCopy := *v
|
||||||
return &vCopy
|
return &vCopy
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the one with higher ProposerPriority.
|
// Cmp returns the one validator with a higher ProposerPriority.
|
||||||
func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
|
// If ProposerPriority is same, it returns the validator with lexicographically smaller address
|
||||||
|
func (v *Validator) Cmp(other *Validator) *Validator {
|
||||||
|
// if both of v and other are nil, nil will be returned and that could possibly lead to nil pointer dereference bubbling up the stack
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return other
|
return other
|
||||||
}
|
}
|
||||||
|
if other == nil {
|
||||||
|
return v
|
||||||
|
}
|
||||||
if v.ProposerPriority > other.ProposerPriority {
|
if v.ProposerPriority > other.ProposerPriority {
|
||||||
return v
|
return v
|
||||||
} else if v.ProposerPriority < other.ProposerPriority {
|
} else if v.ProposerPriority < other.ProposerPriority {
|
||||||
|
|
@ -53,7 +58,6 @@ func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
|
||||||
return other
|
return other
|
||||||
} else {
|
} else {
|
||||||
panic("Cannot compare identical validators")
|
panic("Cannot compare identical validators")
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -182,7 +182,7 @@ func computeMaxMinPriorityDiff(vals *ValidatorSet) int64 {
|
||||||
func (vals *ValidatorSet) getValWithMostPriority() *Validator {
|
func (vals *ValidatorSet) getValWithMostPriority() *Validator {
|
||||||
var res *Validator
|
var res *Validator
|
||||||
for _, val := range vals.Validators {
|
for _, val := range vals.Validators {
|
||||||
res = res.CompareProposerPriority(val)
|
res = res.Cmp(val)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
@ -298,7 +298,7 @@ func (vals *ValidatorSet) findProposer() *Validator {
|
||||||
var proposer *Validator
|
var proposer *Validator
|
||||||
for _, val := range vals.Validators {
|
for _, val := range vals.Validators {
|
||||||
if proposer == nil || !bytes.Equal(val.Address.Bytes(), proposer.Address.Bytes()) {
|
if proposer == nil || !bytes.Equal(val.Address.Bytes(), proposer.Address.Bytes()) {
|
||||||
proposer = proposer.CompareProposerPriority(val)
|
proposer = proposer.Cmp(val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return proposer
|
return proposer
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue