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);
}
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);
}
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);
}
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();
});
}
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());
}
});
});
}
Aggregations