use of org.hyperledger.besu.consensus.common.bft.BftEventQueue in project besu by hyperledger.
the class QbftBesuControllerBuilder method prepForBuild.
@Override
protected void prepForBuild() {
qbftConfig = configOptionsSupplier.get().getQbftConfigOptions();
bftEventQueue = new BftEventQueue(qbftConfig.getMessageQueueLimit());
qbftForksSchedule = QbftForksSchedulesFactory.create(configOptionsSupplier.get());
}
use of org.hyperledger.besu.consensus.common.bft.BftEventQueue 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.BftEventQueue in project besu by hyperledger.
the class TransitionsTest method transitionsBlockPeriod.
@Test
public void transitionsBlockPeriod() throws InterruptedException {
final TestClock clock = new TestClock(Instant.EPOCH);
final List<BftFork> bftForks = List.of(new BftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 1, BftFork.BLOCK_PERIOD_SECONDS_KEY, 10))), new BftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 2, BftFork.BLOCK_PERIOD_SECONDS_KEY, 20))));
final BftEventQueue bftEventQueue = new BftEventQueue(TestContextBuilder.MESSAGE_QUEUE_LIMIT);
final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(0).validatorCount(1).clock(clock).bftForks(bftForks).eventQueue(bftEventQueue).buildAndStart();
clock.stepMillis(10_000);
context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
context.getController().handleNewBlockEvent(new NewChainHead(context.getBlockchain().getChainHeadHeader()));
clock.stepMillis(20_000);
context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
final BlockHeader blockHeader1 = context.getBlockchain().getBlockHeader(1).get();
final BlockHeader blockHeader2 = context.getBlockchain().getBlockHeader(2).get();
assertThat(blockHeader1.getTimestamp()).isEqualTo(genesisBlock.getTimestamp() + 10);
assertThat(blockHeader2.getTimestamp()).isEqualTo(blockHeader1.getTimestamp() + 20);
}
use of org.hyperledger.besu.consensus.common.bft.BftEventQueue 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.BftEventQueue in project besu by hyperledger.
the class TransitionsTest method transitionsBlockPeriod.
@Test
public void transitionsBlockPeriod() throws InterruptedException {
final TestClock clock = new TestClock(Instant.EPOCH);
final List<QbftFork> qbftForks = List.of(new QbftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 1, BftFork.BLOCK_PERIOD_SECONDS_KEY, 10))), new QbftFork(JsonUtil.objectNodeFromMap(Map.of(BftFork.FORK_BLOCK_KEY, 2, BftFork.BLOCK_PERIOD_SECONDS_KEY, 20))));
final BftEventQueue bftEventQueue = new BftEventQueue(TestContextBuilder.MESSAGE_QUEUE_LIMIT);
final TestContext context = new TestContextBuilder().indexOfFirstLocallyProposedBlock(0).validatorCount(1).clock(clock).qbftForks(qbftForks).eventQueue(bftEventQueue).buildAndStart();
clock.stepMillis(10_000);
context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
context.getController().handleNewBlockEvent(new NewChainHead(context.getBlockchain().getChainHeadHeader()));
clock.stepMillis(20_000);
context.getEventMultiplexer().handleBftEvent(bftEventQueue.poll(1, TimeUnit.SECONDS));
final BlockHeader genesisBlock = context.getBlockchain().getBlockHeader(0).get();
final BlockHeader blockHeader1 = context.getBlockchain().getBlockHeader(1).get();
final BlockHeader blockHeader2 = context.getBlockchain().getBlockHeader(2).get();
assertThat(blockHeader1.getTimestamp()).isEqualTo(genesisBlock.getTimestamp() + 10);
assertThat(blockHeader2.getTimestamp()).isEqualTo(blockHeader1.getTimestamp() + 20);
}
Aggregations