Search in sources :

Example 1 with FindNeighborsPacketData

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

the class PeerDiscoveryPacketSedesTest method deserializeDifferentPacketData.

@Test
public void deserializeDifferentPacketData() {
    final byte[] r = new byte[64];
    new Random().nextBytes(r);
    final Bytes target = Bytes.wrap(r);
    final FindNeighborsPacketData packet = FindNeighborsPacketData.create(target);
    final Bytes serialized = RLP.encode(packet::writeTo);
    assertThat(serialized).isNotNull();
    final RLPInput input = RLP.input(serialized);
    assertThatThrownBy(() -> NeighborsPacketData.readFrom(input)).isInstanceOf(RLPException.class);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) Random(java.util.Random) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) Test(org.junit.Test)

Example 2 with FindNeighborsPacketData

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

the class PeerDiscoveryPacketSedesTest method integrityCheckFailsUnmatchedHash.

@Test
public void integrityCheckFailsUnmatchedHash() {
    final byte[] r = new byte[64];
    new Random().nextBytes(r);
    final Bytes target = Bytes.wrap(r);
    final NodeKey nodeKey = NodeKeyUtils.generate();
    final FindNeighborsPacketData data = FindNeighborsPacketData.create(target);
    final Packet packet = Packet.create(PacketType.FIND_NEIGHBORS, data, nodeKey);
    final Bytes encoded = Bytes.wrapBuffer(packet.encode());
    final MutableBytes garbled = encoded.mutableCopy();
    final int i = garbled.size() - 1;
    // Change one bit in the last byte, which belongs to the payload, hence the hash will not match
    // any longer.
    garbled.set(i, (byte) (garbled.get(i) + 0x01));
    final Buffer input = Buffer.buffer(garbled.toArray());
    assertThatThrownBy(() -> Packet.decode(input)).isInstanceOf(PeerDiscoveryPacketDecodingException.class);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Bytes(org.apache.tuweni.bytes.Bytes) MutableBytes(org.apache.tuweni.bytes.MutableBytes) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Random(java.util.Random) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) MutableBytes(org.apache.tuweni.bytes.MutableBytes) NodeKey(org.hyperledger.besu.crypto.NodeKey) Test(org.junit.Test)

Example 3 with FindNeighborsPacketData

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

the class PeerDiscoveryAgentTest method requestNeighbors.

protected void requestNeighbors(final MockPeerDiscoveryAgent fromAgent, final MockPeerDiscoveryAgent toAgent) {
    final FindNeighborsPacketData data = FindNeighborsPacketData.create(Peer.randomId());
    final Packet packet = Packet.create(PacketType.FIND_NEIGHBORS, data, fromAgent.getNodeKey());
    helper.sendMessageBetweenAgents(fromAgent, toAgent, packet);
}
Also used : Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData)

Example 4 with FindNeighborsPacketData

use of org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData 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);
}
Also used : PacketType(org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType) PongPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData) Collections.emptyList(java.util.Collections.emptyList) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) Collections(java.util.Collections) Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) IncomingPacket(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) PongPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData) Test(org.junit.Test)

Example 5 with FindNeighborsPacketData

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

the class PeerDiscoveryPacketPcapSedesTest method testUDPFindNeighborsSerializeDeserialize.

@Test
public void testUDPFindNeighborsSerializeDeserialize() {
    final byte[] data = Hex.decode(findNeighborsHexData);
    final Packet packet = Packet.decode(Buffer.buffer(data));
    final Instant timestamp = Instant.ofEpochSecond(1608127678L);
    assertThat(packet.getType()).isNotNull();
    assertThat(packet.getNodeId()).isNotNull();
    assertThat(packet.getNodeId().toArray()).hasSize(64);
    assertThat(packet.getType()).isEqualTo(PacketType.FIND_NEIGHBORS);
    assertThat(packet.getPacketData(FindNeighborsPacketData.class)).isPresent();
    final FindNeighborsPacketData findNeighborsPacketData = packet.getPacketData(FindNeighborsPacketData.class).orElse(null);
    assertThat(findNeighborsPacketData).isNotNull();
    assertThat(findNeighborsPacketData.getExpiration()).isBetween(timestamp.getEpochSecond() - 10000, timestamp.getEpochSecond() + 10000);
    assertThat(findNeighborsPacketData.getTarget().toArray()).hasSize(64);
    assertThat(packet.getNodeId().toArray()).hasSize(64);
    final byte[] encoded = packet.encode().getBytes();
    assertThat(encoded).isEqualTo(data);
}
Also used : Packet(org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet) FindNeighborsPacketData(org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData) Instant(java.time.Instant) Test(org.junit.Test)

Aggregations

FindNeighborsPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.FindNeighborsPacketData)7 Test (org.junit.Test)6 Packet (org.hyperledger.besu.ethereum.p2p.discovery.internal.Packet)5 Random (java.util.Random)4 Bytes (org.apache.tuweni.bytes.Bytes)4 MutableBytes (org.apache.tuweni.bytes.MutableBytes)4 Buffer (io.vertx.core.buffer.Buffer)2 NodeKey (org.hyperledger.besu.crypto.NodeKey)2 IncomingPacket (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent.IncomingPacket)2 Instant (java.time.Instant)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 MockPeerDiscoveryAgent (org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent)1 PacketType (org.hyperledger.besu.ethereum.p2p.discovery.internal.PacketType)1 PongPacketData (org.hyperledger.besu.ethereum.p2p.discovery.internal.PongPacketData)1 RLPInput (org.hyperledger.besu.ethereum.rlp.RLPInput)1