Skip to content

Commit

Permalink
Merge pull request bnb-chain#63 from node-real/fix_mini_bug_for_markV…
Browse files Browse the repository at this point in the history
…otes

fix mini bug for markVotes
  • Loading branch information
NathanBSC authored Mar 15, 2023
2 parents 53254e8 + 8ed8a5a commit 812674f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
5 changes: 2 additions & 3 deletions eth/handler_bsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,8 @@ func testRecvVotes(t *testing.T, protocol uint) {
},
}

if err := remoteBsc.SendVotes([]*types.VoteEnvelope{&vote}); err != nil {
t.Fatalf("failed to send vote: %v", err)
}
remoteBsc.AsyncSendVotes([]*types.VoteEnvelope{&vote})
time.Sleep(100 * time.Millisecond)
select {
case event := <-votesCh:
if event.Vote.Hash() != vote.Hash() {
Expand Down
15 changes: 7 additions & 8 deletions eth/protocols/bsc/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ func (p *Peer) KnownVote(hash common.Hash) bool {
// will never be repropagated to this particular peer.
func (p *Peer) markVotes(votes []*types.VoteEnvelope) {
for _, vote := range votes {
// If we reached the memory allowance, drop a previously known vote hash
p.knownVotes.add(vote.Hash())
if !p.knownVotes.contains(vote.Hash()) {
// If we reached the memory allowance, drop a previously known vote hash
p.knownVotes.add(vote.Hash())
}
}
}

// SendVotes propagates a batch of votes to the remote peer.
func (p *Peer) SendVotes(votes []*types.VoteEnvelope) error {
// sendVotes propagates a batch of votes to the remote peer.
func (p *Peer) sendVotes(votes []*types.VoteEnvelope) error {
// Mark all the votes as known, but ensure we don't overflow our limits
p.markVotes(votes)
return p2p.Send(p.rw, VotesMsg, &VotesPacket{votes})
Expand All @@ -102,9 +104,6 @@ func (p *Peer) SendVotes(votes []*types.VoteEnvelope) error {
func (p *Peer) AsyncSendVotes(votes []*types.VoteEnvelope) {
select {
case p.voteBroadcast <- votes:
// Mark all the votes as known, but ensure we don't overflow our limits
p.markVotes(votes)

case <-p.term:
p.Log().Debug("Dropping vote propagation", "count", len(votes))
}
Expand All @@ -117,7 +116,7 @@ func (p *Peer) broadcastVotes() {
for {
select {
case votes := <-p.voteBroadcast:
if err := p.SendVotes(votes); err != nil {
if err := p.sendVotes(votes); err != nil {
return
}
p.Log().Trace("Sent votes", "count", len(votes))
Expand Down

0 comments on commit 812674f

Please sign in to comment.