This repository was archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.rs
149 lines (137 loc) · 5.75 KB
/
connect.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
mod common;
use async_std::{
channel,
task,
};
use chrono::{DateTime, Utc};
use common::{
action_dispatcher,
deserialize,
PeerEvent,
PeerManager,
};
use tp2p::{
event::Event,
message::MessageWrapped,
peer::{Keypair, PeeringConfig, ConfirmationMode, SubscriptionConfig},
sync::Sync,
};
fn now() -> DateTime<Utc> {
Utc::now()
}
macro_rules! main_loop {
($sync:expr, $peer:expr, $tx:expr, $rx:expr, $name:expr, $U:ty, $T:ty, $S:ty) => {
loop {
match $rx.try_recv() {
Ok(PeerEvent::Connect(from)) => {
println!(concat!($name, ": new connection! {:?}"), from);
}
Ok(PeerEvent::Recv(from, message_bytes)) => {
let msg: MessageWrapped = deserialize(message_bytes.as_ref())?;
let actions = match &msg {
MessageWrapped::Init(pubkey) => {
$sync.process_init_message(&msg, &from, &now())?
}
MessageWrapped::Sealed(sealed) => {
let message_opened = $sync.unwrap_incoming_message(sealed)?;
match message_opened.body() {
Event::Hello => {
$sync.process_event_hello(&message_opened, sealed.pubkey_sender(), &from, &now())?
}
Event::PeerInit { .. } => {
$sync.process_event_peer_init(&message_opened, sealed.pubkey_sender(), &from, &now())?
}
Event::PeerConfirm { .. } => {
$sync.process_event_peer_confirm(&message_opened, sealed.pubkey_sender(), &now())?
}
Event::Ping => {
$sync.process_event_ping(&message_opened, sealed.pubkey_sender(), &now())?
}
Event::Pong => {
$sync.process_event_pong(&message_opened, sealed.pubkey_sender(), &now())?
}
Event::QueryMessagesByID { ids } => {
let messages = {
drop(ids);
vec![]
};
$sync.process_event_query_messages_by_id(&message_opened, sealed.pubkey_sender(), &messages)?
}
Event::QueryMessagesByDepth { topic, depth } => {
let messages = {
drop(topic);
drop(depth);
vec![]
};
$sync.process_event_query_messages_by_depth(&message_opened, sealed.pubkey_sender(), &messages)?
}
Event::Subscribe(..) => {
}
_ => panic!("oi"),
}
}
};
for action in actions {
println!(concat!($name, ": action -- {:?}"), action);
action_dispatcher::<$U, $T, $S>(&mut $sync, &$tx, action).await?;
}
}
Err(channel::TryRecvError::Closed) => {
break;
}
_ => {}
}
task::sleep(std::time::Duration::from_millis(50)).await;
}
}
}
#[async_std::test]
async fn peer_connect() -> Result<(), String> {
let peer1_task = task::spawn(async move {
let keypair = Keypair::new_random();
let peering_config = PeeringConfig::new(
ConfirmationMode::PublicAgent {
whitelist: vec![],
blacklist: vec![],
},
SubscriptionConfig::Blacklist(vec![]),
);
let mut sync = Sync::new("uno".into(), keypair, peering_config);
let (peer, tx, rx) = PeerManager::new();
let peer_task = task::spawn(async move {
peer.start("127.0.0.1", 50020).await.expect("error running peer");
});
main_loop! { sync , peer, tx, rx, "peer1", (), (), () }
peer_task.await;
let res: Result<(), String> = Ok(());
res
});
let peer2_task = task::spawn(async move {
task::sleep(std::time::Duration::from_millis(50)).await;
let keypair = Keypair::new_random();
let peering_config = PeeringConfig::new(
ConfirmationMode::PublicAgent {
whitelist: vec![],
blacklist: vec![],
},
SubscriptionConfig::Blacklist(vec![]),
);
let mut sync = Sync::new("twofer".into(), keypair, peering_config);
let (peer, tx, rx) = PeerManager::new();
let peer_task = task::spawn(async move {
peer.start("127.0.0.1", 50021).await.expect("error running peer");
});
let actions = sync.init_comm::<(), ()>("127.0.0.1:50020", &now()).expect("peer_init failed");
for action in actions {
println!("peer2: action -- {:?}", action);
action_dispatcher(&mut sync, &tx, action).await?;
}
main_loop! { sync , peer, tx, rx, "peer2", (), (), () }
peer_task.await;
let res: Result<(), String> = Ok(());
res
});
let res = futures::try_join!(peer1_task, peer2_task);
assert_eq!(res, Ok(((), ())));
Ok(())
}