Skip to content

Commit

Permalink
fix: sync peer ordering (#6802)
Browse files Browse the repository at this point in the history
Description
---
Fixes peer sync order. 
Currently, the syncing peers are ordered by latency. This is important,
but we also need to sync to the highest node tip.
This changes the order for the sync peers to first order by higher, then
by latency.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Enhanced synchronization by updating peer prioritization to weigh
difficulty claims before latency.
- Introduced a new interface to retrieve each peer's claimed difficulty.

- **Tests**
- Revised the expected outcome for median timestamp calculations to
ensure consistency and accuracy.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
SWvheerden authored Feb 18, 2025
1 parent 26a8fec commit e25ac10
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ pub struct HeaderSyncState {
impl HeaderSyncState {
pub fn new(mut sync_peers: Vec<SyncPeer>, local_metadata: ChainMetadata) -> Self {
// Sort by latency lowest to highest
sync_peers.sort_by(|a, b| match (a.latency(), b.latency()) {
(None, None) => Ordering::Equal,
sync_peers.sort_by(|a, b| match a.claimed_difficulty().cmp(&b.claimed_difficulty()) {
Ordering::Less => Ordering::Less,
// No latency goes to the end
(Some(_), None) => Ordering::Less,
(None, Some(_)) => Ordering::Greater,
(Some(la), Some(lb)) => la.cmp(&lb),
Ordering::Greater => Ordering::Greater,
Ordering::Equal => {
match (a.latency(), b.latency()) {
(None, None) => Ordering::Equal,
// No latency goes to the end
(Some(_), None) => Ordering::Less,
(None, Some(_)) => Ordering::Greater,
(Some(la), Some(lb)) => la.cmp(&lb),
}
},
});
Self {
sync_peers,
Expand Down
5 changes: 5 additions & 0 deletions base_layer/core/src/base_node/sync/sync_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::{
time::Duration,
};

use primitive_types::U256;
use tari_common_types::chain_metadata::ChainMetadata;
use tari_comms::peer_manager::NodeId;

Expand All @@ -46,6 +47,10 @@ impl SyncPeer {
self.peer_metadata.claimed_chain_metadata()
}

pub fn claimed_difficulty(&self) -> U256 {
self.peer_metadata.claimed_chain_metadata().accumulated_difficulty()
}

pub fn latency(&self) -> Option<Duration> {
self.peer_metadata.latency()
}
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/validation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ mod test {
assert_eq!(median_timestamp, 3.into());

let median_timestamp = calc_median_timestamp(&[0.into(), 100.into(), 0.into()]).unwrap();
assert_eq!(median_timestamp, 100.into());
assert_eq!(median_timestamp, 0.into());

let median_timestamp = calc_median_timestamp(&[1.into(), 2.into(), 3.into(), 4.into()]).unwrap();
assert_eq!(median_timestamp, 2.into());
Expand Down

0 comments on commit e25ac10

Please sign in to comment.