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