loom_node_json_rpc/
node_block_logs_worker.rs

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
use alloy_network::{primitives::HeaderResponse, Network};
use std::time::Duration;

use alloy_provider::Provider;
use alloy_rpc_types::{Filter, Header};
use alloy_transport::Transport;
use tokio::sync::broadcast::Receiver;
use tracing::{debug, error};

use loom_core_actors::{subscribe, Broadcaster, WorkerResult};
use loom_types_events::{BlockLogs, Message, MessageBlockLogs};

pub async fn new_node_block_logs_worker<T: Transport + Clone, N: Network, P: Provider<T, N> + Send + Sync + 'static>(
    client: P,
    block_header_receiver: Broadcaster<Header>,
    sender: Broadcaster<MessageBlockLogs>,
) -> WorkerResult {
    subscribe!(block_header_receiver);

    loop {
        if let Ok(block_header) = block_header_receiver.recv().await {
            let (block_number, block_hash) = (block_header.number, block_header.hash);
            debug!("BlockLogs header received {} {}", block_number, block_hash);
            let filter = Filter::new().at_block_hash(block_header.hash());

            let mut err_counter = 0;

            while err_counter < 3 {
                match client.get_logs(&filter).await {
                    Ok(logs) => {
                        if let Err(e) = sender.send(Message::new_with_time(BlockLogs { block_header, logs })).await {
                            error!("Broadcaster error {}", e);
                        }
                        break;
                    }
                    Err(e) => {
                        error!("client.get_logs error: {}", e);
                        err_counter += 1;
                        tokio::time::sleep(Duration::from_millis(100)).await;
                    }
                }
            }

            debug!("BlockLogs processing finished {} {}", block_number, block_hash);
        }
    }
}

#[allow(dead_code)]
pub async fn new_node_block_logs_worker_reth<T: Transport + Clone, N: Network, P: Provider<T, N> + Send + Sync + 'static>(
    client: P,
    mut block_header_receiver: Receiver<Header>,
    sender: Broadcaster<MessageBlockLogs>,
) -> WorkerResult {
    loop {
        if let Ok(block_header) = block_header_receiver.recv().await {
            let filter = Filter::new().at_block_hash(block_header.hash());

            let logs = client.get_logs(&filter).await?;
            if let Err(e) = sender.send(Message::new_with_time(BlockLogs { block_header, logs })).await {
                error!("Broadcaster error {}", e);
            }
        }
    }
}