Search in sources :

Example 1 with DiscoveryPeer

use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.

the class PeerDiscoveryControllerTest method shouldNotBondWithBlacklistedPeer.

@Test
public void shouldNotBondWithBlacklistedPeer() {
    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 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
    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(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));
    // 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);
    // Blacklist peer
    blacklist.add(otherPeer);
    final Packet neighborsPacket = MockPacketDataFactory.mockNeighborsPacket(discoPeer, otherPeer, otherPeer2);
    controller.onMessage(neighborsPacket, discoPeer);
    verify(controller, times(0)).bond(otherPeer);
    verify(controller, times(1)).bond(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 2 with DiscoveryPeer

use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.

the class PeerDiscoveryControllerTest method shouldNotAddNewPeerWhenReceivedPongFromBlacklistedPeer.

@Test
public void shouldNotAddNewPeerWhenReceivedPongFromBlacklistedPeer() {
    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 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
    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);
    // Blacklist otherPeer2 before sending return pong
    blacklist.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 3 with DiscoveryPeer

use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.

the class PeerTableTest method addSelf.

@Test
public void addSelf() {
    final DiscoveryPeer localPeer = DiscoveryPeer.fromEnode(EnodeURLImpl.builder().nodeId(Peer.randomId()).ipAddress("127.0.0.1").discoveryAndListeningPorts(12345).build());
    final PeerTable table = new PeerTable(localPeer.getId(), 16);
    final PeerTable.AddResult result = table.tryAdd(localPeer);
    assertThat(result.getOutcome()).isEqualTo(AddOutcome.SELF);
    assertThat(table.streamAllPeers()).hasSize(0);
}
Also used : DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Test(org.junit.Test)

Example 4 with DiscoveryPeer

use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.

the class PeerTableTest method peerExists_withDifferentIp.

@Test
public void peerExists_withDifferentIp() {
    final PeerTable table = new PeerTable(Peer.randomId(), 16);
    final Bytes peerId = SIGNATURE_ALGORITHM.get().generateKeyPair().getPublicKey().getEncodedBytes();
    final DiscoveryPeer peer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.1", 30303, Optional.empty()));
    assertThat(table.tryAdd(peer).getOutcome()).isEqualTo(AddOutcome.ADDED);
    final DiscoveryPeer duplicatePeer = DiscoveryPeer.fromIdAndEndpoint(peerId, new Endpoint("1.1.1.2", 30303, Optional.empty()));
    assertThat(table.tryAdd(duplicatePeer)).satisfies(result -> {
        assertThat(result.getOutcome()).isEqualTo(AddOutcome.ALREADY_EXISTED);
        assertThat(result.getEvictionCandidate()).isNull();
    });
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) Test(org.junit.Test)

Example 5 with DiscoveryPeer

use of org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer in project besu by hyperledger.

the class PeerTableTest method evictPeerFromEmptyTableShouldNotEvict.

@Test
public void evictPeerFromEmptyTableShouldNotEvict() {
    final PeerTable table = new PeerTable(Peer.randomId(), 16);
    final DiscoveryPeer peer = helper.createDiscoveryPeer();
    final EvictResult evictResult = table.tryEvict(peer);
    assertThat(evictResult.getOutcome()).isEqualTo(EvictOutcome.ABSENT);
}
Also used : DiscoveryPeer(org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer) EvictResult(org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerTable.EvictResult) Test(org.junit.Test)

Aggregations

DiscoveryPeer (org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer)60 Test (org.junit.Test)50 Endpoint (org.hyperledger.besu.ethereum.p2p.discovery.Endpoint)21 NodeKey (org.hyperledger.besu.crypto.NodeKey)18 Bytes (org.apache.tuweni.bytes.Bytes)12 PeerPermissionsDenylist (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist)6 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)5 EvictResult (org.hyperledger.besu.ethereum.p2p.discovery.internal.PeerTable.EvictResult)4 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 PeerDiscoveryEvent (org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryEvent)3 List (java.util.List)2 Optional (java.util.Optional)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 PeerDiscoveryStatus (org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryStatus)2 PeerConnection (org.hyperledger.besu.ethereum.p2p.rlpx.connections.PeerConnection)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1