Search in sources :

Example 1 with EthPeers

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);
}
Also used : EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) Transaction(org.hyperledger.besu.ethereum.core.Transaction) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) Hash(org.hyperledger.besu.datatypes.Hash) Test(org.junit.Test)

Example 2 with EthPeers

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);
    }
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection) StatusMessage(org.hyperledger.besu.ethereum.eth.messages.StatusMessage) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) PeerSendHandler(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) EthProtocol(org.hyperledger.besu.ethereum.eth.EthProtocol) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) HashSet(java.util.HashSet) GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) Lists(com.google.common.collect.Lists) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) BigInteger(java.math.BigInteger) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) Set(java.util.Set) Test(org.junit.Test) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) List(java.util.List) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) Collections(java.util.Collections) TestClock(org.hyperledger.besu.testutil.TestClock) DefaultMessage(org.hyperledger.besu.ethereum.p2p.rlpx.wire.DefaultMessage) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) MockPeerConnection(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) MessageData(org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) CompletableFuture(java.util.concurrent.CompletableFuture) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) BigInteger(java.math.BigInteger) List(java.util.List) GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) BlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.BlockHeadersMessage) PeerSendHandler(org.hyperledger.besu.ethereum.eth.manager.MockPeerConnection.PeerSendHandler) Test(org.junit.Test)

Example 3 with EthPeers

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);
        }
    });
}
Also used : Logger(org.slf4j.Logger) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) LoggerFactory(org.slf4j.LoggerFactory) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers)

Example 4 with EthPeers

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));
}
Also used : EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Before(org.junit.Before)

Example 5 with EthPeers

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());
}
Also used : EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthMessages(org.hyperledger.besu.ethereum.eth.manager.EthMessages) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) ForkIdManager(org.hyperledger.besu.ethereum.eth.manager.ForkIdManager) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) SyncState(org.hyperledger.besu.ethereum.eth.sync.state.SyncState) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) Test(org.junit.Test)

Aggregations

EthPeers (org.hyperledger.besu.ethereum.eth.manager.EthPeers)16 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)15 EthScheduler (org.hyperledger.besu.ethereum.eth.manager.EthScheduler)9 EthMessages (org.hyperledger.besu.ethereum.eth.manager.EthMessages)7 Test (org.junit.Test)7 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)6 TransactionPool (org.hyperledger.besu.ethereum.eth.transactions.TransactionPool)5 Block (org.hyperledger.besu.ethereum.core.Block)4 EthPeer (org.hyperledger.besu.ethereum.eth.manager.EthPeer)4 SyncState (org.hyperledger.besu.ethereum.eth.sync.state.SyncState)4 NoOpMetricsSystem (org.hyperledger.besu.metrics.noop.NoOpMetricsSystem)4 FilterManagerBuilder (org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManagerBuilder)3 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)3 EthProtocolManager (org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager)3 GasPricePendingTransactionsSorter (org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter)3 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)3 ArrayList (java.util.ArrayList)2 Supplier (java.util.function.Supplier)2 EthGetFilterChanges (org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.EthGetFilterChanges)2 EthProtocolConfiguration (org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration)2