From 37ce6b195a2c26748bb7cc61466116748a589da0 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 29 Mar 2018 13:32:14 +0200 Subject: [PATCH] ValidatorSet#GetByAddress: return -1 if no validator was found --- consensus/state.go | 6 +----- types/validator_set.go | 5 ++++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 6acad698b..57c7b32f0 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -720,11 +720,7 @@ func (cs *ConsensusState) needProofBlock(height int64) bool { func (cs *ConsensusState) proposalHeartbeat(height int64, round int) { counter := 0 addr := cs.privValidator.GetAddress() - valIndex, v := cs.Validators.GetByAddress(addr) - if v == nil { - // not a validator - valIndex = -1 - } + valIndex, _ := cs.Validators.GetByAddress(addr) chainID := cs.state.ChainID for { rs := cs.GetRoundState() diff --git a/types/validator_set.go b/types/validator_set.go index dc6b66d68..ce32772da 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -90,14 +90,17 @@ func (valSet *ValidatorSet) HasAddress(address []byte) bool { return idx != len(valSet.Validators) && bytes.Equal(valSet.Validators[idx].Address, address) } +// GetByAddress returns an index of the validator with address and validator +// itself if found. Otherwise, -1 and nil are returned. func (valSet *ValidatorSet) GetByAddress(address []byte) (index int, val *Validator) { idx := sort.Search(len(valSet.Validators), func(i int) bool { return bytes.Compare(address, valSet.Validators[i].Address) <= 0 }) if idx != len(valSet.Validators) && bytes.Equal(valSet.Validators[idx].Address, address) { return idx, valSet.Validators[idx].Copy() + } else { + return -1, nil } - return 0, nil } // GetByIndex returns the validator by index.