Skip to content

Commit

Permalink
Updated Peers discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
bayk committed Dec 7, 2024
1 parent 5999748 commit fd25d7b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
63 changes: 59 additions & 4 deletions chain/src/txhashset/headers_desegmenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ impl<T> HeadersRecieveCache<T> {
.insert(first_header.height, (bhs, peer_info));

// Apply data from cache if possible
let mut headers_all: Vec<BlockHeader> = Vec::new();
let mut headers_by_peer: Vec<(Vec<BlockHeader>, T)> = Vec::new();
let tip = self
.chain
.header_head()
Expand All @@ -399,13 +401,66 @@ impl<T> HeadersRecieveCache<T> {
if *height > tip_height + 1 {
break;
}
let (_, (bhs, peer)) = self.main_headers_cache.pop_first().unwrap();
let (_, (mut bhs, peer)) = self.main_headers_cache.pop_first().unwrap();
tip_height = bhs.last().expect("bhs can't be empty").height;

// Adding headers into the blockchian. Adding by 512 is optimal, DB not design to add large number of headers
match self.chain.sync_block_headers(&bhs, tip, Options::NONE) {
headers_by_peer.push((bhs.clone(), peer));
headers_all.append(&mut bhs);

if headers_all.len() > 5000 {
match self
.chain
.sync_block_headers(&headers_all, tip, Options::NONE)
{
Ok(_) => {}
Err(e) => {
warn!(
"add_headers in bulk is failed, will add one by one. Error: {}",
e
);
// apply one by one
for (hdr, peer) in headers_by_peer {
let tip = self
.chain
.header_head()
.expect("Header head must be always defined");

match self.chain.sync_block_headers(&hdr, tip, Options::NONE) {
Ok(_) => {}
Err(e) => return Err((peer, e)),
}
}
}
}
headers_all = Vec::new();
headers_by_peer = Vec::new();
}
}

if !headers_all.is_empty() {
match self
.chain
.sync_block_headers(&headers_all, tip, Options::NONE)
{
Ok(_) => {}
Err(e) => return Err((peer, e)),
Err(e) => {
warn!(
"add_headers in bulk is failed, will add one by one. Error: {}",
e
);
// apply one by one
for (hdr, peer) in headers_by_peer {
let tip = self
.chain
.header_head()
.expect("Header head must be always defined");

match self.chain.sync_block_headers(&hdr, tip, Options::NONE) {
Ok(_) => {}
Err(e) => return Err((peer, e)),
}
}
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion p2p/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,14 @@ impl PeerStore {
cap: Capabilities,
count: usize,
) -> Result<Vec<PeerData>, Error> {
// All new peers has flags Capabilities::UNKNOWN, that is why we better to return themn as well.
// Node will try to connect to them and find the capability.
let mut peers = self
.peers_iter()?
.filter(|p| p.flags == state && p.capabilities.contains(cap))
.filter(|p| {
p.flags == state
&& (p.capabilities == Capabilities::UNKNOWN || p.capabilities.contains(cap))
})
.collect::<Vec<_>>();
peers[..].shuffle(&mut thread_rng());
Ok(peers.iter().take(count).cloned().collect())
Expand Down

0 comments on commit fd25d7b

Please sign in to comment.