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
// Copyright 2022 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.
use ;
use Bytes;
use QuicP2p;
use Keypair;
use ServiceMsg;
use ;
use crate;
use ;
use *;
use XorName;
use craterouting;
/*
#[tokio::test(flavor = "multi_thread")]
async fn test_messages_client_node() -> Result<()> {
let (node, mut event_stream) = create_node(Config {
first: true,
..Default::default()
})
.await?;
// create a client message
let mut rng = rand::thread_rng();
let keypair = Keypair::new_ed25519(&mut rng);
let pk = keypair.public_key();
let auth = ServiceAuth {
public_key: pk,
signature: keypair.sign(b"the msg"),
};
let id = MsgId::new();
// create a client which sends a message to the node
let mut config = routing::TransportConfig {
local_ip: Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))),
..Default::default()
};
config.local_ip = Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
let node_addr = node.our_connection_info();
let section_prefix = node.our_prefix().await;
let section_key = *node.section_chain().await.last_key();
let client = QuicP2p::<XorName>::with_config(Some(config), &[node_addr], false)?;
let (client_endpoint, _, mut incoming_messages, _) = client.new_endpoint().await?;
client_endpoint.connect_to(&node_addr).await?;
let query = ServiceMsg::Query {
id,
query: Query::Transfer(TransferQuery::GetBalance(pk)),
auth,
});
let query_clone = query.clone();
// spawn node events listener
let node_handler = tokio::spawn(async move {
while let Some(event) = event_stream.next().await {
match event {
Event::ServiceMsgReceived { msg, user } => {
assert_eq!(*msg, query_clone.clone());
node.send_msg(
Itinerary {
src: SrcLocation::Node(node.name().await),
dst: DstLocation::EndUser(user),
aggregation: Aggregation::None,
},
query_clone
.clone()
.serialize(XorName::from(pk), section_key)?,
None,
)
.await?;
break;
}
other => println!("Ignoring msg: {:?}", other),
}
}
Ok::<(), Error>(())
});
let query_bytes = query.serialize(XorName::from(pk), section_key)?;
client_endpoint
.send_msg(query_bytes.clone(), &node_addr)
.await?;
// just await for node to respond to client
node_handler.await??;
if let Some((_, resp)) = incoming_messages.next().await {
// the xorname assigned to each end user is computed from
// the client socket addr plus the client section prefix
let socket_id =
XorName::from_content(&[&bincode::serialize(&client_endpoint.socket_addr())?]);
let user_xorname = section_prefix.substituted_in(socket_id);
let expected_bytes = query.serialize(user_xorname, section_key)?;
assert_eq!(resp, expected_bytes);
let response_decoded = ServiceMsg::from(resp)?;
assert_eq!(response_decoded, query);
Ok(())
} else {
Err(anyhow!("Failed to read from incoming messages channel"))
}
}
#[tokio::test(flavor = "multi_thread")]
async fn test_messages_between_nodes() -> Result<()> {
let msg = b"hello!";
let response = b"good bye!";
let (node1, mut event_stream) = create_node(Config {
first: true,
..Default::default()
})
.await?;
let node1_contact = node1.our_connection_info();
let node1_name = node1.name().await;
println!("spawning node handler");
// spawn node events listener
let node_handler = tokio::spawn(async move {
while let Some(event) = event_stream.next().await {
match event {
Event::MessageReceived { content, src, .. } => {
assert_eq!(content, Bytes::from_static(msg));
return Ok(src.to_dst());
}
_other => {}
}
}
Err(format_err!("message not received"))
});
println!("node handler spawned");
// start a second node which sends a message to the first node
let (node2, mut event_stream) = create_node(config_with_contact(node1_contact)).await?;
assert_event!(
event_stream,
Event::EldersChanged {
self_status_change: NodeElderChange::Promoted,
..
}
);
let node2_name = node2.name().await;
println!("sending msg..");
let itinerary = Itinerary {
src: SrcLocation::Node(node2_name),
dst: DstLocation::Node(node1_name),
aggregation: Aggregation::None,
};
node2
.send_msg(itinerary, Bytes::from_static(msg), None)
.await?;
println!("msg sent");
// just await for node1 to receive message from node2
let dst = node_handler.await??;
println!("Got dst: {:?} (expecting: {}", dst.name(), node2_name);
println!("sending response from {:?}..", node1_name);
let itinerary = Itinerary {
src: SrcLocation::Node(node1_name),
dst,
aggregation: Aggregation::None,
};
// send response from node1 to node2
node1
.send_msg(itinerary, Bytes::from_static(response), None)
.await?;
println!("checking response received..");
// check we received the response message from node1
while let Some(event) = event_stream.next().await {
match event {
Event::MessageReceived { content, src, .. } => {
assert_eq!(content, Bytes::from_static(response));
assert_eq!(src, SrcLocation::Node(node1_name));
return Ok(());
}
_other => {}
}
}
Err(format_err!("message not received"))
}
*/