Search in sources :

Example 91 with NodeKey

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));
}
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 92 with NodeKey

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));
}
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 93 with NodeKey

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());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Bytes(org.apache.tuweni.bytes.Bytes) Mockito.spy(org.mockito.Mockito.spy) Subscribers(org.hyperledger.besu.util.Subscribers) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UInt64(org.apache.tuweni.units.bigints.UInt64) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Mockito.atLeast(org.mockito.Mockito.atLeast) Assertions(org.assertj.core.api.Assertions) PeerDiscoveryStatus(org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryStatus) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) List(java.util.List) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Optional(java.util.Optional) PeerDiscoveryTestHelper(org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryTestHelper) Mockito.mock(org.mockito.Mockito.mock) NodeKey(org.hyperledger.besu.crypto.NodeKey) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) ArrayList(java.util.ArrayList) Bytes(org.apache.tuweni.bytes.Bytes) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) NodeKey(org.hyperledger.besu.crypto.NodeKey) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 94 with NodeKey

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

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