use of org.hyperledger.besu.ethereum.eth.manager.EthPeers in project besu by hyperledger.
the class PendingTransactionsSenderTest method testSendEth65PeersOnly.
@Test
public void testSendEth65PeersOnly() {
PeerPendingTransactionTracker peerPendingTransactionTracker = mock(PeerPendingTransactionTracker.class);
PendingTransactionsMessageSender pendingTransactionsMessageSender = mock(PendingTransactionsMessageSender.class);
EthContext ethContext = mock(EthContext.class);
EthScheduler ethScheduler = mock(EthScheduler.class);
when(ethContext.getScheduler()).thenReturn(ethScheduler);
PendingTransactionSender sender = new PendingTransactionSender(peerPendingTransactionTracker, pendingTransactionsMessageSender, ethContext);
EthPeer peer1 = mock(EthPeer.class);
EthPeer peer2 = mock(EthPeer.class);
Transaction tx = mock(Transaction.class);
Hash hash = Hash.wrap(Bytes32.random());
when(tx.getHash()).thenReturn(hash);
EthPeers ethPeers = mock(EthPeers.class);
when(ethContext.getEthPeers()).thenReturn(ethPeers);
when(ethPeers.streamAvailablePeers()).thenReturn(Arrays.asList(peer1, peer2).stream());
when(peerPendingTransactionTracker.isPeerSupported(peer1, EthProtocol.ETH65)).thenReturn(true);
when(peerPendingTransactionTracker.isPeerSupported(peer2, EthProtocol.ETH65)).thenReturn(false);
sender.onTransactionsAdded(Collections.singleton(tx));
verify(peerPendingTransactionTracker, times(1)).addToPeerSendQueue(peer1, hash);
verify(peerPendingTransactionTracker, never()).addToPeerSendQueue(peer2, hash);
}
use of org.hyperledger.besu.ethereum.eth.manager.EthPeers 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.EthPeers in project besu by hyperledger.
the class WaitForPeersTask method executeTask.
@Override
protected void executeTask() {
final EthPeers ethPeers = ethContext.getEthPeers();
if (ethPeers.peerCount() >= targetPeerCount) {
// We already hit our target
result.complete(null);
return;
}
LOG.info("Waiting for {} total peers to connect. {} peers currently connected.", targetPeerCount, ethPeers.peerCount());
// Listen for peer connections and complete task when we hit our target
peerListenerId = ethPeers.subscribeConnect((peer) -> {
final int peerCount = ethPeers.peerCount();
if (peerCount >= targetPeerCount) {
LOG.info("Complete: {} peers connected.", targetPeerCount);
// We hit our target
result.complete(null);
} else {
LOG.info("Waiting for {} total peers to connect. {} peers currently connected.", targetPeerCount, peerCount);
}
});
}
use of org.hyperledger.besu.ethereum.eth.manager.EthPeers in project besu by hyperledger.
the class TransactionPoolTest method setUp.
@Before
public void setUp() {
blockchain = executionContext.getBlockchain();
transactions = new GasPricePendingTransactionsSorter(TransactionPoolConfiguration.DEFAULT_TX_RETENTION_HOURS, MAX_TRANSACTIONS, TestClock.fixed(), metricsSystem, blockchain::getChainHeadHeader, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP);
when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);
when(protocolSpec.getTransactionValidator()).thenReturn(transactionValidator);
when(protocolSpec.getFeeMarket()).thenReturn(FeeMarket.legacy());
genesisBlockGasLimit = executionContext.getGenesis().getHeader().getGasLimit();
ethContext = mock(EthContext.class);
final EthScheduler ethScheduler = mock(EthScheduler.class);
syncTaskCapture = ArgumentCaptor.forClass(Runnable.class);
doNothing().when(ethScheduler).scheduleSyncWorkerTask(syncTaskCapture.capture());
when(ethContext.getScheduler()).thenReturn(ethScheduler);
ethPeers = mock(EthPeers.class);
when(ethContext.getEthPeers()).thenReturn(ethPeers);
peerTransactionTracker = new PeerTransactionTracker();
transactionBroadcaster = spy(new TransactionBroadcaster(ethContext, transactions, peerTransactionTracker, transactionsMessageSender, newPooledTransactionHashesMessageSender));
transactionPool = createTransactionPool();
blockchain.observeBlockAdded(transactionPool);
when(miningParameters.getMinTransactionGasPrice()).thenReturn(Wei.of(2));
}
use of org.hyperledger.besu.ethereum.eth.manager.EthPeers in project besu by hyperledger.
the class TransactionPoolFactoryTest method testDisconnect.
@Test
public void testDisconnect() {
final ProtocolSchedule schedule = mock(ProtocolSchedule.class);
final ProtocolContext context = mock(ProtocolContext.class);
final MutableBlockchain blockchain = mock(MutableBlockchain.class);
when(blockchain.getBlockByNumber(anyLong())).thenReturn(Optional.of(mock(Block.class)));
when(blockchain.getBlockHashByNumber(anyLong())).thenReturn(Optional.of(mock(Hash.class)));
when(context.getBlockchain()).thenReturn(blockchain);
final EthPeers ethPeers = new EthPeers("ETH", TestClock.fixed(), new NoOpMetricsSystem(), 25);
final EthContext ethContext = mock(EthContext.class);
when(ethContext.getEthMessages()).thenReturn(mock(EthMessages.class));
when(ethContext.getEthPeers()).thenReturn(ethPeers);
final EthScheduler ethScheduler = mock(EthScheduler.class);
when(ethContext.getScheduler()).thenReturn(ethScheduler);
final SyncState state = mock(SyncState.class);
final GasPricePendingTransactionsSorter pendingTransactions = mock(GasPricePendingTransactionsSorter.class);
final PeerTransactionTracker peerTransactionTracker = mock(PeerTransactionTracker.class);
final TransactionsMessageSender transactionsMessageSender = mock(TransactionsMessageSender.class);
doNothing().when(transactionsMessageSender).sendTransactionsToPeer(any(EthPeer.class));
final NewPooledTransactionHashesMessageSender newPooledTransactionHashesMessageSender = mock(NewPooledTransactionHashesMessageSender.class);
final TransactionPool pool = TransactionPoolFactory.createTransactionPool(schedule, context, ethContext, new NoOpMetricsSystem(), state, new MiningParameters.Builder().minTransactionGasPrice(Wei.ONE).build(), ImmutableTransactionPoolConfiguration.of(1, 1, 1, TransactionPoolConfiguration.DEFAULT_PRICE_BUMP, TransactionPoolConfiguration.ETH65_TRX_ANNOUNCED_BUFFERING_PERIOD, TransactionPoolConfiguration.DEFAULT_RPC_TX_FEE_CAP, TransactionPoolConfiguration.DEFAULT_STRICT_TX_REPLAY_PROTECTION_ENABLED), pendingTransactions, peerTransactionTracker, transactionsMessageSender, newPooledTransactionHashesMessageSender);
final EthProtocolManager ethProtocolManager = new EthProtocolManager(blockchain, BigInteger.ONE, mock(WorldStateArchive.class), pool, new EthProtocolConfiguration(5, 5, 5, 5, 5, false), ethPeers, mock(EthMessages.class), ethContext, Collections.emptyList(), true, mock(EthScheduler.class), mock(ForkIdManager.class));
final RespondingEthPeer ethPeer = RespondingEthPeer.builder().ethProtocolManager(ethProtocolManager).build();
assertThat(ethPeer.getEthPeer()).isNotNull();
assertThat(ethPeer.getEthPeer().isDisconnected()).isFalse();
ethPeer.disconnect(DisconnectMessage.DisconnectReason.CLIENT_QUITTING);
verify(peerTransactionTracker, times(1)).onDisconnect(ethPeer.getEthPeer());
}
Aggregations