loom_node_json_rpc/
node_block_state_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
use alloy_network::Network;

use alloy_provider::Provider;
use alloy_rpc_types::{BlockId, Header};
use alloy_transport::Transport;
use tracing::{debug, error};

use loom_core_actors::{subscribe, Broadcaster, WorkerResult};
use loom_node_debug_provider::DebugProviderExt;
use loom_types_blockchain::debug_trace_block;
use loom_types_events::{BlockStateUpdate, Message, MessageBlockStateUpdate};

pub async fn new_node_block_state_worker<P, T, N>(
    client: P,
    block_header_receiver: Broadcaster<Header>,
    sender: Broadcaster<MessageBlockStateUpdate>,
) -> WorkerResult
where
    T: Transport + Clone,
    N: Network,
    P: Provider<T, N> + DebugProviderExt<T, N> + Send + Sync + Clone + 'static,
{
    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!("BlockState header received {} {}", block_number, block_hash);

            match debug_trace_block(client.clone(), BlockId::Hash(block_header.hash.into()), true).await {
                Ok((_, post)) => {
                    if let Err(e) = sender.send(Message::new_with_time(BlockStateUpdate { block_header, state_update: post })).await {
                        error!("Broadcaster error {}", e)
                    }
                }
                Err(e) => {
                    error!("debug_trace_block error : {e}")
                }
            }
            debug!("BlockState processing finished {} {}", block_number, block_hash);
        }
    }
}