Skip to content

eth/chain_tracker.hpp

Namespaces

Name
eth

Classes

Name
class eth::ChainTracker
Tracks the chain tip and deduplicates block processing requests.

Source code

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

#pragma once

#include <eth/objects.hpp>
#include <cstdint>
#include <optional>
#include <set>

namespace eth {

class ChainTracker
{
public:
    static constexpr size_t kDefaultWindowSize = 1024;

    explicit ChainTracker(size_t window_size = kDefaultWindowSize) noexcept;

    ~ChainTracker() = default;

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

    bool mark_seen(const codec::Hash256& block_hash,
                   uint64_t              block_number) noexcept;

    [[nodiscard]] bool is_seen(const codec::Hash256& block_hash) const noexcept;

    [[nodiscard]] uint64_t tip() const noexcept;

    [[nodiscard]] std::optional<codec::Hash256> tip_hash() const noexcept;

    [[nodiscard]] size_t seen_count() const noexcept;

    void reset() noexcept;

private:
    size_t                          window_size_;
    uint64_t                        tip_number_ = 0;
    std::optional<codec::Hash256>   tip_hash_;

    std::set<codec::Hash256>        seen_set_;

    std::vector<codec::Hash256>     eviction_queue_;
    size_t                          eviction_head_ = 0;  
};

} // namespace eth

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