PeerGroup: Don't immediately select a download peer in startBlockChainDownload(). Wait for enough peers to be connected.

This commit is contained in:
Andreas Schildbach 2020-02-27 20:21:21 +01:00
parent 98f8c42140
commit 8d983649d3
2 changed files with 6 additions and 10 deletions

View file

@ -1394,7 +1394,7 @@ public class PeerGroup implements TransactionBroadcaster {
}
/**
* <p>Start downloading the blockchain from the first available peer.</p>
* <p>Start downloading the blockchain.</p>
*
* <p>If no peers are currently connected, the download will be started once a peer starts. If the peer dies,
* the download will resume with another peer.</p>
@ -1413,12 +1413,6 @@ public class PeerGroup implements TransactionBroadcaster {
}
}
this.downloadListener = listener;
// TODO: be more nuanced about which peer to download from. We can also try
// downloading from multiple peers and handle the case when a new peer comes along
// with a longer chain after we thought we were done.
if (!peers.isEmpty()) {
startBlockChainDownloadFromPeer(peers.iterator().next()); // Will add the new download listener
}
} finally {
lock.unlock();
}
@ -1859,7 +1853,8 @@ public class PeerGroup implements TransactionBroadcaster {
}
@Nullable private ChainDownloadSpeedCalculator chainDownloadSpeedCalculator;
private void startBlockChainDownloadFromPeer(Peer peer) {
@VisibleForTesting
void startBlockChainDownloadFromPeer(Peer peer) {
lock.lock();
try {
setDownloadPeer(peer);

View file

@ -326,8 +326,8 @@ public class PeerGroupTest extends TestWithPeerGroup {
Block b3 = FakeTxBuilder.makeSolvedTestBlock(b2);
// Expect a zero hash getblocks on p1. This is how the process starts.
peerGroup.startBlockChainDownload(new AbstractPeerDataEventListener() {
});
peerGroup.startBlockChainDownload(new AbstractPeerDataEventListener() {});
peerGroup.startBlockChainDownloadFromPeer(peerGroup.getConnectedPeers().iterator().next());
GetBlocksMessage getblocks = (GetBlocksMessage) outbound(p1);
assertEquals(Sha256Hash.ZERO_HASH, getblocks.getStopHash());
// We give back an inv with some blocks in it.
@ -804,6 +804,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
assertTrue(p1.lastReceivedFilter.contains(keys.get(5).getPubKeyHash()));
assertFalse(p1.lastReceivedFilter.contains(keys.get(keys.size() - 1).getPubKey()));
peerGroup.startBlockChainDownload(null);
peerGroup.startBlockChainDownloadFromPeer(peerGroup.getConnectedPeers().iterator().next());
assertNextMessageIs(p1, GetBlocksMessage.class);
// Make some transactions and blocks that send money to the wallet thus using up all the keys.