Search in sources :

Example 1 with MutableBlockchain

use of org.hyperledger.besu.ethereum.chain.MutableBlockchain in project besu by hyperledger.

the class QbftBesuControllerBuilder method createMiningCoordinator.

@Override
protected MiningCoordinator createMiningCoordinator(final ProtocolSchedule protocolSchedule, final ProtocolContext protocolContext, final TransactionPool transactionPool, final MiningParameters miningParameters, final SyncState syncState, final EthProtocolManager ethProtocolManager) {
    final MutableBlockchain blockchain = protocolContext.getBlockchain();
    final BftExecutors bftExecutors = BftExecutors.create(metricsSystem, BftExecutors.ConsensusType.QBFT);
    final Address localAddress = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final BftBlockCreatorFactory<?> blockCreatorFactory = new QbftBlockCreatorFactory(transactionPool.getPendingTransactions(), protocolContext, protocolSchedule, qbftForksSchedule, miningParameters, localAddress, bftExtraDataCodec().get());
    final ValidatorProvider validatorProvider = protocolContext.getConsensusContext(BftContext.class).getValidatorProvider();
    final ProposerSelector proposerSelector = new ProposerSelector(blockchain, bftBlockInterface().get(), true, validatorProvider);
    // NOTE: peers should not be used for accessing the network as it does not enforce the
    // "only send once" filter applied by the UniqueMessageMulticaster.
    peers = new ValidatorPeers(validatorProvider, Istanbul100SubProtocol.NAME);
    final UniqueMessageMulticaster uniqueMessageMulticaster = new UniqueMessageMulticaster(peers, qbftConfig.getGossipedHistoryLimit());
    final QbftGossip gossiper = new QbftGossip(uniqueMessageMulticaster, bftExtraDataCodec().get());
    final BftFinalState finalState = new BftFinalState(validatorProvider, nodeKey, Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, uniqueMessageMulticaster, new RoundTimer(bftEventQueue, qbftConfig.getRequestTimeoutSeconds(), bftExecutors), new BlockTimer(bftEventQueue, qbftForksSchedule, bftExecutors, clock), blockCreatorFactory, clock);
    final MessageValidatorFactory messageValidatorFactory = new MessageValidatorFactory(proposerSelector, protocolSchedule, protocolContext, bftExtraDataCodec().get());
    final Subscribers<MinedBlockObserver> minedBlockObservers = Subscribers.create();
    minedBlockObservers.subscribe(ethProtocolManager);
    minedBlockObservers.subscribe(blockLogger(transactionPool, localAddress));
    final FutureMessageBuffer futureMessageBuffer = new FutureMessageBuffer(qbftConfig.getFutureMessagesMaxDistance(), qbftConfig.getFutureMessagesLimit(), blockchain.getChainHeadBlockNumber());
    final MessageTracker duplicateMessageTracker = new MessageTracker(qbftConfig.getDuplicateMessageLimit());
    final MessageFactory messageFactory = new MessageFactory(nodeKey);
    final BftEventHandler qbftController = new QbftController(blockchain, finalState, new QbftBlockHeightManagerFactory(finalState, new QbftRoundFactory(finalState, protocolContext, protocolSchedule, minedBlockObservers, messageValidatorFactory, messageFactory, bftExtraDataCodec().get()), messageValidatorFactory, messageFactory, new ValidatorModeTransitionLogger(qbftForksSchedule)), gossiper, duplicateMessageTracker, futureMessageBuffer, new EthSynchronizerUpdater(ethProtocolManager.ethContext().getEthPeers()), bftExtraDataCodec().get());
    final EventMultiplexer eventMultiplexer = new EventMultiplexer(qbftController);
    final BftProcessor bftProcessor = new BftProcessor(bftEventQueue, eventMultiplexer);
    final MiningCoordinator miningCoordinator = new BftMiningCoordinator(bftExecutors, qbftController, bftProcessor, blockCreatorFactory, blockchain, bftEventQueue);
    miningCoordinator.enable();
    return miningCoordinator;
}
Also used : ValidatorPeers(org.hyperledger.besu.consensus.common.bft.network.ValidatorPeers) MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) EthSynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.EthSynchronizerUpdater) Address(org.hyperledger.besu.datatypes.Address) ValidatorModeTransitionLogger(org.hyperledger.besu.consensus.qbft.validator.ValidatorModeTransitionLogger) BftProcessor(org.hyperledger.besu.consensus.common.bft.BftProcessor) RoundTimer(org.hyperledger.besu.consensus.common.bft.RoundTimer) BftEventHandler(org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler) QbftBlockCreatorFactory(org.hyperledger.besu.consensus.qbft.blockcreation.QbftBlockCreatorFactory) EventMultiplexer(org.hyperledger.besu.consensus.common.bft.EventMultiplexer) BftFinalState(org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState) ProposerSelector(org.hyperledger.besu.consensus.common.bft.blockcreation.ProposerSelector) UniqueMessageMulticaster(org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster) BftExecutors(org.hyperledger.besu.consensus.common.bft.BftExecutors) BlockValidatorProvider(org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider) TransactionValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) ForkingValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider) MiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator) BftMiningCoordinator(org.hyperledger.besu.consensus.common.bft.blockcreation.BftMiningCoordinator) BftContext(org.hyperledger.besu.consensus.common.bft.BftContext) MessageFactory(org.hyperledger.besu.consensus.qbft.payload.MessageFactory) MessageValidatorFactory(org.hyperledger.besu.consensus.qbft.validation.MessageValidatorFactory) MessageTracker(org.hyperledger.besu.consensus.common.bft.MessageTracker) QbftBlockHeightManagerFactory(org.hyperledger.besu.consensus.qbft.statemachine.QbftBlockHeightManagerFactory) QbftController(org.hyperledger.besu.consensus.qbft.statemachine.QbftController) BftMiningCoordinator(org.hyperledger.besu.consensus.common.bft.blockcreation.BftMiningCoordinator) FutureMessageBuffer(org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer) BlockTimer(org.hyperledger.besu.consensus.common.bft.BlockTimer) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) QbftGossip(org.hyperledger.besu.consensus.qbft.QbftGossip) QbftRoundFactory(org.hyperledger.besu.consensus.qbft.statemachine.QbftRoundFactory)

Example 2 with MutableBlockchain

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

use of org.hyperledger.besu.ethereum.chain.MutableBlockchain in project besu by hyperledger.

the class TransactionSmartContractPermissioningControllerTest method setupController.

private TransactionSmartContractPermissioningController setupController(final String resourceName, final String contractAddressString) throws IOException {
    final ProtocolSchedule protocolSchedule = ProtocolScheduleFixture.MAINNET;
    final String emptyContractFile = Resources.toString(this.getClass().getResource(resourceName), UTF_8);
    final GenesisState genesisState = GenesisState.fromConfig(GenesisConfigFile.fromConfig(emptyContractFile), protocolSchedule);
    final MutableBlockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
    final WorldStateArchive worldArchive = createInMemoryWorldStateArchive();
    genesisState.writeStateTo(worldArchive.getMutable());
    final TransactionSimulator ts = new TransactionSimulator(blockchain, worldArchive, protocolSchedule);
    final Address contractAddress = Address.fromHexString(contractAddressString);
    when(metricsSystem.createCounter(BesuMetricCategory.PERMISSIONING, "transaction_smart_contract_check_count", "Number of times the transaction smart contract permissioning provider has been checked")).thenReturn(checkCounter);
    when(metricsSystem.createCounter(BesuMetricCategory.PERMISSIONING, "transaction_smart_contract_check_count_permitted", "Number of times the transaction smart contract permissioning provider has been checked and returned permitted")).thenReturn(checkPermittedCounter);
    when(metricsSystem.createCounter(BesuMetricCategory.PERMISSIONING, "transaction_smart_contract_check_count_unpermitted", "Number of times the transaction smart contract permissioning provider has been checked and returned unpermitted")).thenReturn(checkUnpermittedCounter);
    return new TransactionSmartContractPermissioningController(contractAddress, ts, metricsSystem);
}
Also used : GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) Address(org.hyperledger.besu.datatypes.Address) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) TransactionSimulator(org.hyperledger.besu.ethereum.transaction.TransactionSimulator)

Example 4 with MutableBlockchain

use of org.hyperledger.besu.ethereum.chain.MutableBlockchain 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 5 with MutableBlockchain

use of org.hyperledger.besu.ethereum.chain.MutableBlockchain in project besu by hyperledger.

the class MergeCoordinator method updateForkChoice.

@Override
public ForkchoiceResult updateForkChoice(final BlockHeader newHead, final Hash finalizedBlockHash, final Hash safeBlockHash) {
    MutableBlockchain blockchain = protocolContext.getBlockchain();
    Optional<BlockHeader> currentFinalized = mergeContext.getFinalized();
    final Optional<BlockHeader> newFinalized = blockchain.getBlockHeader(finalizedBlockHash);
    final Optional<Hash> latestValid = getLatestValidAncestor(newHead);
    if (currentFinalized.isPresent() && newFinalized.isPresent() && !isDescendantOf(currentFinalized.get(), newFinalized.get())) {
        return ForkchoiceResult.withFailure(String.format("new finalized block %s is not a descendant of current finalized block %s", finalizedBlockHash, currentFinalized.get().getBlockHash()), latestValid);
    }
    // ensure new head is descendant of finalized
    Optional<String> descendantError = newFinalized.map(Optional::of).orElse(currentFinalized).filter(finalized -> !isDescendantOf(finalized, newHead)).map(finalized -> String.format("new head block %s is not a descendant of current finalized block %s", newHead.getBlockHash(), finalized.getBlockHash()));
    if (descendantError.isPresent()) {
        return ForkchoiceResult.withFailure(descendantError.get(), latestValid);
    }
    Optional<BlockHeader> parentOfNewHead = blockchain.getBlockHeader(newHead.getParentHash());
    if (parentOfNewHead.isPresent() && parentOfNewHead.get().getTimestamp() >= newHead.getTimestamp()) {
        return ForkchoiceResult.withFailure("new head timestamp not greater than parent", latestValid);
    }
    // set the new head
    blockchain.rewindToBlock(newHead.getHash());
    // set and persist the new finalized block if it is present
    newFinalized.ifPresent(blockHeader -> {
        blockchain.setFinalized(blockHeader.getHash());
        mergeContext.setFinalized(blockHeader);
    });
    blockchain.getBlockHeader(safeBlockHash).ifPresent(newSafeBlock -> {
        blockchain.setSafeBlock(safeBlockHash);
        mergeContext.setSafeBlock(newSafeBlock);
    });
    return ForkchoiceResult.withResult(newFinalized, Optional.of(newHead));
}
Also used : AbstractPendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.AbstractPendingTransactionsSorter) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) Address(org.hyperledger.besu.datatypes.Address) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractGasLimitSpecification(org.hyperledger.besu.ethereum.mainnet.AbstractGasLimitSpecification) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) Wei(org.hyperledger.besu.datatypes.Wei) Result(org.hyperledger.besu.ethereum.BlockValidator.Result) Block(org.hyperledger.besu.ethereum.core.Block) Bytes32(org.apache.tuweni.bytes.Bytes32) BackwardSyncContext(org.hyperledger.besu.ethereum.eth.sync.backwardsync.BackwardSyncContext) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Logger(org.slf4j.Logger) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) TransitionUtils.isTerminalProofOfWorkBlock(org.hyperledger.besu.consensus.merge.TransitionUtils.isTerminalProofOfWorkBlock) Slf4jLambdaHelper.debugLambda(org.hyperledger.besu.util.Slf4jLambdaHelper.debugLambda) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Hash(org.hyperledger.besu.datatypes.Hash)

Aggregations

MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)48 Block (org.hyperledger.besu.ethereum.core.Block)25 Test (org.junit.Test)18 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)17 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)16 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)16 WorldStateArchive (org.hyperledger.besu.ethereum.worldstate.WorldStateArchive)13 Address (org.hyperledger.besu.datatypes.Address)10 ArrayList (java.util.ArrayList)9 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)8 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)8 List (java.util.List)7 Hash (org.hyperledger.besu.datatypes.Hash)7 GenesisState (org.hyperledger.besu.ethereum.chain.GenesisState)7 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)7 InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive)7 MainnetBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)7 NoOpMetricsSystem (org.hyperledger.besu.metrics.noop.NoOpMetricsSystem)7 Path (java.nio.file.Path)6 Collections (java.util.Collections)6