MCPcopy Create free account
hub / github.com/ceramicnetwork/rust-ceramic / new

Method new

p2p/src/node.rs:150–273  ·  view source on GitHub ↗
(
        config: Config,
        rpc_addr: P2pAddr,
        node_key: NodeKey,
        peer_svc: impl PeerService + 'static,
        recons: Option<(P, M)>,
        block_store: Arc<S>,
        metri

Source from the content-addressed store, hash-verified

148 S: iroh_bitswap::Store + Send + Sync,
149{
150 pub async fn new(
151 config: Config,
152 rpc_addr: P2pAddr,
153 node_key: NodeKey,
154 peer_svc: impl PeerService + 'static,
155 recons: Option<(P, M)>,
156 block_store: Arc<S>,
157 metrics: Metrics,
158 ) -> Result<Self> {
159 let (network_sender_in, network_receiver_in) = channel(1024); // TODO: configurable
160
161 let Config {
162 libp2p: libp2p_config,
163 rpc_client,
164 ..
165 } = config;
166
167 // Setup peers message channel
168 let (peers_tx, peers_rx) = channel(1_000);
169
170 let mut swarm = build_swarm(
171 &libp2p_config,
172 node_key.p2p_keypair(),
173 recons,
174 block_store,
175 peers_tx.clone(),
176 metrics.clone(),
177 )
178 .await?;
179
180 if !libp2p_config.external_multiaddrs.is_empty() {
181 peers_tx
182 .send(peers::Message::NewLocalAddresses(
183 libp2p_config.external_multiaddrs.clone(),
184 ))
185 .await?;
186 }
187 for addr in &libp2p_config.external_multiaddrs {
188 swarm.add_external_address(addr.clone());
189 }
190
191 let mut listen_addrs = vec![];
192 for addr in &libp2p_config.listening_multiaddrs {
193 Swarm::listen_on(&mut swarm, addr.clone())
194 .map_err(|e| anyhow!("Failed to listen on swarm address: {}. {:#}", addr, e))?;
195 listen_addrs.push(addr.clone());
196 }
197
198 // The following two statements were intentionally placed right before the return. Having them sooner caused the
199 // daemon to get stuck in a loop during shutdown, unable to bind to a listen address, if there was some
200 // initialization error after the RPC task was spawned, e.g. while parsing bootstrap peer multiaddrs in the
201 // PeerManager.
202 let rpc_task = tokio::task::spawn(async move {
203 // TODO: handle error
204 rpc::new(rpc_addr.clone(), P2p::new(network_sender_in))
205 .await
206 .map_err(|e| {
207 warn!("Failed to run RPC server on {}. {:?}", rpc_addr, e);

Callers

nothing calls this directly

Calls 14

build_swarmFunction · 0.85
newFunction · 0.85
p2p_keypairMethod · 0.80
unwrapMethod · 0.80
mapMethod · 0.80
runFunction · 0.70
channelFunction · 0.50
cloneMethod · 0.45
is_emptyMethod · 0.45
sendMethod · 0.45
pushMethod · 0.45
contextMethod · 0.45

Tested by

no test coverage detected