forked from rs-ipfs/rust-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitswap.rs
73 lines (60 loc) · 2.17 KB
/
bitswap.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
use cid::{Cid, Codec};
use ipfs::Block;
use multihash::Sha2_256;
use std::time::Duration;
use tokio::time;
mod common;
use common::{spawn_nodes, Topology};
// Ensure that the Bitswap object doesn't leak.
#[tokio::test(max_threads = 1)]
async fn check_bitswap_cleanups() {
// create a few nodes and connect the first one to others
let mut nodes = spawn_nodes(3, Topology::Star).await;
let bitswap_peers = nodes[0].get_bitswap_peers().await.unwrap();
assert_eq!(bitswap_peers.len(), 2);
// last node says goodbye; check the number of bitswap peers
if let Some(node) = nodes.pop() {
node.shutdown().await;
time::delay_for(Duration::from_millis(200)).await;
}
let bitswap_peers = nodes[0].get_bitswap_peers().await.unwrap();
assert_eq!(bitswap_peers.len(), 1);
// another node says goodbye; check the number of bitswap peers
if let Some(node) = nodes.pop() {
node.shutdown().await;
time::delay_for(Duration::from_millis(200)).await;
}
let bitswap_peers = nodes[0].get_bitswap_peers().await.unwrap();
assert!(bitswap_peers.is_empty());
}
// this test is designed to trigger unfavorable conditions for the bitswap
// protocol by putting blocks in every second node and attempting to get
// them from the other nodes; intended to be used for debugging or stress
// testing the bitswap protocol (though it would be advised to uncomment
// the tracing_subscriber for stress-testing purposes)
#[ignore]
#[tokio::test(max_threads = 1)]
async fn bitswap_stress_test() {
fn filter(i: usize) -> bool {
i % 2 == 0
}
tracing_subscriber::fmt::init();
let data = b"hello block\n".to_vec().into_boxed_slice();
let cid = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data));
let nodes = spawn_nodes(5, Topology::Mesh).await;
for (i, node) in nodes.iter().enumerate() {
if filter(i) {
node.put_block(Block {
cid: cid.clone(),
data: data.clone(),
})
.await
.unwrap();
}
}
for (i, node) in nodes.iter().enumerate() {
if !filter(i) {
node.get_block(&cid).await.unwrap();
}
}
}