Skip to content

Commit

Permalink
perf(consensus): Make one peer state mutex call in AddVote (cometbft#…
Browse files Browse the repository at this point in the history
…3156)

Closes cometbft#3155

---

- [ ] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
  • Loading branch information
ValarDragon committed Jun 3, 2024
1 parent 2d12e6f commit 3cdc621
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [`consensus`] Make Vote messages only take one peerstate mutex
([\#3156](https://github.com/cometbft/cometbft/issues/3156)
14 changes: 11 additions & 3 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,7 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) {
cs.mtx.RLock()
height, valSize, lastCommitSize := cs.Height, cs.Validators.Size(), cs.LastCommit.Size()
cs.mtx.RUnlock()
ps.EnsureVoteBitArrays(height, valSize)
ps.EnsureVoteBitArrays(height-1, lastCommitSize)
ps.SetHasVote(msg.Vote)
ps.SetHasVoteFromPeer(msg.Vote, height, valSize, lastCommitSize)

cs.peerMsgQueue <- msgInfo{msg, e.Src.ID()}

Expand Down Expand Up @@ -1336,6 +1334,16 @@ func (ps *PeerState) SetHasVote(vote *types.Vote) {
ps.setHasVote(vote.Height, vote.Round, vote.Type, vote.ValidatorIndex)
}

// SetHasVote sets the given vote as known by the peer.
func (ps *PeerState) SetHasVoteFromPeer(vote *types.Vote, csHeight int64, valSize, lastCommitSize int) {
ps.mtx.Lock()
defer ps.mtx.Unlock()

ps.ensureVoteBitArrays(csHeight, valSize)
ps.ensureVoteBitArrays(csHeight-1, lastCommitSize)
ps.setHasVote(vote.Height, vote.Round, vote.Type, vote.ValidatorIndex)
}

func (ps *PeerState) setHasVote(height int64, round int32, voteType cmtproto.SignedMsgType, index int32) {
// ps.logger.Debug("setHasVote",
// "peerH/R",
Expand Down

0 comments on commit 3cdc621

Please sign in to comment.