use of org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler in project besu by hyperledger.
the class Istanbul99ProtocolManagerTest method respondToEth65GetHeadersUsingIstanbul99.
@Test
public void respondToEth65GetHeadersUsingIstanbul99() throws ExecutionException, InterruptedException, TimeoutException {
final CompletableFuture<Void> done = new CompletableFuture<>();
final EthScheduler ethScheduler = new DeterministicEthScheduler(() -> false);
EthPeers peers = new EthPeers(Istanbul99Protocol.NAME, TestClock.fixed(), new NoOpMetricsSystem(), 25);
EthMessages messages = new EthMessages();
final BigInteger networkId = BigInteger.ONE;
try (final EthProtocolManager ethManager = new Istanbul99ProtocolManager(blockchain, networkId, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig(), peers, messages, new EthContext(peers, messages, ethScheduler), Collections.emptyList(), false, ethScheduler)) {
final long startBlock = blockchain.getChainHeadBlockNumber() + 1;
final int blockCount = 5;
final MessageData messageData = GetBlockHeadersMessage.create(startBlock, blockCount, 0, false);
final PeerSendHandler onSend = (cap, message, conn) -> {
if (message.getCode() == EthPV62.STATUS) {
// Ignore status message
return;
}
assertThat(message.getCode()).isEqualTo(EthPV62.BLOCK_HEADERS);
final BlockHeadersMessage headersMsg = BlockHeadersMessage.readFrom(message);
final List<BlockHeader> headers = Lists.newArrayList(headersMsg.getHeaders(protocolSchedule));
assertThat(headers.size()).isEqualTo(0);
done.complete(null);
};
final PeerConnection peer = setupPeer(ethManager, onSend);
ethManager.processMessage(Istanbul99Protocol.ISTANBUL99, new DefaultMessage(peer, messageData));
done.get(10, TimeUnit.SECONDS);
}
}
use of org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler in project besu by hyperledger.
the class FastWorldStateDownloaderTest method testCancellation.
@SuppressWarnings("unchecked")
private void testCancellation(final boolean shouldCancelFuture) {
final EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create();
// Prevent the persistence service from running
final MockExecutorService serviceExecutor = ((DeterministicEthScheduler) ethProtocolManager.ethContext().getScheduler()).mockServiceExecutor();
serviceExecutor.setAutoRun(false);
// Setup "remote" state
final WorldStateArchive remoteWorldStateArchive = createInMemoryWorldStateArchive();
final MutableWorldState remoteWorldState = remoteWorldStateArchive.getMutable();
// Generate accounts and save corresponding state root
dataGen.createRandomContractAccountsWithNonEmptyStorage(remoteWorldState, 20);
final Hash stateRoot = remoteWorldState.rootHash();
final BlockHeader header = dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader();
// Create some peers
final List<RespondingEthPeer> peers = Stream.generate(() -> EthProtocolManagerTestUtil.createPeer(ethProtocolManager, header.getNumber())).limit(5).collect(Collectors.toList());
final InMemoryTasksPriorityQueues<NodeDataRequest> taskCollection = new InMemoryTasksPriorityQueues<>();
final WorldStateStorage localStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
final WorldStateDownloader downloader = createDownloader(ethProtocolManager.ethContext(), localStorage, taskCollection);
final FastSyncState fastSyncState = new FastSyncState(header);
final CompletableFuture<Void> result = downloader.run(null, fastSyncState);
// Send a few responses
final RespondingEthPeer.Responder responder = RespondingEthPeer.blockchainResponder(mock(Blockchain.class), remoteWorldStateArchive);
for (int i = 0; i < 3; i++) {
for (final RespondingEthPeer peer : peers) {
peer.respond(responder);
}
giveOtherThreadsAGo();
}
// Sanity check
assertThat(result.isDone()).isFalse();
// Reset taskCollection so we can track interactions after the cancellation
reset(taskCollection);
if (shouldCancelFuture) {
result.cancel(true);
} else {
downloader.cancel();
assertThat(result).isCancelled();
}
// Send some more responses after cancelling
for (int i = 0; i < 3; i++) {
for (final RespondingEthPeer peer : peers) {
peer.respond(responder);
}
giveOtherThreadsAGo();
}
// Now allow the persistence service to run which should exit immediately
serviceExecutor.runPendingFutures();
verify(taskCollection, times(1)).clear();
verify(taskCollection, never()).remove();
verify(taskCollection, never()).add(any(NodeDataRequest.class));
// Target world state should not be available
assertThat(localStorage.isWorldStateAvailable(header.getStateRoot(), header.getHash())).isFalse();
}
use of org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler in project besu by hyperledger.
the class FastWorldStateDownloaderTest method canRecoverFromTimeouts.
@Test
public void canRecoverFromTimeouts() {
final DeterministicEthScheduler.TimeoutPolicy timeoutPolicy = DeterministicEthScheduler.TimeoutPolicy.timeoutXTimes(2);
final EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create(timeoutPolicy);
final MockExecutorService serviceExecutor = ((DeterministicEthScheduler) ethProtocolManager.ethContext().getScheduler()).mockServiceExecutor();
serviceExecutor.setAutoRun(false);
// Setup "remote" state
final WorldStateArchive remoteWorldStateArchive = createInMemoryWorldStateArchive();
final MutableWorldState remoteWorldState = remoteWorldStateArchive.getMutable();
// Generate accounts and save corresponding state root
final List<Account> accounts = dataGen.createRandomAccounts(remoteWorldState, 20);
final Hash stateRoot = remoteWorldState.rootHash();
// Sanity check
assertThat(stateRoot).isNotEqualTo(EMPTY_TRIE_ROOT);
final BlockHeader header = dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader();
// Create some peers
final List<RespondingEthPeer> peers = Stream.generate(() -> EthProtocolManagerTestUtil.createPeer(ethProtocolManager, header.getNumber())).limit(5).collect(Collectors.toList());
final InMemoryTasksPriorityQueues<NodeDataRequest> taskCollection = new InMemoryTasksPriorityQueues<>();
final WorldStateStorage localStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
final WorldStateDownloader downloader = createDownloader(ethProtocolManager.ethContext(), localStorage, taskCollection);
final FastSyncState fastSyncState = new FastSyncState(header);
final CompletableFuture<Void> result = downloader.run(null, fastSyncState);
serviceExecutor.runPendingFuturesInSeparateThreads(persistenceThread);
// Respond to node data requests
final RespondingEthPeer.Responder responder = RespondingEthPeer.blockchainResponder(mock(Blockchain.class), remoteWorldStateArchive);
respondUntilDone(peers, responder, result);
// Check that all expected account data was downloaded
final WorldStateArchive localWorldStateArchive = new DefaultWorldStateArchive(localStorage, createPreimageStorage());
final WorldState localWorldState = localWorldStateArchive.get(stateRoot, null).get();
assertThat(result).isDone();
assertAccountsMatch(localWorldState, accounts);
}
use of org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler in project besu by hyperledger.
the class AbstractMessageTaskTest method setupTest.
@Before
public void setupTest() {
peersDoTimeout = new AtomicBoolean(false);
peerCountToTimeout = new AtomicInteger(0);
ethPeers = spy(new EthPeers(EthProtocol.NAME, TestClock.fixed(), metricsSystem, 25));
final EthMessages ethMessages = new EthMessages();
final EthScheduler ethScheduler = new DeterministicEthScheduler(() -> peerCountToTimeout.getAndDecrement() > 0 || peersDoTimeout.get());
ethContext = new EthContext(ethPeers, ethMessages, ethScheduler);
final SyncState syncState = new SyncState(blockchain, ethContext.getEthPeers());
transactionPool = TransactionPoolFactory.createTransactionPool(protocolSchedule, protocolContext, ethContext, TestClock.fixed(), metricsSystem, syncState, new MiningParameters.Builder().minTransactionGasPrice(Wei.ONE).build(), TransactionPoolConfiguration.DEFAULT);
ethProtocolManager = EthProtocolManagerTestUtil.create(blockchain, ethScheduler, protocolContext.getWorldStateArchive(), transactionPool, EthProtocolConfiguration.defaultConfig(), ethPeers, ethMessages, ethContext);
}
Aggregations