|
@ -33,8 +33,7 @@ const ( |
|
|
// We atomically copy the RoundState struct before using it.
|
|
|
// We atomically copy the RoundState struct before using it.
|
|
|
type ConsensusReactor struct { |
|
|
type ConsensusReactor struct { |
|
|
sw *p2p.Switch |
|
|
sw *p2p.Switch |
|
|
started uint32 |
|
|
|
|
|
stopped uint32 |
|
|
|
|
|
|
|
|
running uint32 |
|
|
quit chan struct{} |
|
|
quit chan struct{} |
|
|
|
|
|
|
|
|
blockStore *bc.BlockStore |
|
|
blockStore *bc.BlockStore |
|
@ -52,7 +51,7 @@ func NewConsensusReactor(consensusState *ConsensusState, blockStore *bc.BlockSto |
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
// Implements Reactor
|
|
|
func (conR *ConsensusReactor) Start(sw *p2p.Switch) { |
|
|
func (conR *ConsensusReactor) Start(sw *p2p.Switch) { |
|
|
if atomic.CompareAndSwapUint32(&conR.started, 0, 1) { |
|
|
|
|
|
|
|
|
if atomic.CompareAndSwapUint32(&conR.running, 0, 1) { |
|
|
log.Info("Starting ConsensusReactor") |
|
|
log.Info("Starting ConsensusReactor") |
|
|
conR.sw = sw |
|
|
conR.sw = sw |
|
|
conR.conS.Start() |
|
|
conR.conS.Start() |
|
@ -62,15 +61,15 @@ func (conR *ConsensusReactor) Start(sw *p2p.Switch) { |
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
// Implements Reactor
|
|
|
func (conR *ConsensusReactor) Stop() { |
|
|
func (conR *ConsensusReactor) Stop() { |
|
|
if atomic.CompareAndSwapUint32(&conR.stopped, 0, 1) { |
|
|
|
|
|
|
|
|
if atomic.CompareAndSwapUint32(&conR.running, 1, 0) { |
|
|
log.Info("Stopping ConsensusReactor") |
|
|
log.Info("Stopping ConsensusReactor") |
|
|
conR.conS.Stop() |
|
|
conR.conS.Stop() |
|
|
close(conR.quit) |
|
|
close(conR.quit) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (conR *ConsensusReactor) IsStopped() bool { |
|
|
|
|
|
return atomic.LoadUint32(&conR.stopped) == 1 |
|
|
|
|
|
|
|
|
func (conR *ConsensusReactor) IsRunning() bool { |
|
|
|
|
|
return atomic.LoadUint32(&conR.running) == 0 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
// Implements Reactor
|
|
@ -94,6 +93,10 @@ func (conR *ConsensusReactor) GetChannels() []*p2p.ChannelDescriptor { |
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
// Implements Reactor
|
|
|
func (conR *ConsensusReactor) AddPeer(peer *p2p.Peer) { |
|
|
func (conR *ConsensusReactor) AddPeer(peer *p2p.Peer) { |
|
|
|
|
|
if !conR.IsRunning() { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Create peerState for peer
|
|
|
// Create peerState for peer
|
|
|
peerState := NewPeerState(peer) |
|
|
peerState := NewPeerState(peer) |
|
|
peer.Data.Set(peerStateKey, peerState) |
|
|
peer.Data.Set(peerStateKey, peerState) |
|
@ -108,11 +111,18 @@ func (conR *ConsensusReactor) AddPeer(peer *p2p.Peer) { |
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
// Implements Reactor
|
|
|
func (conR *ConsensusReactor) RemovePeer(peer *p2p.Peer, reason interface{}) { |
|
|
func (conR *ConsensusReactor) RemovePeer(peer *p2p.Peer, reason interface{}) { |
|
|
|
|
|
if !conR.IsRunning() { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//peer.Data.Get(peerStateKey).(*PeerState).Disconnect()
|
|
|
//peer.Data.Get(peerStateKey).(*PeerState).Disconnect()
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
// Implements Reactor
|
|
|
func (conR *ConsensusReactor) Receive(chId byte, peer *p2p.Peer, msgBytes []byte) { |
|
|
func (conR *ConsensusReactor) Receive(chId byte, peer *p2p.Peer, msgBytes []byte) { |
|
|
|
|
|
if !conR.IsRunning() { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Get round state
|
|
|
// Get round state
|
|
|
rs := conR.conS.GetRoundState() |
|
|
rs := conR.conS.GetRoundState() |
|
@ -215,6 +225,10 @@ func (conR *ConsensusReactor) SetPrivValidator(priv *sm.PrivValidator) { |
|
|
conR.conS.SetPrivValidator(priv) |
|
|
conR.conS.SetPrivValidator(priv) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (conR *ConsensusReactor) UpdateToState(state *sm.State) { |
|
|
|
|
|
conR.conS.updateToState(state, false) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//--------------------------------------
|
|
|
//--------------------------------------
|
|
|
|
|
|
|
|
|
func makeRoundStepMessages(rs *RoundState) (nrsMsg *NewRoundStepMessage, csMsg *CommitStepMessage) { |
|
|
func makeRoundStepMessages(rs *RoundState) (nrsMsg *NewRoundStepMessage, csMsg *CommitStepMessage) { |
|
@ -279,7 +293,7 @@ func (conR *ConsensusReactor) gossipDataRoutine(peer *p2p.Peer, ps *PeerState) { |
|
|
OUTER_LOOP: |
|
|
OUTER_LOOP: |
|
|
for { |
|
|
for { |
|
|
// Manage disconnects from self or peer.
|
|
|
// Manage disconnects from self or peer.
|
|
|
if peer.IsStopped() || conR.IsStopped() { |
|
|
|
|
|
|
|
|
if !peer.IsRunning() || !conR.IsRunning() { |
|
|
log.Info(Fmt("Stopping gossipDataRoutine for %v.", peer)) |
|
|
log.Info(Fmt("Stopping gossipDataRoutine for %v.", peer)) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
@ -382,7 +396,7 @@ func (conR *ConsensusReactor) gossipVotesRoutine(peer *p2p.Peer, ps *PeerState) |
|
|
OUTER_LOOP: |
|
|
OUTER_LOOP: |
|
|
for { |
|
|
for { |
|
|
// Manage disconnects from self or peer.
|
|
|
// Manage disconnects from self or peer.
|
|
|
if peer.IsStopped() || conR.IsStopped() { |
|
|
|
|
|
|
|
|
if !peer.IsRunning() || !conR.IsRunning() { |
|
|
log.Info(Fmt("Stopping gossipVotesRoutine for %v.", peer)) |
|
|
log.Info(Fmt("Stopping gossipVotesRoutine for %v.", peer)) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|