forked from dexloom/loom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_block_hash_worker.rs
77 lines (68 loc) · 2.84 KB
/
node_block_hash_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
66
67
68
69
70
71
72
73
74
75
76
77
use std::collections::HashMap;
use alloy_network::Ethereum;
use alloy_primitives::BlockHash;
use alloy_provider::Provider;
use alloy_pubsub::PubSubConnect;
use alloy_rpc_types::{Block, Header};
use alloy_transport::Transport;
use chrono::Utc;
use defi_events::{BlockHeader, MessageBlockHeader};
use eyre::Result;
use futures::StreamExt;
use loom_actors::{run_async, Broadcaster, WorkerResult};
use tracing::{error, info};
#[allow(dead_code)]
pub async fn new_node_block_hash_worker<P: Provider + PubSubConnect>(client: P, sender: Broadcaster<Header>) -> Result<()> {
info!("Starting node block hash worker");
let sub = client.subscribe_blocks().await?;
let mut block_processed: HashMap<BlockHash, chrono::DateTime<Utc>> = HashMap::new();
let mut stream = sub.into_stream();
loop {
tokio::select! {
block = stream.next() => {
if let Some(block) = block {
info!("Block hash received: {:?}" , block.header.hash );
if let std::collections::hash_map::Entry::Vacant(e) = block_processed.entry(block.header.hash) {
e.insert(Utc::now());
run_async!(sender.send(block.header));
block_processed.retain(|_, &mut v| v > Utc::now() - chrono::TimeDelta::minutes(10) );
}
}
}
}
}
}
pub async fn new_node_block_header_worker<P, T>(
client: P,
new_block_header_channel: Broadcaster<Header>,
block_header_channel: Broadcaster<MessageBlockHeader>,
) -> WorkerResult
where
T: Transport + Clone,
P: Provider<T, Ethereum> + Send + Sync + Clone + 'static,
{
info!("Starting node block header worker");
let sub = client.subscribe_blocks().await?;
let mut stream = sub.into_stream();
let mut block_processed: HashMap<BlockHash, chrono::DateTime<Utc>> = HashMap::new();
loop {
tokio::select! {
block_msg = stream.next() => {
if let Some(block_header) = block_msg {
let block : Block = block_header;
let block_hash = block.header.hash;
info!("Block hash received: {:?}" , block_hash);
if let std::collections::hash_map::Entry::Vacant(e) = block_processed.entry(block_hash) {
e.insert(Utc::now());
if let Err(e) = new_block_header_channel.send(block.header.clone()).await {
error!("Block hash broadcaster error {}", e);
}
if let Err(e) = block_header_channel.send(MessageBlockHeader::new_with_time(BlockHeader::new(block.header))).await {
error!("Block header broadcaster error {}", e);
}
}
}
}
}
}
}