Search in sources :

Example 1 with EthPeer

use of org.hyperledger.besu.ethereum.eth.manager.EthPeer 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 EthPeer

use of org.hyperledger.besu.ethereum.eth.manager.EthPeer in project besu by hyperledger.

the class EthSynchronizerUpdater method updatePeerChainState.

@Override
public void updatePeerChainState(final long knownBlockNumber, final PeerConnection peerConnection) {
    final EthPeer ethPeer = ethPeers.peer(peerConnection);
    if (ethPeer == null) {
        LOG.debug("Received message from a peer with no corresponding EthPeer.");
        return;
    }
    ethPeer.chainState().updateHeightEstimate(knownBlockNumber);
}
Also used : EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer)

Example 3 with EthPeer

use of org.hyperledger.besu.ethereum.eth.manager.EthPeer in project besu by hyperledger.

the class AdminPeersTest method peerList.

private Collection<EthPeer> peerList() {
    final PeerInfo peerInfo = new PeerInfo(5, "0x0", Collections.emptyList(), 30303, Bytes.fromHexString(VALID_NODE_ID));
    final PeerConnection p = MockPeerConnection.create(peerInfo, new InetSocketAddress("1.2.3.4", 9876), new InetSocketAddress("4.3.2.1", 6789));
    final EthPeer ethPeer = new EthPeer(p, "eth", c -> {
    }, List.of(), TestClock.fixed(), Collections.emptyList());
    return Lists.newArrayList(ethPeer);
}
Also used : MockPeerConnection(org.hyperledger.besu.ethereum.api.jsonrpc.MockPeerConnection) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) PeerInfo(org.hyperledger.besu.ethereum.p2p.rlpx.wire.PeerInfo) InetSocketAddress(java.net.InetSocketAddress)

Example 4 with EthPeer

use of org.hyperledger.besu.ethereum.eth.manager.EthPeer in project besu by hyperledger.

the class FastSyncTargetManager method confirmPivotBlockHeader.

private CompletableFuture<Optional<EthPeer>> confirmPivotBlockHeader(final EthPeer bestPeer) {
    final BlockHeader pivotBlockHeader = fastSyncState.getPivotBlockHeader().get();
    final RetryingGetHeaderFromPeerByNumberTask task = RetryingGetHeaderFromPeerByNumberTask.forSingleNumber(protocolSchedule, ethContext, metricsSystem, pivotBlockHeader.getNumber(), MAX_QUERY_RETRIES_PER_PEER);
    task.assignPeer(bestPeer);
    return ethContext.getScheduler().timeout(task).thenCompose(result -> {
        if (peerHasDifferentPivotBlock(result)) {
            if (!hasPivotChanged(pivotBlockHeader)) {
                // if the pivot block has not changed, then warn and disconnect this peer
                LOG.warn("Best peer has wrong pivot block (#{}) expecting {} but received {}.  Disconnect: {}", pivotBlockHeader.getNumber(), pivotBlockHeader.getHash(), result.size() == 1 ? result.get(0).getHash() : "invalid response", bestPeer);
                bestPeer.disconnect(DisconnectReason.USELESS_PEER);
                return CompletableFuture.completedFuture(Optional.<EthPeer>empty());
            }
            LOG.debug("Retrying best peer {} with new pivot block {}", bestPeer.getShortNodeId(), pivotBlockHeader.toLogString());
            return confirmPivotBlockHeader(bestPeer);
        } else {
            return CompletableFuture.completedFuture(Optional.of(bestPeer));
        }
    }).exceptionally(error -> {
        LOG.debug("Could not confirm best peer had pivot block", error);
        return Optional.empty();
    });
}
Also used : Logger(org.slf4j.Logger) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) SyncTargetManager(org.hyperledger.besu.ethereum.eth.sync.SyncTargetManager) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) LoggerFactory(org.slf4j.LoggerFactory) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) CompletableFuture(java.util.concurrent.CompletableFuture) SynchronizerConfiguration(org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) List(java.util.List) RetryingGetHeaderFromPeerByNumberTask(org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) MAX_QUERY_RETRIES_PER_PEER(org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotBlockRetriever.MAX_QUERY_RETRIES_PER_PEER) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) RetryingGetHeaderFromPeerByNumberTask(org.hyperledger.besu.ethereum.eth.sync.tasks.RetryingGetHeaderFromPeerByNumberTask) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 5 with EthPeer

use of org.hyperledger.besu.ethereum.eth.manager.EthPeer in project besu by hyperledger.

the class BlockPropagationManager method getBlockFromPeers.

private CompletableFuture<Block> getBlockFromPeers(final Optional<EthPeer> preferredPeer, final long blockNumber, final Optional<Hash> blockHash) {
    final RetryingGetBlockFromPeersTask getBlockTask = RetryingGetBlockFromPeersTask.create(protocolContext, protocolSchedule, ethContext, metricsSystem, ethContext.getEthPeers().getMaxPeers(), blockHash, blockNumber);
    preferredPeer.ifPresent(getBlockTask::assignPeer);
    return ethContext.getScheduler().scheduleSyncWorkerTask(getBlockTask::run).thenCompose(r -> importOrSavePendingBlock(r.getResult(), r.getPeer().nodeId())).whenComplete((r, t) -> {
        requestedNonAnnouncedBlocks.remove(blockNumber);
        blockHash.ifPresentOrElse(requestedBlocks::remove, () -> {
            if (r != null) {
                // in case we successfully retrieved only by block number, when can remove
                // the request by hash too
                requestedBlocks.remove(r.getHash());
            }
        });
    });
}
Also used : RLPException(org.hyperledger.besu.ethereum.rlp.RLPException) EthPV62(org.hyperledger.besu.ethereum.eth.messages.EthPV62) SyncState(org.hyperledger.besu.ethereum.eth.sync.state.SyncState) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RetryingGetBlockFromPeersTask(org.hyperledger.besu.ethereum.eth.manager.task.RetryingGetBlockFromPeersTask) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) PendingBlocksManager(org.hyperledger.besu.ethereum.eth.sync.state.PendingBlocksManager) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) Lists(com.google.common.collect.Lists) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) BlockAddedEvent(org.hyperledger.besu.ethereum.chain.BlockAddedEvent) Block(org.hyperledger.besu.ethereum.core.Block) EventType(org.hyperledger.besu.ethereum.chain.BlockAddedEvent.EventType) EthMessage(org.hyperledger.besu.ethereum.eth.manager.EthMessage) Slf4jLambdaHelper.traceLambda(org.hyperledger.besu.util.Slf4jLambdaHelper.traceLambda) ProcessableBlockHeader(org.hyperledger.besu.ethereum.core.ProcessableBlockHeader) DisconnectReason(org.hyperledger.besu.ethereum.p2p.rlpx.wire.messages.DisconnectMessage.DisconnectReason) PersistBlockTask(org.hyperledger.besu.ethereum.eth.sync.tasks.PersistBlockTask) NewBlockHash(org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage.NewBlockHash) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Logger(org.slf4j.Logger) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) NewBlockHashesMessage(org.hyperledger.besu.ethereum.eth.messages.NewBlockHashesMessage) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Collection(java.util.Collection) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) NewBlockMessage(org.hyperledger.besu.ethereum.eth.messages.NewBlockMessage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Range(com.google.common.collect.Range) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ProtocolSpec(org.hyperledger.besu.ethereum.mainnet.ProtocolSpec) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) RetryingGetBlockFromPeersTask(org.hyperledger.besu.ethereum.eth.manager.task.RetryingGetBlockFromPeersTask)

Aggregations

EthPeer (org.hyperledger.besu.ethereum.eth.manager.EthPeer)38 Test (org.junit.Test)24 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)13 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)6 EthPeers (org.hyperledger.besu.ethereum.eth.manager.EthPeers)6 EthProtocolManager (org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager)6 List (java.util.List)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Optional (java.util.Optional)4 Block (org.hyperledger.besu.ethereum.core.Block)4 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)4 ChainState (org.hyperledger.besu.ethereum.eth.manager.ChainState)4 ArrayList (java.util.ArrayList)3 Bytes (org.apache.tuweni.bytes.Bytes)3 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)3 BlockAddedEvent (org.hyperledger.besu.ethereum.chain.BlockAddedEvent)3 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)3 NewBlockMessage (org.hyperledger.besu.ethereum.eth.messages.NewBlockMessage)3 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)3