Search in sources :

Example 1 with NodeDataRequest

use of org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.NodeDataRequest in project besu by hyperledger.

the class FastDownloaderFactory method create.

public static Optional<FastSyncDownloader> create(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 (syncConfig.getSyncMode() != SyncMode.FAST) {
        if (fastSyncStateStorage.isFastSyncInProgress()) {
            throw new IllegalStateException("Unable to change the sync mode when fast sync is incomplete, please restart with fast 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("Fast sync was requested, but cannot be enabled because the local blockchain is not empty.");
        return Optional.empty();
    }
    if (worldStateStorage instanceof BonsaiWorldStateKeyValueStorage) {
        worldStateStorage.clear();
    } else {
        final Path queueDataDir = fastSyncDataDirectory.resolve("statequeue");
        if (queueDataDir.toFile().exists()) {
            LOG.warn("Fast sync is picking up after old fast sync version. Pruning the world state and starting from scratch.");
            clearOldFastSyncWorldStateData(worldStateStorage, queueDataDir);
        }
    }
    final InMemoryTasksPriorityQueues<NodeDataRequest> taskCollection = createWorldStateDownloaderTaskCollection(metricsSystem, syncConfig.getWorldStateTaskCacheSize());
    final WorldStateDownloader worldStateDownloader = new FastWorldStateDownloader(ethContext, worldStateStorage, taskCollection, syncConfig.getWorldStateHashCountPerRequest(), syncConfig.getWorldStateRequestParallelism(), syncConfig.getWorldStateMaxRequestsWithoutProgress(), syncConfig.getWorldStateMinMillisBeforeStalling(), clock, metricsSystem);
    final FastSyncDownloader fastSyncDownloader = new FastSyncDownloader(new FastSyncActions(syncConfig, protocolSchedule, protocolContext, ethContext, syncState, metricsSystem), worldStateStorage, worldStateDownloader, fastSyncStateStorage, taskCollection, fastSyncDataDirectory, fastSyncState);
    syncState.setWorldStateDownloadStatus(worldStateDownloader);
    return Optional.of(fastSyncDownloader);
}
Also used : Path(java.nio.file.Path) FastWorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.FastWorldStateDownloader) BonsaiWorldStateKeyValueStorage(org.hyperledger.besu.ethereum.bonsai.BonsaiWorldStateKeyValueStorage) NodeDataRequest(org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.NodeDataRequest) FastWorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.FastWorldStateDownloader) WorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader)

Example 2 with NodeDataRequest

use of org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.NodeDataRequest in project besu by hyperledger.

the class FastSyncDownloaderTest method shouldResumeFastSync.

@Test
public void shouldResumeFastSync() {
    final BlockHeader pivotBlockHeader = new BlockHeaderTestFixture().number(50).buildHeader();
    final FastSyncState fastSyncState = new FastSyncState(pivotBlockHeader);
    final CompletableFuture<FastSyncState> complete = completedFuture(fastSyncState);
    when(fastSyncActions.waitForSuitablePeers(fastSyncState)).thenReturn(complete);
    when(fastSyncActions.selectPivotBlock(fastSyncState)).thenReturn(complete);
    when(fastSyncActions.downloadPivotBlockHeader(fastSyncState)).thenReturn(complete);
    when(fastSyncActions.createChainDownloader(fastSyncState)).thenReturn(chainDownloader);
    when(chainDownloader.start()).thenReturn(completedFuture(null));
    when(worldStateDownloader.run(any(FastSyncActions.class), eq(new FastSyncState(pivotBlockHeader)))).thenReturn(completedFuture(null));
    final FastSyncDownloader<NodeDataRequest> resumedDownloader = new FastSyncDownloader<>(fastSyncActions, worldStateStorage, worldStateDownloader, storage, taskCollection, fastSyncDataDirectory, fastSyncState);
    final CompletableFuture<FastSyncState> result = resumedDownloader.start();
    verify(fastSyncActions).waitForSuitablePeers(fastSyncState);
    verify(fastSyncActions).selectPivotBlock(fastSyncState);
    verify(fastSyncActions).downloadPivotBlockHeader(fastSyncState);
    verify(storage).storeState(fastSyncState);
    verify(fastSyncActions).createChainDownloader(fastSyncState);
    verify(chainDownloader).start();
    verify(worldStateDownloader).run(any(FastSyncActions.class), eq(new FastSyncState(pivotBlockHeader)));
    verifyNoMoreInteractions(fastSyncActions, worldStateDownloader, storage);
    assertThat(result).isCompletedWithValue(fastSyncState);
}
Also used : BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) NodeDataRequest(org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.NodeDataRequest) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

NodeDataRequest (org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.NodeDataRequest)2 Path (java.nio.file.Path)1 BonsaiWorldStateKeyValueStorage (org.hyperledger.besu.ethereum.bonsai.BonsaiWorldStateKeyValueStorage)1 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)1 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)1 FastWorldStateDownloader (org.hyperledger.besu.ethereum.eth.sync.fastsync.worldstate.FastWorldStateDownloader)1 WorldStateDownloader (org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader)1 Test (org.junit.Test)1