|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use iroh_rpc_client::Client; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use std::{collections::HashMap, path::PathBuf}; |
| 5 | + |
| 6 | +use iroh_api::{ChunkerConfig, Cid, IpfsPath, Multiaddr, PeerId, DEFAULT_CHUNKS_SIZE}; |
| 7 | +use iroh_one::config::{Config, CONFIG_FILE_NAME, ENV_PREFIX}; |
| 8 | +use iroh_rpc_types::Addr; |
| 9 | +use iroh_util::{iroh_config_path, make_config}; |
| 10 | +use tokio::{sync::mpsc, task::JoinHandle, time::Instant}; |
| 11 | + |
| 12 | +use crate::service::{self, DEFAULT_SERVICE_SERVER_PORT, RequestTopic}; |
| 13 | + |
| 14 | +pub struct Node { |
| 15 | + pub config: Config, |
| 16 | + pub p2p_task: JoinHandle<()>, |
| 17 | + pub store_task: JoinHandle<()>, |
| 18 | +} |
| 19 | + |
| 20 | +impl Node { |
| 21 | + pub async fn new(port: u16, server: bool) -> Self { |
| 22 | + let cfg_path = iroh_config_path(CONFIG_FILE_NAME).unwrap(); |
| 23 | + let sources = [Some(cfg_path.as_path())]; |
| 24 | + let mut config = make_config( |
| 25 | + // default |
| 26 | + Config::default(), |
| 27 | + // potential config files |
| 28 | + &sources, |
| 29 | + // env var prefix for this config |
| 30 | + ENV_PREFIX, |
| 31 | + // map of present command line arguments |
| 32 | + HashMap::<&str, String>::new(), |
| 33 | + ) |
| 34 | + .unwrap(); |
| 35 | + |
| 36 | + let store_dir = tempfile::tempdir().unwrap(); |
| 37 | + let store_db = store_dir.path().join("store"); |
| 38 | + config.store.path = store_db; |
| 39 | + |
| 40 | + let keystore_db = store_dir.path().join("keystore"); |
| 41 | + config.p2p.key_store_path = keystore_db; |
| 42 | + |
| 43 | + let listening_multiaddrs = vec![ |
| 44 | + format!("/ip4/0.0.0.0/tcp/{}", port).parse().unwrap(), |
| 45 | + format!("/ip4/0.0.0.0/udp/{}/quic-v1", port + 1) |
| 46 | + .parse() |
| 47 | + .unwrap(), |
| 48 | + ]; |
| 49 | + |
| 50 | + config.p2p.libp2p.listening_multiaddrs = listening_multiaddrs; |
| 51 | + if server { |
| 52 | + config.p2p.libp2p.bitswap_client = false; |
| 53 | + config.p2p.libp2p.bitswap_server = true; |
| 54 | + } else { |
| 55 | + config.p2p.libp2p.bitswap_client = true; |
| 56 | + config.p2p.libp2p.bitswap_server = false; |
| 57 | + } |
| 58 | + |
| 59 | + #[cfg(unix)] |
| 60 | + { |
| 61 | + match iroh_util::increase_fd_limit() { |
| 62 | + Ok(soft) => println!("NOFILE limit: soft = {}", soft), |
| 63 | + Err(err) => eprintln!("Error increasing NOFILE limit: {}", err), |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + let (store, p2p) = { |
| 68 | + let store_recv = Addr::new_mem(); |
| 69 | + let store_sender = store_recv.clone(); |
| 70 | + let p2p_recv = Addr::new_mem(); |
| 71 | + let p2p_sender = p2p_recv.clone(); |
| 72 | + config.rpc_client.store_addr = Some(store_sender); |
| 73 | + config.rpc_client.p2p_addr = Some(p2p_sender); |
| 74 | + config.synchronize_subconfigs(); |
| 75 | + |
| 76 | + let store_rpc = iroh_one::mem_store::start(store_recv, config.store.clone()).await; |
| 77 | + if let Err(store_rpc) = store_rpc { |
| 78 | + eprintln!("Error starting store: {}", store_rpc); |
| 79 | + std::process::exit(1); |
| 80 | + } |
| 81 | + let store_rpc = store_rpc.unwrap(); |
| 82 | + |
| 83 | + let p2p_rpc = iroh_one::mem_p2p::start(p2p_recv, config.p2p.clone()) |
| 84 | + .await |
| 85 | + .unwrap(); |
| 86 | + (store_rpc, p2p_rpc) |
| 87 | + }; |
| 88 | + |
| 89 | + Self { |
| 90 | + config, |
| 91 | + p2p_task: p2p, |
| 92 | + store_task: store, |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +pub async fn run_server(node: &Node, target_host: String) -> Result<()> { |
| 98 | + println!("starting iroh server"); |
| 99 | + let mut api_cfg = iroh_api::config::Config::default(); |
| 100 | + api_cfg.rpc_client = node.config.rpc_client.clone(); |
| 101 | + let api = iroh_api::Api::new_from_config(api_cfg).await?; |
| 102 | + let path = PathBuf::from("./fixtures/10MiB.car"); |
| 103 | + let cid = api |
| 104 | + .add(&path, false, ChunkerConfig::Fixed(DEFAULT_CHUNKS_SIZE)) |
| 105 | + .await?; |
| 106 | + println!("cid: {:?}", cid); |
| 107 | + |
| 108 | + let p2p_rpc = Client::new(node.config.p2p.rpc_client.clone()) |
| 109 | + .await? |
| 110 | + .try_p2p() |
| 111 | + .unwrap(); |
| 112 | + let (peer_id, addrs) = p2p_rpc |
| 113 | + .get_listening_addrs() |
| 114 | + .await |
| 115 | + .context("getting p2p info")?; |
| 116 | + |
| 117 | + println!("peer_id: {:?}", peer_id); |
| 118 | + println!("addrs: {:?}", addrs); |
| 119 | + |
| 120 | + let cid_req = CidRequest { |
| 121 | + cid, |
| 122 | + peer_id, |
| 123 | + addrs, |
| 124 | + }; |
| 125 | + |
| 126 | + let mut service = service::Client::new(target_host.clone()).await; |
| 127 | + let req = service::Request::new(RequestTopic::CidRequest, cid_req.as_bytes()); |
| 128 | + service.send(req).await?; |
| 129 | + Ok(()) |
| 130 | +} |
| 131 | + |
| 132 | +pub async fn run_client(node: &Node) -> Result<()> { |
| 133 | + println!("starting iroh client"); |
| 134 | + let (tx, mut rx) = mpsc::channel(32); |
| 135 | + let mut service = |
| 136 | + service::Server::new(format!("0.0.0.0:{}", DEFAULT_SERVICE_SERVER_PORT), tx).await?; |
| 137 | + tokio::spawn(async move { |
| 138 | + if let Err(e) = service.run().await { |
| 139 | + eprintln!("failed to run service; err = {:?}", e); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + while let Some(request) = rx.recv().await { |
| 144 | + println!("got request"); |
| 145 | + match request.topic { |
| 146 | + RequestTopic::CidRequest => { |
| 147 | + let cid_req = CidRequest::from_bytes(&request.data)?; |
| 148 | + handle_cid(&cid_req, &node).await?; |
| 149 | + } |
| 150 | + _ => { |
| 151 | + println!("unknown topic: {:?}", request.topic); |
| 152 | + continue; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + Ok(()) |
| 157 | +} |
| 158 | + |
| 159 | +async fn handle_cid(cid_req: &CidRequest, node: &Node) -> Result<()> { |
| 160 | + let cid = cid_req.cid.clone(); |
| 161 | + println!("handling cid: {:?}", cid); |
| 162 | + let mut api_cfg = iroh_api::config::Config::default(); |
| 163 | + api_cfg.rpc_client = node.config.rpc_client.clone(); |
| 164 | + let api = iroh_api::Api::new_from_config(api_cfg).await?; |
| 165 | + |
| 166 | + let p2p_rpc = Client::new(node.config.p2p.rpc_client.clone()) |
| 167 | + .await? |
| 168 | + .try_p2p() |
| 169 | + .unwrap(); |
| 170 | + println!("connecting to peer: {:?}", cid_req.peer_id); |
| 171 | + |
| 172 | + let current = Instant::now(); |
| 173 | + |
| 174 | + p2p_rpc |
| 175 | + .connect(cid_req.peer_id, cid_req.addrs.clone()) |
| 176 | + .await?; |
| 177 | + println!("connected to peer: {:?}", cid_req.peer_id); |
| 178 | + |
| 179 | + let path = IpfsPath::from_cid(cid.clone()); |
| 180 | + let blocks = api.get(&path).unwrap(); |
| 181 | + |
| 182 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 183 | + let output = Some(temp_dir.path().join("test.car")); |
| 184 | + let root_path = iroh_api::fs::write_get_stream(&path, blocks, output.as_deref()).await?; |
| 185 | + let duration = current.elapsed(); |
| 186 | + println!("done"); |
| 187 | + println!("saving file(s) to {}", root_path.to_str().unwrap()); |
| 188 | + println!("duration: {:?}", duration); |
| 189 | + Ok(()) |
| 190 | +} |
| 191 | + |
| 192 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 193 | +struct CidRequest { |
| 194 | + pub cid: Cid, |
| 195 | + pub peer_id: PeerId, |
| 196 | + pub addrs: Vec<Multiaddr>, |
| 197 | +} |
| 198 | + |
| 199 | +impl CidRequest { |
| 200 | + pub fn as_bytes(&self) -> Vec<u8> { |
| 201 | + bincode::serialize(self).expect("failed to serialize") |
| 202 | + } |
| 203 | + |
| 204 | + pub fn from_bytes(bytes: &[u8]) -> Result<Self> { |
| 205 | + let cidreq = bincode::deserialize(bytes)?; |
| 206 | + Ok(cidreq) |
| 207 | + } |
| 208 | +} |
0 commit comments