Search in sources :

Example 1 with BlockchainStorage

use of org.hyperledger.besu.ethereum.chain.BlockchainStorage in project besu by hyperledger.

the class PrunerTest method shouldCleanUpPruningStrategyOnShutdown.

@Test
public void shouldCleanUpPruningStrategyOnShutdown() throws InterruptedException {
    final BlockchainStorage blockchainStorage = new KeyValueStoragePrefixedKeyBlockchainStorage(new InMemoryKeyValueStorage(), new MainnetBlockHeaderFunctions());
    final MutableBlockchain blockchain = DefaultBlockchain.createMutable(genesisBlock, blockchainStorage, metricsSystem, 0);
    final Pruner pruner = new Pruner(markSweepPruner, blockchain, new PrunerConfiguration(0, 1), mockExecutorService);
    pruner.start();
    pruner.stop();
    verify(markSweepPruner).cleanup();
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockchainStorage(org.hyperledger.besu.ethereum.chain.BlockchainStorage) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) Test(org.junit.Test)

Example 2 with BlockchainStorage

use of org.hyperledger.besu.ethereum.chain.BlockchainStorage in project besu by hyperledger.

the class PrunerTest method abortsPruningWhenFullyMarkedBlockNoLongerOnCanonicalChain.

@Test
public void abortsPruningWhenFullyMarkedBlockNoLongerOnCanonicalChain() {
    final BlockchainStorage blockchainStorage = new KeyValueStoragePrefixedKeyBlockchainStorage(new InMemoryKeyValueStorage(), new MainnetBlockHeaderFunctions());
    final MutableBlockchain blockchain = DefaultBlockchain.createMutable(genesisBlock, blockchainStorage, metricsSystem, 0);
    // start pruner so it can start handling block added events
    final Pruner pruner = new Pruner(markSweepPruner, blockchain, new PrunerConfiguration(0, 1), mockExecutorService);
    pruner.start();
    /*
     Set up pre-marking state:
      O <---- marking of this block's parent will begin when this block is added
      |
      |  O <- this is a fork as of now (non-canonical)
      O  | <- this is the initially canonical block that will be marked
       \/
       O <--- the common ancestor when the reorg happens
    */
    final Block initiallyCanonicalBlock = appendBlockWithParent(blockchain, genesisBlock);
    appendBlockWithParent(blockchain, initiallyCanonicalBlock);
    final Block forkBlock = appendBlockWithParent(blockchain, genesisBlock);
    /*
      Cause reorg:
     Set up pre-marking state:
      O
      |  O <---- this block causes a reorg; this branch becomes canonical
      |  O <---- which means that state here is referring to nodes from the common ancestor block,
      O  | <- because this was block at which marking began
       \/
       O
    */
    appendBlockWithParent(blockchain, forkBlock);
    verify(markSweepPruner).mark(initiallyCanonicalBlock.getHeader().getStateRoot());
    verify(markSweepPruner, never()).sweepBefore(anyLong());
    pruner.stop();
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockchainStorage(org.hyperledger.besu.ethereum.chain.BlockchainStorage) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) Test(org.junit.Test)

Example 3 with BlockchainStorage

use of org.hyperledger.besu.ethereum.chain.BlockchainStorage in project besu by hyperledger.

the class PrunerTest method shouldMarkCorrectBlockAndSweep.

@Test
public void shouldMarkCorrectBlockAndSweep() throws ExecutionException, InterruptedException {
    final BlockchainStorage blockchainStorage = new KeyValueStoragePrefixedKeyBlockchainStorage(new InMemoryKeyValueStorage(), new MainnetBlockHeaderFunctions());
    final MutableBlockchain blockchain = DefaultBlockchain.createMutable(genesisBlock, blockchainStorage, metricsSystem, 0);
    final Pruner pruner = new Pruner(markSweepPruner, blockchain, new PrunerConfiguration(0, 1), mockExecutorService);
    pruner.start();
    final Block block1 = appendBlockWithParent(blockchain, genesisBlock);
    appendBlockWithParent(blockchain, block1);
    appendBlockWithParent(blockchain, blockchain.getChainHeadBlock());
    verify(markSweepPruner).mark(block1.getHeader().getStateRoot());
    verify(markSweepPruner).sweepBefore(1);
    pruner.stop();
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockchainStorage(org.hyperledger.besu.ethereum.chain.BlockchainStorage) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) Test(org.junit.Test)

Example 4 with BlockchainStorage

use of org.hyperledger.besu.ethereum.chain.BlockchainStorage in project besu by hyperledger.

the class PrunerTest method shouldOnlySweepAfterBlockConfirmationPeriodAndRetentionPeriodEnds.

@Test
public void shouldOnlySweepAfterBlockConfirmationPeriodAndRetentionPeriodEnds() {
    final BlockchainStorage blockchainStorage = new KeyValueStoragePrefixedKeyBlockchainStorage(new InMemoryKeyValueStorage(), new MainnetBlockHeaderFunctions());
    final MutableBlockchain blockchain = DefaultBlockchain.createMutable(genesisBlock, blockchainStorage, metricsSystem, 0);
    final Pruner pruner = new Pruner(markSweepPruner, blockchain, new PrunerConfiguration(1, 2), mockExecutorService);
    pruner.start();
    final Hash markBlockStateRootHash = appendBlockWithParent(blockchain, genesisBlock).getHeader().getStateRoot();
    verify(markSweepPruner, never()).mark(markBlockStateRootHash);
    verify(markSweepPruner, never()).sweepBefore(anyLong());
    appendBlockWithParent(blockchain, blockchain.getChainHeadBlock());
    verify(markSweepPruner).mark(markBlockStateRootHash);
    verify(markSweepPruner, never()).sweepBefore(anyLong());
    appendBlockWithParent(blockchain, blockchain.getChainHeadBlock());
    verify(markSweepPruner).sweepBefore(1);
    pruner.stop();
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockchainStorage(org.hyperledger.besu.ethereum.chain.BlockchainStorage) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) Hash(org.hyperledger.besu.datatypes.Hash) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) KeyValueStoragePrefixedKeyBlockchainStorage(org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage) Test(org.junit.Test)

Example 5 with BlockchainStorage

use of org.hyperledger.besu.ethereum.chain.BlockchainStorage in project besu by hyperledger.

the class BesuControllerBuilder method build.

public BesuController build() {
    checkNotNull(genesisConfig, "Missing genesis config");
    checkNotNull(syncConfig, "Missing sync config");
    checkNotNull(ethereumWireProtocolConfiguration, "Missing ethereum protocol configuration");
    checkNotNull(networkId, "Missing network ID");
    checkNotNull(miningParameters, "Missing mining parameters");
    checkNotNull(metricsSystem, "Missing metrics system");
    checkNotNull(privacyParameters, "Missing privacy parameters");
    // Why do we need this?
    checkNotNull(dataDirectory, "Missing data directory");
    checkNotNull(clock, "Missing clock");
    checkNotNull(transactionPoolConfiguration, "Missing transaction pool configuration");
    checkNotNull(nodeKey, "Missing node key");
    checkNotNull(storageProvider, "Must supply a storage provider");
    checkNotNull(gasLimitCalculator, "Missing gas limit calculator");
    checkNotNull(evmConfiguration, "Missing evm config");
    prepForBuild();
    final ProtocolSchedule protocolSchedule = createProtocolSchedule();
    final GenesisState genesisState = GenesisState.fromConfig(genesisConfig, protocolSchedule);
    final WorldStateStorage worldStateStorage = storageProvider.createWorldStateStorage(dataStorageConfiguration.getDataStorageFormat());
    final BlockchainStorage blockchainStorage = storageProvider.createBlockchainStorage(protocolSchedule);
    final MutableBlockchain blockchain = DefaultBlockchain.createMutable(genesisState.getBlock(), blockchainStorage, metricsSystem, reorgLoggingThreshold, dataDirectory.toString());
    final WorldStateArchive worldStateArchive = createWorldStateArchive(worldStateStorage, blockchain);
    if (blockchain.getChainHeadBlockNumber() < 1) {
        genesisState.writeStateTo(worldStateArchive.getMutable());
    }
    final ProtocolContext protocolContext = createProtocolContext(blockchain, worldStateArchive, protocolSchedule, this::createConsensusContext);
    validateContext(protocolContext);
    protocolSchedule.setPublicWorldStateArchiveForPrivacyBlockProcessor(protocolContext.getWorldStateArchive());
    Optional<Pruner> maybePruner = Optional.empty();
    if (isPruningEnabled) {
        if (!storageProvider.isWorldStateIterable()) {
            LOG.warn("Cannot enable pruning with current database version. Disabling. Resync to get the latest database version or disable pruning explicitly on the command line to remove this warning.");
        } else if (dataStorageConfiguration.getDataStorageFormat().equals(DataStorageFormat.BONSAI)) {
            LOG.warn("Cannot enable pruning with Bonsai data storage format. Disabling. Change the data storage format or disable pruning explicitly on the command line to remove this warning.");
        } else {
            maybePruner = Optional.of(new Pruner(new MarkSweepPruner(((DefaultWorldStateArchive) worldStateArchive).getWorldStateStorage(), blockchain, storageProvider.getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.PRUNING_STATE), metricsSystem), blockchain, prunerConfiguration));
        }
    }
    final EthPeers ethPeers = new EthPeers(getSupportedProtocol(), clock, metricsSystem, maxPeers, messagePermissioningProviders);
    final EthMessages ethMessages = new EthMessages();
    final EthMessages snapMessages = new EthMessages();
    final EthScheduler scheduler = new EthScheduler(syncConfig.getDownloaderParallelism(), syncConfig.getTransactionsParallelism(), syncConfig.getComputationParallelism(), metricsSystem);
    final GenesisConfigOptions configOptions = genesisConfig.getConfigOptions(genesisConfigOverrides);
    Optional<Checkpoint> checkpoint = Optional.empty();
    if (configOptions.getCheckpointOptions().isValid()) {
        checkpoint = Optional.of(ImmutableCheckpoint.builder().blockHash(Hash.fromHexString(// NOSONAR
        configOptions.getCheckpointOptions().getHash().get())).blockNumber(configOptions.getCheckpointOptions().getNumber().getAsLong()).totalDifficulty(Difficulty.fromHexString(configOptions.getCheckpointOptions().getTotalDifficulty().get())).build());
    }
    final EthContext ethContext = new EthContext(ethPeers, ethMessages, snapMessages, scheduler);
    final boolean fastSyncEnabled = !SyncMode.isFullSync(syncConfig.getSyncMode());
    final SyncState syncState = new SyncState(blockchain, ethPeers, fastSyncEnabled, checkpoint);
    syncState.subscribeTTDReached(new PandaPrinter());
    final TransactionPool transactionPool = TransactionPoolFactory.createTransactionPool(protocolSchedule, protocolContext, ethContext, clock, metricsSystem, syncState, miningParameters, transactionPoolConfiguration);
    final List<PeerValidator> peerValidators = createPeerValidators(protocolSchedule);
    final EthProtocolManager ethProtocolManager = createEthProtocolManager(protocolContext, fastSyncEnabled, transactionPool, ethereumWireProtocolConfiguration, ethPeers, ethContext, ethMessages, scheduler, peerValidators);
    final Optional<SnapProtocolManager> maybeSnapProtocolManager = createSnapProtocolManager(peerValidators, ethPeers, snapMessages, worldStateArchive);
    final PivotBlockSelector pivotBlockSelector = createPivotSelector(protocolContext);
    final Synchronizer synchronizer = createSynchronizer(protocolSchedule, worldStateStorage, protocolContext, maybePruner, ethContext, syncState, ethProtocolManager, pivotBlockSelector);
    final MiningCoordinator miningCoordinator = createMiningCoordinator(protocolSchedule, protocolContext, transactionPool, miningParameters, syncState, ethProtocolManager);
    final PluginServiceFactory additionalPluginServices = createAdditionalPluginServices(blockchain, protocolContext);
    final SubProtocolConfiguration subProtocolConfiguration = createSubProtocolConfiguration(ethProtocolManager, maybeSnapProtocolManager);
    final JsonRpcMethods additionalJsonRpcMethodFactory = createAdditionalJsonRpcMethodFactory(protocolContext);
    final List<Closeable> closeables = new ArrayList<>();
    closeables.add(storageProvider);
    if (privacyParameters.getPrivateStorageProvider() != null) {
        closeables.add(privacyParameters.getPrivateStorageProvider());
    }
    return new BesuController(protocolSchedule, protocolContext, ethProtocolManager, configOptionsSupplier.get(), subProtocolConfiguration, synchronizer, syncState, transactionPool, miningCoordinator, privacyParameters, miningParameters, additionalJsonRpcMethodFactory, nodeKey, closeables, additionalPluginServices);
}
Also used : GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) BlockchainStorage(org.hyperledger.besu.ethereum.chain.BlockchainStorage) MiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator) JsonRpcMethods(org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods) SnapProtocolManager(org.hyperledger.besu.ethereum.eth.manager.snap.SnapProtocolManager) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) SyncState(org.hyperledger.besu.ethereum.eth.sync.state.SyncState) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) MarkSweepPruner(org.hyperledger.besu.ethereum.worldstate.MarkSweepPruner) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) Synchronizer(org.hyperledger.besu.ethereum.core.Synchronizer) DefaultSynchronizer(org.hyperledger.besu.ethereum.eth.sync.DefaultSynchronizer) Pruner(org.hyperledger.besu.ethereum.worldstate.Pruner) MarkSweepPruner(org.hyperledger.besu.ethereum.worldstate.MarkSweepPruner) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) PivotBlockSelector(org.hyperledger.besu.ethereum.eth.sync.PivotBlockSelector) ImmutableCheckpoint(org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.ImmutableCheckpoint) Checkpoint(org.hyperledger.besu.ethereum.eth.sync.fastsync.checkpoint.Checkpoint) DaoForkPeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.DaoForkPeerValidator) ClassicForkPeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.ClassicForkPeerValidator) CheckpointBlocksPeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.CheckpointBlocksPeerValidator) RequiredBlocksPeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.RequiredBlocksPeerValidator) PeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator) SubProtocolConfiguration(org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) BonsaiWorldStateArchive(org.hyperledger.besu.ethereum.bonsai.BonsaiWorldStateArchive) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) PandaPrinter(org.hyperledger.besu.consensus.merge.PandaPrinter)

Aggregations

BlockchainStorage (org.hyperledger.besu.ethereum.chain.BlockchainStorage)5 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)5 MainnetBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)4 KeyValueStoragePrefixedKeyBlockchainStorage (org.hyperledger.besu.ethereum.storage.keyvalue.KeyValueStoragePrefixedKeyBlockchainStorage)4 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)4 Test (org.junit.Test)4 Block (org.hyperledger.besu.ethereum.core.Block)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)1 PandaPrinter (org.hyperledger.besu.consensus.merge.PandaPrinter)1 Hash (org.hyperledger.besu.datatypes.Hash)1 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)1 JsonRpcMethods (org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods)1 MiningCoordinator (org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator)1 BonsaiWorldStateArchive (org.hyperledger.besu.ethereum.bonsai.BonsaiWorldStateArchive)1 GenesisState (org.hyperledger.besu.ethereum.chain.GenesisState)1 Synchronizer (org.hyperledger.besu.ethereum.core.Synchronizer)1 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)1 EthMessages (org.hyperledger.besu.ethereum.eth.manager.EthMessages)1