Search in sources :

Example 26 with NodeKey

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);
    }
}
Also used : EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Bytes(org.apache.tuweni.bytes.Bytes) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) PeerConnection(org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection) SubProtocol(org.hyperledger.besu.ethereum.p2p.rlpx.wire.SubProtocol) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 27 with NodeKey

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));
    }
}
Also used : DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) NodeKey(org.hyperledger.besu.crypto.NodeKey) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) Test(org.junit.Test)

Example 28 with NodeKey

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);
}
Also used : PeerPermissionsDenylist(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 29 with NodeKey

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));
}
Also used : DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 30 with NodeKey

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));
    }
}
Also used : DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) NodeKey(org.hyperledger.besu.crypto.NodeKey) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) Test(org.junit.Test)

Aggregations

NodeKey (org.hyperledger.besu.crypto.NodeKey)94 Test (org.junit.Test)78 Address (org.hyperledger.besu.datatypes.Address)43 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)31 BlockHeaderValidator (org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator)24 Bytes (org.apache.tuweni.bytes.Bytes)18 DiscoveryPeer (org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer)17 Endpoint (org.hyperledger.besu.ethereum.p2p.discovery.Endpoint)16 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)14 List (java.util.List)13 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)13 EnodeURL (org.hyperledger.besu.plugin.data.EnodeURL)13 Optional (java.util.Optional)12 NodeKeyUtils (org.hyperledger.besu.crypto.NodeKeyUtils)11 Collectors (java.util.stream.Collectors)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 Util (org.hyperledger.besu.ethereum.core.Util)9 PeerPermissionsDenylist (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist)9 Before (org.junit.Before)9 MessageFactory (org.hyperledger.besu.consensus.ibft.payload.MessageFactory)8