use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldNotRespondToExpiredPingRequest.
@Test
public void shouldNotRespondToExpiredPingRequest() {
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 1);
final DiscoveryPeer discoPeer = peers.get(0);
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
controller = getControllerBuilder().peers(discoPeer).outboundMessageHandler(outboundMessageHandler).build();
final Endpoint localEndpoint = localPeer.getEndpoint();
// Setup ping to be sent to discoPeer
final List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
final PingPacketData pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), discoPeer.getEndpoint(), Instant.now().getEpochSecond() - PacketData.DEFAULT_EXPIRATION_PERIOD_SEC, UInt64.ONE);
final Packet discoPeerPing = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(discoPeer, discoPeerPing);
controller.onMessage(discoPeerPing, discoPeer);
verify(outboundMessageHandler, times(0)).send(eq(discoPeer), matchPacketOfType(PacketType.PONG));
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldNotRespondToNeighborsRequestFromDenylistedPeer.
@Test
public void shouldNotRespondToNeighborsRequestFromDenylistedPeer() {
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 1);
final DiscoveryPeer discoPeer = peers.get(0);
final PeerPermissionsDenylist denylist = PeerPermissionsDenylist.create();
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
controller = getControllerBuilder().peers(discoPeer).peerPermissions(denylist).outboundMessageHandler(outboundMessageHandler).build();
final Endpoint localEndpoint = localPeer.getEndpoint();
// Setup ping to be sent to discoPeer
final List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
final PingPacketData pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), discoPeer.getEndpoint(), UInt64.ONE);
final Packet discoPeerPing = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(discoPeer, discoPeerPing);
controller.start();
verify(outboundMessageHandler, times(1)).send(any(), matchPacketOfType(PacketType.PING));
final Packet pongFromDiscoPeer = MockPacketDataFactory.mockPongPacket(discoPeer, discoPeerPing.getHash());
controller.onMessage(pongFromDiscoPeer, discoPeer);
verify(outboundMessageHandler, times(1)).send(eq(discoPeer), matchPacketOfType(PacketType.FIND_NEIGHBORS));
denylist.add(discoPeer);
final Packet findNeighborsPacket = MockPacketDataFactory.mockFindNeighborsPacket(discoPeer);
controller.onMessage(findNeighborsPacket, discoPeer);
verify(outboundMessageHandler, times(0)).send(eq(discoPeer), matchPacketOfType(PacketType.NEIGHBORS));
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryTableRefreshTest method tableRefreshSingleNode.
@Test
public void tableRefreshSingleNode() {
final List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(2);
final List<DiscoveryPeer> peers = helper.createDiscoveryPeers(nodeKeys);
final DiscoveryPeer localPeer = peers.get(0);
final DiscoveryPeer remotePeer = peers.get(1);
final NodeKey localKeyPair = nodeKeys.get(0);
final NodeKey remoteKeyPair = nodeKeys.get(1);
// Create and start the PeerDiscoveryController
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
final MockTimerUtil timer = new MockTimerUtil();
final PeerDiscoveryController controller = spy(PeerDiscoveryController.builder().nodeKey(localKeyPair).localPeer(localPeer).peerTable(new PeerTable(localPeer.getId())).outboundMessageHandler(outboundMessageHandler).timerUtil(timer).workerExecutor(new BlockingAsyncExecutor()).tableRefreshIntervalMs(0).peerBondedObservers(Subscribers.create()).metricsSystem(new NoOpMetricsSystem()).build());
controller.start();
final PingPacketData mockPing = PingPacketData.create(Optional.ofNullable(localPeer.getEndpoint()), remotePeer.getEndpoint(), UInt64.ONE);
final Packet mockPingPacket = Packet.create(PacketType.PING, mockPing, localKeyPair);
doAnswer(invocation -> {
final Consumer<Packet> handler = invocation.getArgument(2);
handler.accept(mockPingPacket);
return null;
}).when(controller).createPacket(eq(PacketType.PING), any(), any());
// Send a PING, so as to add a Peer in the controller.
final PingPacketData ping = PingPacketData.create(Optional.ofNullable(remotePeer.getEndpoint()), localPeer.getEndpoint(), UInt64.ONE);
final Packet pingPacket = Packet.create(PacketType.PING, ping, remoteKeyPair);
controller.onMessage(pingPacket, remotePeer);
// Answer localPeer PING to complete bonding
final PongPacketData pong = PongPacketData.create(localPeer.getEndpoint(), mockPingPacket.getHash(), UInt64.ONE);
final Packet pongPacket = Packet.create(PacketType.PONG, pong, remoteKeyPair);
controller.onMessage(pongPacket, remotePeer);
// Wait until the controller has added the newly found peer.
assertThat(controller.streamDiscoveredPeers()).hasSize(1);
final ArgumentCaptor<Packet> captor = ArgumentCaptor.forClass(Packet.class);
for (int i = 0; i < 5; i++) {
controller.getRecursivePeerRefreshState().cancel();
timer.runPeriodicHandlers();
controller.streamDiscoveredPeers().forEach(p -> p.setStatus(PeerDiscoveryStatus.KNOWN));
controller.onMessage(pingPacket, remotePeer);
controller.onMessage(pongPacket, remotePeer);
}
verify(outboundMessageHandler, atLeast(5)).send(eq(remotePeer), captor.capture());
final List<Packet> capturedFindNeighborsPackets = captor.getAllValues().stream().filter(p -> p.getType().equals(PacketType.FIND_NEIGHBORS)).collect(Collectors.toList());
assertThat(capturedFindNeighborsPackets.size()).isEqualTo(5);
// Collect targets from find neighbors packets
final List<Bytes> targets = new ArrayList<>();
for (final Packet captured : capturedFindNeighborsPackets) {
final Optional<FindNeighborsPacketData> maybeData = captured.getPacketData(FindNeighborsPacketData.class);
Assertions.assertThat(maybeData).isPresent();
final FindNeighborsPacketData neighborsData = maybeData.get();
targets.add(neighborsData.getTarget());
}
// All targets are unique.
assertThat(targets.size()).isEqualTo(new HashSet<>(targets).size());
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldNotRespondToNeighborsRequestFromBlacklistedPeer.
@Test
public void shouldNotRespondToNeighborsRequestFromBlacklistedPeer() {
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 1);
final DiscoveryPeer discoPeer = peers.get(0);
final PeerPermissionsDenylist blacklist = PeerPermissionsDenylist.create();
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
controller = getControllerBuilder().peers(discoPeer).peerPermissions(blacklist).outboundMessageHandler(outboundMessageHandler).build();
final Endpoint localEndpoint = localPeer.getEndpoint();
// Setup ping to be sent to discoPeer
final List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
final PingPacketData pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), discoPeer.getEndpoint(), UInt64.ONE);
final Packet discoPeerPing = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(discoPeer, discoPeerPing);
controller.start();
verify(outboundMessageHandler, times(1)).send(any(), matchPacketOfType(PacketType.PING));
final Packet pongFromDiscoPeer = MockPacketDataFactory.mockPongPacket(discoPeer, discoPeerPing.getHash());
controller.onMessage(pongFromDiscoPeer, discoPeer);
verify(outboundMessageHandler, times(1)).send(eq(discoPeer), matchPacketOfType(PacketType.FIND_NEIGHBORS));
blacklist.add(discoPeer);
final Packet findNeighborsPacket = MockPacketDataFactory.mockFindNeighborsPacket(discoPeer);
controller.onMessage(findNeighborsPacket, discoPeer);
verify(outboundMessageHandler, times(0)).send(eq(discoPeer), matchPacketOfType(PacketType.NEIGHBORS));
}
Aggregations