-
Notifications
You must be signed in to change notification settings - Fork 1k
snap sync - apply BALs before flat db heal #10151
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8b03577
Store first pivot in SnapSyncProcessState
mirgee 3cfef4b
Enqueue BAL tasks
mirgee 4bd1a47
Apply changes in BAL to flat db in doPersist
mirgee 108e652
Use account trie as source of truth in doPersist
mirgee 4ee4c1f
Remove storage root verification
mirgee 3ad965f
Merge branch 'main' into snap-2-apply-bals
matkt 8d87109
Merge branch 'main' into snap-2-apply-bals
matkt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
.../java/org/hyperledger/besu/ethereum/mainnet/block/access/list/BlockAccessListChanges.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright contributors to Besu. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.hyperledger.besu.ethereum.mainnet.block.access.list; | ||
|
|
||
| import org.hyperledger.besu.datatypes.Address; | ||
| import org.hyperledger.besu.datatypes.StorageSlotKey; | ||
| import org.hyperledger.besu.datatypes.Wei; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import org.apache.tuweni.bytes.Bytes; | ||
| import org.apache.tuweni.units.bigints.UInt256; | ||
|
|
||
| public final class BlockAccessListChanges { | ||
|
|
||
| private BlockAccessListChanges() {} | ||
|
|
||
| public static List<AccountFinalChanges> latestChanges(final BlockAccessList blockAccessList) { | ||
| final List<AccountFinalChanges> accountFinalChanges = new ArrayList<>(); | ||
|
|
||
| for (final BlockAccessList.AccountChanges accountChanges : blockAccessList.accountChanges()) { | ||
| if (!accountChanges.hasAnyChange()) { | ||
| continue; | ||
| } | ||
|
|
||
| final List<StorageFinalChange> storageFinalChanges = new ArrayList<>(); | ||
| for (final BlockAccessList.SlotChanges slotChanges : accountChanges.storageChanges()) { | ||
| final Optional<BlockAccessList.StorageChange> latestStorageChange = | ||
| lastOf(slotChanges.changes()); | ||
| if (latestStorageChange.isPresent()) { | ||
| storageFinalChanges.add( | ||
| new StorageFinalChange( | ||
| slotChanges.slot(), | ||
| latestStorageChange.get().newValue() == null | ||
| ? UInt256.ZERO | ||
| : latestStorageChange.get().newValue())); | ||
| } | ||
| } | ||
|
|
||
| accountFinalChanges.add( | ||
| new AccountFinalChanges( | ||
| accountChanges.address(), | ||
| lastOf(accountChanges.balanceChanges()) | ||
| .map(BlockAccessList.BalanceChange::postBalance), | ||
| lastOf(accountChanges.nonceChanges()).map(BlockAccessList.NonceChange::newNonce), | ||
| lastOf(accountChanges.codeChanges()).map(BlockAccessList.CodeChange::newCode), | ||
| storageFinalChanges)); | ||
| } | ||
|
|
||
| return accountFinalChanges; | ||
| } | ||
|
|
||
| public record AccountFinalChanges( | ||
| Address address, | ||
| Optional<Wei> balance, | ||
| Optional<Long> nonce, | ||
| Optional<Bytes> code, | ||
| List<StorageFinalChange> storageChanges) {} | ||
|
|
||
| public record StorageFinalChange(StorageSlotKey slot, UInt256 value) {} | ||
|
|
||
| private static <T> Optional<T> lastOf(final List<T> list) { | ||
| return list.isEmpty() ? Optional.empty() : Optional.of(list.getLast()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| import static org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest.createAccountFlatHealingRangeRequest; | ||
| import static org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest.createAccountTrieNodeDataRequest; | ||
| import static org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest.createBlockAccessListDataRequest; | ||
| import static org.hyperledger.besu.ethereum.worldstate.WorldStateStorageCoordinator.applyForStrategy; | ||
|
|
||
| import org.hyperledger.besu.ethereum.chain.BlockAddedObserver; | ||
|
|
@@ -32,6 +33,7 @@ | |
| 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.mainnet.ProtocolSchedule; | ||
| import org.hyperledger.besu.ethereum.trie.RangeManager; | ||
| import org.hyperledger.besu.ethereum.trie.pathbased.bonsai.storage.BonsaiWorldStateKeyValueStorage; | ||
| import org.hyperledger.besu.ethereum.worldstate.FlatDbMode; | ||
|
|
@@ -51,6 +53,7 @@ | |
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.function.Consumer; | ||
|
|
@@ -90,6 +93,7 @@ public class SnapWorldDownloadState extends WorldDownloadState<SnapDataRequest> | |
|
|
||
| private final SnapSyncStatePersistenceManager snapContext; | ||
| private final SnapSyncProcessState snapSyncState; | ||
| private final ProtocolSchedule protocolSchedule; | ||
|
|
||
| // blockchain | ||
| private final Blockchain blockchain; | ||
|
|
@@ -101,12 +105,14 @@ public class SnapWorldDownloadState extends WorldDownloadState<SnapDataRequest> | |
|
|
||
| private final AtomicBoolean trieHealStartedBefore = new AtomicBoolean(false); | ||
| private final AtomicBoolean worldStateHealFinishedNotified = new AtomicBoolean(false); | ||
| private final AtomicBoolean blockAccessListHealEnqueued = new AtomicBoolean(false); | ||
|
|
||
| public SnapWorldDownloadState( | ||
| final WorldStateStorageCoordinator worldStateStorageCoordinator, | ||
| final SnapSyncStatePersistenceManager snapContext, | ||
| final Blockchain blockchain, | ||
| final SnapSyncProcessState snapSyncState, | ||
| final ProtocolSchedule protocolSchedule, | ||
| final InMemoryTasksPriorityQueues<SnapDataRequest> pendingRequests, | ||
| final int maxRequestsWithoutProgress, | ||
| final long minMillisBeforeStalling, | ||
|
|
@@ -124,6 +130,7 @@ public SnapWorldDownloadState( | |
| this.snapContext = snapContext; | ||
| this.blockchain = blockchain; | ||
| this.snapSyncState = snapSyncState; | ||
| this.protocolSchedule = protocolSchedule; | ||
| this.metricsManager = metricsManager; | ||
| this.blockObserverId = blockchain.observeBlockAdded(createBlockchainObserver()); | ||
| this.ethContext = ethContext; | ||
|
|
@@ -207,6 +214,9 @@ else if (pivotBlockSelector.isBlockchainBehind()) { | |
| if (!snapSyncState.isHealFlatDatabaseInProgress() | ||
| && (worldStateStorageCoordinator.isMatchingFlatMode(FlatDbMode.FULL) | ||
| || worldStateStorageCoordinator.isMatchingFlatMode(FlatDbMode.ARCHIVE))) { | ||
| if (enqueueBlockAccessListsForPivotRangeIfRequired()) { | ||
| return false; | ||
| } | ||
| startFlatDatabaseHeal(header); | ||
| } | ||
| // If the flat database healing process is in progress or the flat database mode is not FULL | ||
|
|
@@ -305,6 +315,50 @@ public synchronized void startFlatDatabaseHeal(final BlockHeader header) { | |
| createAccountFlatHealingRangeRequest(header.getStateRoot(), key, value))); | ||
| } | ||
|
|
||
| private boolean enqueueBlockAccessListsForPivotRangeIfRequired() { | ||
| if (!blockAccessListHealEnqueued.compareAndSet(false, true)) { | ||
| return false; | ||
| } | ||
|
|
||
| final Optional<BlockHeader> maybeFirstPivotHeader = snapSyncState.getFirstPivotBlockHeader(); | ||
| final Optional<BlockHeader> maybeLastPivotHeader = snapSyncState.getPivotBlockHeader(); | ||
|
|
||
| LOG.debug("Starting BAL apply attempt"); | ||
|
|
||
| if (maybeFirstPivotHeader.isEmpty() || maybeLastPivotHeader.isEmpty()) { | ||
| LOG.debug( | ||
| "Skipping BAL apply - firstPivotHeader={}, lastPivotHeader={}", | ||
| maybeFirstPivotHeader, | ||
| maybeLastPivotHeader); | ||
| return false; | ||
| } | ||
|
|
||
| final BlockHeader firstPivotHeader = maybeFirstPivotHeader.get(); | ||
| if (!protocolSchedule.getByBlockHeader(firstPivotHeader).isBlockAccessListEnabled()) { | ||
| LOG.debug("Skipping BAL apply - BALs not enabled on first pivot {}", firstPivotHeader); | ||
| return false; | ||
| } | ||
|
|
||
| final long fromBlock = firstPivotHeader.getNumber(); | ||
| final long toBlock = maybeLastPivotHeader.get().getNumber(); | ||
| if (toBlock < fromBlock) { | ||
| LOG.error("Attempted to apply BALs with fromBlock {} > {} toBlock", fromBlock, toBlock); | ||
| return false; | ||
| } | ||
|
|
||
| LOG.info("Queueing block access list heal from block {} to {}", fromBlock, toBlock); | ||
| for (long blockNumber = fromBlock; blockNumber <= toBlock; blockNumber++) { | ||
| final Optional<BlockHeader> maybeBlockHeader = blockchain.getBlockHeader(blockNumber); | ||
| if (maybeBlockHeader.isPresent()) { | ||
| final BlockHeader blockHeader = maybeBlockHeader.get(); | ||
| enqueueRequest(createBlockAccessListDataRequest(blockHeader.getStateRoot(), blockHeader)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in another PR I think it will be better to download the BAL before in the pipeline where we are downloading header, body |
||
| } else { | ||
| LOG.warn("Unable to queue block access list heal for missing block {}", blockNumber); | ||
| } | ||
| } | ||
| return !pendingBlockAccessListRequests.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void enqueueRequest(final SnapDataRequest request) { | ||
| if (!internalFuture.isDone()) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we keep all the pivot blocks used during snapsync you can completely skip the fladb healing if all the pivot blocks used are canonical and only trigger the heal if one of the block is not canonical
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily all pivots, as we already discussed. It's a valid point but hopefully you don't want to address it in this PR :)