-
Notifications
You must be signed in to change notification settings - Fork 159
/
service.rs
458 lines (422 loc) · 19.9 KB
/
service.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use super::chain_exchange::{
make_chain_exchange_response, ChainExchangeRequest, ChainExchangeResponse,
};
use super::{ForestBehaviour, ForestBehaviourEvent, Libp2pConfig};
use crate::{
hello::{HelloRequest, HelloResponse},
rpc::RequestResponseError,
};
use async_std::channel::{unbounded, Receiver, Sender};
use async_std::{stream, task};
use cid::{multihash::Code::Blake2b256, Cid};
use forest_blocks::GossipBlock;
use forest_chain::ChainStore;
use forest_ipld_blockstore::{BlockStore, BlockStoreExt};
use forest_message::SignedMessage;
use forest_utils::read_file_to_vec;
use futures::channel::oneshot::Sender as OneShotSender;
use futures::select;
use futures_util::stream::StreamExt;
use fvm_ipld_encoding::from_slice;
pub use libp2p::gossipsub::IdentTopic;
pub use libp2p::gossipsub::Topic;
use libp2p::multiaddr::Protocol;
use libp2p::multihash::Multihash;
use libp2p::request_response::ResponseChannel;
use libp2p::swarm::SwarmEvent;
use libp2p::{
core,
core::muxing::StreamMuxerBox,
core::transport::Boxed,
identity::{ed25519, Keypair},
mplex, noise,
swarm::ConnectionLimits,
yamux, PeerId, Swarm, Transport,
};
use libp2p::{core::Multiaddr, swarm::SwarmBuilder};
use log::{debug, error, info, trace, warn};
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
/// `Gossipsub` Filecoin blocks topic identifier.
pub const PUBSUB_BLOCK_STR: &str = "/fil/blocks";
/// `Gossipsub` Filecoin messages topic identifier.
pub const PUBSUB_MSG_STR: &str = "/fil/msgs";
const PUBSUB_TOPICS: [&str; 2] = [PUBSUB_BLOCK_STR, PUBSUB_MSG_STR];
/// Events emitted by this Service.
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum NetworkEvent {
PubsubMessage {
source: PeerId,
message: PubsubMessage,
},
HelloRequest {
request: HelloRequest,
source: PeerId,
},
ChainExchangeRequest {
request: ChainExchangeRequest,
channel: ResponseChannel<ChainExchangeResponse>,
},
PeerConnected(PeerId),
PeerDisconnected(PeerId),
BitswapBlock {
cid: Cid,
},
}
/// Message types that can come over `GossipSub`
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum PubsubMessage {
/// Messages that come over the block topic
Block(GossipBlock),
/// Messages that come over the message topic
Message(SignedMessage),
}
/// Messages into the service to handle.
#[derive(Debug)]
pub enum NetworkMessage {
PubsubMessage {
topic: IdentTopic,
message: Vec<u8>,
},
ChainExchangeRequest {
peer_id: PeerId,
request: ChainExchangeRequest,
response_channel: OneShotSender<Result<ChainExchangeResponse, RequestResponseError>>,
},
HelloRequest {
peer_id: PeerId,
request: HelloRequest,
response_channel: OneShotSender<Result<HelloResponse, RequestResponseError>>,
},
BitswapRequest {
cid: Cid,
response_channel: OneShotSender<()>,
},
JSONRPCRequest {
method: NetRPCMethods,
},
}
/// Network RPC API methods used to gather data from libp2p node.
#[derive(Debug)]
pub enum NetRPCMethods {
NetAddrsListen(OneShotSender<(PeerId, Vec<Multiaddr>)>),
NetPeers(OneShotSender<HashMap<PeerId, Vec<Multiaddr>>>),
NetConnect(OneShotSender<bool>, PeerId, Vec<Multiaddr>),
NetDisconnect(OneShotSender<()>, PeerId),
}
/// The `Libp2pService` listens to events from the libp2p swarm.
pub struct Libp2pService<DB> {
swarm: Swarm<ForestBehaviour>,
cs: Arc<ChainStore<DB>>,
network_receiver_in: Receiver<NetworkMessage>,
network_sender_in: Sender<NetworkMessage>,
network_receiver_out: Receiver<NetworkEvent>,
network_sender_out: Sender<NetworkEvent>,
network_name: String,
bitswap_response_channels: HashMap<Cid, Vec<OneShotSender<()>>>,
}
impl<DB> Libp2pService<DB>
where
DB: BlockStore + Sync + Send + 'static,
{
pub async fn new(
config: Libp2pConfig,
cs: Arc<ChainStore<DB>>,
net_keypair: Keypair,
network_name: &str,
) -> Self {
let peer_id = PeerId::from(net_keypair.public());
let transport = build_transport(net_keypair.clone()).await;
let limits = ConnectionLimits::default()
.with_max_pending_incoming(Some(10))
.with_max_pending_outgoing(Some(30))
.with_max_established_incoming(Some(config.target_peer_count))
.with_max_established_outgoing(Some(config.target_peer_count))
.with_max_established_per_peer(Some(5));
let mut swarm = SwarmBuilder::new(
transport,
ForestBehaviour::new(&net_keypair, &config, network_name).await,
peer_id,
)
.connection_limits(limits)
.notify_handler_buffer_size(std::num::NonZeroUsize::new(20).expect("Not zero"))
.connection_event_buffer_size(64)
.build();
Swarm::listen_on(&mut swarm, config.listening_multiaddr).unwrap();
// Subscribe to gossipsub topics with the network name suffix
for topic in PUBSUB_TOPICS.iter() {
let t = Topic::new(format!("{}/{}", topic, network_name));
swarm.behaviour_mut().subscribe(&t).unwrap();
}
// Bootstrap with Kademlia
if let Err(e) = swarm.behaviour_mut().bootstrap() {
warn!("Failed to bootstrap with Kademlia: {}", e);
}
let (network_sender_in, network_receiver_in) = unbounded();
let (network_sender_out, network_receiver_out) = unbounded();
Libp2pService {
swarm,
cs,
network_receiver_in,
network_sender_in,
network_receiver_out,
network_sender_out,
network_name: network_name.to_owned(),
bitswap_response_channels: Default::default(),
}
}
/// Starts the libp2p service networking stack. This Future resolves when shutdown occurs.
pub async fn run(mut self) {
let mut swarm_stream = self.swarm.fuse();
let mut network_stream = self.network_receiver_in.fuse();
let mut interval = stream::interval(Duration::from_secs(15)).fuse();
let pubsub_block_str = format!("{}/{}", PUBSUB_BLOCK_STR, self.network_name);
let pubsub_msg_str = format!("{}/{}", PUBSUB_MSG_STR, self.network_name);
loop {
select! {
swarm_event = swarm_stream.next() => match swarm_event {
// outbound events
Some(event) => match event {
SwarmEvent::Behaviour(ForestBehaviourEvent::PeerConnected(peer_id)) => {
debug!("Peer connected, {:?}", peer_id);
emit_event(&self.network_sender_out,
NetworkEvent::PeerConnected(peer_id)).await;
}
SwarmEvent::Behaviour(ForestBehaviourEvent::PeerDisconnected(peer_id)) => {
emit_event(&self.network_sender_out, NetworkEvent::PeerDisconnected(peer_id)).await;
}
SwarmEvent::Behaviour(ForestBehaviourEvent::GossipMessage {
source,
topic,
message,
}) => {
trace!("Got a Gossip Message from {:?}", source);
let topic = topic.as_str();
if topic == pubsub_block_str {
match from_slice::<GossipBlock>(&message) {
Ok(b) => {
emit_event(&self.network_sender_out, NetworkEvent::PubsubMessage{
source,
message: PubsubMessage::Block(b),
}).await;
}
Err(e) => {
warn!("Gossip Block from peer {:?} could not be deserialized: {}", source, e);
}
}
} else if topic == pubsub_msg_str {
match from_slice::<SignedMessage>(&message) {
Ok(m) => {
emit_event(&self.network_sender_out, NetworkEvent::PubsubMessage{
source,
message: PubsubMessage::Message(m),
}).await;
}
Err(e) => {
warn!("Gossip Message from peer {:?} could not be deserialized: {}", source, e);
}
}
} else {
warn!("Getting gossip messages from unknown topic: {}", topic);
}
}
SwarmEvent::Behaviour(ForestBehaviourEvent::HelloRequest { request, peer } )=> {
debug!("Received hello request (peer_id: {:?})", peer);
emit_event(&self.network_sender_out, NetworkEvent::HelloRequest {
request,
source: peer,
}).await;
}
SwarmEvent::Behaviour(ForestBehaviourEvent::ChainExchangeRequest { channel, peer, request }) => {
debug!("Received chain_exchange request (peer_id: {:?})", peer);
let db = self.cs.clone();
task::spawn(async move {
channel.send(make_chain_exchange_response(db.as_ref(), &request).await)
});
}
SwarmEvent::Behaviour(ForestBehaviourEvent::BitswapReceivedBlock(_peer_id, cid, block)) => {
let res: Result<_, String> = self.cs.blockstore().put_raw(block.into(), Blake2b256).map_err(|e| e.to_string());
match res {
Ok(actual_cid) => {
if actual_cid != cid {
warn!("Bitswap cid mismatch: cid {:?}, expected cid: {:?}", actual_cid, cid);
} else if let Some (chans) = self.bitswap_response_channels.remove(&cid) {
for chan in chans.into_iter(){
if chan.send(()).is_err() {
debug!("Bitswap response channel send failed");
}
trace!("Saved Bitswap block with cid {:?}", cid);
}
} else {
debug!("Received Bitswap response, but response channel cannot be found");
}
emit_event(&self.network_sender_out, NetworkEvent::BitswapBlock{cid}).await;
}
Err(e) => {
warn!("failed to save bitswap block: {:?}", e.to_string());
}
}
},
SwarmEvent::Behaviour(ForestBehaviourEvent::BitswapReceivedWant(peer_id, cid,)) => match self.cs.blockstore().get_obj(&cid) {
Ok(Some(data)) => {
match swarm_stream.get_mut().behaviour_mut().send_block(&peer_id, cid, data) {
Ok(_) => {
trace!("Sent bitswap message successfully");
},
Err(e) => {
warn!("Failed to send Bitswap reply: {}", e.to_string());
},
}
}
Ok(None) => {
trace!("Don't have data for: {}", cid);
}
Err(e) => {
trace!("Failed to get data: {}", e.to_string());
}
},
_ => {
continue;
}
}
None => { break; }
},
rpc_message = network_stream.next() => match rpc_message {
// Inbound messages
Some(message) => match message {
NetworkMessage::PubsubMessage { topic, message } => {
if let Err(e) = swarm_stream.get_mut().behaviour_mut().publish(topic, message) {
warn!("Failed to send gossipsub message: {:?}", e);
}
}
NetworkMessage::HelloRequest { peer_id, request, response_channel } => {
swarm_stream.get_mut().behaviour_mut().send_hello_request(&peer_id, request, response_channel);
}
NetworkMessage::ChainExchangeRequest { peer_id, request, response_channel } => {
swarm_stream.get_mut().behaviour_mut().send_chain_exchange_request(&peer_id, request, response_channel);
}
NetworkMessage::BitswapRequest { cid, response_channel } => {
if let Err(e) = swarm_stream.get_mut().behaviour_mut().want_block(cid, 1000) {
warn!("Failed to send a bitswap want_block: {}", e.to_string());
} else if let Some(chans) = self.bitswap_response_channels.get_mut(&cid) {
chans.push(response_channel);
} else {
self.bitswap_response_channels.insert(cid, vec![response_channel]);
}
}
NetworkMessage::JSONRPCRequest { method } => {
match method {
NetRPCMethods::NetAddrsListen(response_channel) => {
let listeners: Vec<_> = Swarm::listeners( swarm_stream.get_mut()).cloned().collect();
let peer_id = Swarm::local_peer_id(swarm_stream.get_mut());
if response_channel.send((*peer_id, listeners)).is_err() {
warn!("Failed to get Libp2p listeners");
}
}
NetRPCMethods::NetPeers(response_channel) => {
let peer_addresses: &HashMap<PeerId, Vec<Multiaddr>> = swarm_stream.get_mut().behaviour_mut().peer_addresses();
if response_channel.send(peer_addresses.to_owned()).is_err() {
warn!("Failed to get Libp2p peers");
}
}
NetRPCMethods::NetConnect(response_channel, peer_id, addresses) => {
let mut addresses = addresses.clone();
let mut success = false;
for multiaddr in addresses.iter_mut() {
multiaddr.push(Protocol::P2p(Multihash::from_bytes(&peer_id.to_bytes()).unwrap()));
if Swarm::dial(swarm_stream.get_mut(), multiaddr.clone()).is_ok() {
success = true;
break;
};
}
if response_channel.send(success).is_err() {
warn!("Failed to connect to a peer");
}
}
NetRPCMethods::NetDisconnect(response_channel, peer_id) => {
let _ = Swarm::disconnect_peer_id(swarm_stream.get_mut(), peer_id);
if response_channel.send(()).is_err() {
warn!("Failed to disconnect from a peer");
}
}
}
}
}
None => { break; }
},
interval_event = interval.next() => if interval_event.is_some() {
// Print peer count on an interval.
debug!("Peers connected: {}", swarm_stream.get_mut().behaviour_mut().peers().len());
}
};
}
}
/// Returns a sender which allows sending messages to the libp2p service.
pub fn network_sender(&self) -> Sender<NetworkMessage> {
self.network_sender_in.clone()
}
/// Returns a receiver to listen to network events emitted from the service.
pub fn network_receiver(&self) -> Receiver<NetworkEvent> {
self.network_receiver_out.clone()
}
}
async fn emit_event(sender: &Sender<NetworkEvent>, event: NetworkEvent) {
if sender.send(event).await.is_err() {
error!("Failed to emit event: Network channel receiver has been dropped");
}
}
/// Builds the transport stack that libp2p will communicate over.
pub async fn build_transport(local_key: Keypair) -> Boxed<(PeerId, StreamMuxerBox)> {
let tcp_transport =
|| libp2p::tcp::TcpTransport::new(libp2p::tcp::GenTcpConfig::new().nodelay(true));
let transport = libp2p::websocket::WsConfig::new(tcp_transport()).or_transport(tcp_transport());
let transport = libp2p::dns::DnsConfig::system(transport).await.unwrap();
let auth_config = {
let dh_keys = noise::Keypair::<noise::X25519Spec>::new()
.into_authentic(&local_key)
.expect("Noise key generation failed");
noise::NoiseConfig::xx(dh_keys).into_authenticated()
};
let mplex_config = {
let mut mplex_config = mplex::MplexConfig::new();
mplex_config.set_max_buffer_size(usize::MAX);
let mut yamux_config = yamux::YamuxConfig::default();
yamux_config.set_max_buffer_size(16 * 1024 * 1024);
yamux_config.set_receive_window_size(16 * 1024 * 1024);
// yamux_config.set_window_update_mode(WindowUpdateMode::OnRead);
core::upgrade::SelectUpgrade::new(yamux_config, mplex_config)
};
transport
.upgrade(core::upgrade::Version::V1)
.authenticate(auth_config)
.multiplex(mplex_config)
.timeout(Duration::from_secs(20))
.boxed()
}
/// Fetch key-pair from disk, returning none if it cannot be decoded.
pub fn get_keypair(path: &Path) -> Option<Keypair> {
match read_file_to_vec(path) {
Err(e) => {
info!("Networking keystore not found!");
trace!("Error {:?}", e);
None
}
Ok(mut vec) => match ed25519::Keypair::decode(&mut vec) {
Ok(kp) => {
info!("Recovered libp2p keypair from {:?}", &path);
Some(Keypair::Ed25519(kp))
}
Err(e) => {
info!("Could not decode networking keystore!");
trace!("Error {:?}", e);
None
}
},
}
}