Search in sources :

Example 1 with IbftGossip

use of org.hyperledger.besu.consensus.ibft.IbftGossip in project besu by hyperledger.

the class TestContextBuilder method build.

public TestContext build() {
    final NetworkLayout networkNodes = NetworkLayout.createNetworkLayout(validatorCount, indexOfFirstLocallyProposedBlock);
    final Block genesisBlock = createGenesisBlock(networkNodes.getValidatorAddresses());
    final MutableBlockchain blockChain = createInMemoryBlockchain(genesisBlock, BftBlockHeaderFunctions.forOnchainBlock(IBFT_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 IbftGossip(uniqueMulticaster) : mock(Gossiper.class);
    final StubbedSynchronizerUpdater synchronizerUpdater = new StubbedSynchronizerUpdater();
    final ControllerAndState controllerAndState = createControllerAndFinalState(blockChain, multicaster, networkNodes.getLocalNode().getNodeKey(), clock, bftEventQueue, gossiper, synchronizerUpdater, bftForks);
    // 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.getIbftExecutors(), controllerAndState.getEventHandler(), controllerAndState.getFinalState(), controllerAndState.getEventMultiplexer(), controllerAndState.getMessageFactory());
}
Also used : BftFinalState(org.hyperledger.besu.consensus.common.bft.statemachine.BftFinalState) BftEventQueue(org.hyperledger.besu.consensus.common.bft.BftEventQueue) Subscribers(org.hyperledger.besu.util.Subscribers) BftBlockHeaderFunctions(org.hyperledger.besu.consensus.common.bft.BftBlockHeaderFunctions) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) IbftBlockHeightManagerFactory(org.hyperledger.besu.consensus.ibft.statemachine.IbftBlockHeightManagerFactory) BftContext(org.hyperledger.besu.consensus.common.bft.BftContext) StubbedSynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.inttest.StubbedSynchronizerUpdater) ProposerSelector(org.hyperledger.besu.consensus.common.bft.blockcreation.ProposerSelector) UniqueMessageMulticaster(org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster) MessageValidatorFactory(org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory) Map(java.util.Map) BlockTimer(org.hyperledger.besu.consensus.common.bft.BlockTimer) Block(org.hyperledger.besu.ethereum.core.Block) BftFork(org.hyperledger.besu.config.BftFork) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) RoundTimer(org.hyperledger.besu.consensus.common.bft.RoundTimer) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) BftEventHandler(org.hyperledger.besu.consensus.common.bft.statemachine.BftEventHandler) MessageTracker(org.hyperledger.besu.consensus.common.bft.MessageTracker) Set(java.util.Set) Instant(java.time.Instant) EventMultiplexer(org.hyperledger.besu.consensus.common.bft.EventMultiplexer) BlockValidatorProvider(org.hyperledger.besu.consensus.common.validator.blockbased.BlockValidatorProvider) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) EvmConfiguration(org.hyperledger.besu.evm.internal.EvmConfiguration) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) IbftController(org.hyperledger.besu.consensus.ibft.statemachine.IbftController) BftExecutors(org.hyperledger.besu.consensus.common.bft.BftExecutors) List(java.util.List) TransactionPoolConfiguration(org.hyperledger.besu.ethereum.eth.transactions.TransactionPoolConfiguration) NodeParams(org.hyperledger.besu.consensus.common.bft.inttest.NodeParams) 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) Mockito.mock(org.mockito.Mockito.mock) NodeKey(org.hyperledger.besu.crypto.NodeKey) Hash(org.hyperledger.besu.datatypes.Hash) MinedBlockObserver(org.hyperledger.besu.ethereum.chain.MinedBlockObserver) IbftProtocolSchedule(org.hyperledger.besu.consensus.ibft.IbftProtocolSchedule) Iterables(com.google.common.collect.Iterables) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) BftBlockCreatorFactory(org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreatorFactory) IbftRoundFactory(org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Bytes(org.apache.tuweni.bytes.Bytes) Address(org.hyperledger.besu.datatypes.Address) Util(org.hyperledger.besu.ethereum.core.Util) ArrayList(java.util.ArrayList) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) LinkedHashMap(java.util.LinkedHashMap) InMemoryKeyValueStorageProvider.createInMemoryBlockchain(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain) FutureMessageBuffer(org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer) ForksSchedule(org.hyperledger.besu.consensus.common.ForksSchedule) NetworkLayout(org.hyperledger.besu.consensus.common.bft.inttest.NetworkLayout) IbftGossip(org.hyperledger.besu.consensus.ibft.IbftGossip) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) BftBlockInterface(org.hyperledger.besu.consensus.common.bft.BftBlockInterface) Wei(org.hyperledger.besu.datatypes.Wei) EpochManager(org.hyperledger.besu.consensus.common.EpochManager) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData) UTF_8(java.nio.charset.StandardCharsets.UTF_8) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) StubValidatorMulticaster(org.hyperledger.besu.consensus.common.bft.inttest.StubValidatorMulticaster) IbftForksSchedulesFactory(org.hyperledger.besu.consensus.ibft.IbftForksSchedulesFactory) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) AddressHelpers(org.hyperledger.besu.ethereum.core.AddressHelpers) TestTransitions(org.hyperledger.besu.consensus.common.bft.inttest.TestTransitions) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Gossiper(org.hyperledger.besu.consensus.common.bft.Gossiper) IbftExtraDataCodec(org.hyperledger.besu.consensus.ibft.IbftExtraDataCodec) BftConfigOptions(org.hyperledger.besu.config.BftConfigOptions) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) Clock(java.time.Clock) BftHelpers(org.hyperledger.besu.consensus.common.bft.BftHelpers) SynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.SynchronizerUpdater) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) StubValidatorMulticaster(org.hyperledger.besu.consensus.common.bft.inttest.StubValidatorMulticaster) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) Address(org.hyperledger.besu.datatypes.Address) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) ArrayList(java.util.ArrayList) NetworkLayout(org.hyperledger.besu.consensus.common.bft.inttest.NetworkLayout) UniqueMessageMulticaster(org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster) DefaultValidatorPeer(org.hyperledger.besu.consensus.common.bft.inttest.DefaultValidatorPeer) Gossiper(org.hyperledger.besu.consensus.common.bft.Gossiper) StubbedSynchronizerUpdater(org.hyperledger.besu.consensus.common.bft.inttest.StubbedSynchronizerUpdater) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) IbftGossip(org.hyperledger.besu.consensus.ibft.IbftGossip)

Example 2 with IbftGossip

use of org.hyperledger.besu.consensus.ibft.IbftGossip in project besu by hyperledger.

the class IbftBesuControllerBuilder 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.IBFT);
    final Address localAddress = Util.publicKeyToAddress(nodeKey.getPublicKey());
    final BftBlockCreatorFactory<?> blockCreatorFactory = new BftBlockCreatorFactory<>(transactionPool.getPendingTransactions(), protocolContext, protocolSchedule, forksSchedule, 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, IbftSubProtocol.NAME);
    final UniqueMessageMulticaster uniqueMessageMulticaster = new UniqueMessageMulticaster(peers, bftConfig.getGossipedHistoryLimit());
    final IbftGossip gossiper = new IbftGossip(uniqueMessageMulticaster);
    final BftFinalState finalState = new BftFinalState(validatorProvider, nodeKey, Util.publicKeyToAddress(nodeKey.getPublicKey()), proposerSelector, uniqueMessageMulticaster, new RoundTimer(bftEventQueue, bftConfig.getRequestTimeoutSeconds(), bftExecutors), new BlockTimer(bftEventQueue, forksSchedule, 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(bftConfig.getFutureMessagesMaxDistance(), bftConfig.getFutureMessagesLimit(), blockchain.getChainHeadBlockNumber());
    final MessageTracker duplicateMessageTracker = new MessageTracker(bftConfig.getDuplicateMessageLimit());
    final MessageFactory messageFactory = new MessageFactory(nodeKey);
    final BftEventHandler ibftController = new IbftController(blockchain, finalState, new IbftBlockHeightManagerFactory(finalState, new IbftRoundFactory(finalState, protocolContext, protocolSchedule, minedBlockObservers, messageValidatorFactory, messageFactory, bftExtraDataCodec().get()), messageValidatorFactory, messageFactory), gossiper, duplicateMessageTracker, futureMessageBuffer, new EthSynchronizerUpdater(ethProtocolManager.ethContext().getEthPeers()));
    final EventMultiplexer eventMultiplexer = new EventMultiplexer(ibftController);
    final BftProcessor bftProcessor = new BftProcessor(bftEventQueue, eventMultiplexer);
    final MiningCoordinator ibftMiningCoordinator = new BftMiningCoordinator(bftExecutors, ibftController, bftProcessor, blockCreatorFactory, blockchain, bftEventQueue);
    ibftMiningCoordinator.enable();
    return ibftMiningCoordinator;
}
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) BftBlockCreatorFactory(org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreatorFactory) 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) 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) ValidatorProvider(org.hyperledger.besu.consensus.common.validator.ValidatorProvider) MiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator) BftMiningCoordinator(org.hyperledger.besu.consensus.common.bft.blockcreation.BftMiningCoordinator) BftContext(org.hyperledger.besu.consensus.common.bft.BftContext) IbftController(org.hyperledger.besu.consensus.ibft.statemachine.IbftController) MessageFactory(org.hyperledger.besu.consensus.ibft.payload.MessageFactory) MessageValidatorFactory(org.hyperledger.besu.consensus.ibft.validation.MessageValidatorFactory) MessageTracker(org.hyperledger.besu.consensus.common.bft.MessageTracker) IbftRoundFactory(org.hyperledger.besu.consensus.ibft.statemachine.IbftRoundFactory) BftMiningCoordinator(org.hyperledger.besu.consensus.common.bft.blockcreation.BftMiningCoordinator) FutureMessageBuffer(org.hyperledger.besu.consensus.common.bft.statemachine.FutureMessageBuffer) IbftBlockHeightManagerFactory(org.hyperledger.besu.consensus.ibft.statemachine.IbftBlockHeightManagerFactory) BlockTimer(org.hyperledger.besu.consensus.common.bft.BlockTimer) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) IbftGossip(org.hyperledger.besu.consensus.ibft.IbftGossip)

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 RoundTimer (org.hyperledger.besu.consensus.common.bft.RoundTimer)2 UniqueMessageMulticaster (org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster)2 BftBlockCreatorFactory (org.hyperledger.besu.consensus.common.bft.blockcreation.BftBlockCreatorFactory)2 ProposerSelector (org.hyperledger.besu.consensus.common.bft.blockcreation.ProposerSelector)2 Iterables (com.google.common.collect.Iterables)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)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 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1