Skip to content

Commit

Permalink
eth: select sync peer randomly when td equal
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Sep 13, 2024
1 parent 10b714a commit 8ec6ca8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions eth/peerset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sync"
"time"

"github.com/cometbft/cometbft/libs/rand"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/eth/protocols/bsc"
Expand Down Expand Up @@ -539,22 +540,26 @@ func (ps *peerSet) peerWithHighestHead() *eth.Peer {
bestPeer *eth.Peer
bestJustified *uint64
bestTd *big.Int
randUint = rand.Uint()
)
for _, p := range knowJustifiedPeers {
_, justifiedNumber, td := p.Head()
if bestPeer == nil {
bestPeer, bestJustified, bestTd = p.Peer, justifiedNumber, td
} else if *justifiedNumber > *bestJustified ||
(*justifiedNumber == *bestJustified && td.Cmp(bestTd) > 0) {
} else if *justifiedNumber > *bestJustified {
bestPeer, bestJustified = p.Peer, justifiedNumber
if td.Cmp(bestTd) > 0 {
bestTd = td // may be not equal `to bestPeer.td`
}
} else if *justifiedNumber == *bestJustified {
if td.Cmp(bestTd) > 0 || (td.Cmp(bestTd) == 0 && randUint%2 == 0) {
bestPeer, bestTd = p.Peer, td
}
}
}
// if some nodes does not have justified number, back to behavior without fast finality
for _, p := range notKnowJustifiedPeers {
if _, _, td := p.Head(); bestPeer == nil || td.Cmp(bestTd) > 0 {
if _, _, td := p.Head(); bestPeer == nil || td.Cmp(bestTd) > 0 || (td.Cmp(bestTd) == 0 && randUint%2 == 0) {
bestPeer, bestTd = p.Peer, td
}
}
Expand Down

0 comments on commit 8ec6ca8

Please sign in to comment.