Search in sources :

Example 1 with QbftGossip

use of org.hyperledger.besu.consensus.qbft.QbftGossip 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 QbftGossip

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

the class TestContextBuilder method build.

public TestContext build() {
    final NetworkLayout networkNodes;
    if (nodeParams.isEmpty()) {
        networkNodes = NetworkLayout.createNetworkLayout(validatorCount, indexOfFirstLocallyProposedBlock);
    } else {
        final TreeMap<Address, NodeParams> addressKeyMap = new TreeMap<>();
        for (NodeParams params : nodeParams) {
            addressKeyMap.put(params.getAddress(), params);
        }
        final NodeParams localNode = Iterables.get(addressKeyMap.values(), indexOfFirstLocallyProposedBlock);
        networkNodes = new NetworkLayout(localNode, addressKeyMap);
    }
    final MutableBlockchain blockChain;
    final DefaultWorldStateArchive worldStateArchive = createInMemoryWorldStateArchive();
    if (genesisFile.isPresent()) {
        try {
            final GenesisState genesisState = createGenesisBlock(genesisFile.get());
            blockChain = createInMemoryBlockchain(genesisState.getBlock(), BftBlockHeaderFunctions.forOnchainBlock(BFT_EXTRA_DATA_ENCODER));
            genesisState.writeStateTo(worldStateArchive.getMutable());
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    } else {
        final Block genesisBlock = createGenesisBlock(networkNodes.getValidatorAddresses());
        blockChain = createInMemoryBlockchain(genesisBlock, BftBlockHeaderFunctions.forOnchainBlock(BFT_EXTRA_DATA_ENCODER));
    }
    // Use a stubbed version of the multicaster, to prevent creating PeerConnections etc.
    final StubValidatorMulticaster multicaster = new StubValidatorMulticaster();
    final UniqueMessageMulticaster uniqueMulticaster = new UniqueMessageMulticaster(multicaster, GOSSIPED_HISTORY_LIMIT);
    final Gossiper gossiper = useGossip ? new QbftGossip(uniqueMulticaster, BFT_EXTRA_DATA_ENCODER) : mock(Gossiper.class);
    final StubbedSynchronizerUpdater synchronizerUpdater = new StubbedSynchronizerUpdater();
    final ControllerAndState controllerAndState = createControllerAndFinalState(blockChain, worldStateArchive, multicaster, networkNodes.getLocalNode().getNodeKey(), clock, bftEventQueue, gossiper, synchronizerUpdater, useValidatorContract, qbftForks);
    // Add each networkNode to the Multicaster (such that each can receive msgs from local node).
    // NOTE: the remotePeers needs to be ordered based on Address (as this is used to determine
    // the proposer order which must be managed in test).
    final Map<Address, ValidatorPeer> remotePeers = networkNodes.getRemotePeers().stream().collect(Collectors.toMap(NodeParams::getAddress, nodeParams -> new ValidatorPeer(nodeParams, new MessageFactory(nodeParams.getNodeKey()), controllerAndState.getEventMultiplexer()), (u, v) -> {
        throw new IllegalStateException(String.format("Duplicate key %s", u));
    }, LinkedHashMap::new));
    final List<DefaultValidatorPeer> peerCollection = new ArrayList<>(remotePeers.values());
    multicaster.addNetworkPeers(peerCollection);
    synchronizerUpdater.addNetworkPeers(peerCollection);
    return new TestContext(remotePeers, blockChain, controllerAndState.getBftExecutors(), controllerAndState.getEventHandler(), controllerAndState.getFinalState(), controllerAndState.getEventMultiplexer(), controllerAndState.getMessageFactory(), controllerAndState.getValidatorProvider(), BFT_EXTRA_DATA_ENCODER);
}
Also used : BftFinalState(org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState) QbftFork(org.hyperledger.besu.config.QbftFork) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) Subscribers(org.hyperledger.besu.util.Subscribers) BftBlockHeaderFunctions(org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions) BftContext(org.hyperledger.besu.consensus.common.bft.BftContext) ProposerSelector(org.hyperledger.besu.consensus.common.bft.blockcreation.ProposerSelector) UniqueMessageMulticaster(org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster) MessageValidatorFactory(org.hyperledger.besu.consensus.qbft.validation.MessageValidatorFactory) Map(java.util.Map) BlockTimer(org.hyperledger.besu.consensus.common.bft.BlockTimer) Block(org.hyperledger.besu.ethereum.core.Block) BftExtraDataCodec(org.hyperledger.besu.consensus.common.bft.BftExtraDataCodec) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Path(java.nio.file.Path) RoundTimer(org.hyperledger.besu.consensus.common.bft.RoundTimer) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BftEventHandler(org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler) QbftRoundFactory(org.hyperledger.besu.consensus.qbft.statemachine.QbftRoundFactory) Set(java.util.Set) JsonUtil(org.hyperledger.besu.config.JsonUtil) EventMultiplexer(org.hyperledger.besu.consensus.common.bft.EventMultiplexer) EvmConfiguration(org.hyperledger.besu.evm.internal.EvmConfiguration) ZoneId(java.time.ZoneId) BftExecutors(org.hyperledger.besu.consensus.common.bft.BftExecutors) TransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) MessageFactory(org.hyperledger.besu.consensus.qbft.payload.MessageFactory) ProtocolScheduleFixture(org.hyperledger.besu.ethereum.core.ProtocolScheduleFixture) ValidatorContractController(org.hyperledger.besu.consensus.qbft.validator.ValidatorContractController) ForkingValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.ForkingValidatorProvider) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) Mockito.mock(org.mockito.Mockito.mock) Hash(org.hyperledger.besu.datatypes.Hash) MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) Iterables(com.google.common.collect.Iterables) BftValidatorOverrides(org.hyperledger.besu.consensus.common.BftValidatorOverrides) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) BftBlockCreatorFactory(org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreatorFactory) Bytes(org.apache.tuweni.bytes.Bytes) ArrayList(java.util.ArrayList) QbftController(org.hyperledger.besu.consensus.qbft.statemachine.QbftController) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) LinkedHashMap(java.util.LinkedHashMap) FutureMessageBuffer(org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer) QbftProtocolSchedule(org.hyperledger.besu.consensus.qbft.QbftProtocolSchedule) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) NetworkLayout(org.hyperledger.besu.consensus.common.bft.inttest.NetworkLayout) BftBlockInterface(org.hyperledger.besu.consensus.common.bft.BftBlockInterface) Wei(org.hyperledger.besu.datatypes.Wei) TransactionSimulator(org.hyperledger.besu.ethereum.transaction.TransactionSimulator) QbftContext(org.hyperledger.besu.consensus.qbft.QbftContext) Files(java.nio.file.Files) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) AddressHelpers(org.hyperledger.besu.ethereum.core.AddressHelpers) IOException(java.io.IOException) MutableQbftConfigOptions(org.hyperledger.besu.consensus.qbft.MutableQbftConfigOptions) ValidatorModeTransitionLogger(org.hyperledger.besu.consensus.qbft.validator.ValidatorModeTransitionLogger) TestTransitions(org.hyperledger.besu.consensus.common.bft.inttest.TestTransitions) QbftExtraDataCodec(org.hyperledger.besu.consensus.qbft.QbftExtraDataCodec) QbftBlockCreatorFactory(org.hyperledger.besu.consensus.qbft.blockcreation.QbftBlockCreatorFactory) TreeMap(java.util.TreeMap) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) SynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.SynchronizerUpdater) QbftGossip(org.hyperledger.besu.consensus.qbft.QbftGossip) TestClock(org.hyperledger.besu.testutil.TestClock) BftEventQueue(org.hyperledger.besu.consensus.common.bft.BftEventQueue) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) StubbedSynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.inttest.StubbedSynchronizerUpdater) BftFork(org.hyperledger.besu.config.BftFork) MessageTracker(org.hyperledger.besu.consensus.common.bft.MessageTracker) Instant(java.time.Instant) QbftConfigOptions(org.hyperledger.besu.config.QbftConfigOptions) BlockValidatorProvider(org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) Collectors(java.util.stream.Collectors) JsonQbftConfigOptions(org.hyperledger.besu.config.JsonQbftConfigOptions) List(java.util.List) TransactionValidatorProvider(org.hyperledger.besu.consensus.qbft.validator.TransactionValidatorProvider) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) Optional(java.util.Optional) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) NodeKey(org.hyperledger.besu.crypto.NodeKey) HashMap(java.util.HashMap) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Address(org.hyperledger.besu.datatypes.Address) Util(org.hyperledger.besu.ethereum.core.Util) InMemoryKeyValueStorageProvider.createInMemoryBlockchain(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) QbftForksSchedulesFactory(org.hyperledger.besu.consensus.qbft.QbftForksSchedulesFactory) EpochManager(org.hyperledger.besu.consensus.common.EpochManager) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StubValidatorMulticaster(org.hyperledger.besu.consensus.common.bft.inttest.StubValidatorMulticaster) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Gossiper(org.hyperledger.besu.consensus.common.bft.Gossiper) QbftBlockHeightManagerFactory(org.hyperledger.besu.consensus.qbft.statemachine.QbftBlockHeightManagerFactory) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Clock(java.time.Clock) BftHelpers(org.hyperledger.besu.consensus.common.bft.BftHelpers) Collections(java.util.Collections) GenesisState(org.hyperledger.besu.ethereum.chain.GenesisState) Address(org.hyperledger.besu.datatypes.Address) ArrayList(java.util.ArrayList) UniqueMessageMulticaster(org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) StubbedSynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.inttest.StubbedSynchronizerUpdater) StubValidatorMulticaster(org.hyperledger.besu.consensus.common.bft.inttest.StubValidatorMulticaster) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) MessageFactory(org.hyperledger.besu.consensus.qbft.payload.MessageFactory) NetworkLayout(org.hyperledger.besu.consensus.common.bft.inttest.NetworkLayout) IOException(java.io.IOException) TreeMap(java.util.TreeMap) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) Gossiper(org.hyperledger.besu.consensus.common.bft.Gossiper) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) QbftGossip(org.hyperledger.besu.consensus.qbft.QbftGossip)

Aggregations

BftContext (org.hyperledger.besu.consensus.common.bft.BftContext)2 BftExecutors (org.hyperledger.besu.consensus.common.bft.BftExecutors)2 BlockTimer (org.hyperledger.besu.consensus.common.bft.BlockTimer)2 EventMultiplexer (org.hyperledger.besu.consensus.common.bft.EventMultiplexer)2 MessageTracker (org.hyperledger.besu.consensus.common.bft.MessageTracker)2 Iterables (com.google.common.collect.Iterables)1 IOException (java.io.IOException)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Clock (java.time.Clock)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1