Skip to content

eth/eth_watch_service.hpp

Namespaces

Name
eth

Classes

Name
class eth::EthWatchService
Ties together EventWatcher, ABI decoding, and eth message dispatch.

Types

Name
using WatchId EventWatchId
Subscription handle returned by EthWatchService::watch_event().
using std::function< void( const MatchedEvent &, const std::vector< abi::AbiValue > &)> DecodedEventCallback
Typed callback for a decoded event log.
using std::function< void(uint8_t eth_msg_id, std::vector< uint8_t > payload)> SendCallback
Callback used by EthWatchService to send an outgoing eth message.

Types Documentation

using EventWatchId

using eth::EventWatchId = WatchId;

Subscription handle returned by EthWatchService::watch_event().

using DecodedEventCallback

using eth::DecodedEventCallback = std::function<void(
   const MatchedEvent&,
   const std::vector<abi::AbiValue>&)>;

Typed callback for a decoded event log.

using SendCallback

using eth::SendCallback = std::function<void(uint8_t eth_msg_id, std::vector<uint8_t> payload)>;

Callback used by EthWatchService to send an outgoing eth message.

Parameters:

  • eth_msg_id Eth-layer message id (before adding the rlpx offset).
  • payload Encoded message bytes.

Source code

// Copyright 2025 GeniusVentures
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <eth/abi_decoder.hpp>
#include <eth/chain_tracker.hpp>
#include <eth/event_filter.hpp>
#include <eth/messages.hpp>
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <vector>

namespace eth {

using EventWatchId = WatchId;

using DecodedEventCallback = std::function<void(
    const MatchedEvent&,
    const std::vector<abi::AbiValue>&)>;

using SendCallback = std::function<void(uint8_t eth_msg_id, std::vector<uint8_t> payload)>;

class EthWatchService
{
public:
    EthWatchService() = default;
    ~EthWatchService() = default;

    EthWatchService(const EthWatchService&) = delete;
    EthWatchService& operator=(const EthWatchService&) = delete;
    EthWatchService(EthWatchService&&) = default;
    EthWatchService& operator=(EthWatchService&&) = default;

    void set_send_callback(SendCallback cb) noexcept;

    EventWatchId watch_event(
        const codec::Address&             contract_address,
        const std::string&                event_signature,
        const std::vector<abi::AbiParam>& params,
        DecodedEventCallback              callback,
        std::optional<uint64_t>           from_block = std::nullopt,
        std::optional<uint64_t>           to_block   = std::nullopt) noexcept;

    [[nodiscard]] uint64_t tip() const noexcept
    {
        return chain_tracker_.tip();
    }

    [[nodiscard]] std::optional<Hash256> tip_hash() const noexcept
    {
        return chain_tracker_.tip_hash();
    }

    void unwatch(EventWatchId id) noexcept;

    [[nodiscard]] size_t subscription_count() const noexcept
    {
        return watcher_.subscription_count();
    }

    void process_message(uint8_t eth_msg_id, rlp::ByteView payload) noexcept;

    void process_receipts(
        const std::vector<codec::Receipt>& receipts,
        const std::vector<Hash256>&        tx_hashes,
        uint64_t                           block_number,
        const Hash256&                     block_hash) noexcept;

    void process_new_block(const NewBlockMessage& msg,
                           const Hash256&         block_hash) noexcept;

    void request_receipts(const Hash256& block_hash, uint64_t block_number) noexcept;

private:
    struct PendingRequest
    {
        Hash256  block_hash;
        uint64_t block_number = 0;
    };

    struct Subscription
    {
        EventWatchId               id;
        std::string                event_signature;
        std::vector<abi::AbiParam> params;
        DecodedEventCallback       callback;
    };

    SendCallback              send_cb_;
    ChainTracker              chain_tracker_;
    EventWatcher              watcher_;
    std::vector<Subscription> subscriptions_;
    EventWatchId              next_id_     = 1;
    uint64_t                  next_req_id_ = 1;

    std::map<uint64_t, PendingRequest> pending_requests_;
};

} // namespace eth

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