From b95ab354e55f8fd414d2b42af86e3b967fa9baf5 Mon Sep 17 00:00:00 2001 From: Manfred Karrer Date: Thu, 13 Apr 2017 15:16:11 -0500 Subject: [PATCH] Move snapshot handling to manager class. use callback for each block --- .../dao/blockchain/BsqBlockchainManager.java | 42 ++++++++++--------- .../blockchain/BsqBlockchainRpcService.java | 6 +-- .../dao/blockchain/BsqBlockchainService.java | 2 +- .../bisq/core/dao/blockchain/BsqParser.java | 8 +--- .../gui/main/dao/wallet/tx/BsqTxView.java | 2 +- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java index b898e676d2..20a7b7cfd1 100644 --- a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java +++ b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainManager.java @@ -207,9 +207,9 @@ public class BsqBlockchainManager { genesisBlockHeight, genesisTxId, txOutputMap, - snapshotTxOutputMap -> { - applyNewTxOutputMap(snapshotTxOutputMap); - updateSnapshotOnTrigger(snapshotTxOutputMap.getBlockHeight()); + newBlockMap -> { + applyNewTxOutputMap(newBlockMap); + updateSnapshotIfTrigger(newBlockMap.getBlockHeight()); }, chainTipTxOutputMap -> { // we are done but it might be that new blocks have arrived in the meantime, // so we try again with startBlockHeight set to current chainHeadHeight @@ -236,23 +236,23 @@ public class BsqBlockchainManager { genesisBlockHeight, genesisTxId, txOutputMap, - newTxOutputMap -> { - if (txOutputMap.getBlockHeight() < newTxOutputMap.getBlockHeight()) { - applyNewTxOutputMap(newTxOutputMap); - updateSnapshotOnTrigger(newTxOutputMap.getBlockHeight()); + newBlockMap -> { + if (txOutputMap.getBlockHeight() < newBlockMap.getBlockHeight()) { + applyNewTxOutputMap(newBlockMap); + updateSnapshotIfTrigger(newBlockMap.getBlockHeight()); log.debug("new block parsed. bsqBlock={}", bsqBlock); } else { - log.warn("We got a newTxOutputMap with a lower block height than the one from the " + + log.warn("We got a newBlockMap with a lower block height than the one from the " + "map we requested. That should not happen, but theoretically could be " + "if 2 blocks arrive at nearly the same time and the second is faster in " + "parsing than the first, so the callback of the first will have a lower " + "height. " + "txOutputMap.getBlockHeight()={}; " + - "newTxOutputMap.getBlockHeight()={}\n" + + "newBlockMap.getBlockHeight()={}\n" + "To avoid conflicts we start a reorg from the last snapshot.", txOutputMap.getBlockHeight(), - newTxOutputMap.getBlockHeight()); - startReOrgFromLastSnapshot(newTxOutputMap.getBlockHeight()); + newBlockMap.getBlockHeight()); + startReOrgFromLastSnapshot(newBlockMap.getBlockHeight()); } }, throwable -> { if (throwable instanceof OrphanDetectedException) { @@ -297,16 +297,18 @@ public class BsqBlockchainManager { txOutputMapListeners.stream().forEach(l -> l.onTxOutputMapChanged(txOutputMap)); } - private void updateSnapshotOnTrigger(int blockHeight) { - // At trigger time we store the last memory stored map to disc - if (snapshotTxOutputMap != null) { - // We clone because storage is in a threaded context - TxOutputMap clonedSnapshotTxOutputMap = TxOutputMap.getClonedMap(snapshotTxOutputMap); - snapshotTxOutputMapStorage.queueUpForSave(clonedSnapshotTxOutputMap); - } + private void updateSnapshotIfTrigger(int blockHeight) { + if (triggersSnapshot(blockHeight)) { + // At trigger time we store the last memory stored map to disc + if (snapshotTxOutputMap != null) { + // We clone because storage is in a threaded context + TxOutputMap clonedSnapshotTxOutputMap = TxOutputMap.getClonedMap(snapshotTxOutputMap); + snapshotTxOutputMapStorage.queueUpForSave(clonedSnapshotTxOutputMap); + } - // Now we save the map in memory for the next trigger - snapshotTxOutputMap = TxOutputMap.getClonedMap(txOutputMap); + // Now we save the map in memory for the next trigger + snapshotTxOutputMap = TxOutputMap.getClonedMap(txOutputMap); + } } private void onBsqTxoChanged() { diff --git a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainRpcService.java b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainRpcService.java index cac07caad9..8db8d02b15 100644 --- a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainRpcService.java +++ b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainRpcService.java @@ -180,7 +180,7 @@ public class BsqBlockchainRpcService extends BsqBlockchainService { int genesisBlockHeight, String genesisTxId, TxOutputMap txOutputMap, - Consumer snapShotHandler, + Consumer newBlockHandler, Consumer resultHandler, Consumer errorHandler) { ListenableFuture future = parseBlocksExecutor.submit(() -> { @@ -193,9 +193,9 @@ public class BsqBlockchainRpcService extends BsqBlockchainService { genesisBlockHeight, genesisTxId, clonedMap, - clonedSnapShotMap -> { + newBlockMap -> { // We map to UserThread. We don't need to clone as it was created already newly in the parser. - UserThread.execute(() -> snapShotHandler.accept(clonedSnapShotMap)); + UserThread.execute(() -> newBlockHandler.accept(newBlockMap)); }); log.info("parseBlockchain took {} ms", System.currentTimeMillis() - startTs); return clonedMap; diff --git a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainService.java b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainService.java index 5bc2758fb0..3b3ff1c432 100644 --- a/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainService.java +++ b/core/src/main/java/io/bisq/core/dao/blockchain/BsqBlockchainService.java @@ -60,7 +60,7 @@ abstract public class BsqBlockchainService { int genesisBlockHeight, String genesisTxId, TxOutputMap txOutputMap, - Consumer snapShotHandler, + Consumer newBlockHandler, Consumer resultHandler, Consumer errorHandler); diff --git a/core/src/main/java/io/bisq/core/dao/blockchain/BsqParser.java b/core/src/main/java/io/bisq/core/dao/blockchain/BsqParser.java index f5bb945df2..c0c733add2 100644 --- a/core/src/main/java/io/bisq/core/dao/blockchain/BsqParser.java +++ b/core/src/main/java/io/bisq/core/dao/blockchain/BsqParser.java @@ -49,7 +49,7 @@ public class BsqParser { int genesisBlockHeight, String genesisTxId, TxOutputMap txOutputMap, - Consumer snapShotHandler) throws BsqBlockchainException, OrphanDetectedException { + Consumer newBlockHandler) throws BsqBlockchainException, OrphanDetectedException { try { log.info("chainHeadHeight=" + chainHeadHeight); long startTotalTs = System.currentTimeMillis(); @@ -62,11 +62,7 @@ public class BsqParser { genesisTxId, txOutputMap); - if (BsqBlockchainManager.triggersSnapshot(blockHeight)) { - TxOutputMap clonedSnapShotMap = TxOutputMap.getClonedMap(txOutputMap); - //clonedSnapShotMap.printUnspentTxOutputs("triggersSnapshot"); - snapShotHandler.accept(clonedSnapShotMap); - } + newBlockHandler.accept(TxOutputMap.getClonedMap(txOutputMap)); /* StringBuilder sb = new StringBuilder("recursionMap:\n"); List list = new ArrayList<>(); diff --git a/gui/src/main/java/io/bisq/gui/main/dao/wallet/tx/BsqTxView.java b/gui/src/main/java/io/bisq/gui/main/dao/wallet/tx/BsqTxView.java index a649618406..f474ee3eb3 100644 --- a/gui/src/main/java/io/bisq/gui/main/dao/wallet/tx/BsqTxView.java +++ b/gui/src/main/java/io/bisq/gui/main/dao/wallet/tx/BsqTxView.java @@ -160,7 +160,7 @@ public class BsqTxView extends ActivatableView { if (!invalidBsqTransactions.isEmpty() && bsqBlockchainManager.isParseBlockchainComplete()) { Set txIds = invalidBsqTransactions.stream() .filter(t -> t != null) - .map(t -> t.getHashAsString()).collect(Collectors.toSet()); + .map(Transaction::getHashAsString).collect(Collectors.toSet()); log.error("invalidBsqTransactions " + txIds); String key = "invalidBsqTransactionsWarning_" + txIds;