diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/SnapWorldDownloadState.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/SnapWorldDownloadState.java index 3abd1b0d5ec..d7c29335388 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/SnapWorldDownloadState.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/SnapWorldDownloadState.java @@ -28,7 +28,6 @@ import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.heal.AccountFlatDatabaseHealingRangeRequest; import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.heal.StorageFlatDatabaseHealingRangeRequest; import org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldDownloadState; -import org.hyperledger.besu.ethereum.worldstate.DataStorageFormat; import org.hyperledger.besu.ethereum.worldstate.FlatDbMode; import org.hyperledger.besu.ethereum.worldstate.WorldStateStorage; import org.hyperledger.besu.metrics.BesuMetricCategory; @@ -168,22 +167,23 @@ public synchronized boolean checkCompletion(final BlockHeader header) { startTrieHeal(); } else if (pivotBlockSelector.isBlockchainBehind()) { LOG.info("Pausing world state download while waiting for sync to complete"); - if (blockObserverId.isEmpty()) - blockObserverId = OptionalLong.of(blockchain.observeBlockAdded(getBlockAddedListener())); snapSyncState.setWaitingBlockchain(true); - } else if (!snapSyncState.isHealFlatDatabaseInProgress() - && worldStateStorage.getFlatDbMode().equals(FlatDbMode.FULL)) { - // only doing a flat db heal for bonsai - startFlatDatabaseHeal(header); } else { - final WorldStateStorage.Updater updater = worldStateStorage.updater(); - updater.saveWorldState(header.getHash(), header.getStateRoot(), rootNodeData); - updater.commit(); - metricsManager.notifySnapSyncCompleted(); - snapContext.clear(); - internalFuture.complete(null); - - return true; + blockObserverId.ifPresent(blockchain::removeObserver); + if (!snapSyncState.isHealFlatDatabaseInProgress() + && worldStateStorage.getFlatDbMode().equals(FlatDbMode.FULL)) { + // only doing a flat db heal for bonsai + startFlatDatabaseHeal(header); + } else { + final WorldStateStorage.Updater updater = worldStateStorage.updater(); + updater.saveWorldState(header.getHash(), header.getStateRoot(), rootNodeData); + updater.commit(); + metricsManager.notifySnapSyncCompleted(); + snapContext.clear(); + internalFuture.complete(null); + + return true; + } } } @@ -203,6 +203,8 @@ protected synchronized void cleanupQueues() { public synchronized void startTrieHeal() { snapContext.clearAccountRangeTasks(); snapSyncState.setHealTrieStatus(true); + if (blockObserverId.isEmpty()) + blockObserverId = OptionalLong.of(blockchain.observeBlockAdded(getBlockAddedListener())); // try to find new pivot block before healing pivotBlockSelector.switchToNewPivotBlock( (blockHeader, newPivotBlockFound) -> { @@ -235,10 +237,6 @@ public synchronized void startFlatDatabaseHeal(final BlockHeader header) { createAccountFlatHealingRangeRequest(header.getStateRoot(), key, value))); } - public boolean isBonsaiStorageFormat() { - return worldStateStorage.getDataStorageFormat().equals(DataStorageFormat.BONSAI); - } - @Override public synchronized void enqueueRequest(final SnapDataRequest request) { if (!internalFuture.isDone()) { @@ -274,7 +272,7 @@ public synchronized void setAccountsToBeRepaired(final HashSet accountsTo * * @param account The account to be added for repair. */ - public synchronized void addAccountsToBeRepaired(final Bytes account) { + public synchronized void addAccountToHealingList(final Bytes account) { if (!accountsToBeRepaired.contains(account)) { snapContext.addAccountsToBeRepaired(account); accountsToBeRepaired.add(account); @@ -387,7 +385,7 @@ public void setPivotBlockSelector(final DynamicPivotBlockSelector pivotBlockSele public BlockAddedObserver getBlockAddedListener() { return addedBlockContext -> { - if (snapSyncState.isWaitingBlockchain()) { + if (snapSyncState.isHealTrieInProgress()) { // if we receive a new pivot block we can restart the heal pivotBlockSelector.check( (____, isNewPivotBlock) -> { @@ -400,8 +398,6 @@ public BlockAddedObserver getBlockAddedListener() { snapSyncState.setWaitingBlockchain(false); } if (!snapSyncState.isWaitingBlockchain()) { - blockObserverId.ifPresent(blockchain::removeObserver); - blockObserverId = OptionalLong.empty(); reloadTrieHeal(); } } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/StorageRangeDataRequest.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/StorageRangeDataRequest.java index 3ef48c2383c..c18d063d74d 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/StorageRangeDataRequest.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/StorageRangeDataRequest.java @@ -126,7 +126,7 @@ public void addResponse( startKeyHash, endKeyHash, storageRoot, proofs, slots)) { // If the proof is invalid, it means that the storage will be a mix of several blocks. // Therefore, it will be necessary to heal the account's storage subsequently - downloadState.addAccountsToBeRepaired(CompactEncoding.bytesToPath(accountHash)); + downloadState.addAccountToHealingList(CompactEncoding.bytesToPath(accountHash)); // We will request the new storage root of the account because it is apparently no longer // valid with the new pivot block. downloadState.enqueueRequest( @@ -178,7 +178,7 @@ public Stream getChildRequests( }); if (startKeyHash.equals(MIN_RANGE) && endKeyHash.equals(MAX_RANGE)) { // need to heal this account storage - downloadState.addAccountsToBeRepaired(CompactEncoding.bytesToPath(accountHash)); + downloadState.addAccountToHealingList(CompactEncoding.bytesToPath(accountHash)); } }); diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountFlatDatabaseHealingRangeRequest.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountFlatDatabaseHealingRangeRequest.java index b53a10c5014..07bc99a8269 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountFlatDatabaseHealingRangeRequest.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountFlatDatabaseHealingRangeRequest.java @@ -194,7 +194,7 @@ protected int doPersist( if (!value.equals(flatDbEntry)) { Hash accountHash = Hash.wrap(key); // Add the account to the list of accounts to be repaired - downloadState.addAccountsToBeRepaired(CompactEncoding.bytesToPath(accountHash)); + downloadState.addAccountToHealingList(CompactEncoding.bytesToPath(accountHash)); // Update the account info state bonsaiUpdater.putAccountInfoState(accountHash, value); } @@ -207,7 +207,7 @@ protected int doPersist( .forEach( key -> { Hash accountHash = Hash.wrap(key); - downloadState.addAccountsToBeRepaired(CompactEncoding.bytesToPath(accountHash)); + downloadState.addAccountToHealingList(CompactEncoding.bytesToPath(accountHash)); bonsaiUpdater.removeAccountInfoState(accountHash); }); } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountTrieNodeHealingRequest.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountTrieNodeHealingRequest.java index 8d7b392f18e..ce08b9a2b89 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountTrieNodeHealingRequest.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/heal/AccountTrieNodeHealingRequest.java @@ -165,7 +165,7 @@ protected Stream getRequestsFromTrieNodeValue( if (!storageRootFoundInDb.equals(accountValue.getStorageRoot())) { // If the storage root is not found in the database, add the account to the list of accounts // to be repaired - downloadState.addAccountsToBeRepaired(CompactEncoding.bytesToPath(accountHash)); + downloadState.addAccountToHealingList(CompactEncoding.bytesToPath(accountHash)); // If the account's storage root is not empty, // fill it with trie heal before completing with a flat heal if (!accountValue.getStorageRoot().equals(MerkleTrie.EMPTY_TRIE_NODE_HASH)) {