Skip to content

Commit

Permalink
Merge pull request #2963 from sqrrm/initial-block-sync-fixes
Browse files Browse the repository at this point in the history
Initial block sync fixes
  • Loading branch information
ripcurlx authored Jul 15, 2019
2 parents 9d2740e + a8bb832 commit bea4d06
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/bisq/core/dao/node/BsqNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ protected Optional<Block> doParseBlock(RawBlock rawBlock) throws RequiredReorgFr
// 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);
Expand Down
13 changes: 9 additions & 4 deletions core/src/main/java/bisq/core/dao/node/lite/LiteNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ protected void onP2PNetworkReady() {

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
Expand Down Expand Up @@ -175,7 +176,7 @@ protected void startReOrgFromLastSnapshot() {
///////////////////////////////////////////////////////////////////////////////////////////

// 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);
Expand All @@ -199,7 +200,11 @@ private void onRequestedBlocksReceived(List<RawBlock> blockList) {
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();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -269,11 +269,12 @@ public void onComplete(GetBlocksResponse getBlocksResponse) {
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. " +
Expand Down

0 comments on commit bea4d06

Please sign in to comment.