Merge pull request #2963 from sqrrm/initial-block-sync-fixes

Initial block sync fixes
This commit is contained in:
Christoph Atteneder 2019-07-15 09:35:00 +02:00 committed by GitHub
commit bea4d0635b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 11 deletions

View file

@ -239,7 +239,9 @@ public abstract class BsqNode implements DaoSetupService {
// We take only first element after sorting (so it is the block with the next height) to avoid that
// we would repeat calls in recursions in case we would iterate the list.
pendingBlocks.sort(Comparator.comparing(RawBlock::getHeight));
doParseBlock(pendingBlocks.get(0));
RawBlock nextPending = pendingBlocks.get(0);
if (nextPending.getHeight() == daoStateService.getChainHeight() + 1)
doParseBlock(nextPending);
}
return Optional.of(block);

View file

@ -131,8 +131,9 @@ public class LiteNode extends BsqNode {
liteNodeNetworkService.addListener(new LiteNodeNetworkService.Listener() {
@Override
public void onRequestedBlocksReceived(GetBlocksResponse getBlocksResponse) {
LiteNode.this.onRequestedBlocksReceived(new ArrayList<>(getBlocksResponse.getBlocks()));
public void onRequestedBlocksReceived(GetBlocksResponse getBlocksResponse, Runnable onParsingComplete) {
LiteNode.this.onRequestedBlocksReceived(new ArrayList<>(getBlocksResponse.getBlocks()),
onParsingComplete);
}
@Override
@ -175,7 +176,7 @@ public class LiteNode extends BsqNode {
///////////////////////////////////////////////////////////////////////////////////////////
// We received the missing blocks
private void onRequestedBlocksReceived(List<RawBlock> blockList) {
private void onRequestedBlocksReceived(List<RawBlock> blockList, Runnable onParsingComplete) {
if (!blockList.isEmpty()) {
chainTipHeight = blockList.get(blockList.size() - 1).getHeight();
log.info("We received blocks from height {} to {}", blockList.get(0).getHeight(), chainTipHeight);
@ -199,7 +200,11 @@ public class LiteNode extends BsqNode {
runDelayedBatchProcessing(new ArrayList<>(blockList),
() -> {
log.info("Parsing {} blocks took {} seconds.", blockList.size(), (System.currentTimeMillis() - ts) / 1000d);
onParseBlockChainComplete();
if (daoStateService.getChainHeight() < bsqWalletService.getBestChainHeight())
liteNodeNetworkService.requestBlocks(getStartBlockHeight());
else
onParsingComplete.run();
onParseBlockChainComplete();
});
}

View file

@ -76,7 +76,7 @@ public class LiteNodeNetworkService implements MessageListener, ConnectionListen
public interface Listener {
void onNoSeedNodeAvailable();
void onRequestedBlocksReceived(GetBlocksResponse getBlocksResponse);
void onRequestedBlocksReceived(GetBlocksResponse getBlocksResponse, Runnable onParsingComplete);
void onNewBlockReceived(NewBlockBroadcastMessage newBlockBroadcastMessage);
@ -269,11 +269,12 @@ public class LiteNodeNetworkService implements MessageListener, ConnectionListen
if (startBlockHeight >= lastReceivedBlockHeight) {
lastReceivedBlockHeight = startBlockHeight;
// After we received the blocks we allow to disconnect seed nodes.
// We delay 20 seconds to allow multiple requests to finish.
UserThread.runAfter(() -> peerManager.setAllowDisconnectSeedNodes(true), 20);
listeners.forEach(listener -> listener.onRequestedBlocksReceived(getBlocksResponse));
listeners.forEach(listener -> listener.onRequestedBlocksReceived(getBlocksResponse,
() -> {
// After we received the blocks we allow to disconnect seed nodes.
// We delay 20 seconds to allow multiple requests to finish.
UserThread.runAfter(() -> peerManager.setAllowDisconnectSeedNodes(true), 20);
}));
} else {
log.warn("We got a response which is already obsolete because we receive a " +
"response from a request with a higher block height. " +