Search in sources :

Example 1 with NeighborsPacketData

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData 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 NeighborsPacketData

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData 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 NeighborsPacketData

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

the class PeerDiscoveryPacketPcapSedesTest method testUDPNeighborsSerializeDeserialize.

@Test
public void testUDPNeighborsSerializeDeserialize() {
    final byte[] data = Hex.decode(neighborsHexData);
    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.NEIGHBORS);
    assertThat(packet.getPacketData(NeighborsPacketData.class)).isPresent();
    final NeighborsPacketData neighborsPacketData = packet.getPacketData(NeighborsPacketData.class).orElse(null);
    assertThat(neighborsPacketData).isNotNull();
    assertThat(neighborsPacketData.getExpiration()).isPositive();
    assertThat(neighborsPacketData.getNodes()).isNotEmpty();
    for (final DiscoveryPeer p : neighborsPacketData.getNodes()) {
        assertThat(NetworkUtility.isValidPort(p.getEndpoint().getUdpPort())).isTrue();
        assertThat(p.getEndpoint().getHost()).satisfies(validInetAddressCondition);
        assertThat(p.getId().toArray()).hasSize(64);
    }
    final byte[] encoded = packet.encode().getBytes();
    assertThat(encoded).isEqualTo(data);
}
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) Test(org.junit.Test)

Example 4 with NeighborsPacketData

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

the class PeerDiscoveryPacketSedesTest method neighborsPacketData.

@Test
public void neighborsPacketData() {
    final List<DiscoveryPeer> peers = helper.createDiscoveryPeers(5);
    final NeighborsPacketData packet = NeighborsPacketData.create(peers);
    final Bytes serialized = RLP.encode(packet::writeTo);
    assertThat(serialized).isNotNull();
    final NeighborsPacketData deserialized = NeighborsPacketData.readFrom(RLP.input(serialized));
    assertThat(deserialized.getNodes()).isEqualTo(peers);
    // Fuzziness: allow a skew of 2 seconds between the time the message was generated until the
    // assertion.
    assertThat(deserialized.getExpiration()).isCloseTo(PacketData.defaultExpiration(), offset(2L));
}
Also used : NeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) Test(org.junit.Test)

Aggregations

FindNeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData)4 NeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.NeighborsPacketData)4 Test (org.junit.Test)4 Packet (org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet)3 Bytes (org.apache.tuweni.bytes.Bytes)2 MockPeerDiscoveryAgent (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent)2 IncomingPacket (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket)2 Supplier (com.google.common.base.Supplier)1 Suppliers (com.google.common.base.Suppliers)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 MutableBytes (org.apache.tuweni.bytes.MutableBytes)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatCode (org.assertj.core.api.Assertions.assertThatCode)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)1