Search in sources :

Example 1 with Endpoint

use of org.hyperledger.besu.ethereum.p2p.discovery.Endpoint 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 Endpoint

use of org.hyperledger.besu.ethereum.p2p.discovery.Endpoint 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 Endpoint

use of org.hyperledger.besu.ethereum.p2p.discovery.Endpoint 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 4 with Endpoint

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

the class PeerTableTest method peerExists_withDifferentIdAndUdpPort.

@Test
public void peerExists_withDifferentIdAndUdpPort() {
    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", 30301, 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 Endpoint

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

the class PingPacketDataTest method handleOptionalSourceIP.

@Test
public void handleOptionalSourceIP() {
    final Endpoint to = new Endpoint("127.0.0.2", 30303, Optional.empty());
    final long time = System.currentTimeMillis();
    final UInt64 enrSeq = UInt64.ONE;
    final PingPacketData anon = PingPacketData.create(Optional.empty(), to, time, enrSeq);
    final BytesValueRLPOutput out = new BytesValueRLPOutput();
    anon.writeTo(out);
    final Bytes serialized = out.encoded();
    System.out.println(serialized.toHexString());
    final PingPacketData deserialized = PingPacketData.readFrom(RLP.input(serialized));
    assertThat(deserialized.getFrom()).isEmpty();
    assertThat(deserialized.getTo()).isEqualTo(to);
    assertThat(deserialized.getExpiration()).isEqualTo(time);
    assertThat(deserialized.getEnrSeq().isPresent()).isTrue();
    assertThat(deserialized.getEnrSeq().get()).isEqualTo(enrSeq);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) Endpoint(org.hyperledger.besu.ethereum.p2p.discovery.Endpoint) UInt64(org.apache.tuweni.units.bigints.UInt64) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Test(org.junit.Test)

Aggregations

Endpoint (org.hyperledger.besu.ethereum.p2p.discovery.Endpoint)36 Test (org.junit.Test)32 Bytes (org.apache.tuweni.bytes.Bytes)19 UInt64 (org.apache.tuweni.units.bigints.UInt64)19 DiscoveryPeer (org.hyperledger.besu.ethereum.p2p.discovery.DiscoveryPeer)15 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)14 NodeKey (org.hyperledger.besu.crypto.NodeKey)11 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 PeerPermissionsDenylist (org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist)6 MalformedRLPInputException (org.hyperledger.besu.ethereum.rlp.MalformedRLPInputException)2 PeerDiscoveryEvent (org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryEvent)1 RLPOutput (org.hyperledger.besu.ethereum.rlp.RLPOutput)1