Search in sources :

Example 1 with PingPacketData

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

the class PeerDiscoveryPacketPcapSedesTest method testUDPPingSerializeDeserialize.

@Test
public void testUDPPingSerializeDeserialize() {
    final byte[] data = Hex.decode(pingHexData);
    final Packet packet = Packet.decode(Buffer.buffer(data));
    assertThat(packet.getType()).isNotNull();
    assertThat(packet.getNodeId()).isNotNull();
    assertThat(packet.getNodeId().toArray()).hasSize(64);
    assertThat(packet.getType()).isEqualTo(PacketType.PING);
    assertThat(packet.getPacketData(PingPacketData.class)).isPresent();
    final PingPacketData pingPacketData = packet.getPacketData(PingPacketData.class).orElse(null);
    assertThat(pingPacketData).isNotNull();
    assertThat(pingPacketData.getTo()).isNotNull();
    assertThat(pingPacketData.getFrom()).isNotNull();
    assertThat(pingPacketData.getTo().getHost()).satisfies(validInetAddressCondition);
    assertThat(pingPacketData.getFrom().map(Endpoint::getHost)).hasValueSatisfying(validInetAddressCondition);
    assertThat(pingPacketData.getTo().getUdpPort()).isPositive();
    assertThat(pingPacketData.getFrom().get().getUdpPort()).isPositive();
    pingPacketData.getTo().getTcpPort().ifPresent(p -> assertThat(p).isPositive());
    pingPacketData.getFrom().get().getTcpPort().ifPresent(p -> assertThat(p).isPositive());
    assertThat(pingPacketData.getExpiration()).isPositive();
    assertThat(pingPacketData.getEnrSeq().isPresent()).isTrue();
    assertThat(pingPacketData.getEnrSeq().get()).isGreaterThan(UInt64.ZERO);
    final byte[] encoded = packet.encode().getBytes();
    assertThat(encoded).isEqualTo(data);
}
Also used : Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) Test(org.junit.Test)

Example 2 with PingPacketData

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

the class PeerDiscoveryBootstrappingTest method bootstrappingPingsSentMultipleBootstrapPeers.

@Test
public void bootstrappingPingsSentMultipleBootstrapPeers() {
    // Use these peers as bootstrap peers.
    final List<MockPeerDiscoveryAgent> bootstrapAgents = helper.startDiscoveryAgents(3);
    final List<DiscoveryPeer> bootstrapPeers = bootstrapAgents.stream().map(PeerDiscoveryAgent::getAdvertisedPeer).map(Optional::get).collect(toList());
    // Start five agents.
    final List<MockPeerDiscoveryAgent> agents = helper.startDiscoveryAgents(5, bootstrapPeers);
    // Assert that all test peers received a Find Neighbors packet.
    for (final MockPeerDiscoveryAgent bootstrapAgent : bootstrapAgents) {
        // Five messages per peer (sent by each of the five agents).
        final List<Packet> packets = bootstrapAgent.getIncomingPackets().stream().map(p -> p.packet).collect(toList());
        // Assert that the node IDs we received belong to the test agents.
        final List<Bytes> senderIds = packets.stream().map(Packet::getNodeId).distinct().collect(toList());
        final List<Bytes> agentIds = agents.stream().map(PeerDiscoveryAgent::getAdvertisedPeer).map(Optional::get).map(Peer::getId).distinct().collect(toList());
        assertThat(senderIds).containsExactlyInAnyOrderElementsOf(agentIds);
        // Traverse all received pings.
        final List<Packet> pingPackets = packets.stream().filter(p -> p.getType().equals(PacketType.PING)).collect(toList());
        for (final Packet packet : pingPackets) {
            // Assert that the packet was a Find Neighbors one.
            assertThat(packet.getType()).isEqualTo(PacketType.PING);
            // Assert on the content of the packet data.
            assertThat(packet.getPacketData(PingPacketData.class).isPresent()).isTrue();
            final PingPacketData ping = packet.getPacketData(PingPacketData.class).get();
            assertThat(ping.getExpiration()).isGreaterThanOrEqualTo(System.currentTimeMillis() / 1000 - 10000);
            assertThat(bootstrapAgent.getAdvertisedPeer().isPresent()).isTrue();
            assertThat(ping.getTo()).isEqualTo(bootstrapAgent.getAdvertisedPeer().get().getEndpoint());
        }
    }
}
Also used : PacketType(org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType) Collections.emptyList(java.util.Collections.emptyList) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Collections.singletonList(java.util.Collections.singletonList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Optional(java.util.Optional) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Bytes(org.apache.tuweni.bytes.Bytes) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 3 with PingPacketData

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

the class PeerDiscoveryBootstrappingTest method bootstrappingPingsSentSingleBootstrapPeer.

@Test
public void bootstrappingPingsSentSingleBootstrapPeer() {
    // Start one test peer and use it as a bootstrap peer.
    final MockPeerDiscoveryAgent testAgent = helper.startDiscoveryAgent();
    // Start an agent.
    assertThat(testAgent.getAdvertisedPeer().isPresent()).isTrue();
    final PeerDiscoveryAgent agent = helper.startDiscoveryAgent(testAgent.getAdvertisedPeer().get());
    final List<IncomingPacket> incomingPackets = testAgent.getIncomingPackets().stream().filter(p -> p.packet.getType().equals(PacketType.PING)).collect(toList());
    assertThat(incomingPackets.size()).isEqualTo(1);
    final Packet pingPacket = incomingPackets.get(0).packet;
    assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
    assertThat(pingPacket.getNodeId()).isEqualTo(agent.getAdvertisedPeer().get().getId());
    assertThat(pingPacket.getPacketData(PingPacketData.class).isPresent()).isTrue();
    final PingPacketData pingData = pingPacket.getPacketData(PingPacketData.class).get();
    assertThat(pingData.getExpiration()).isGreaterThanOrEqualTo(System.currentTimeMillis() / 1000 - 10000);
    assertThat(pingData.getFrom()).contains(agent.getAdvertisedPeer().get().getEndpoint());
    assertThat(pingData.getTo()).isEqualTo(testAgent.getAdvertisedPeer().get().getEndpoint());
}
Also used : PacketType(org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType) Collections.emptyList(java.util.Collections.emptyList) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Collections.singletonList(java.util.Collections.singletonList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Optional(java.util.Optional) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Aggregations

Packet (org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet)3 PingPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData)3 Test (org.junit.Test)3 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors.toList (java.util.stream.Collectors.toList)2 Bytes (org.apache.tuweni.bytes.Bytes)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 MockPeerDiscoveryAgent (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent)2 IncomingPacket (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket)2 PacketType (org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType)2 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)2