Search in sources :

Example 1 with PongPacketData

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

the class PeerDiscoveryPacketPcapSedesTest method testUDPPongSerializeDeserialize.

@Test
public void testUDPPongSerializeDeserialize() {
    final byte[] data = Hex.decode(pongHexData);
    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.PONG);
    assertThat(packet.getPacketData(PongPacketData.class)).isPresent();
    final PongPacketData pongPacketData = packet.getPacketData(PongPacketData.class).orElse(null);
    assertThat(pongPacketData).isNotNull();
    assertThat(pongPacketData.getTo()).isNotNull();
    assertThat(pongPacketData.getTo().getHost()).satisfies(validInetAddressCondition);
    assertThat(pongPacketData.getTo().getUdpPort()).isPositive();
    pongPacketData.getTo().getTcpPort().ifPresent(p -> assertThat(p).isPositive());
    assertThat(pongPacketData.getPingHash().toArray()).hasSize(32);
    assertThat(pongPacketData.getExpiration()).isPositive();
    assertThat(pongPacketData.getEnrSeq().isPresent()).isTrue();
    assertThat(pongPacketData.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) PongPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData) Test(org.junit.Test)

Example 2 with PongPacketData

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

the class PeerDiscoveryBondingTest method neighborsPacketNotSentUnlessBonded.

@Test
public void neighborsPacketNotSentUnlessBonded() {
    // Start an agent.
    final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(emptyList());
    // Start a test peer that will send a FIND_NEIGHBORS to the agent under test. It should be
    // ignored because
    // we haven't bonded.
    final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
    final FindNeighborsPacketData data = FindNeighborsPacketData.create(otherNode.getId());
    final Packet packet = Packet.create(PacketType.FIND_NEIGHBORS, data, otherNode.getNodeKey());
    helper.sendMessageBetweenAgents(otherNode, agent, packet);
    // No responses received
    final List<IncomingPacket> incoming = otherNode.getIncomingPackets();
    assertThat(incoming.size()).isEqualTo(0);
    assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
    otherNode.bond(agent.getAdvertisedPeer().get());
    // Now we received a PONG.
    final List<IncomingPacket> incomingPongs = otherNode.getIncomingPackets().stream().filter(p -> p.packet.getType().equals(PacketType.PONG)).collect(Collectors.toList());
    assertThat(incomingPongs.size()).isEqualTo(1);
    final Optional<PongPacketData> maybePongData = incomingPongs.get(0).packet.getPacketData(PongPacketData.class);
    assertThat(maybePongData).isPresent();
    assertThat(otherNode.getAdvertisedPeer().isPresent()).isTrue();
    assertThat(maybePongData.get().getTo()).isEqualTo(otherNode.getAdvertisedPeer().get().getEndpoint());
    // No more packets.
    assertThat(otherNode.getIncomingPackets()).hasSize(0);
}
Also used : PacketType(org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType) PongPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData) Collections.emptyList(java.util.Collections.emptyList) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Collections(java.util.Collections) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) PongPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData) Test(org.junit.Test)

Example 3 with PongPacketData

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

the class PeerDiscoveryBondingTest method pongSentUponPing.

@Test
public void pongSentUponPing() {
    // Start an agent with no bootstrap peers.
    final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(Collections.emptyList());
    // Start a test peer and send a PING packet to the agent under test.
    final MockPeerDiscoveryAgent otherAgent = helper.startDiscoveryAgent();
    assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
    otherAgent.bond(agent.getAdvertisedPeer().get());
    final List<IncomingPacket> otherAgentIncomingPongs = otherAgent.getIncomingPackets().stream().filter(p -> p.packet.getType().equals(PacketType.PONG)).collect(Collectors.toList());
    assertThat(otherAgentIncomingPongs.size()).isEqualTo(1);
    assertThat(otherAgentIncomingPongs.get(0).packet.getPacketData(PongPacketData.class).isPresent()).isTrue();
    final PongPacketData pong = otherAgentIncomingPongs.get(0).packet.getPacketData(PongPacketData.class).get();
    assertThat(otherAgent.getAdvertisedPeer().isPresent()).isTrue();
    assertThat(pong.getTo()).isEqualTo(otherAgent.getAdvertisedPeer().get().getEndpoint());
    // The agent considers the test peer BONDED.
    assertThat(agent.streamDiscoveredPeers()).hasSize(1);
    assertThat(agent.streamDiscoveredPeers()).allMatch(p -> p.getStatus() == PeerDiscoveryStatus.BONDED);
}
Also used : PacketType(org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType) PongPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData) Collections.emptyList(java.util.Collections.emptyList) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Collections(java.util.Collections) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) PongPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData) Test(org.junit.Test)

Aggregations

Packet (org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet)3 PongPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData)3 Test (org.junit.Test)3 Collections (java.util.Collections)2 Collections.emptyList (java.util.Collections.emptyList)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 FindNeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData)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