Browse Source

consensus: track index of privVal

pull/319/head
Ethan Buchman 8 years ago
parent
commit
a3d863f83b
5 changed files with 28 additions and 22 deletions
  1. +1
    -0
      consensus/common_test.go
  2. +0
    -5
      consensus/reactor.go
  3. +1
    -5
      consensus/reactor_test.go
  4. +17
    -10
      consensus/state.go
  5. +9
    -2
      node/node.go

+ 1
- 0
consensus/common_test.go View File

@ -262,6 +262,7 @@ func randConsensusNet(nValidators int) []*ConsensusState {
thisConfig := tendermint_test.ResetConfig(Fmt("consensus_reactor_test_%d", i)) thisConfig := tendermint_test.ResetConfig(Fmt("consensus_reactor_test_%d", i))
EnsureDir(thisConfig.GetString("cs_wal_dir"), 0700) // dir for wal EnsureDir(thisConfig.GetString("cs_wal_dir"), 0700) // dir for wal
css[i] = newConsensusStateWithConfig(thisConfig, state, privVals[i], counter.NewCounterApplication(true)) css[i] = newConsensusStateWithConfig(thisConfig, state, privVals[i], counter.NewCounterApplication(true))
css[i].SetPrivValidatorIndex(i)
} }
return css return css
} }


+ 0
- 5
consensus/reactor.go View File

@ -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 // implements events.Eventable
func (conR *ConsensusReactor) SetEventSwitch(evsw types.EventSwitch) { func (conR *ConsensusReactor) SetEventSwitch(evsw types.EventSwitch) {
conR.evsw = evsw conR.evsw = evsw


+ 1
- 5
consensus/reactor_test.go View File

@ -41,7 +41,6 @@ func TestReactor(t *testing.T) {
eventChans := make([]chan interface{}, N) eventChans := make([]chan interface{}, N)
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
reactors[i] = NewConsensusReactor(css[i], false) reactors[i] = NewConsensusReactor(css[i], false)
reactors[i].SetPrivValidator(css[i].privValidator)
eventSwitch := events.NewEventSwitch() eventSwitch := events.NewEventSwitch()
_, err := eventSwitch.Start() _, err := eventSwitch.Start()
@ -101,10 +100,8 @@ func TestByzantine(t *testing.T) {
reactors := make([]p2p.Reactor, N) reactors := make([]p2p.Reactor, N)
eventChans := make([]chan interface{}, N) eventChans := make([]chan interface{}, N)
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
var privVal PrivValidator
privVal = css[i].privValidator
if i == 0 { if i == 0 {
privVal = NewByzantinePrivValidator(privVal.(*types.PrivValidator))
css[i].privValidator = NewByzantinePrivValidator(css[i].privValidator.(*types.PrivValidator))
// make byzantine // make byzantine
css[i].decideProposal = func(j int) func(int, int) { css[i].decideProposal = func(j int) func(int, int) {
return func(height, round int) { return func(height, round int) {
@ -122,7 +119,6 @@ func TestByzantine(t *testing.T) {
eventChans[i] = subscribeToEvent(eventSwitch, "tester", types.EventStringNewBlock(), 1) eventChans[i] = subscribeToEvent(eventSwitch, "tester", types.EventStringNewBlock(), 1)
conR := NewConsensusReactor(css[i], false) conR := NewConsensusReactor(css[i], false)
conR.SetPrivValidator(privVal)
conR.SetEventSwitch(eventSwitch) conR.SetEventSwitch(eventSwitch)
var conRI p2p.Reactor var conRI p2p.Reactor


+ 17
- 10
consensus/state.go View File

@ -221,11 +221,13 @@ type PrivValidator interface {
type ConsensusState struct { type ConsensusState struct {
BaseService 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 mtx sync.Mutex
RoundState RoundState
@ -313,12 +315,20 @@ func (cs *ConsensusState) GetValidators() (int, []*types.Validator) {
return cs.state.LastBlockHeight, cs.state.Validators.Copy().Validators return cs.state.LastBlockHeight, cs.state.Validators.Copy().Validators
} }
// Sets our private validator account for signing votes.
func (cs *ConsensusState) SetPrivValidator(priv PrivValidator) { func (cs *ConsensusState) SetPrivValidator(priv PrivValidator) {
cs.mtx.Lock() cs.mtx.Lock()
defer cs.mtx.Unlock() defer cs.mtx.Unlock()
cs.privValidator = priv 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 { func (cs *ConsensusState) LoadCommit(height int) *types.Commit {
cs.mtx.Lock() cs.mtx.Lock()
defer cs.mtx.Unlock() 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) { 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{ vote := &types.Vote{
ValidatorAddress: addr,
ValidatorIndex: valIndex,
ValidatorAddress: cs.privValidator.GetAddress(),
ValidatorIndex: cs.privValidatorIndex,
Height: cs.Height, Height: cs.Height,
Round: cs.Round, Round: cs.Round,
Type: type_, Type: type_,


+ 9
- 2
node/node.go View File

@ -101,10 +101,17 @@ func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreato
// Make ConsensusReactor // Make ConsensusReactor
consensusState := consensus.NewConsensusState(config, state.Copy(), proxyApp.Consensus(), blockStore, mempool) consensusState := consensus.NewConsensusState(config, state.Copy(), proxyApp.Consensus(), blockStore, mempool)
consensusReactor := consensus.NewConsensusReactor(consensusState, fastSync)
if privValidator != nil { 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 // Make p2p network switch
sw := p2p.NewSwitch(config.GetConfig("p2p")) sw := p2p.NewSwitch(config.GetConfig("p2p"))


Loading…
Cancel
Save