use of org.hyperledger.besu.ethereum.core.BlockImporter in project besu by hyperledger.
the class JsonBlockImporter method importBlock.
private void importBlock(final Block block) {
final BlockImporter importer = controller.getProtocolSchedule().getByBlockNumber(block.getHeader().getNumber()).getBlockImporter();
final boolean imported = importer.importBlock(controller.getProtocolContext(), block, HeaderValidationMode.NONE);
if (imported) {
LOG.info("Successfully created and imported block at height {} ({})", block.getHeader().getNumber(), block.getHash());
} else {
throw new IllegalStateException("Newly created block " + block.getHeader().getNumber() + " failed validation.");
}
}
use of org.hyperledger.besu.ethereum.core.BlockImporter in project besu by hyperledger.
the class RlpBlockImporter method evaluateBlock.
private void evaluateBlock(final ProtocolContext context, final Block block, final BlockHeader header, final ProtocolSpec protocolSpec, final boolean skipPowValidation) {
try {
cumulativeTimer.start();
segmentTimer.start();
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final boolean blockImported = blockImporter.importBlock(context, block, skipPowValidation ? HeaderValidationMode.LIGHT_SKIP_DETACHED : HeaderValidationMode.SKIP_DETACHED, skipPowValidation ? HeaderValidationMode.LIGHT : HeaderValidationMode.FULL);
if (!blockImported) {
throw new IllegalStateException("Invalid block at block number " + header.getNumber() + ".");
}
} finally {
blockBacklog.release();
cumulativeTimer.stop();
segmentTimer.stop();
final long thisGas = block.getHeader().getGasUsed();
cumulativeGas += thisGas;
segmentGas += thisGas;
if (header.getNumber() % SEGMENT_SIZE == 0) {
logProgress(header.getNumber());
}
}
}
use of org.hyperledger.besu.ethereum.core.BlockImporter in project besu by hyperledger.
the class BlockchainReferenceTestTools method executeTest.
public static void executeTest(final BlockchainReferenceTestCaseSpec spec) {
final BlockHeader genesisBlockHeader = spec.getGenesisBlockHeader();
final MutableWorldState worldState = spec.getWorldStateArchive().getMutable(genesisBlockHeader.getStateRoot(), genesisBlockHeader.getHash()).get();
assertThat(worldState.rootHash()).isEqualTo(genesisBlockHeader.getStateRoot());
final ProtocolSchedule schedule = REFERENCE_TEST_PROTOCOL_SCHEDULES.getByName(spec.getNetwork());
final MutableBlockchain blockchain = spec.getBlockchain();
final ProtocolContext context = spec.getProtocolContext();
for (final BlockchainReferenceTestCaseSpec.CandidateBlock candidateBlock : spec.getCandidateBlocks()) {
if (!candidateBlock.isExecutable()) {
return;
}
try {
final Block block = candidateBlock.getBlock();
final ProtocolSpec protocolSpec = schedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final HeaderValidationMode validationMode = "NoProof".equalsIgnoreCase(spec.getSealEngine()) ? HeaderValidationMode.LIGHT : HeaderValidationMode.FULL;
final boolean imported = blockImporter.importBlock(context, block, validationMode, validationMode);
assertThat(imported).isEqualTo(candidateBlock.isValid());
} catch (final RLPException e) {
assertThat(candidateBlock.isValid()).isFalse();
}
}
Assertions.assertThat(blockchain.getChainHeadHash()).isEqualTo(spec.getLastBlockHash());
}
use of org.hyperledger.besu.ethereum.core.BlockImporter in project besu by hyperledger.
the class RunnerTest method setupState.
private static void setupState(final int count, final ProtocolSchedule protocolSchedule, final ProtocolContext protocolContext) {
final List<Block> blocks = BlockSyncTestUtils.firstBlocks(count + 1);
for (int i = 1; i < count + 1; ++i) {
final Block block = blocks.get(i);
final ProtocolSpec protocolSpec = protocolSchedule.getByBlockNumber(block.getHeader().getNumber());
final BlockImporter blockImporter = protocolSpec.getBlockImporter();
final boolean result = blockImporter.importBlock(protocolContext, block, HeaderValidationMode.FULL);
if (!result) {
throw new IllegalStateException("Unable to import block " + block.getHeader().getNumber());
}
}
}
use of org.hyperledger.besu.ethereum.core.BlockImporter in project besu by hyperledger.
the class BlockMinerTest method blockCreatedIsAddedToBlockChain.
@Test
public void blockCreatedIsAddedToBlockChain() throws InterruptedException {
final BlockHeaderTestFixture headerBuilder = new BlockHeaderTestFixture();
final Block blockToCreate = new Block(headerBuilder.buildHeader(), new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
final ProtocolContext protocolContext = new ProtocolContext(null, null, null);
final PoWBlockCreator blockCreator = mock(PoWBlockCreator.class);
final Function<BlockHeader, PoWBlockCreator> blockCreatorSupplier = (parentHeader) -> blockCreator;
when(blockCreator.createBlock(anyLong())).thenReturn(blockToCreate);
final BlockImporter blockImporter = mock(BlockImporter.class);
final ProtocolSpec protocolSpec = mock(ProtocolSpec.class);
final ProtocolSchedule protocolSchedule = singleSpecSchedule(protocolSpec);
when(protocolSpec.getBlockImporter()).thenReturn(blockImporter);
when(blockImporter.importBlock(any(), any(), any())).thenReturn(true);
final MinedBlockObserver observer = mock(MinedBlockObserver.class);
final DefaultBlockScheduler scheduler = mock(DefaultBlockScheduler.class);
when(scheduler.waitUntilNextBlockCanBeMined(any())).thenReturn(5L);
final BlockMiner<PoWBlockCreator> miner = new PoWBlockMiner(blockCreatorSupplier, protocolSchedule, protocolContext, subscribersContaining(observer), scheduler, // parent header is arbitrary for the test.
headerBuilder.buildHeader());
miner.run();
verify(blockImporter).importBlock(protocolContext, blockToCreate, HeaderValidationMode.FULL);
verify(observer, times(1)).blockMined(blockToCreate);
}
Aggregations