use of org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet in project besu by hyperledger.
the class PeerDiscoveryAgentTest method neighborsPacketFromUnbondedPeerIsDropped.
@Test
public void neighborsPacketFromUnbondedPeerIsDropped() {
// Start an agent with no bootstrap peers.
final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(Collections.emptyList());
assertThat(agent.streamDiscoveredPeers()).isEmpty();
// Start a test peer
final MockPeerDiscoveryAgent otherNode = helper.startDiscoveryAgent();
// Generate an out-of-band NEIGHBORS message.
final List<DiscoveryPeer> peers = helper.createDiscoveryPeers(5);
final NeighborsPacketData data = NeighborsPacketData.create(peers);
final Packet packet = Packet.create(PacketType.NEIGHBORS, data, otherNode.getNodeKey());
helper.sendMessageBetweenAgents(otherNode, agent, packet);
assertThat(agent.streamDiscoveredPeers()).isEmpty();
}
use of org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet in project besu by hyperledger.
the class PeerDiscoveryAgentTest method neighborsPacketLimited.
@Test
public void neighborsPacketLimited() {
// Start 20 agents with no bootstrap peers.
final List<MockPeerDiscoveryAgent> otherAgents = helper.startDiscoveryAgents(20, Collections.emptyList());
final List<DiscoveryPeer> otherPeers = otherAgents.stream().map(MockPeerDiscoveryAgent::getAdvertisedPeer).map(Optional::get).collect(Collectors.toList());
// Start another peer pointing to those 20 agents.
final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(otherPeers);
// We used to do a hasSize match but we had issues with duplicate peers getting added to the
// list. By moving to a contains we make sure that all the peers are loaded with tolerance for
// duplicates. If we fix the duplication problem we should use containsExactlyInAnyOrder to
// hedge against missing one and duplicating another.
assertThat(agent.streamDiscoveredPeers()).contains(otherPeers.toArray(new DiscoveryPeer[20]));
assertThat(agent.streamDiscoveredPeers()).allMatch(p -> p.getStatus() == PeerDiscoveryStatus.BONDED);
// Use additional agent to exchange messages with agent
final MockPeerDiscoveryAgent testAgent = helper.startDiscoveryAgent();
// Send a PING so we can exchange messages with the latter agent.
Packet packet = helper.createPingPacket(testAgent, agent);
helper.sendMessageBetweenAgents(testAgent, agent, packet);
// Send a FIND_NEIGHBORS message.
assertThat(otherAgents.get(0).getAdvertisedPeer().isPresent()).isTrue();
packet = Packet.create(PacketType.FIND_NEIGHBORS, FindNeighborsPacketData.create(otherAgents.get(0).getAdvertisedPeer().get().getId()), testAgent.getNodeKey());
helper.sendMessageBetweenAgents(testAgent, agent, packet);
// Check response packet
final List<IncomingPacket> incomingPackets = testAgent.getIncomingPackets().stream().filter(p -> p.packet.getType().equals(PacketType.NEIGHBORS)).collect(toList());
assertThat(incomingPackets.size()).isEqualTo(1);
final IncomingPacket neighborsPacket = incomingPackets.get(0);
assertThat(neighborsPacket.fromAgent).isEqualTo(agent);
// Assert that we only received 13 items.
assertThat(neighborsPacket.packet.getPacketData(NeighborsPacketData.class).isPresent()).isTrue();
final NeighborsPacketData neighbors = neighborsPacket.packet.getPacketData(NeighborsPacketData.class).get();
assertThat(neighbors).isNotNull();
assertThat(neighbors.getNodes()).hasSize(13);
// under max MTU
assertThat(neighborsPacket.packet.encode().length()).isLessThanOrEqualTo(1280);
// Assert that after removing those 13 items we're left with either 7 or 8.
// If we are left with 8, the test peer was returned as an item, assert that this is the case.
otherPeers.removeAll(neighbors.getNodes());
assertThat(otherPeers.size()).isBetween(7, 8);
if (otherPeers.size() == 8) {
assertThat(testAgent.getAdvertisedPeer().isPresent()).isTrue();
assertThat(neighbors.getNodes()).contains(testAgent.getAdvertisedPeer().get());
}
}
use of org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet 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.Packet in project besu by hyperledger.
the class PeerDiscoveryPacketPcapSedesTest method testUDPENRResponseSerializeDeserialize.
@Test
public void testUDPENRResponseSerializeDeserialize() {
final byte[] data = Hex.decode(enrResponseHexData);
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.ENR_RESPONSE);
final ENRResponsePacketData enrResponsePacketData = packet.getPacketData(ENRResponsePacketData.class).orElse(null);
assertThat(enrResponsePacketData).isNotNull();
assertThat(enrResponsePacketData.getEnr()).isNotNull();
assertThat(enrResponsePacketData.getEnr().getSeq()).isGreaterThan(UInt64.ZERO);
assertThat(enrResponsePacketData.getEnr().getSignature()).isNotNull();
assertThat(enrResponsePacketData.getRequestHash()).isNotNull();
assertThat(enrResponsePacketData.getRequestHash().toArray()).hasSize(32);
final byte[] encoded = packet.encode().getBytes();
assertThat(encoded).isEqualTo(data);
}
use of org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet 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);
}
Aggregations