make conR.FastSync() thread safe
This commit is contained in:
parent
e560dd839f
commit
92ada55e5a
|
@ -29,9 +29,11 @@ const (
|
||||||
type ConsensusReactor struct {
|
type ConsensusReactor struct {
|
||||||
p2p.BaseReactor // BaseService + p2p.Switch
|
p2p.BaseReactor // BaseService + p2p.Switch
|
||||||
|
|
||||||
conS *ConsensusState
|
conS *ConsensusState
|
||||||
|
evsw types.EventSwitch
|
||||||
|
|
||||||
|
mtx sync.RWMutex
|
||||||
fastSync bool
|
fastSync bool
|
||||||
evsw types.EventSwitch
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool) *ConsensusReactor {
|
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool) *ConsensusReactor {
|
||||||
|
@ -44,14 +46,14 @@ func NewConsensusReactor(consensusState *ConsensusState, fastSync bool) *Consens
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conR *ConsensusReactor) OnStart() error {
|
func (conR *ConsensusReactor) OnStart() error {
|
||||||
conR.Logger.Info("ConsensusReactor ", "fastSync", conR.fastSync)
|
conR.Logger.Info("ConsensusReactor ", "fastSync", conR.FastSync())
|
||||||
conR.BaseReactor.OnStart()
|
conR.BaseReactor.OnStart()
|
||||||
|
|
||||||
// callbacks for broadcasting new steps and votes to peers
|
// callbacks for broadcasting new steps and votes to peers
|
||||||
// upon their respective events (ie. uses evsw)
|
// upon their respective events (ie. uses evsw)
|
||||||
conR.registerEventCallbacks()
|
conR.registerEventCallbacks()
|
||||||
|
|
||||||
if !conR.fastSync {
|
if !conR.FastSync() {
|
||||||
_, err := conR.conS.Start()
|
_, err := conR.conS.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -73,7 +75,11 @@ func (conR *ConsensusReactor) SwitchToConsensus(state *sm.State) {
|
||||||
// NOTE: The line below causes broadcastNewRoundStepRoutine() to
|
// NOTE: The line below causes broadcastNewRoundStepRoutine() to
|
||||||
// broadcast a NewRoundStepMessage.
|
// broadcast a NewRoundStepMessage.
|
||||||
conR.conS.updateToState(state)
|
conR.conS.updateToState(state)
|
||||||
|
|
||||||
|
conR.mtx.Lock()
|
||||||
conR.fastSync = false
|
conR.fastSync = false
|
||||||
|
conR.mtx.Unlock()
|
||||||
|
|
||||||
conR.conS.Start()
|
conR.conS.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +130,7 @@ func (conR *ConsensusReactor) AddPeer(peer *p2p.Peer) {
|
||||||
|
|
||||||
// Send our state to peer.
|
// Send our state to peer.
|
||||||
// If we're fast_syncing, broadcast a RoundStepMessage later upon SwitchToConsensus().
|
// If we're fast_syncing, broadcast a RoundStepMessage later upon SwitchToConsensus().
|
||||||
if !conR.fastSync {
|
if !conR.FastSync() {
|
||||||
conR.sendNewRoundStepMessages(peer)
|
conR.sendNewRoundStepMessages(peer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -204,7 +210,7 @@ func (conR *ConsensusReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
case DataChannel:
|
case DataChannel:
|
||||||
if conR.fastSync {
|
if conR.FastSync() {
|
||||||
conR.Logger.Info("Ignoring message received during fastSync", "msg", msg)
|
conR.Logger.Info("Ignoring message received during fastSync", "msg", msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -222,7 +228,7 @@ func (conR *ConsensusReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
case VoteChannel:
|
case VoteChannel:
|
||||||
if conR.fastSync {
|
if conR.FastSync() {
|
||||||
conR.Logger.Info("Ignoring message received during fastSync", "msg", msg)
|
conR.Logger.Info("Ignoring message received during fastSync", "msg", msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -244,7 +250,7 @@ func (conR *ConsensusReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte)
|
||||||
}
|
}
|
||||||
|
|
||||||
case VoteSetBitsChannel:
|
case VoteSetBitsChannel:
|
||||||
if conR.fastSync {
|
if conR.FastSync() {
|
||||||
conR.Logger.Info("Ignoring message received during fastSync", "msg", msg)
|
conR.Logger.Info("Ignoring message received during fastSync", "msg", msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -292,6 +298,8 @@ func (conR *ConsensusReactor) SetEventSwitch(evsw types.EventSwitch) {
|
||||||
|
|
||||||
// FastSync returns whether the consensus reactor is currently fast syncing
|
// FastSync returns whether the consensus reactor is currently fast syncing
|
||||||
func (conR *ConsensusReactor) FastSync() bool {
|
func (conR *ConsensusReactor) FastSync() bool {
|
||||||
|
conR.mtx.RLock()
|
||||||
|
defer conR.mtx.RUnlock()
|
||||||
return conR.fastSync
|
return conR.fastSync
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue