Skip to content
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
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.mainnet.block.access.list.BlockAccessList;
import org.hyperledger.besu.ethereum.mainnet.block.access.list.BlockAccessList.AccountChanges;
import org.hyperledger.besu.ethereum.mainnet.block.access.list.BlockAccessList.SlotChanges;
import org.hyperledger.besu.ethereum.mainnet.block.access.list.BlockAccessListChanges;
import org.hyperledger.besu.ethereum.mainnet.staterootcommitter.BalRootComputation;
import org.hyperledger.besu.ethereum.trie.pathbased.common.provider.WorldStateQueryParams;
import org.hyperledger.besu.ethereum.trie.pathbased.common.storage.PathBasedWorldStateKeyValueStorage;
Expand All @@ -29,12 +27,9 @@
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.plugin.data.BlockHeader;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import org.apache.tuweni.units.bigints.UInt256;

@SuppressWarnings("rawtypes")
public class BalStateRootCalculator {

Expand Down Expand Up @@ -79,29 +74,16 @@ private static BonsaiWorldState openParentWorldState(

private static void applyBalChanges(
final PathBasedWorldStateUpdateAccumulator accumulator, final BlockAccessList bal) {
for (final AccountChanges changes : bal.accountChanges()) {
if (!changes.hasAnyChange()) {
continue;
}
for (final var changes : BlockAccessListChanges.latestChanges(bal)) {
final Address address = changes.address();
final MutableAccount account = accumulator.getOrCreate(address);

lastOf(changes.balanceChanges())
.ifPresent(c -> account.setBalance(Wei.wrap(c.postBalance())));
lastOf(changes.nonceChanges()).ifPresent(c -> account.setNonce(c.newNonce()));
lastOf(changes.codeChanges()).ifPresent(c -> account.setCode(c.newCode()));
changes.balance().ifPresent(account::setBalance);
changes.nonce().ifPresent(account::setNonce);
changes.code().ifPresent(account::setCode);

for (final SlotChanges slot : changes.storageChanges()) {
lastOf(slot.changes())
.ifPresent(
change ->
slot.slot()
.getSlotKey()
.ifPresent(
key -> {
final UInt256 value = change.newValue();
account.setStorageValue(key, value == null ? UInt256.ZERO : value);
}));
for (final var storage : changes.storageChanges()) {
storage.slot().getSlotKey().ifPresent(key -> account.setStorageValue(key, storage.value()));
}
}
accumulator.clearAccountsThatAreEmpty();
Expand All @@ -116,8 +98,4 @@ private static BalRootComputation computeRoot(final PathBasedWorldState worldSta
updater.commit();
return new BalRootComputation(root, accumulator);
}

private static <T> Optional<T> lastOf(final List<T> list) {
return list.isEmpty() ? Optional.empty() : Optional.of(list.getLast());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ public static Optional<PivotSyncDownloader> createSnapDownloader(
chainSyncState != null
? new PivotSyncState(chainSyncState.pivotBlockHeader(), false)
: PivotSyncState.EMPTY_SYNC_STATE;
final SnapSyncProcessState snapSyncState = new SnapSyncProcessState(pivotSyncState);
final Optional<BlockHeader> firstPivotHeader =
chainSyncState != null
? Optional.of(chainSyncState.firstPivotBlockHeader())
: Optional.empty();
final SnapSyncProcessState snapSyncState =
new SnapSyncProcessState(pivotSyncState, firstPivotHeader);

final InMemoryTasksPriorityQueues<SnapDataRequest> snapTaskCollection =
createSnapWorldStateDownloaderTaskCollection();
Expand All @@ -101,6 +106,7 @@ public static Optional<PivotSyncDownloader> createSnapDownloader(
ethContext,
snapContext,
protocolContext,
protocolSchedule,
worldStateStorageCoordinator,
snapTaskCollection,
syncConfig.getSnapSyncConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.hyperledger.besu.ethereum.eth.sync.snapsync;

import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.eth.sync.common.PivotSyncActions;
import org.hyperledger.besu.ethereum.eth.sync.common.PivotSyncDownloader;
import org.hyperledger.besu.ethereum.eth.sync.common.PivotSyncState;
Expand All @@ -22,6 +23,7 @@
import org.hyperledger.besu.metrics.SyncDurationMetrics;

import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

public class SnapSyncDownloader extends PivotSyncDownloader {
Expand Down Expand Up @@ -50,7 +52,11 @@ protected CompletableFuture<PivotSyncState> start(final PivotSyncState fastSyncS

@Override
protected PivotSyncState storeState(final PivotSyncState fastSyncState) {
final Optional<BlockHeader> firstPivotBlockHeader =
initialPivotSyncState instanceof SnapSyncProcessState snapSyncState
? snapSyncState.getFirstPivotBlockHeader().or(fastSyncState::getPivotBlockHeader)
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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 :)

: fastSyncState.getPivotBlockHeader();
initialPivotSyncState = fastSyncState;
return new SnapSyncProcessState(fastSyncState);
return new SnapSyncProcessState(fastSyncState, firstPivotBlockHeader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
*/
package org.hyperledger.besu.ethereum.eth.sync.snapsync;

import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.SealableBlockHeader;
import org.hyperledger.besu.ethereum.eth.sync.common.PivotSyncState;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,16 +31,23 @@
public class SnapSyncProcessState extends PivotSyncState {
private static final Logger LOG = LoggerFactory.getLogger(SnapSyncProcessState.class);

private final Optional<BlockHeader> firstPivotBlockHeader;
private boolean isHealTrieInProgress;
private boolean isHealFlatDatabaseInProgress;
private boolean isWaitingBlockchain;

public SnapSyncProcessState(final PivotSyncState fastSyncState) {
public SnapSyncProcessState(
final PivotSyncState fastSyncState, final Optional<BlockHeader> firstPivotBlockHeader) {
super(
fastSyncState.getPivotBlockNumber(),
fastSyncState.getPivotBlockHash(),
fastSyncState.getPivotBlockHeader(),
fastSyncState.isSourceTrusted());
this.firstPivotBlockHeader = firstPivotBlockHeader;
}

public Optional<BlockHeader> getFirstPivotBlockHeader() {
return firstPivotBlockHeader;
}

public boolean isHealTrieInProgress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.AccountRangeDataRequest;
import org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest;
import org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader;
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.WorldStateStorageCoordinator;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class SnapWorldStateDownloader implements WorldStateDownloader {
private final int maxOutstandingRequests;
private final int maxNodeRequestsWithoutProgress;
private final ProtocolContext protocolContext;
private final ProtocolSchedule protocolSchedule;
private final WorldStateStorageCoordinator worldStateStorageCoordinator;

private final AtomicReference<SnapWorldDownloadState> downloadState = new AtomicReference<>();
Expand All @@ -76,6 +78,7 @@ public SnapWorldStateDownloader(
final EthContext ethContext,
final SnapSyncStatePersistenceManager snapContext,
final ProtocolContext protocolContext,
final ProtocolSchedule protocolSchedule,
final WorldStateStorageCoordinator worldStateStorageCoordinator,
final InMemoryTasksPriorityQueues<SnapDataRequest> snapTaskCollection,
final SnapSyncConfiguration snapSyncConfiguration,
Expand All @@ -87,6 +90,7 @@ public SnapWorldStateDownloader(
final SyncDurationMetrics syncDurationMetrics) {
this.ethContext = ethContext;
this.protocolContext = protocolContext;
this.protocolSchedule = protocolSchedule;
this.worldStateStorageCoordinator = worldStateStorageCoordinator;
this.snapContext = snapContext;
this.snapTaskCollection = snapTaskCollection;
Expand Down Expand Up @@ -160,6 +164,7 @@ public CompletableFuture<Void> run(
snapContext,
protocolContext.getBlockchain(),
snapSyncState,
protocolSchedule,
snapTaskCollection,
maxNodeRequestsWithoutProgress,
minMillisBeforeStalling,
Expand Down
Loading
Loading