Search in sources :

Example 1 with SnapSyncState

use of org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState in project besu by hyperledger.

the class CheckpointDownloaderFactory method createCheckpointDownloader.

public static Optional<FastSyncDownloader<?>> createCheckpointDownloader(final PivotBlockSelector pivotBlockSelector, final SynchronizerConfiguration syncConfig, final Path dataDirectory, final ProtocolSchedule protocolSchedule, final ProtocolContext protocolContext, final MetricsSystem metricsSystem, final EthContext ethContext, final WorldStateStorage worldStateStorage, final SyncState syncState, final Clock clock) {
    final Path fastSyncDataDirectory = dataDirectory.resolve(FAST_SYNC_FOLDER);
    final FastSyncStateStorage fastSyncStateStorage = new FastSyncStateStorage(fastSyncDataDirectory);
    if (SyncMode.isFullSync(syncConfig.getSyncMode())) {
        if (fastSyncStateStorage.isFastSyncInProgress()) {
            throw new IllegalStateException("Unable to change the sync mode when snap sync is incomplete, please restart with checkpoint sync mode");
        } else {
            return Optional.empty();
        }
    }
    ensureDirectoryExists(fastSyncDataDirectory.toFile());
    final FastSyncState fastSyncState = fastSyncStateStorage.loadState(ScheduleBasedBlockHeaderFunctions.create(protocolSchedule));
    if (fastSyncState.getPivotBlockHeader().isEmpty() && protocolContext.getBlockchain().getChainHeadBlockNumber() != BlockHeader.GENESIS_BLOCK_NUMBER) {
        LOG.info("Checkpoint sync was requested, but cannot be enabled because the local blockchain is not empty.");
        return Optional.empty();
    }
    final FastSyncActions fastSyncActions;
    if (syncState.getCheckpoint().isEmpty()) {
        LOG.warn("Unable to find a valid checkpoint configuration. The genesis will be used");
        fastSyncActions = new FastSyncActions(syncConfig, worldStateStorage, protocolSchedule, protocolContext, ethContext, syncState, pivotBlockSelector, metricsSystem);
    } else {
        LOG.info("Checkpoint sync start with block {} and hash {}", syncState.getCheckpoint().get().blockNumber(), syncState.getCheckpoint().get().blockHash());
        fastSyncActions = new CheckpointSyncActions(syncConfig, worldStateStorage, protocolSchedule, protocolContext, ethContext, syncState, pivotBlockSelector, metricsSystem);
    }
    final SnapSyncState snapSyncState = new SnapSyncState(fastSyncStateStorage.loadState(ScheduleBasedBlockHeaderFunctions.create(protocolSchedule)));
    worldStateStorage.clear();
    final InMemoryTasksPriorityQueues<SnapDataRequest> snapTaskCollection = createSnapWorldStateDownloaderTaskCollection();
    final WorldStateDownloader snapWorldStateDownloader = new SnapWorldStateDownloader(ethContext, worldStateStorage, snapTaskCollection, syncConfig.getSnapSyncConfiguration(), syncConfig.getWorldStateRequestParallelism(), syncConfig.getWorldStateMaxRequestsWithoutProgress(), syncConfig.getWorldStateMinMillisBeforeStalling(), clock, metricsSystem);
    final FastSyncDownloader<SnapDataRequest> fastSyncDownloader = new SnapSyncDownloader(fastSyncActions, worldStateStorage, snapWorldStateDownloader, fastSyncStateStorage, snapTaskCollection, fastSyncDataDirectory, snapSyncState);
    syncState.setWorldStateDownloadStatus(snapWorldStateDownloader);
    return Optional.of(fastSyncDownloader);
}
Also used : Path(java.nio.file.Path) SnapDataRequest(org.hyperledger.besu.ethereum.eth.sync.snapsync.request.SnapDataRequest) SnapWorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapWorldStateDownloader) WorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader) FastSyncState(org.hyperledger.besu.ethereum.eth.sync.fastsync.FastSyncState) SnapSyncState(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState) FastSyncActions(org.hyperledger.besu.ethereum.eth.sync.fastsync.FastSyncActions) FastSyncStateStorage(org.hyperledger.besu.ethereum.eth.sync.fastsync.FastSyncStateStorage) SnapSyncDownloader(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncDownloader) SnapWorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapWorldStateDownloader)

Example 2 with SnapSyncState

use of org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState in project besu by hyperledger.

the class StorageRangeDataRequest method doPersist.

@Override
protected int doPersist(final WorldStateStorage worldStateStorage, final Updater updater, final SnapWorldDownloadState downloadState, final SnapSyncState snapSyncState) {
    // search incomplete nodes in the range
    final AtomicInteger nbNodesSaved = new AtomicInteger();
    final AtomicReference<Updater> updaterTmp = new AtomicReference<>(worldStateStorage.updater());
    final NodeUpdater nodeUpdater = (location, hash, value) -> {
        updaterTmp.get().putAccountStorageTrieNode(accountHash, location, hash, value);
        if (nbNodesSaved.getAndIncrement() % 1000 == 0) {
            updaterTmp.get().commit();
            updaterTmp.set(worldStateStorage.updater());
        }
    };
    stackTrie.commit(nodeUpdater);
    updaterTmp.get().commit();
    downloadState.getMetricsManager().notifySlotsDownloaded(stackTrie.getElementsCount().get());
    return nbNodesSaved.get();
}
Also used : STORAGE_RANGE(org.hyperledger.besu.ethereum.eth.sync.snapsync.RequestType.STORAGE_RANGE) RangeManager.findNewBeginElementInRange(org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.findNewBeginElementInRange) NodeUpdater(org.hyperledger.besu.ethereum.trie.NodeUpdater) LoggerFactory(org.slf4j.LoggerFactory) MIN_RANGE(org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.MIN_RANGE) Bytes(org.apache.tuweni.bytes.Bytes) SnapWorldDownloadState(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapWorldDownloadState) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) WorldDownloadState(org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldDownloadState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MAX_RANGE(org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.MAX_RANGE) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) SnapSyncState(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState) Logger(org.slf4j.Logger) ArrayDeque(kotlin.collections.ArrayDeque) CompactEncoding(org.hyperledger.besu.ethereum.trie.CompactEncoding) List(java.util.List) Stream(java.util.stream.Stream) StackTrie(org.hyperledger.besu.ethereum.eth.sync.snapsync.StackTrie) TreeMap(java.util.TreeMap) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Updater(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage.Updater) RangeManager(org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager) Hash(org.hyperledger.besu.datatypes.Hash) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeUpdater(org.hyperledger.besu.ethereum.trie.NodeUpdater) Updater(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage.Updater) AtomicReference(java.util.concurrent.atomic.AtomicReference) NodeUpdater(org.hyperledger.besu.ethereum.trie.NodeUpdater)

Example 3 with SnapSyncState

use of org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState in project besu by hyperledger.

the class AccountRangeDataRequest method doPersist.

@Override
protected int doPersist(final WorldStateStorage worldStateStorage, final Updater updater, final SnapWorldDownloadState downloadState, final SnapSyncState snapSyncState) {
    if (startStorageRange.isPresent() && endStorageRange.isPresent()) {
        // rootHash
        return 0;
    }
    // search incomplete nodes in the range
    final AtomicInteger nbNodesSaved = new AtomicInteger();
    final NodeUpdater nodeUpdater = (location, hash, value) -> {
        updater.putAccountStateTrieNode(location, hash, value);
        nbNodesSaved.getAndIncrement();
    };
    stackTrie.commit(nodeUpdater);
    downloadState.getMetricsManager().notifyAccountsDownloaded(stackTrie.getElementsCount().get());
    return nbNodesSaved.get();
}
Also used : RangeManager.findNewBeginElementInRange(org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.findNewBeginElementInRange) NodeUpdater(org.hyperledger.besu.ethereum.trie.NodeUpdater) LoggerFactory(org.slf4j.LoggerFactory) MIN_RANGE(org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.MIN_RANGE) Bytes(org.apache.tuweni.bytes.Bytes) SnapWorldDownloadState(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapWorldDownloadState) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MAX_RANGE(org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.MAX_RANGE) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Bytes32(org.apache.tuweni.bytes.Bytes32) SnapSyncState(org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState) Logger(org.slf4j.Logger) RLP(org.hyperledger.besu.ethereum.rlp.RLP) ArrayDeque(kotlin.collections.ArrayDeque) List(java.util.List) Stream(java.util.stream.Stream) StackTrie(org.hyperledger.besu.ethereum.eth.sync.snapsync.StackTrie) StateTrieAccountValue(org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue) TreeMap(java.util.TreeMap) ACCOUNT_RANGE(org.hyperledger.besu.ethereum.eth.sync.snapsync.RequestType.ACCOUNT_RANGE) WorldStateProofProvider(org.hyperledger.besu.ethereum.proof.WorldStateProofProvider) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Updater(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage.Updater) Hash(org.hyperledger.besu.datatypes.Hash) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeUpdater(org.hyperledger.besu.ethereum.trie.NodeUpdater)

Aggregations

SnapSyncState (org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapSyncState)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 TreeMap (java.util.TreeMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Stream (java.util.stream.Stream)2 ArrayDeque (kotlin.collections.ArrayDeque)2 Bytes (org.apache.tuweni.bytes.Bytes)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 Hash (org.hyperledger.besu.datatypes.Hash)2 MAX_RANGE (org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.MAX_RANGE)2 MIN_RANGE (org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.MIN_RANGE)2 RangeManager.findNewBeginElementInRange (org.hyperledger.besu.ethereum.eth.sync.snapsync.RangeManager.findNewBeginElementInRange)2 SnapWorldDownloadState (org.hyperledger.besu.ethereum.eth.sync.snapsync.SnapWorldDownloadState)2 StackTrie (org.hyperledger.besu.ethereum.eth.sync.snapsync.StackTrie)2 WorldStateProofProvider (org.hyperledger.besu.ethereum.proof.WorldStateProofProvider)2 NodeUpdater (org.hyperledger.besu.ethereum.trie.NodeUpdater)2 WorldStateStorage (org.hyperledger.besu.ethereum.worldstate.WorldStateStorage)2