Search in sources :

Example 1 with Packet

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();
}
Also used : NeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 2 with Packet

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());
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EnodeURLImpl(org.hyperledger.besu.ethereum.p2p.peers.EnodeURLImpl) PacketType(org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Supplier(com.google.common.base.Supplier) NeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData) Peer(org.hyperledger.besu.ethereum.p2p.peers.Peer) Bytes(org.apache.tuweni.bytes.Bytes) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DefaultPeer(org.hyperledger.besu.ethereum.p2p.peers.DefaultPeer) AgentBuilder(org.hyperledger.besu.ethereum.p2p.discovery.PeerDiscoveryTestHelper.AgentBuilder) Suppliers(com.google.common.base.Suppliers) Bytes32(org.apache.tuweni.bytes.Bytes32) NodeKeyUtils(org.hyperledger.besu.crypto.NodeKeyUtils) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) PeerPermissionsDenylist(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissionsDenylist) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SignatureAlgorithmFactory(org.hyperledger.besu.crypto.SignatureAlgorithmFactory) Action(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions.Action) Collectors(java.util.stream.Collectors) EnodeURL(org.hyperledger.besu.plugin.data.EnodeURL) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) KeyPair(org.hyperledger.besu.crypto.KeyPair) Optional(java.util.Optional) PeerPermissions(org.hyperledger.besu.ethereum.p2p.permissions.PeerPermissions) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Collections(java.util.Collections) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Mockito.mock(org.mockito.Mockito.mock) NodeKey(org.hyperledger.besu.crypto.NodeKey) NeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) 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) Test(org.junit.Test)

Example 3 with Packet

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);
}
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 4 with Packet

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);
}
Also used : Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) ENRResponsePacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.ENRResponsePacketData) Test(org.junit.Test)

Example 5 with Packet

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);
}
Also used : Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) PingPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData) Test(org.junit.Test)

Aggregations

Packet (org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet)19 Test (org.junit.Test)17 FindNeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData)10 MockPeerDiscoveryAgent (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent)9 IncomingPacket (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket)9 List (java.util.List)6 Optional (java.util.Optional)6 Bytes (org.apache.tuweni.bytes.Bytes)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 PacketType (org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType)6 Collections (java.util.Collections)4 Collections.emptyList (java.util.Collections.emptyList)4 Collectors (java.util.stream.Collectors)4 Collectors.toList (java.util.stream.Collectors.toList)4 NodeKey (org.hyperledger.besu.crypto.NodeKey)4 NeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData)4 Peer (org.hyperledger.besu.ethereum.p2p.peers.Peer)4 PingPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.PingPacketData)3 PongPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData)3 Supplier (com.google.common.base.Supplier)2