-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use peermanager scores for blocksync peers and don't error out on block mismatch #162
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5786063
Use peermanager scores for blocksync peers
philipsu522 1ac69bc
Add debug
philipsu522 fe9f6e0
Randomize
philipsu522 08f8163
debug
philipsu522 2f4517f
use state to filter
philipsu522 bfc637c
debug
philipsu522 533f759
debug
philipsu522 8fa8638
debug
philipsu522 1137ac4
debug
philipsu522 2bc2fbb
add comments
philipsu522 3610170
Merge branch 'main' into block-sync-prio-peers
yzang2019 a1bd259
don't err
philipsu522 a83139b
revert timeout
philipsu522 ae4b8bc
Add missing param
philipsu522 e398b04
Merge branch 'main' into block-sync-prio-peers
philipsu522 3f06493
Remove flaky test
philipsu522 a70ffd2
fix nil
philipsu522 e47270c
debug
philipsu522 36d31f9
debug
philipsu522 ecf6e7f
debug
philipsu522 a149386
debug
philipsu522 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/tendermint/tendermint/internal/p2p" | ||
"math" | ||
"math/rand" | ||
"sort" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
@@ -80,6 +83,7 @@ type BlockPool struct { | |
height int64 // the lowest key in requesters. | ||
// peers | ||
peers map[types.NodeID]*bpPeer | ||
peerManager *p2p.PeerManager | ||
maxPeerHeight int64 // the biggest reported height | ||
|
||
// atomic | ||
|
@@ -101,8 +105,8 @@ func NewBlockPool( | |
start int64, | ||
requestsCh chan<- BlockRequest, | ||
errorsCh chan<- peerError, | ||
peerManager *p2p.PeerManager, | ||
) *BlockPool { | ||
|
||
bp := &BlockPool{ | ||
logger: logger, | ||
peers: make(map[types.NodeID]*bpPeer), | ||
|
@@ -113,6 +117,7 @@ func NewBlockPool( | |
requestsCh: requestsCh, | ||
errorsCh: errorsCh, | ||
lastSyncRate: 0, | ||
peerManager: peerManager, | ||
} | ||
bp.BaseService = *service.NewBaseService(logger, "BlockPool", bp) | ||
return bp | ||
|
@@ -408,13 +413,44 @@ func (pool *BlockPool) updateMaxPeerHeight() { | |
pool.maxPeerHeight = max | ||
} | ||
|
||
func (pool *BlockPool) getSortedPeers(peers map[types.NodeID]*bpPeer) []types.NodeID { | ||
// Generate a sorted list | ||
sortedPeers := make([]types.NodeID, 0, len(peers)) | ||
|
||
for peer := range peers { | ||
sortedPeers = append(sortedPeers, peer) | ||
} | ||
// Sort from high to low score | ||
sort.Slice(sortedPeers, func(i, j int) bool { | ||
return pool.peerManager.Score(sortedPeers[i]) > pool.peerManager.Score(sortedPeers[j]) | ||
}) | ||
return sortedPeers | ||
} | ||
|
||
// Pick an available peer with the given height available. | ||
// If no peers are available, returns nil. | ||
func (pool *BlockPool) pickIncrAvailablePeer(height int64) *bpPeer { | ||
pool.mtx.Lock() | ||
defer pool.mtx.Unlock() | ||
|
||
for _, peer := range pool.peers { | ||
// Generate a sorted list | ||
sortedPeers := pool.getSortedPeers(pool.peers) | ||
var goodPeers []types.NodeID | ||
// Remove peers with 0 score and shuffle list | ||
for _, peer := range sortedPeers { | ||
// We only want to work with peers that are ready & connected (not dialing) | ||
if pool.peerManager.State(peer) == "ready,connected" { | ||
goodPeers = append(goodPeers, peer) | ||
} | ||
if pool.peerManager.Score(peer) == 0 { | ||
break | ||
} | ||
} | ||
rand.Seed(time.Now().UnixNano()) | ||
rand.Shuffle(len(goodPeers), func(i, j int) { goodPeers[i], goodPeers[j] = goodPeers[j], goodPeers[i] }) | ||
|
||
for _, nodeId := range sortedPeers { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few further optimizations we can do:
|
||
peer := pool.peers[nodeId] | ||
if peer.didTimeout { | ||
pool.removePeer(peer.id) | ||
continue | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: fix lint issue