-
Notifications
You must be signed in to change notification settings - Fork 107
/
lib.rs
82 lines (65 loc) · 2.18 KB
/
lib.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
78
79
80
81
82
use log::info;
use serde_json::Value;
use tokio::sync::mpsc;
use trin_core::cli::TrinConfig;
use trin_core::portalnet::protocol::{
HistoryEndpointKind, HistoryNetworkEndpoint, PortalnetConfig, PortalnetProtocol,
};
pub struct HistoryRequestHandler {
pub history_rx: mpsc::UnboundedReceiver<HistoryNetworkEndpoint>,
}
impl HistoryRequestHandler {
pub async fn handle_client_queries(mut self) {
while let Some(cmd) = self.history_rx.recv().await {
use HistoryEndpointKind::*;
match cmd.kind {
GetHistoryNetworkData => {
let _ = cmd
.resp
.send(Ok(Value::String("0xmockhistorydata".to_string())));
}
}
}
}
}
pub fn initialize(
history_rx: mpsc::UnboundedReceiver<HistoryNetworkEndpoint>,
) -> Result<HistoryRequestHandler, Box<dyn std::error::Error>> {
let handler = HistoryRequestHandler { history_rx };
Ok(handler)
}
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Launching trin-history...");
let mut trin_config = TrinConfig::new();
// Set chain history default params
trin_config.discovery_port = 9001;
trin_config.display_config();
let bootnode_enrs = trin_config
.bootnodes
.iter()
.map(|nodestr| nodestr.parse().unwrap())
.collect();
let portalnet_config = PortalnetConfig {
external_addr: trin_config.external_addr,
private_key: trin_config.private_key.clone(),
listen_port: trin_config.discovery_port,
bootnode_enrs,
..Default::default()
};
info!(
"About to spawn portal p2p with boot nodes: {:?}",
portalnet_config.bootnode_enrs
);
tokio::spawn(async move {
let (mut p2p, events) = PortalnetProtocol::new(portalnet_config).await.unwrap();
tokio::spawn(events.process_discv5_requests());
// hacky test: make sure we establish a session with the boot node
p2p.ping_bootnodes().await.unwrap();
tokio::signal::ctrl_c()
.await
.expect("failed to pause until ctrl-c");
})
.await
.unwrap();
Ok(())
}