Search in sources :

Example 1 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class BackwardChain method merge.

public void merge(final BackwardChain historicalBackwardChain) {
    BlockHeader firstHeader = getFirstAncestorHeader().orElseThrow(() -> new BackwardSyncException("Cannot merge when syncing forward..."));
    Block historicalPivot = historicalBackwardChain.getPivot();
    Block pivot = getPivot();
    if (firstHeader.getParentHash().equals(historicalPivot.getHash())) {
        Collections.reverse(historicalBackwardChain.successors);
        this.ancestors.addAll(historicalBackwardChain.successors.stream().map(Block::getHeader).collect(Collectors.toList()));
        this.ancestors.addAll(historicalBackwardChain.ancestors);
        debugLambda(LOG, "Merged backward chain led by block {} into chain led by block {}, new backward chain starts at height {} and ends at height {}", () -> historicalPivot.getHash().toString().substring(0, 20), () -> pivot.getHash().toString().substring(0, 20), () -> pivot.getHeader().getNumber(), () -> getFirstAncestorHeader().orElseThrow().getNumber());
        trustedBlocks.putAll(historicalBackwardChain.trustedBlocks);
    } else {
        warnLambda(LOG, "Cannot merge previous historical run because headers of {} and {} do not equal. Ignoring previous run. Did someone lie to us?", () -> firstHeader.getHash().toString().substring(0, 20), () -> historicalPivot.getHash().toString().substring(0, 20));
    }
}
Also used : Block(org.hyperledger.besu.ethereum.core.Block) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 2 with Block

use of org.hyperledger.besu.ethereum.core.Block 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 3 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class RestoreState method restoreBlocks.

private void restoreBlocks() throws IOException {
    try (final RollingFileReader headerReader = new RollingFileReader(this::headerFileName, compressed);
        final RollingFileReader bodyReader = new RollingFileReader(this::bodyFileName, compressed);
        final RollingFileReader receiptReader = new RollingFileReader(this::receiptFileName, compressed)) {
        final MutableBlockchain blockchain = besuController.getProtocolContext().getBlockchain();
        // target block is "including" the target block, so LE test not LT.
        for (long i = 0; i <= targetBlock; i++) {
            if (i % 100000 == 0) {
                LOG.info("Loading chain data {} / {}", i, targetBlock);
            }
            final byte[] headerEntry = headerReader.readBytes();
            final byte[] bodyEntry = bodyReader.readBytes();
            final byte[] receiptEntry = receiptReader.readBytes();
            final BlockHeaderFunctions functions = new MainnetBlockHeaderFunctions();
            final BlockHeader header = BlockHeader.readFrom(new BytesValueRLPInput(Bytes.wrap(headerEntry), false, true), functions);
            final BlockBody body = BlockBody.readFrom(new BytesValueRLPInput(Bytes.wrap(bodyEntry), false, true), functions);
            final RLPInput receiptsRlp = new BytesValueRLPInput(Bytes.wrap(receiptEntry), false, true);
            final int receiptsCount = receiptsRlp.enterList();
            final List<TransactionReceipt> receipts = new ArrayList<>(receiptsCount);
            for (int j = 0; j < receiptsCount; j++) {
                receipts.add(TransactionReceipt.readFrom(receiptsRlp, true));
            }
            receiptsRlp.leaveList();
            blockchain.appendBlock(new Block(header, body), receipts);
        }
    }
    LOG.info("Chain data loaded");
}
Also used : RollingFileReader(org.hyperledger.besu.util.io.RollingFileReader) RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) ArrayList(java.util.ArrayList) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)

Example 4 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class ProposalPayloadValidatorTest method validationFailsWhenExpectedProposerDoesNotMatchPayloadsAuthor.

@Test
public void validationFailsWhenExpectedProposerDoesNotMatchPayloadsAuthor() {
    final ProposalPayloadValidator payloadValidator = new ProposalPayloadValidator(Address.fromHexString("0x1"), roundIdentifier, blockValidator, protocolContext, bftExtraDataCodec);
    final Block block = ProposedBlockHelpers.createProposalBlock(emptyList(), roundIdentifier);
    final Proposal proposal = messageFactory.createProposal(roundIdentifier, block, emptyList(), emptyList());
    assertThat(payloadValidator.validate(proposal.getSignedPayload())).isFalse();
    verifyNoMoreInteractions(blockValidator);
}
Also used : Block(org.hyperledger.besu.ethereum.core.Block) Proposal(org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal) Test(org.junit.Test)

Example 5 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class ProposalPayloadValidatorTest method validationFailsWhenMessageMismatchesExpectedHeight.

@Test
public void validationFailsWhenMessageMismatchesExpectedHeight() {
    final ProposalPayloadValidator payloadValidator = new ProposalPayloadValidator(expectedProposer, roundIdentifier, blockValidator, protocolContext, bftExtraDataCodec);
    final Block block = ProposedBlockHelpers.createProposalBlock(emptyList(), roundIdentifier);
    final Proposal proposal = messageFactory.createProposal(ConsensusRoundHelpers.createFrom(roundIdentifier, +1, 0), block, emptyList(), emptyList());
    assertThat(payloadValidator.validate(proposal.getSignedPayload())).isFalse();
    verifyNoMoreInteractions(blockValidator);
}
Also used : Block(org.hyperledger.besu.ethereum.core.Block) Proposal(org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal) Test(org.junit.Test)

Aggregations

Block (org.hyperledger.besu.ethereum.core.Block)425 Test (org.junit.Test)236 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)118 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)91 List (java.util.List)57 Hash (org.hyperledger.besu.datatypes.Hash)54 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)52 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)47 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)40 ArrayList (java.util.ArrayList)37 Test (org.junit.jupiter.api.Test)37 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)36 Transaction (org.hyperledger.besu.ethereum.core.Transaction)34 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)34 Optional (java.util.Optional)33 Bytes (org.apache.tuweni.bytes.Bytes)33 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)31 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)31 Address (org.hyperledger.besu.datatypes.Address)28 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)28