1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use crate::BlockProvider;
use alloy_eips::BlockNumberOrTag;
use reqwest::Client;
use reth_node_core::rpc::types::Block;
use reth_tracing::tracing::warn;
use serde::Deserialize;
use std::time::Duration;
use tokio::{sync::mpsc, time::interval};

/// Block provider that fetches new blocks from Etherscan API.
#[derive(Debug, Clone)]
pub struct EtherscanBlockProvider {
    http_client: Client,
    base_url: String,
    api_key: String,
    interval: Duration,
}

impl EtherscanBlockProvider {
    /// Create a new Etherscan block provider with the given base URL and API key.
    pub fn new(base_url: String, api_key: String) -> Self {
        Self { http_client: Client::new(), base_url, api_key, interval: Duration::from_secs(3) }
    }

    /// Sets the interval at which the provider fetches new blocks.
    pub const fn with_interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }

    /// Load block using Etherscan API. Note: only `BlockNumberOrTag::Latest`,
    /// `BlockNumberOrTag::Earliest`, `BlockNumberOrTag::Pending`, `BlockNumberOrTag::Number(u64)`
    /// are supported.
    pub async fn load_block(&self, block_number_or_tag: BlockNumberOrTag) -> eyre::Result<Block> {
        let block: EtherscanBlockResponse = self
            .http_client
            .get(&self.base_url)
            .query(&[
                ("module", "proxy"),
                ("action", "eth_getBlockByNumber"),
                ("tag", &block_number_or_tag.to_string()),
                ("boolean", "true"),
                ("apikey", &self.api_key),
            ])
            .send()
            .await?
            .json()
            .await?;
        Ok(block.result)
    }
}

impl BlockProvider for EtherscanBlockProvider {
    async fn subscribe_blocks(&self, tx: mpsc::Sender<Block>) {
        let mut last_block_number: Option<u64> = None;
        let mut interval = interval(self.interval);
        loop {
            interval.tick().await;
            let block = match self.load_block(BlockNumberOrTag::Latest).await {
                Ok(block) => block,
                Err(err) => {
                    warn!(target: "consensus::debug-client", %err, "failed to fetch a block from Etherscan");
                    continue
                }
            };
            let block_number = block.header.number;
            if Some(block_number) == last_block_number {
                continue;
            }

            if tx.send(block).await.is_err() {
                // channel closed
                break;
            }

            last_block_number = Some(block_number);
        }
    }

    async fn get_block(&self, block_number: u64) -> eyre::Result<Block> {
        self.load_block(BlockNumberOrTag::Number(block_number)).await
    }
}

#[derive(Deserialize, Debug)]
struct EtherscanBlockResponse {
    result: Block,
}