Skip to content

Commit

Permalink
Backport cometbft#3211
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Jun 15, 2024
1 parent a644898 commit 62e372c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- `[consensus]` Make the consensus reactor no longer have packets on receive take the consensus lock.
Consensus will now update the reactor's view after every relevant change through the existing
synchronous event bus subscription.
([\#3211](https://github.com/cometbft/cometbft/pull/3211))
47 changes: 29 additions & 18 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ type Reactor struct {

conS *State

mtx cmtsync.RWMutex
waitSync bool
eventBus *types.EventBus
rs *cstypes.RoundState
mtx cmtsync.RWMutex
waitSync bool
eventBus *types.EventBus
rs *cstypes.RoundState
initialHeight int64

Metrics *Metrics
}
Expand Down Expand Up @@ -262,9 +263,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
case StateChannel:
switch msg := msg.(type) {
case *NewRoundStepMessage:
conR.conS.mtx.RLock()
initialHeight := conR.conS.state.InitialHeight
conR.conS.mtx.RUnlock()
conR.mtx.RLock()
initialHeight := conR.initialHeight
conR.mtx.RUnlock()
if err = msg.ValidateHeight(initialHeight); err != nil {
conR.Logger.Error("Peer sent us invalid msg", "peer", e.Src, "msg", msg, "err", err)
conR.Switch.StopPeerForError(e.Src, err)
Expand All @@ -276,10 +277,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
case *HasVoteMessage:
ps.ApplyHasVoteMessage(msg)
case *VoteSetMaj23Message:
cs := conR.conS
cs.mtx.RLock()
height, votes := cs.Height, cs.Votes
cs.mtx.RUnlock()
conR.mtx.RLock()
height, votes := conR.rs.Height, conR.rs.Votes
conR.mtx.RUnlock()
if height != msg.Height {
return
}
Expand Down Expand Up @@ -344,9 +344,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
switch msg := msg.(type) {
case *VoteMessage:
cs := conR.conS
cs.mtx.RLock()
height, valSize, lastCommitSize := cs.Height, cs.Validators.Size(), cs.LastCommit.Size()
cs.mtx.RUnlock()
conR.mtx.RLock()
height, valSize, lastCommitSize := conR.rs.Height, conR.rs.Validators.Size(), conR.rs.LastCommit.Size()
conR.mtx.RUnlock()
ps.SetHasVoteFromPeer(msg.Vote, height, valSize, lastCommitSize)

cs.peerMsgQueue <- msgInfo{msg, e.Src.ID()}
Expand All @@ -363,10 +363,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
}
switch msg := msg.(type) {
case *VoteSetBitsMessage:
cs := conR.conS
cs.mtx.RLock()
height, votes := cs.Height, cs.Votes
cs.mtx.RUnlock()
conR.mtx.RLock()
height, votes := conR.rs.Height, conR.rs.Votes
conR.mtx.RUnlock()

if height == msg.Height {
var ourVotes *bits.BitArray
Expand Down Expand Up @@ -414,20 +413,23 @@ func (conR *Reactor) subscribeToBroadcastEvents() {
const subscriber = "consensus-reactor"
if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventNewRoundStep,
func(data cmtevents.EventData) {
conR.updateRoundStateNoCsLock()
conR.broadcastNewRoundStepMessage(data.(*cstypes.RoundState))
}); err != nil {
conR.Logger.Error("Error adding listener for events", "err", err)
}

if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventValidBlock,
func(data cmtevents.EventData) {
conR.updateRoundStateNoCsLock()
conR.broadcastNewValidBlockMessage(data.(*cstypes.RoundState))
}); err != nil {
conR.Logger.Error("Error adding listener for events", "err", err)
}

if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventVote,
func(data cmtevents.EventData) {
conR.updateRoundStateNoCsLock()
conR.broadcastHasVoteMessage(data.(*types.Vote))
}); err != nil {
conR.Logger.Error("Error adding listener for events", "err", err)
Expand Down Expand Up @@ -536,10 +538,19 @@ func (conR *Reactor) updateRoundStateRoutine() {
rs := conR.conS.GetRoundState()
conR.mtx.Lock()
conR.rs = rs
conR.initialHeight = conR.conS.state.InitialHeight
conR.mtx.Unlock()
}
}

func (conR *Reactor) updateRoundStateNoCsLock() {
rs := conR.conS.getRoundState()
conR.mtx.Lock()
conR.rs = rs
conR.initialHeight = conR.conS.state.InitialHeight
conR.mtx.Unlock()
}

func (conR *Reactor) getRoundState() *cstypes.RoundState {
conR.mtx.RLock()
defer conR.mtx.RUnlock()
Expand Down
8 changes: 8 additions & 0 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,21 @@ func (cs *State) GetLastHeight() int64 {
}

// GetRoundState returns a shallow copy of the internal consensus state.
// This function is thread-safe.
func (cs *State) GetRoundState() *cstypes.RoundState {
cs.mtx.RLock()
rs := cs.RoundState // copy
cs.mtx.RUnlock()
return &rs
}

// getRoundState returns a shallow copy of the internal consensus state.
// This function is not thread-safe. Use GetRoundState for the thread-safe version.
func (cs *State) getRoundState() *cstypes.RoundState {
rs := cs.RoundState // copy
return &rs
}

// GetRoundStateJSON returns a json of RoundState.
func (cs *State) GetRoundStateJSON() ([]byte, error) {
cs.mtx.RLock()
Expand Down

0 comments on commit 62e372c

Please sign in to comment.