use of org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster 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;
}
use of org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster 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());
}
use of org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster 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);
}
use of org.hyperledger.besu.consensus.common.bft.UniqueMessageMulticaster 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;
}
Aggregations