diff --git a/consensus/common_test.go b/consensus/common_test.go index 297b842e9..d7c090aa2 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -262,6 +262,7 @@ func randConsensusNet(nValidators int) []*ConsensusState { thisConfig := tendermint_test.ResetConfig(Fmt("consensus_reactor_test_%d", i)) EnsureDir(thisConfig.GetString("cs_wal_dir"), 0700) // dir for wal css[i] = newConsensusStateWithConfig(thisConfig, state, privVals[i], counter.NewCounterApplication(true)) + css[i].SetPrivValidatorIndex(i) } return css } diff --git a/consensus/reactor.go b/consensus/reactor.go index 60d5a8f5d..418bdc62d 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -287,11 +287,6 @@ func (conR *ConsensusReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte) } } -// Sets our private validator account for signing votes. -func (conR *ConsensusReactor) SetPrivValidator(priv PrivValidator) { - conR.conS.SetPrivValidator(priv) -} - // implements events.Eventable func (conR *ConsensusReactor) SetEventSwitch(evsw types.EventSwitch) { conR.evsw = evsw diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index 6b76d2aaa..ce1b63c9b 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -41,7 +41,6 @@ func TestReactor(t *testing.T) { eventChans := make([]chan interface{}, N) for i := 0; i < N; i++ { reactors[i] = NewConsensusReactor(css[i], false) - reactors[i].SetPrivValidator(css[i].privValidator) eventSwitch := events.NewEventSwitch() _, err := eventSwitch.Start() @@ -101,10 +100,8 @@ func TestByzantine(t *testing.T) { reactors := make([]p2p.Reactor, N) eventChans := make([]chan interface{}, N) for i := 0; i < N; i++ { - var privVal PrivValidator - privVal = css[i].privValidator if i == 0 { - privVal = NewByzantinePrivValidator(privVal.(*types.PrivValidator)) + css[i].privValidator = NewByzantinePrivValidator(css[i].privValidator.(*types.PrivValidator)) // make byzantine css[i].decideProposal = func(j int) func(int, int) { return func(height, round int) { @@ -122,7 +119,6 @@ func TestByzantine(t *testing.T) { eventChans[i] = subscribeToEvent(eventSwitch, "tester", types.EventStringNewBlock(), 1) conR := NewConsensusReactor(css[i], false) - conR.SetPrivValidator(privVal) conR.SetEventSwitch(eventSwitch) var conRI p2p.Reactor diff --git a/consensus/state.go b/consensus/state.go index 85d4d1679..0442fb829 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -221,11 +221,13 @@ type PrivValidator interface { type ConsensusState struct { BaseService - config cfg.Config - proxyAppConn proxy.AppConnConsensus - blockStore *bc.BlockStore - mempool *mempl.Mempool - privValidator PrivValidator + config cfg.Config + proxyAppConn proxy.AppConnConsensus + blockStore *bc.BlockStore + mempool *mempl.Mempool + + privValidator PrivValidator + privValidatorIndex int // TODO: update if validator set changes mtx sync.Mutex RoundState @@ -313,12 +315,20 @@ func (cs *ConsensusState) GetValidators() (int, []*types.Validator) { return cs.state.LastBlockHeight, cs.state.Validators.Copy().Validators } +// Sets our private validator account for signing votes. func (cs *ConsensusState) SetPrivValidator(priv PrivValidator) { cs.mtx.Lock() defer cs.mtx.Unlock() cs.privValidator = priv } +// Caches the index of our privValidator in the validator set to use when voting +func (cs *ConsensusState) SetPrivValidatorIndex(index int) { + cs.mtx.Lock() + defer cs.mtx.Unlock() + cs.privValidatorIndex = index +} + func (cs *ConsensusState) LoadCommit(height int) *types.Commit { cs.mtx.Lock() defer cs.mtx.Unlock() @@ -1498,12 +1508,9 @@ func (cs *ConsensusState) addVote(vote *types.Vote, peerKey string) (added bool, } func (cs *ConsensusState) signVote(type_ byte, hash []byte, header types.PartSetHeader) (*types.Vote, error) { - // TODO: store our index in the cs so we don't have to do this every time - addr := cs.privValidator.GetAddress() - valIndex, _ := cs.Validators.GetByAddress(addr) vote := &types.Vote{ - ValidatorAddress: addr, - ValidatorIndex: valIndex, + ValidatorAddress: cs.privValidator.GetAddress(), + ValidatorIndex: cs.privValidatorIndex, Height: cs.Height, Round: cs.Round, Type: type_, diff --git a/node/node.go b/node/node.go index f4a04d82c..8dbf2fac9 100644 --- a/node/node.go +++ b/node/node.go @@ -101,10 +101,17 @@ func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreato // Make ConsensusReactor consensusState := consensus.NewConsensusState(config, state.Copy(), proxyApp.Consensus(), blockStore, mempool) - consensusReactor := consensus.NewConsensusReactor(consensusState, fastSync) if privValidator != nil { - consensusReactor.SetPrivValidator(privValidator) + consensusState.SetPrivValidator(privValidator) + // TODO: just return -1 for not found + valIdx, val := state.Validators.GetByAddress(privValidator.GetAddress()) + if val == nil { + consensusState.SetPrivValidatorIndex(-1) + } else { + consensusState.SetPrivValidatorIndex(valIdx) + } } + consensusReactor := consensus.NewConsensusReactor(consensusState, fastSync) // Make p2p network switch sw := p2p.NewSwitch(config.GetConfig("p2p"))