-
Notifications
You must be signed in to change notification settings - Fork 149
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
fix(p2p): validate chain exchange responses for block headers #4454
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
21cba11
fix(p2p): validate chain exchange responses for block headers
hanabi1224 baa83c7
NonZeroU64
hanabi1224 4c05938
named fn and doc
hanabi1224 24524db
Merge remote-tracking branch 'origin/main' into hm/validate-result--c…
hanabi1224 77380cf
unit test
hanabi1224 0acd8e2
refine stateless peer filtering logic
hanabi1224 71c190e
fix stateless node CI test
hanabi1224 453834b
Merge branch 'main' into hm/validate-result--chain_exchange_headers
hanabi1224 1bd1f1f
Merge branch 'main' into hm/validate-result--chain_exchange_headers
hanabi1224 c9ed885
Merge branch 'main' into hm/validate-result--chain_exchange_headers
hanabi1224 cf9a82e
Merge remote-tracking branch 'origin/main' into hm/validate-result--c…
hanabi1224 ac2f507
Merge branch 'main' into hm/validate-result--chain_exchange_headers
hanabi1224 778edb5
Merge remote-tracking branch 'origin/main' into hm/validate-result--c…
hanabi1224 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,30 +124,49 @@ impl PeerManager { | |
pub(in crate::libp2p) fn sorted_peers(&self) -> Vec<PeerId> { | ||
let peer_lk = self.peers.read(); | ||
let average_time = self.avg_global_time.read(); | ||
let mut n_stateful = 0; | ||
let mut peers: Vec<_> = peer_lk | ||
.full_peers | ||
.iter() | ||
.filter_map(|(p, info)| { | ||
// Filter out nodes that are stateless (or far behind) | ||
if info.head.epoch() > 0 { | ||
let cost = if info.successes > 0 { | ||
// Calculate cost based on fail rate and latency | ||
let fail_rate = f64::from(info.failures) / f64::from(info.successes); | ||
info.average_time.as_secs_f64() + fail_rate * average_time.as_secs_f64() | ||
} else { | ||
// There have been no failures or successes | ||
average_time.as_secs_f64() * NEW_PEER_MUL | ||
}; | ||
Some((p, cost)) | ||
} else { | ||
None | ||
.map(|(&p, info)| { | ||
let is_stateful = info.head.epoch() > 0; | ||
if is_stateful { | ||
n_stateful += 1; | ||
} | ||
let cost = if info.successes + info.failures > 0 { | ||
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. wibble: this was changed to fn main() {
let x = 100.0 + f64::from(1) / f64::from(0);
println!("{x}");
}
// Output: inf |
||
// Calculate cost based on fail rate and latency | ||
// Note that when `success` is zero, the result is `inf` | ||
let fail_rate = f64::from(info.failures) / f64::from(info.successes); | ||
info.average_time.as_secs_f64() + fail_rate * average_time.as_secs_f64() | ||
} else { | ||
// There have been no failures or successes | ||
average_time.as_secs_f64() * NEW_PEER_MUL | ||
}; | ||
(p, is_stateful, cost) | ||
}) | ||
.collect(); | ||
|
||
// Unstable sort because hashmap iter order doesn't need to be preserved. | ||
peers.sort_unstable_by(|(_, v1), (_, v2)| v1.partial_cmp(v2).unwrap_or(Ordering::Equal)); | ||
peers.into_iter().map(|(&peer, _)| peer).collect() | ||
peers.sort_unstable_by(|(_, _, v1), (_, _, v2)| { | ||
v1.partial_cmp(v2).unwrap_or(Ordering::Equal) | ||
}); | ||
// Filter out nodes that are stateless when `n_stateful > 0` | ||
if n_stateful > 0 { | ||
peers | ||
.into_iter() | ||
.filter_map( | ||
|(peer, is_stateful, _)| { | ||
if is_stateful { | ||
Some(peer) | ||
} else { | ||
None | ||
} | ||
}, | ||
) | ||
.collect() | ||
} else { | ||
peers.into_iter().map(|(peer, _, _)| peer).collect() | ||
} | ||
} | ||
|
||
/// Return shuffled slice of ordered peers from the peer manager. Ordering | ||
|
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.
What variables are unused?
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.
genesis_header
,first_header
etc.