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