Search in sources :

Example 1 with BlockData

use of org.hyperledger.besu.chainimport.internal.BlockData in project besu by hyperledger.

the class JsonBlockImporter method getParentHeader.

private BlockHeader getParentHeader(final BlockData blockData, final List<Block> importedBlocks) {
    if (blockData.getParentHash().isPresent()) {
        final Hash parentHash = blockData.getParentHash().get();
        return controller.getProtocolContext().getBlockchain().getBlockHeader(parentHash).orElseThrow(() -> new IllegalArgumentException("Unable to locate block parent at " + parentHash));
    }
    if (importedBlocks.size() > 0 && blockData.getNumber().isPresent()) {
        final long targetParentBlockNumber = blockData.getNumber().get() - 1L;
        Optional<BlockHeader> maybeHeader = importedBlocks.stream().map(Block::getHeader).filter(h -> h.getNumber() == targetParentBlockNumber).findFirst();
        if (maybeHeader.isPresent()) {
            return maybeHeader.get();
        }
    }
    long blockNumber;
    if (blockData.getNumber().isPresent()) {
        blockNumber = blockData.getNumber().get() - 1L;
    } else if (importedBlocks.size() > 0) {
        // If there is no number or hash, import blocks in order
        blockNumber = importedBlocks.get(importedBlocks.size() - 1).getHeader().getNumber();
    } else {
        blockNumber = BlockHeader.GENESIS_BLOCK_NUMBER;
    }
    if (blockNumber < BlockHeader.GENESIS_BLOCK_NUMBER) {
        throw new IllegalArgumentException("Invalid block number: " + blockNumber + 1);
    }
    return controller.getProtocolContext().getBlockchain().getBlockHeader(blockNumber).orElseThrow(() -> new IllegalArgumentException("Unable to locate block parent at " + blockNumber));
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) PowAlgorithm(org.hyperledger.besu.config.PowAlgorithm) GenesisConfigOptions(org.hyperledger.besu.config.GenesisConfigOptions) Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) BesuController(org.hyperledger.besu.controller.BesuController) ChainData(org.hyperledger.besu.chainimport.internal.ChainData) ArrayList(java.util.ArrayList) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) MiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator) Block(org.hyperledger.besu.ethereum.core.Block) WorldState(org.hyperledger.besu.evm.worldstate.WorldState) BlockData(org.hyperledger.besu.chainimport.internal.BlockData) Logger(org.slf4j.Logger) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter) Block(org.hyperledger.besu.ethereum.core.Block) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 2 with BlockData

use of org.hyperledger.besu.chainimport.internal.BlockData in project besu by hyperledger.

the class JsonBlockImporter method importChain.

public void importChain(final String chainJson) throws IOException {
    warnIfDatabaseIsNotEmpty();
    final ChainData chainData = mapper.readValue(chainJson, ChainData.class);
    final List<Block> importedBlocks = new ArrayList<>();
    for (final BlockData blockData : chainData.getBlocks()) {
        final BlockHeader parentHeader = getParentHeader(blockData, importedBlocks);
        final Block importedBlock = processBlockData(blockData, parentHeader);
        importedBlocks.add(importedBlock);
    }
    this.warnIfImportedBlocksAreNotOnCanonicalChain(importedBlocks);
}
Also used : ChainData(org.hyperledger.besu.chainimport.internal.ChainData) ArrayList(java.util.ArrayList) Block(org.hyperledger.besu.ethereum.core.Block) BlockData(org.hyperledger.besu.chainimport.internal.BlockData) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Aggregations

ArrayList (java.util.ArrayList)2 BlockData (org.hyperledger.besu.chainimport.internal.BlockData)2 ChainData (org.hyperledger.besu.chainimport.internal.ChainData)2 Block (org.hyperledger.besu.ethereum.core.Block)2 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)2 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Bytes (org.apache.tuweni.bytes.Bytes)1 GenesisConfigOptions (org.hyperledger.besu.config.GenesisConfigOptions)1 PowAlgorithm (org.hyperledger.besu.config.PowAlgorithm)1 BesuController (org.hyperledger.besu.controller.BesuController)1 Address (org.hyperledger.besu.datatypes.Address)1 Hash (org.hyperledger.besu.datatypes.Hash)1