Search in sources :

Example 1 with BlockImporter

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.");
    }
}
Also used : BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter)

Example 2 with BlockImporter

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());
        }
    }
}
Also used : BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter)

Example 3 with BlockImporter

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());
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter) BlockchainReferenceTestCaseSpec(org.hyperledger.besu.ethereum.referencetests.BlockchainReferenceTestCaseSpec) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode)

Example 4 with BlockImporter

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());
        }
    }
}
Also used : ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Block(org.hyperledger.besu.ethereum.core.Block) BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter)

Example 5 with BlockImporter

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);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Subscribers(org.hyperledger.besu.util.Subscribers) Function(java.util.function.Function) Mockito.verify(org.mockito.Mockito.verify) MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) Lists(com.google.common.collect.Lists) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) BigInteger(java.math.BigInteger) Block(org.hyperledger.besu.ethereum.core.Block) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Mockito.mock(org.mockito.Mockito.mock) BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter) MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) MutableProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.MutableProtocolSchedule) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) BlockImporter(org.hyperledger.besu.ethereum.core.BlockImporter) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Block(org.hyperledger.besu.ethereum.core.Block) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

BlockImporter (org.hyperledger.besu.ethereum.core.BlockImporter)17 Block (org.hyperledger.besu.ethereum.core.Block)11 ProtocolSpec (org.hyperledger.besu.ethereum.mainnet.ProtocolSpec)10 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)7 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)6 HeaderValidationMode (org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode)4 Test (org.junit.Test)4 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)3 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 Lists (com.google.common.collect.Lists)2 BigInteger (java.math.BigInteger)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 BlockValidator (org.hyperledger.besu.ethereum.BlockValidator)2 MinedBlockObserver (org.hyperledger.besu.ethereum.chain.MinedBlockObserver)2 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)2 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)2 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)2 Responder (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer.Responder)2 NewBlockHashesMessage (org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage)2