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();
}
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();
}
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();
}
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();
}
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);
}
Aggregations