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