From bfab1d932dd7ce2bf596a26b5e1b99a25a24f1ee Mon Sep 17 00:00:00 2001 From: atvanguard <93arpit@gmail.com> Date: Tue, 21 Apr 2020 11:31:45 +0530 Subject: [PATCH] dev: rename CompareProposerPriority to Cmp, add nil check (2.27) --- consensus/bor/validator.go | 12 ++++++++---- consensus/bor/validator_set.go | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/consensus/bor/validator.go b/consensus/bor/validator.go index 1d1266613..dab50e7b2 100644 --- a/consensus/bor/validator.go +++ b/consensus/bor/validator.go @@ -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. func (v *Validator) Copy() *Validator { vCopy := *v return &vCopy } -// Returns the one with higher ProposerPriority. -func (v *Validator) CompareProposerPriority(other *Validator) *Validator { +// Cmp returns the one validator with a higher ProposerPriority. +// 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 { return other } + if other == nil { + return v + } if v.ProposerPriority > other.ProposerPriority { return v } else if v.ProposerPriority < other.ProposerPriority { @@ -53,7 +58,6 @@ func (v *Validator) CompareProposerPriority(other *Validator) *Validator { return other } else { panic("Cannot compare identical validators") - return nil } } } diff --git a/consensus/bor/validator_set.go b/consensus/bor/validator_set.go index 2f22d5b59..89e0c16b1 100644 --- a/consensus/bor/validator_set.go +++ b/consensus/bor/validator_set.go @@ -182,7 +182,7 @@ func computeMaxMinPriorityDiff(vals *ValidatorSet) int64 { func (vals *ValidatorSet) getValWithMostPriority() *Validator { var res *Validator for _, val := range vals.Validators { - res = res.CompareProposerPriority(val) + res = res.Cmp(val) } return res } @@ -298,7 +298,7 @@ func (vals *ValidatorSet) findProposer() *Validator { var proposer *Validator for _, val := range vals.Validators { if proposer == nil || !bytes.Equal(val.Address.Bytes(), proposer.Address.Bytes()) { - proposer = proposer.CompareProposerPriority(val) + proposer = proposer.Cmp(val) } } return proposer -- GitLab