use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class P2PPlainNetworkTest method rejectPeerWithNoSharedCaps.
@Test
public void rejectPeerWithNoSharedCaps() throws Exception {
final NodeKey listenerNodeKey = NodeKeyUtils.generate();
final NodeKey connectorNodeKey = NodeKeyUtils.generate();
final SubProtocol subprotocol1 = subProtocol("eth");
final Capability cap1 = Capability.create(subprotocol1.getName(), 63);
final SubProtocol subprotocol2 = subProtocol("oth");
final Capability cap2 = Capability.create(subprotocol2.getName(), 63);
try (final P2PNetwork listener = builder("partner1client1").nodeKey(listenerNodeKey).supportedCapabilities(cap1).build();
final P2PNetwork connector = builder("partner2client1").nodeKey(connectorNodeKey).supportedCapabilities(cap2).build()) {
listener.start();
connector.start();
final EnodeURL listenerEnode = listener.getLocalEnode().get();
final Bytes listenId = listenerEnode.getNodeId();
final int listenPort = listenerEnode.getListeningPort().get();
final Peer listenerPeer = createPeer(listenId, listenPort);
final CompletableFuture<PeerConnection> connectFuture = connector.connect(listenerPeer);
assertThatThrownBy(connectFuture::get).hasCauseInstanceOf(IncompatiblePeerException.class);
}
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldStopRetryingInteractionWhenLimitIsReached.
@Test
public void shouldStopRetryingInteractionWhenLimitIsReached() {
// Create peers.
final List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(3);
final List<DiscoveryPeer> peers = helper.createDiscoveryPeers(nodeKeys);
final MockTimerUtil timer = new MockTimerUtil();
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
controller = getControllerBuilder().peers(peers).timerUtil(timer).outboundMessageHandler(outboundMessageHandler).build();
// Mock the creation of the PING packet, so that we can control the hash,
// which gets validated when receiving the PONG.
final PingPacketData mockPing = PingPacketData.create(Optional.ofNullable(localPeer.getEndpoint()), peers.get(0).getEndpoint(), UInt64.ONE);
final Packet mockPacket = Packet.create(PacketType.PING, mockPing, nodeKeys.get(0));
mockPingPacketCreation(mockPacket);
controller.start();
// Invoke timers several times so that ping to peers should be resent
for (int i = 0; i < 10; i++) {
timer.runTimerHandlers();
}
// Assert PING packet was sent only 6 times (initial attempt plus 5 retries)
for (final DiscoveryPeer peer : peers) {
verify(outboundMessageHandler, times(6)).send(eq(peer), matchPacketOfType(PacketType.PING));
}
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldNotAddNewPeerWhenReceivedPongFromDenylistedPeer.
@Test
public void shouldNotAddNewPeerWhenReceivedPongFromDenylistedPeer() {
final List<DiscoveryPeer> peers = createPeersInLastBucket(localPeer, 3);
final DiscoveryPeer discoPeer = peers.get(0);
final DiscoveryPeer otherPeer = peers.get(1);
final DiscoveryPeer otherPeer2 = peers.get(2);
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
List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
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(eq(peers.get(0)), 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));
// Setup ping to be sent to otherPeer after neighbors packet is received
nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), otherPeer.getEndpoint(), UInt64.ONE);
final Packet pingPacket = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(otherPeer, pingPacket);
// Setup ping to be sent to otherPeer2 after neighbors packet is received
nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(1);
pingPacketData = PingPacketData.create(Optional.ofNullable(localEndpoint), otherPeer2.getEndpoint(), UInt64.ONE);
final Packet pingPacket2 = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(otherPeer2, pingPacket2);
final Packet neighborsPacket = MockPacketDataFactory.mockNeighborsPacket(discoPeer, otherPeer, otherPeer2);
controller.onMessage(neighborsPacket, discoPeer);
verify(outboundMessageHandler, times(peers.size())).send(any(), matchPacketOfType(PacketType.PING));
final Packet pongPacket = MockPacketDataFactory.mockPongPacket(otherPeer, pingPacket.getHash());
controller.onMessage(pongPacket, otherPeer);
// Denylist otherPeer2 before sending return pong
denylist.add(otherPeer2);
final Packet pongPacket2 = MockPacketDataFactory.mockPongPacket(otherPeer2, pingPacket2.getHash());
controller.onMessage(pongPacket2, otherPeer2);
assertThat(controller.streamDiscoveredPeers()).hasSize(2);
assertThat(controller.streamDiscoveredPeers()).contains(discoPeer);
assertThat(controller.streamDiscoveredPeers()).contains(otherPeer);
assertThat(controller.streamDiscoveredPeers()).doesNotContain(otherPeer2);
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryControllerTest method shouldRespondToPingRequest.
@Test
public void shouldRespondToPingRequest() {
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(), UInt64.ONE);
final Packet discoPeerPing = Packet.create(PacketType.PING, pingPacketData, nodeKeys.get(0));
mockPingPacketCreation(discoPeer, discoPeerPing);
controller.onMessage(discoPeerPing, discoPeer);
verify(outboundMessageHandler, times(1)).send(eq(discoPeer), matchPacketOfType(PacketType.PONG));
}
use of org.hyperledger.besu.crypto.NodeKey in project besu by hyperledger.
the class PeerDiscoveryControllerTest method bootstrapPeersRetriesStoppedUponResponse.
@Test
public void bootstrapPeersRetriesStoppedUponResponse() {
// Create peers.
final List<NodeKey> nodeKeys = PeerDiscoveryTestHelper.generateNodeKeys(3);
final List<DiscoveryPeer> peers = helper.createDiscoveryPeers(nodeKeys);
final MockTimerUtil timer = new MockTimerUtil();
final OutboundMessageHandler outboundMessageHandler = mock(OutboundMessageHandler.class);
controller = getControllerBuilder().peers(peers).timerUtil(timer).outboundMessageHandler(outboundMessageHandler).build();
// Mock the creation of the PING packet, so that we can control the hash,
// which gets validated when receiving the PONG.
final PingPacketData mockPing = PingPacketData.create(Optional.ofNullable(localPeer.getEndpoint()), peers.get(0).getEndpoint(), UInt64.ONE);
final Packet mockPacket = Packet.create(PacketType.PING, mockPing, nodeKeys.get(0));
mockPingPacketCreation(mockPacket);
controller.start();
// Invoke timers several times so that ping to peers should be resent
for (int i = 0; i < 3; i++) {
timer.runTimerHandlers();
}
// Assert PING packet was sent for peer[0] 4 times.
for (final DiscoveryPeer peer : peers) {
verify(outboundMessageHandler, times(4)).send(eq(peer), matchPacketOfType(PacketType.PING));
}
// Simulate a PONG message from peer 0.
final PongPacketData packetData = PongPacketData.create(localPeer.getEndpoint(), mockPacket.getHash(), UInt64.ONE);
final Packet packet = Packet.create(PacketType.PONG, packetData, nodeKeys.get(0));
controller.onMessage(packet, peers.get(0));
// Invoke timers again
for (int i = 0; i < 2; i++) {
timer.runTimerHandlers();
}
// Assert PING packet was sent for peer[0] 4 times.
for (final DiscoveryPeer peer : peers) {
final int expectedCount = peer.equals(peers.get(0)) ? 4 : 6;
verify(outboundMessageHandler, times(expectedCount)).send(eq(peer), matchPacketOfType(PacketType.PING));
}
}
Aggregations