Search in sources :

Example 1 with RawBlockIterator

use of org.hyperledger.besu.ethereum.util.RawBlockIterator in project besu by hyperledger.

the class RlpBlockExporterTest method exportBlocks_withRangeBeyondChainHead.

@Test
public void exportBlocks_withRangeBeyondChainHead() throws IOException {
    final File outputPath = folder.newFile();
    final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);
    final long lowerBound = chainHead - 10;
    final long upperBound = chainHead + 10;
    exporter.exportBlocks(outputPath, Optional.of(lowerBound), Optional.of(upperBound));
    // Iterate over blocks and check that they match expectations
    final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
    long currentBlockNumber = lowerBound;
    while (blockIterator.hasNext()) {
        final Block actual = blockIterator.next();
        final Block expected = getBlock(blockchain, currentBlockNumber);
        assertThat(actual).isEqualTo(expected);
        currentBlockNumber++;
    }
    // Check that we iterated to the end of the chain
    assertThat(currentBlockNumber).isEqualTo(chainHead + 1L);
}
Also used : Block(org.hyperledger.besu.ethereum.core.Block) RawBlockIterator(org.hyperledger.besu.ethereum.util.RawBlockIterator) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) File(java.io.File) Test(org.junit.Test)

Example 2 with RawBlockIterator

use of org.hyperledger.besu.ethereum.util.RawBlockIterator in project besu by hyperledger.

the class RlpBlockExporterTest method exportBlocks_withLowerBound.

@Test
public void exportBlocks_withLowerBound() throws IOException {
    final File outputPath = folder.newFile();
    final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);
    final long lowerBound = 990;
    exporter.exportBlocks(outputPath, Optional.of(lowerBound), Optional.empty());
    // Iterate over blocks and check that they match expectations
    final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
    long currentBlockNumber = lowerBound;
    while (blockIterator.hasNext()) {
        final Block actual = blockIterator.next();
        final Block expected = getBlock(blockchain, currentBlockNumber);
        assertThat(actual).isEqualTo(expected);
        currentBlockNumber++;
    }
    // Check that we iterated to the end of the chain
    assertThat(currentBlockNumber).isEqualTo(chainHead + 1L);
}
Also used : Block(org.hyperledger.besu.ethereum.core.Block) RawBlockIterator(org.hyperledger.besu.ethereum.util.RawBlockIterator) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) File(java.io.File) Test(org.junit.Test)

Example 3 with RawBlockIterator

use of org.hyperledger.besu.ethereum.util.RawBlockIterator in project besu by hyperledger.

the class RlpBlockExporterTest method exportBlocks_withUpperAndLowerBounds.

@Test
public void exportBlocks_withUpperAndLowerBounds() throws IOException {
    final File outputPath = folder.newFile();
    final RlpBlockExporter exporter = new RlpBlockExporter(blockchain);
    final long lowerBound = 5;
    final long upperBound = 10;
    exporter.exportBlocks(outputPath, Optional.of(lowerBound), Optional.of(upperBound));
    // Iterate over blocks and check that they match expectations
    final RawBlockIterator blockIterator = getBlockIterator(outputPath.toPath());
    long currentBlockNumber = lowerBound;
    while (blockIterator.hasNext()) {
        final Block actual = blockIterator.next();
        final Block expected = getBlock(blockchain, currentBlockNumber);
        assertThat(actual).isEqualTo(expected);
        currentBlockNumber++;
    }
    // Check that we iterated to the end of the chain
    assertThat(currentBlockNumber).isEqualTo(upperBound);
}
Also used : Block(org.hyperledger.besu.ethereum.core.Block) RawBlockIterator(org.hyperledger.besu.ethereum.util.RawBlockIterator) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) File(java.io.File) Test(org.junit.Test)

Example 4 with RawBlockIterator

use of org.hyperledger.besu.ethereum.util.RawBlockIterator in project besu by hyperledger.

the class BlockchainSetupUtil method create.

private static BlockchainSetupUtil create(final ChainResources chainResources, final DataStorageFormat storageFormat, final ProtocolScheduleProvider protocolScheduleProvider, final ProtocolContextProvider protocolContextProvider, final EthScheduler scheduler) {
    final TemporaryFolder temp = new TemporaryFolder();
    try {
        temp.create();
        final String genesisJson = Resources.toString(chainResources.getGenesisURL(), Charsets.UTF_8);
        final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(genesisJson);
        final ProtocolSchedule protocolSchedule = protocolScheduleProvider.get(genesisConfigFile);
        final GenesisState genesisState = GenesisState.fromJson(genesisJson, protocolSchedule);
        final MutableBlockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
        final WorldStateArchive worldArchive = storageFormat == DataStorageFormat.BONSAI ? createBonsaiInMemoryWorldStateArchive(blockchain) : createInMemoryWorldStateArchive();
        final TransactionPool transactionPool = mock(TransactionPool.class);
        genesisState.writeStateTo(worldArchive.getMutable());
        final ProtocolContext protocolContext = protocolContextProvider.get(blockchain, worldArchive);
        final Path blocksPath = Path.of(chainResources.getBlocksURL().toURI());
        final List<Block> blocks = new ArrayList<>();
        final BlockHeaderFunctions blockHeaderFunctions = ScheduleBasedBlockHeaderFunctions.create(protocolSchedule);
        try (final RawBlockIterator iterator = new RawBlockIterator(blocksPath, rlp -> BlockHeader.readFrom(rlp, blockHeaderFunctions))) {
            while (iterator.hasNext()) {
                blocks.add(iterator.next());
            }
        }
        return new BlockchainSetupUtil(genesisState, blockchain, protocolContext, protocolSchedule, worldArchive, transactionPool, blocks, scheduler);
    } catch (final IOException | URISyntaxException ex) {
        throw new IllegalStateException(ex);
    } finally {
        temp.delete();
    }
}
Also used : Path(java.nio.file.Path) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) ScheduleBasedBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions) GenesisConfigFile(org.hyperledger.besu.config.GenesisConfigFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) MainnetProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule) InMemoryKeyValueStorageProvider.createBonsaiInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createBonsaiInMemoryWorldStateArchive) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) TemporaryFolder(org.junit.rules.TemporaryFolder) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) RawBlockIterator(org.hyperledger.besu.ethereum.util.RawBlockIterator)

Example 5 with RawBlockIterator

use of org.hyperledger.besu.ethereum.util.RawBlockIterator in project besu by hyperledger.

the class RlpBlockImporter method importBlockchain.

public RlpBlockImporter.ImportResult importBlockchain(final Path blocks, final BesuController besuController, final boolean skipPowValidation, final long startBlock, final long endBlock) throws IOException {
    final ProtocolSchedule protocolSchedule = besuController.getProtocolSchedule();
    final ProtocolContext context = besuController.getProtocolContext();
    final MutableBlockchain blockchain = context.getBlockchain();
    int count = 0;
    try (final RawBlockIterator iterator = new RawBlockIterator(blocks, rlp -> BlockHeader.readFrom(rlp, ScheduleBasedBlockHeaderFunctions.create(protocolSchedule)))) {
        BlockHeader previousHeader = null;
        CompletableFuture<Void> previousBlockFuture = null;
        final AtomicReference<Throwable> threadedException = new AtomicReference<>();
        while (iterator.hasNext()) {
            final Block block = iterator.next();
            final BlockHeader header = block.getHeader();
            final long blockNumber = header.getNumber();
            if (blockNumber == BlockHeader.GENESIS_BLOCK_NUMBER || blockNumber < startBlock || blockNumber >= endBlock) {
                continue;
            }
            if (blockchain.contains(header.getHash())) {
                continue;
            }
            if (previousHeader == null) {
                previousHeader = lookupPreviousHeader(blockchain, header);
            }
            final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(blockNumber);
            final BlockHeader lastHeader = previousHeader;
            final CompletableFuture<Void> validationFuture = CompletableFuture.runAsync(() -> validateBlock(protocolSpec, context, lastHeader, header, skipPowValidation), validationExecutor);
            final CompletableFuture<Void> extractingFuture = CompletableFuture.runAsync(() -> extractSignatures(block));
            final CompletableFuture<Void> calculationFutures;
            if (previousBlockFuture == null) {
                calculationFutures = extractingFuture;
            } else {
                calculationFutures = CompletableFuture.allOf(extractingFuture, previousBlockFuture);
            }
            try {
                do {
                    final Throwable t = (Exception) threadedException.get();
                    if (t != null) {
                        throw new RuntimeException("Error importing block " + header.getNumber(), t);
                    }
                } while (!blockBacklog.tryAcquire(1, SECONDS));
            } catch (final InterruptedException e) {
                LOG.error("Interrupted adding to backlog.", e);
                break;
            }
            previousBlockFuture = validationFuture.runAfterBothAsync(calculationFutures, () -> evaluateBlock(context, block, header, protocolSpec, skipPowValidation), importExecutor);
            previousBlockFuture.exceptionally(exception -> {
                threadedException.set(exception);
                return null;
            });
            ++count;
            previousHeader = header;
        }
        if (previousBlockFuture != null) {
            previousBlockFuture.join();
        }
        logProgress(blockchain.getChainHeadBlockNumber());
        return new RlpBlockImporter.ImportResult(blockchain.getChainHead().getTotalDifficulty(), count);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) IOException(java.io.IOException) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) RawBlockIterator(org.hyperledger.besu.ethereum.util.RawBlockIterator) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Aggregations

RawBlockIterator (org.hyperledger.besu.ethereum.util.RawBlockIterator)9 GenesisConfigFile (org.hyperledger.besu.config.GenesisConfigFile)6 Block (org.hyperledger.besu.ethereum.core.Block)6 File (java.io.File)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)2 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)2 MainnetBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)2 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)2 TemporaryFolder (org.junit.rules.TemporaryFolder)2 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 GenesisState (org.hyperledger.besu.ethereum.chain.GenesisState)1 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)1 InMemoryKeyValueStorageProvider.createBonsaiInMemoryWorldStateArchive (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createBonsaiInMemoryWorldStateArchive)1 InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive)1