Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial block sync fixes #2963

Merged
merged 2 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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