Skip to content

discv4/discv4_packet.hpp

Namespaces

Name
discv4

Classes

Name
class discv4::discv4_packet
Base class for all Discovery V4 packets.

Source code

// discv4_packet.h
#ifndef RLP_PEERDISCOVERY_discv4_PACKET_HPP
#define RLP_PEERDISCOVERY_discv4_PACKET_HPP

#include <memory>
#include <string>
#include <vector>

#include <boost/asio.hpp>

// SuperGenius RLP
#include <rlp/rlp_decoder.hpp>
#include <discv4/discv4_constants.hpp>

// Boost.Asio
namespace asio = boost::asio;
using udp = asio::ip::udp;

namespace discv4 {

// Forward declaration
class discv4_packet;

class discv4_packet
{
public:
    virtual ~discv4_packet() = default;

    // Return the RLP-encoded payload (bytes)
    virtual std::vector<uint8_t> RlpPayload() = 0;

    // Return the packet type (e.g., 0x01 for Ping)
    uint8_t PacketType() const noexcept { return packetType_; }

    // Return the protocol version (e.g., 0x04)
    uint8_t Version() const noexcept { return version_; }

    // Return the name of this packet type
    const std::string& Name() const { return name_; }

    // Static helper: validate packet hash (used to verify incoming packets)
    static bool ValidateHash( const std::vector<uint8_t>& payload, const uint8_t* hash );

    static std::array<uint8_t, kWireHashSize> Keccak256( const std::vector<uint8_t>& payload );

protected:
    discv4_packet( uint8_t packetType, uint8_t version, std::string name );

private:
    uint8_t packetType_;
    uint8_t version_;
    std::string name_;
};

} // namespace discv4

#endif // RLP_PEERDISCOVERY_discv4_PACKET_HPP

Updated on 2026-04-13 at 23:22:46 -0700