Search in sources :

Example 1 with QbftContext

use of org.hyperledger.besu.consensus.qbft.QbftContext in project besu by hyperledger.

the class QbftBesuControllerBuilder method createConsensusContext.

@Override
protected BftContext createConsensusContext(final Blockchain blockchain, final WorldStateArchive worldStateArchive, final ProtocolSchedule protocolSchedule) {
    final EpochManager epochManager = new EpochManager(qbftConfig.getEpochLength());
    final BftValidatorOverrides validatorOverrides = convertBftForks(configOptionsSupplier.get().getTransitions().getQbftForks());
    final BlockValidatorProvider blockValidatorProvider = BlockValidatorProvider.forkingValidatorProvider(blockchain, epochManager, bftBlockInterface().get(), validatorOverrides);
    final TransactionSimulator transactionSimulator = new TransactionSimulator(blockchain, worldStateArchive, protocolSchedule);
    transactionValidatorProvider = new TransactionValidatorProvider(blockchain, new ValidatorContractController(transactionSimulator), qbftForksSchedule);
    final ValidatorProvider validatorProvider = new ForkingValidatorProvider(blockchain, qbftForksSchedule, blockValidatorProvider, transactionValidatorProvider);
    return new QbftContext(validatorProvider, epochManager, bftBlockInterface().get(), pkiBlockCreationConfiguration);
}
Also used : ForkingValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider) QbftContext(org.hyperledger.besu.consensus.qbft.QbftContext) ValidatorContractController(org.hyperledger.besu.consensus.qbft.validator.ValidatorContractController) BftValidatorOverrides(org.hyperledger.besu.consensus.common.BftValidatorOverrides) TransactionSimulator(org.hyperledger.besu.ethereum.transaction.TransactionSimulator) EpochManager(org.hyperledger.besu.consensus.common.EpochManager) BlockValidatorProvider(org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider) TransactionValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider) 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)

Example 2 with QbftContext

use of org.hyperledger.besu.consensus.qbft.QbftContext in project besu by hyperledger.

the class TestContextBuilder method createControllerAndFinalState.

private static ControllerAndState createControllerAndFinalState(final MutableBlockchain blockChain, final WorldStateArchive worldStateArchive, final StubValidatorMulticaster multicaster, final NodeKey nodeKey, final Clock clock, final BftEventQueue bftEventQueue, final Gossiper gossiper, final SynchronizerUpdater synchronizerUpdater, final boolean useValidatorContract, final List<QbftFork> qbftForks) {
    final MiningParameters miningParams = new MiningParameters.Builder().coinbase(AddressHelpers.ofValue(1)).minTransactionGasPrice(Wei.ZERO).extraData(Bytes.wrap("Qbft Int tests".getBytes(UTF_8))).miningEnabled(true).build();
    final StubGenesisConfigOptions genesisConfigOptions = new StubGenesisConfigOptions();
    final Map<String, Object> qbftConfigValues = useValidatorContract ? Map.of(JsonQbftConfigOptions.VALIDATOR_CONTRACT_ADDRESS, VALIDATOR_CONTRACT_ADDRESS.toHexString()) : Collections.emptyMap();
    final QbftConfigOptions qbftConfigOptions = createGenesisConfig(useValidatorContract);
    genesisConfigOptions.byzantiumBlock(0);
    genesisConfigOptions.qbftConfigOptions(new JsonQbftConfigOptions(JsonUtil.objectNodeFromMap(qbftConfigValues)));
    genesisConfigOptions.transitions(TestTransitions.createQbftTestTransitions(qbftForks));
    genesisConfigOptions.qbftConfigOptions(qbftConfigOptions);
    final EpochManager epochManager = new EpochManager(EPOCH_LENGTH);
    final BftBlockInterface blockInterface = new BftBlockInterface(BFT_EXTRA_DATA_ENCODER);
    final ForksSchedule<QbftConfigOptions> forksSchedule = QbftForksSchedulesFactory.create(genesisConfigOptions);
    final ProtocolSchedule protocolSchedule = QbftProtocolSchedule.create(genesisConfigOptions, forksSchedule, BFT_EXTRA_DATA_ENCODER, EvmConfiguration.DEFAULT);
    final BftValidatorOverrides validatorOverrides = convertBftForks(qbftForks);
    final TransactionSimulator transactionSimulator = new TransactionSimulator(blockChain, worldStateArchive, protocolSchedule);
    final BlockValidatorProvider blockValidatorProvider = BlockValidatorProvider.forkingValidatorProvider(blockChain, epochManager, blockInterface, validatorOverrides);
    final TransactionValidatorProvider transactionValidatorProvider = new TransactionValidatorProvider(blockChain, new ValidatorContractController(transactionSimulator), forksSchedule);
    final ValidatorProvider validatorProvider = new ForkingValidatorProvider(blockChain, forksSchedule, blockValidatorProvider, transactionValidatorProvider);
    final ProtocolContext protocolContext = new ProtocolContext(blockChain, worldStateArchive, new QbftContext(validatorProvider, epochManager, blockInterface, Optional.empty()));
    final GasPricePendingTransactionsSorter pendingTransactions = new GasPricePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, 1, clock, metricsSystem, blockChain::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
    final Address localAddress = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final BftBlockCreatorFactory<?> blockCreatorFactory = new QbftBlockCreatorFactory(// changed from QbftBesuController
    pendingTransactions, protocolContext, protocolSchedule, forksSchedule, miningParams, localAddress, BFT_EXTRA_DATA_ENCODER);
    final ProposerSelector proposerSelector = new ProposerSelector(blockChain, blockInterface, true, validatorProvider);
    final BftExecutors bftExecutors = BftExecutors.create(new NoOpMetricsSystem(), BftExecutors.ConsensusType.QBFT);
    final BftFinalState finalState = new BftFinalState(protocolContext.getConsensusContext(BftContext.class).getValidatorProvider(), nodeKey, Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, multicaster, new RoundTimer(bftEventQueue, ROUND_TIMER_SEC, bftExecutors), new BlockTimer(bftEventQueue, forksSchedule, bftExecutors, TestClock.fixed()), blockCreatorFactory, clock);
    final MessageFactory messageFactory = new MessageFactory(nodeKey);
    final MessageValidatorFactory messageValidatorFactory = new MessageValidatorFactory(proposerSelector, protocolSchedule, protocolContext, BFT_EXTRA_DATA_ENCODER);
    final Subscribers<MinedBlockObserver> minedBlockObservers = Subscribers.create();
    final MessageTracker duplicateMessageTracker = new MessageTracker(DUPLICATE_MESSAGE_LIMIT);
    final FutureMessageBuffer futureMessageBuffer = new FutureMessageBuffer(FUTURE_MESSAGES_MAX_DISTANCE, FUTURE_MESSAGES_LIMIT, blockChain.getChainHeadBlockNumber());
    final BftEventHandler qbftController = new QbftController(blockChain, finalState, new QbftBlockHeightManagerFactory(finalState, new QbftRoundFactory(finalState, protocolContext, protocolSchedule, minedBlockObservers, messageValidatorFactory, messageFactory, BFT_EXTRA_DATA_ENCODER), messageValidatorFactory, messageFactory, new ValidatorModeTransitionLogger(forksSchedule)), gossiper, duplicateMessageTracker, futureMessageBuffer, synchronizerUpdater, BFT_EXTRA_DATA_ENCODER);
    final EventMultiplexer eventMultiplexer = new EventMultiplexer(qbftController);
    return new ControllerAndState(bftExecutors, qbftController, finalState, eventMultiplexer, messageFactory, validatorProvider);
}
Also used : BftBlockInterface(org.hyperledger.besu.consensus.common.bft.BftBlockInterface) MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) ForkingValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider) Address(org.hyperledger.besu.datatypes.Address) ValidatorModeTransitionLogger(org.hyperledger.besu.consensus.qbft.validator.ValidatorModeTransitionLogger) QbftContext(org.hyperledger.besu.consensus.qbft.QbftContext) RoundTimer(org.hyperledger.besu.consensus.common.bft.RoundTimer) BftEventHandler(org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) QbftBlockCreatorFactory(org.hyperledger.besu.consensus.qbft.blockcreation.QbftBlockCreatorFactory) EventMultiplexer(org.hyperledger.besu.consensus.common.bft.EventMultiplexer) BftValidatorOverrides(org.hyperledger.besu.consensus.common.BftValidatorOverrides) BftFinalState(org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState) ProposerSelector(org.hyperledger.besu.consensus.common.bft.blockcreation.ProposerSelector) TransactionValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider) ForkingValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider) 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) BftExecutors(org.hyperledger.besu.consensus.common.bft.BftExecutors) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) ValidatorContractController(org.hyperledger.besu.consensus.qbft.validator.ValidatorContractController) TransactionSimulator(org.hyperledger.besu.ethereum.transaction.TransactionSimulator) MessageFactory(org.hyperledger.besu.consensus.qbft.payload.MessageFactory) MessageValidatorFactory(org.hyperledger.besu.consensus.qbft.validation.MessageValidatorFactory) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) QbftProtocolSchedule(org.hyperledger.besu.consensus.qbft.QbftProtocolSchedule) MessageTracker(org.hyperledger.besu.consensus.common.bft.MessageTracker) BlockValidatorProvider(org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider) QbftBlockHeightManagerFactory(org.hyperledger.besu.consensus.qbft.statemachine.QbftBlockHeightManagerFactory) QbftController(org.hyperledger.besu.consensus.qbft.statemachine.QbftController) FutureMessageBuffer(org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) BlockTimer(org.hyperledger.besu.consensus.common.bft.BlockTimer) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) QbftConfigOptions(org.hyperledger.besu.config.QbftConfigOptions) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) EpochManager(org.hyperledger.besu.consensus.common.EpochManager) QbftRoundFactory(org.hyperledger.besu.consensus.qbft.statemachine.QbftRoundFactory)

Example 3 with QbftContext

use of org.hyperledger.besu.consensus.qbft.QbftContext in project besu by hyperledger.

the class QbftBlockCreatorFactory method create.

@Override
public BlockCreator create(final BlockHeader parentHeader, final int round) {
    final BlockCreator blockCreator = super.create(parentHeader, round);
    final QbftContext qbftContext = protocolContext.getConsensusContext(QbftContext.class);
    if (qbftContext.getPkiBlockCreationConfiguration().isEmpty()) {
        return blockCreator;
    } else {
        return new PkiQbftBlockCreator(blockCreator, qbftContext.getPkiBlockCreationConfiguration().get(), bftExtraDataCodec);
    }
}
Also used : BlockCreator(org.hyperledger.besu.ethereum.blockcreation.BlockCreator) QbftContext(org.hyperledger.besu.consensus.qbft.QbftContext)

Example 4 with QbftContext

use of org.hyperledger.besu.consensus.qbft.QbftContext in project besu by hyperledger.

the class ProposalPayloadValidatorTest method validationForCmsFailsWhenCmsFailsValidation.

@Test
public void validationForCmsFailsWhenCmsFailsValidation() {
    final PkiQbftExtraDataCodec pkiQbftExtraDataCodec = new PkiQbftExtraDataCodec();
    final QbftContext qbftContext = setupContextWithBftExtraDataEncoder(QbftContext.class, emptyList(), pkiQbftExtraDataCodec);
    final Bytes cms = Bytes.fromHexStringLenient("0x1");
    final ProtocolContext protocolContext = new ProtocolContext(blockChain, worldStateArchive, qbftContext);
    final ProposalPayloadValidator payloadValidator = new ProposalPayloadValidator(expectedProposer, roundIdentifier, blockValidator, protocolContext, pkiQbftExtraDataCodec, Optional.of(cmsValidator));
    final Block block = createPkiProposalBlock(emptyList(), roundIdentifier, pkiQbftExtraDataCodec, cms);
    final Proposal proposal = messageFactory.createProposal(roundIdentifier, block, emptyList(), emptyList());
    final Hash hashWithoutCms = PkiQbftBlockHeaderFunctions.forCmsSignature(pkiQbftExtraDataCodec).hash(block.getHeader());
    when(blockValidator.validateAndProcessBlock(eq(protocolContext), eq(block), eq(HeaderValidationMode.LIGHT), eq(HeaderValidationMode.FULL))).thenReturn(new Result(new BlockProcessingOutputs(null, null)));
    when(cmsValidator.validate(eq(cms), eq(hashWithoutCms))).thenReturn(false);
    assertThat(payloadValidator.validate(proposal.getSignedPayload())).isFalse();
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) BlockProcessingOutputs(org.hyperledger.besu.ethereum.BlockValidator.BlockProcessingOutputs) QbftContext(org.hyperledger.besu.consensus.qbft.QbftContext) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) Hash(org.hyperledger.besu.datatypes.Hash) Proposal(org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal) PkiQbftExtraDataCodec(org.hyperledger.besu.consensus.qbft.pki.PkiQbftExtraDataCodec) Result(org.hyperledger.besu.ethereum.BlockValidator.Result) Test(org.junit.Test)

Example 5 with QbftContext

use of org.hyperledger.besu.consensus.qbft.QbftContext in project besu by hyperledger.

the class ProposalPayloadValidatorTest method validationForCmsPassesWhenCmsIsValid.

@Test
public void validationForCmsPassesWhenCmsIsValid() {
    final PkiQbftExtraDataCodec pkiQbftExtraDataCodec = new PkiQbftExtraDataCodec();
    final QbftContext qbftContext = setupContextWithBftExtraDataEncoder(QbftContext.class, emptyList(), pkiQbftExtraDataCodec);
    final Bytes cms = Bytes.fromHexStringLenient("0x1");
    final ProtocolContext protocolContext = new ProtocolContext(blockChain, worldStateArchive, qbftContext);
    final ProposalPayloadValidator payloadValidator = new ProposalPayloadValidator(expectedProposer, roundIdentifier, blockValidator, protocolContext, pkiQbftExtraDataCodec, Optional.of(cmsValidator));
    final Block block = createPkiProposalBlock(emptyList(), roundIdentifier, pkiQbftExtraDataCodec, cms);
    final Proposal proposal = messageFactory.createProposal(roundIdentifier, block, emptyList(), emptyList());
    final Hash hashWithoutCms = PkiQbftBlockHeaderFunctions.forCmsSignature(pkiQbftExtraDataCodec).hash(block.getHeader());
    when(blockValidator.validateAndProcessBlock(eq(protocolContext), eq(block), eq(HeaderValidationMode.LIGHT), eq(HeaderValidationMode.FULL))).thenReturn(new Result(new BlockProcessingOutputs(null, null)));
    when(cmsValidator.validate(eq(cms), eq(hashWithoutCms))).thenReturn(true);
    assertThat(payloadValidator.validate(proposal.getSignedPayload())).isTrue();
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) BlockProcessingOutputs(org.hyperledger.besu.ethereum.BlockValidator.BlockProcessingOutputs) QbftContext(org.hyperledger.besu.consensus.qbft.QbftContext) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Block(org.hyperledger.besu.ethereum.core.Block) Hash(org.hyperledger.besu.datatypes.Hash) Proposal(org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal) PkiQbftExtraDataCodec(org.hyperledger.besu.consensus.qbft.pki.PkiQbftExtraDataCodec) Result(org.hyperledger.besu.ethereum.BlockValidator.Result) Test(org.junit.Test)

Aggregations

QbftContext (org.hyperledger.besu.consensus.qbft.QbftContext)5 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)3 Bytes (org.apache.tuweni.bytes.Bytes)2 BftValidatorOverrides (org.hyperledger.besu.consensus.common.BftValidatorOverrides)2 EpochManager (org.hyperledger.besu.consensus.common.EpochManager)2 ValidatorProvider (org.hyperledger.besu.consensus.common.validator.ValidatorProvider)2 BlockValidatorProvider (org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider)2 Proposal (org.hyperledger.besu.consensus.qbft.messagewrappers.Proposal)2 PkiQbftExtraDataCodec (org.hyperledger.besu.consensus.qbft.pki.PkiQbftExtraDataCodec)2 ForkingValidatorProvider (org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider)2 TransactionValidatorProvider (org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider)2 ValidatorContractController (org.hyperledger.besu.consensus.qbft.validator.ValidatorContractController)2 Hash (org.hyperledger.besu.datatypes.Hash)2 BlockProcessingOutputs (org.hyperledger.besu.ethereum.BlockValidator.BlockProcessingOutputs)2 Result (org.hyperledger.besu.ethereum.BlockValidator.Result)2 Block (org.hyperledger.besu.ethereum.core.Block)2 Test (org.junit.Test)2 JsonQbftConfigOptions (org.hyperledger.besu.config.JsonQbftConfigOptions)1 QbftConfigOptions (org.hyperledger.besu.config.QbftConfigOptions)1 StubGenesisConfigOptions (org.hyperledger.besu.config.StubGenesisConfigOptions)1