forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmod.rs
More file actions
107 lines (90 loc) · 3.18 KB
/
Copy pathmod.rs
File metadata and controls
107 lines (90 loc) · 3.18 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! A pseudo peer library that ingests multiple block sources to reth
//!
//! This library exposes `start_pseudo_peer` to support reth-side NetworkState/StateFetcher
//! to fetch blocks and feed it to its stages
pub mod block_store;
pub mod cli;
pub mod config;
pub mod network;
pub mod service;
pub mod sources;
pub mod utils;
use std::sync::Arc;
use tokio::sync::mpsc;
use tracing::{error, info};
pub use block_store::*;
pub use cli::*;
pub use config::*;
pub use network::*;
pub use service::*;
pub use sources::*;
/// Re-export commonly used types
pub mod prelude {
pub use super::{
block_store::BlockStore,
config::BlockSourceConfig,
service::{BlockPoller, PseudoPeer},
sources::{BlockSource, LocalBlockSource, RpcBlockSource, S3BlockSource},
};
}
use crate::chainspec::HlChainSpec;
use reth_discv4::NodeRecord;
use reth_network::{NetworkEvent, NetworkEventListenerProvider};
use reth_network_api::Peers;
use std::str::FromStr;
/// Main function that starts the network manager and processes eth requests
pub async fn start_pseudo_peer(
chain_spec: Arc<HlChainSpec>,
destination_peer: String,
block_store: Arc<BlockStore>,
debug_cutoff_height: Option<u64>,
) -> eyre::Result<()> {
// Parse the destination peer enode string
let node_record = NodeRecord::from_str(&destination_peer)
.map_err(|e| eyre::eyre!("Failed to parse destination peer: {e}"))?;
// Create network manager (no boot_nodes — we add the peer directly)
let (mut network, start_tx) = create_network_manager(
(*chain_spec).clone(),
block_store.clone(),
debug_cutoff_height,
)
.await?;
// Create the channels for receiving eth messages
let (eth_tx, mut eth_rx) = mpsc::channel(32);
let (transaction_tx, mut transaction_rx) = mpsc::unbounded_channel();
network.set_eth_request_handler(eth_tx);
network.set_transactions(transaction_tx);
let network_handle = network.handle().clone();
let mut network_events = network_handle.event_listener();
info!("Starting network manager...");
let mut service = PseudoPeer::new(chain_spec, block_store);
tokio::spawn(network);
// Directly add the main node as a peer (bypasses discovery)
info!(
peer_id = %node_record.id,
addr = %node_record.tcp_addr(),
"Adding main node as direct peer"
);
network_handle.add_trusted_peer(node_record.id, node_record.tcp_addr());
let mut first = true;
// Main event loop
loop {
tokio::select! {
Some(event) = tokio_stream::StreamExt::next(&mut network_events) => {
info!("Network event: {event:?}");
if matches!(event, NetworkEvent::ActivePeerSession { .. }) && first {
start_tx.send(()).await?;
first = false;
}
}
_ = transaction_rx.recv() => {}
Some(eth_req) = eth_rx.recv() => {
if let Err(e) = service.process_eth_request(eth_req).await {
error!("Error processing eth request: {e:?}");
} else {
info!("Processed eth request");
}
}
}
}
}