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