Skip to content

Commit

Permalink
Manager: Query peer heights lazily while catching up
Browse files Browse the repository at this point in the history
If we have unresponsive peers, the initial act of querying
each peer for their ledger heights could take a lot of time.
  • Loading branch information
omerfirmak authored and mkykadir committed Mar 17, 2022
1 parent 0c35bfe commit dd2491c
Showing 1 changed file with 5 additions and 19 deletions.
24 changes: 5 additions & 19 deletions source/agora/network/Manager.d
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,7 @@ public class NetworkManager
public void getBlocksFrom (Height height,
scope Height delegate(const(Block)[]) @safe onReceivedBlocks) nothrow
{
struct Pair { Height height; NetworkClient client; }

static Pair[] node_pairs;
node_pairs.length = 0;
assumeSafeAppend(node_pairs);
import std.typecons : tuple;

// return ulong.max if getBlockHeight() fails
Height getHeight (NetworkClient node)
Expand All @@ -840,25 +836,15 @@ public class NetworkManager
return Height(ulong.max);
}

foreach (node; this.peers)
{
auto pair = Pair(getHeight(node), node);
if (pair.height == ulong.max) // request failed
continue;
node_pairs ~= pair;
}

node_pairs.sort!((a, b) => a.height > b.height);

foreach (pair; node_pairs)
foreach (node, peer_height; this.peers.map!(node => tuple(node, getHeight(node))))
{
if (height > pair.height)
if (peer_height == ulong.max || height > peer_height)
continue; // this node does not have newer blocks than us

log.info("Retrieving blocks [{}..{}] from {}..",
height, pair.height, pair.client.addresses);
height, peer_height, node.addresses);
const MaxBlocks = 1024;
auto blocks = pair.client.getBlocksFrom(height, MaxBlocks);
auto blocks = node.getBlocksFrom(height, MaxBlocks);
if (blocks.length == 0)
continue;

Expand Down

0 comments on commit dd2491c

Please sign in to comment.