Skip to content

Commit

Permalink
Merge pull request #6505 from HenrikJannsen/use_thread_for_processing…
Browse files Browse the repository at this point in the history
…_BM_AccountingBlocks

Run processAccountingBlocks async in forkjoinpool thread
  • Loading branch information
alejandrogarcia83 authored Jan 6, 2023
2 parents dda32c0 + ea285e6 commit be1691b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ public static Sha256Hash getSha256Hash(AccountingBlock block) {

@Nullable
public static Sha256Hash getSha256Hash(Collection<AccountingBlock> blocks) {
long ts = System.currentTimeMillis();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
for (AccountingBlock accountingBlock : blocks) {
outputStream.write(accountingBlock.toProtoMessage().toByteArray());
}
return Sha256Hash.of(outputStream.toByteArray());
Sha256Hash hash = Sha256Hash.of(outputStream.toByteArray());
// 2833 blocks takes about 23 ms
log.info("getSha256Hash for {} blocks took {} ms", blocks.size(), System.currentTimeMillis() - ts);
return hash;
} catch (IOException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -191,35 +193,44 @@ protected void applyReOrg() {
///////////////////////////////////////////////////////////////////////////////////////////

private void processAccountingBlocks(List<AccountingBlock> blocks) {
log.info("We received blocks from height {} to {}",
blocks.get(0).getHeight(),
blocks.get(blocks.size() - 1).getHeight());

boolean requiresReOrg = false;
for (AccountingBlock block : blocks) {
try {
burningManAccountingService.addBlock(block);
} catch (BlockHeightNotConnectingException e) {
log.info("Height not connecting. This could happen if we received multiple responses and had already applied a previous one. {}", e.toString());
} catch (BlockHashNotConnectingException e) {
log.warn("Interrupt loop because a reorg is required. {}", e.toString());
requiresReOrg = true;
break;
CompletableFuture.runAsync(() -> {
long ts = System.currentTimeMillis();
log.info("We received blocks from height {} to {}",
blocks.get(0).getHeight(),
blocks.get(blocks.size() - 1).getHeight());

AtomicBoolean requiresReOrg = new AtomicBoolean(false);
for (AccountingBlock block : blocks) {
try {
burningManAccountingService.addBlock(block);
} catch (BlockHeightNotConnectingException e) {
log.info("Height not connecting. This could happen if we received multiple responses and had already applied a previous one. {}", e.toString());
} catch (BlockHashNotConnectingException e) {
log.warn("Interrupt loop because a reorg is required. {}", e.toString());
requiresReOrg.set(true);
break;
}
}
}
if (requiresReOrg) {
applyReOrg();
return;
}

int heightOfLastBlock = burningManAccountingService.getBlockHeightOfLastBlock();
if (walletsSetup.isDownloadComplete() && heightOfLastBlock < bsqWalletService.getBestChainHeight()) {
accountingLiteNodeNetworkService.requestBlocks(heightOfLastBlock + 1);
} else {
if (!initialBlockRequestsComplete) {
onInitialBlockRequestsComplete();
}
}
UserThread.execute(() -> {
if (requiresReOrg.get()) {
applyReOrg();
return;
}

int heightOfLastBlock = burningManAccountingService.getBlockHeightOfLastBlock();
if (walletsSetup.isDownloadComplete() && heightOfLastBlock < bsqWalletService.getBestChainHeight()) {
accountingLiteNodeNetworkService.requestBlocks(heightOfLastBlock + 1);
} else {
if (!initialBlockRequestsComplete) {
onInitialBlockRequestsComplete();
}
}

// 2833 blocks takes about 24 sec
log.info("processAccountingBlocksAsync for {} blocks took {} ms", blocks.size(), System.currentTimeMillis() - ts);
});
});
}

private void processNewAccountingBlock(AccountingBlock accountingBlock) {
Expand Down

0 comments on commit be1691b

Please sign in to comment.