diff --git a/consensus/state.go b/consensus/state.go index 4a70f5b92..73192944c 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -588,12 +588,14 @@ func (cs *ConsensusState) updateToState(state *sm.State) { } func (cs *ConsensusState) setPrivValidatorIndex() { - // TODO: just return -1 for not found - valIdx, val := cs.state.Validators.GetByAddress(cs.privValidator.GetAddress()) - if val == nil { - cs.privValidatorIndex = -1 - } else { - cs.privValidatorIndex = valIdx + if cs.privValidator != nil { + // TODO: just return -1 for not found + valIdx, val := cs.state.Validators.GetByAddress(cs.privValidator.GetAddress()) + if val == nil { + cs.privValidatorIndex = -1 + } else { + cs.privValidatorIndex = valIdx + } } } diff --git a/types/validator.go b/types/validator.go index ae297112b..479824e62 100644 --- a/types/validator.go +++ b/types/validator.go @@ -102,11 +102,6 @@ func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidator) if randPower { votePower += int64(RandUint32()) } - val := &Validator{ - Address: privVal.Address, - PubKey: privVal.PubKey, - VotingPower: votePower, - Accum: 0, - } + val := NewValidator(privVal.PubKey, votePower) return val, privVal } diff --git a/types/validator_set_test.go b/types/validator_set_test.go index a94a4ebbf..9107e77e5 100644 --- a/types/validator_set_test.go +++ b/types/validator_set_test.go @@ -16,12 +16,9 @@ func randPubKey() crypto.PubKeyEd25519 { } func randValidator_() *Validator { - return &Validator{ - Address: RandBytes(20), - PubKey: randPubKey(), - VotingPower: RandInt64(), - Accum: RandInt64(), - } + val := NewValidator(randPubKey(), RandInt64()) + val.Accum = RandInt64() + return val } func randValidatorSet(numValidators int) *ValidatorSet { @@ -147,10 +144,7 @@ func BenchmarkValidatorSetCopy(b *testing.B) { for i := 0; i < 1000; i++ { privKey := crypto.GenPrivKeyEd25519() pubKey := privKey.PubKey().(crypto.PubKeyEd25519) - val := &Validator{ - Address: pubKey.Address(), - PubKey: pubKey, - } + val := NewValidator(pubKey, 0) if !vset.Add(val) { panic("Failed to add validator") }