From 97e980225530133c7b7e2b45e5e65c1f78ace89b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 2 Oct 2017 20:59:46 -0400 Subject: [PATCH] fix out of range error in VoteSet.addVote --- types/validator_set.go | 5 +++++ types/vote.go | 6 +++--- types/vote_set.go | 6 ++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/types/validator_set.go b/types/validator_set.go index 3ec389a48..0e20417a7 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -99,7 +99,12 @@ func (valSet *ValidatorSet) GetByAddress(address []byte) (index int, val *Valida } } +// GetByIndex returns the validator by index. +// It returns nil values if index >= len(ValidatorSet.Validators) func (valSet *ValidatorSet) GetByIndex(index int) (address []byte, val *Validator) { + if index >= len(valSet.Validators) { + return nil, nil + } val = valSet.Validators[index] return val.Address, val.Copy() } diff --git a/types/vote.go b/types/vote.go index 164293c53..658415682 100644 --- a/types/vote.go +++ b/types/vote.go @@ -13,9 +13,9 @@ import ( var ( ErrVoteUnexpectedStep = errors.New("Unexpected step") - ErrVoteInvalidValidatorIndex = errors.New("Invalid round vote validator index") - ErrVoteInvalidValidatorAddress = errors.New("Invalid round vote validator address") - ErrVoteInvalidSignature = errors.New("Invalid round vote signature") + ErrVoteInvalidValidatorIndex = errors.New("Invalid validator index") + ErrVoteInvalidValidatorAddress = errors.New("Invalid validator address") + ErrVoteInvalidSignature = errors.New("Invalid signature") ErrVoteInvalidBlockHash = errors.New("Invalid block hash") ) diff --git a/types/vote_set.go b/types/vote_set.go index 938dbcb61..7a5d52671 100644 --- a/types/vote_set.go +++ b/types/vote_set.go @@ -140,8 +140,10 @@ func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) { blockKey := vote.BlockID.Key() // Ensure that validator index was set - if valIndex < 0 || len(valAddr) == 0 { - panic("Validator index or address was not set in vote.") + if valIndex < 0 { + return false, ErrVoteInvalidValidatorIndex + } else if len(valAddr) == 0 { + return false, ErrVoteInvalidValidatorAddress } // Make sure the step matches.