forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 94
/
lp_network.rs
524 lines (474 loc) · 17.9 KB
/
lp_network.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/******************************************************************************
* Copyright © 2022 Atomic Private Limited and its contributors *
* *
* See the CONTRIBUTOR-LICENSE-AGREEMENT, COPYING, LICENSE-COPYRIGHT-NOTICE *
* and DEVELOPER-CERTIFICATE-OF-ORIGIN files in the LEGAL directory in *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* AtomicDEX software, including this file may be copied, modified, propagated*
* or distributed except according to the terms contained in the *
* LICENSE-COPYRIGHT-NOTICE file. *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
//
// lp_network.rs
// marketmaker
//
use coins::lp_coinfind;
use common::executor::SpawnFuture;
use common::{log, Future01CompatExt};
use derive_more::Display;
use futures::{channel::oneshot, StreamExt};
use instant::Instant;
use keys::KeyPair;
use mm2_core::mm_ctx::{MmArc, MmWeak};
use mm2_err_handle::prelude::*;
use mm2_libp2p::{decode_message, encode_message, DecodingError, GossipsubMessage, Libp2pPublic, Libp2pSecpPublic,
MessageId, NetworkPorts, PeerId, TopicHash, TOPIC_SEPARATOR};
use mm2_libp2p::{AdexBehaviourCmd, AdexBehaviourEvent, AdexCmdTx, AdexEventRx, AdexResponse};
use mm2_libp2p::{PeerAddresses, RequestResponseBehaviourEvent};
use mm2_metrics::{mm_label, mm_timing};
#[cfg(test)] use mocktopus::macros::*;
use parking_lot::Mutex as PaMutex;
use serde::de;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use crate::mm2::lp_ordermatch;
use crate::mm2::{lp_stats, lp_swap};
pub type P2PRequestResult<T> = Result<T, MmError<P2PRequestError>>;
pub trait Libp2pPeerId {
fn libp2p_peer_id(&self) -> PeerId;
}
impl Libp2pPeerId for KeyPair {
#[inline(always)]
fn libp2p_peer_id(&self) -> PeerId { peer_id_from_secp_public(self.public_slice()).expect("valid public") }
}
#[derive(Debug, Display)]
#[allow(clippy::enum_variant_names)]
pub enum P2PRequestError {
EncodeError(String),
DecodeError(String),
SendError(String),
ResponseError(String),
#[display(fmt = "Expected 1 response, found {}", _0)]
ExpectedSingleResponseError(usize),
}
impl From<rmp_serde::encode::Error> for P2PRequestError {
fn from(e: rmp_serde::encode::Error) -> Self { P2PRequestError::EncodeError(e.to_string()) }
}
impl From<rmp_serde::decode::Error> for P2PRequestError {
fn from(e: rmp_serde::decode::Error) -> Self { P2PRequestError::DecodeError(e.to_string()) }
}
#[derive(Eq, Debug, Deserialize, PartialEq, Serialize)]
pub enum P2PRequest {
Ordermatch(lp_ordermatch::OrdermatchRequest),
NetworkInfo(lp_stats::NetworkInfoRequest),
}
pub struct P2PContext {
/// Using Mutex helps to prevent cloning which can actually result to channel being unbounded in case of using 1 tx clone per 1 message.
pub cmd_tx: PaMutex<AdexCmdTx>,
}
#[cfg_attr(test, mockable)]
impl P2PContext {
pub fn new(cmd_tx: AdexCmdTx) -> Self {
P2PContext {
cmd_tx: PaMutex::new(cmd_tx),
}
}
pub fn store_to_mm_arc(self, ctx: &MmArc) { *ctx.p2p_ctx.lock().unwrap() = Some(Arc::new(self)) }
pub fn fetch_from_mm_arc(ctx: &MmArc) -> Arc<Self> {
ctx.p2p_ctx
.lock()
.unwrap()
.as_ref()
.unwrap()
.clone()
.downcast()
.unwrap()
}
}
pub async fn p2p_event_process_loop(ctx: MmWeak, mut rx: AdexEventRx, i_am_relay: bool) {
loop {
let adex_event = rx.next().await;
let ctx = match MmArc::from_weak(&ctx) {
Some(ctx) => ctx,
None => return,
};
match adex_event {
Some(AdexBehaviourEvent::Gossipsub(event)) => match event {
mm2_libp2p::GossipsubEvent::Message {
propagation_source,
message_id,
message,
} => {
let spawner = ctx.spawner();
spawner.spawn(process_p2p_message(
ctx,
propagation_source,
message_id,
message,
i_am_relay,
));
},
mm2_libp2p::GossipsubEvent::GossipsubNotSupported { peer_id } => {
log::error!("Received unsupported event from Peer: {peer_id}");
},
_ => {},
},
Some(AdexBehaviourEvent::RequestResponse(RequestResponseBehaviourEvent::InboundRequest {
peer_id,
request,
response_channel,
})) => {
if let Err(e) = process_p2p_request(ctx, peer_id, request.req, response_channel.into()) {
log::error!("Error on process P2P request: {:?}", e);
}
},
_ => (),
}
}
}
async fn process_p2p_message(
ctx: MmArc,
peer_id: PeerId,
message_id: MessageId,
message: GossipsubMessage,
i_am_relay: bool,
) {
let mut to_propagate = false;
// message.topics.dedup();
drop_mutability!(message);
let inform_about_break = |used: &str, all: &TopicHash| {
log::debug!(
"Topic '{}' proceed and loop is killed. Whole topic list was '{:?}'",
used,
all
);
};
let topic = message.topic;
let mut split = topic.as_str().split(TOPIC_SEPARATOR);
match split.next() {
Some(lp_ordermatch::ORDERBOOK_PREFIX) => {
let fut = lp_ordermatch::handle_orderbook_msg(
ctx.clone(),
&topic,
peer_id.to_string(),
&message.data,
i_am_relay,
);
if let Err(e) = fut.await {
if e.get_inner().is_warning() {
log::warn!("{}", e);
} else {
log::error!("{}", e);
}
return;
}
to_propagate = true;
},
Some(lp_swap::SWAP_PREFIX) => {
if let Err(e) =
lp_swap::process_swap_msg(ctx.clone(), split.next().unwrap_or_default(), &message.data).await
{
log::error!("{}", e);
return;
}
to_propagate = true;
inform_about_break(topic.as_str(), &topic);
},
Some(lp_swap::WATCHER_PREFIX) => {
if ctx.is_watcher() {
if let Err(e) = lp_swap::process_watcher_msg(ctx.clone(), &message.data) {
log::error!("{}", e);
return;
}
}
to_propagate = true;
inform_about_break(topic.as_str(), &topic);
},
Some(lp_swap::TX_HELPER_PREFIX) => {
if let Some(pair) = split.next() {
if let Ok(Some(coin)) = lp_coinfind(&ctx, pair).await {
if let Err(e) = coin.tx_enum_from_bytes(&message.data) {
log::error!("Message cannot continue the process due to: {:?}", e);
return;
};
let fut = coin.send_raw_tx_bytes(&message.data);
ctx.spawner().spawn(async {
match fut.compat().await {
Ok(id) => log::debug!("Transaction broadcasted successfully: {:?} ", id),
// TODO (After https://github.com/KomodoPlatform/atomicDEX-API/pull/1433)
// Maybe do not log an error if the transaction is already sent to
// the blockchain
Err(e) => log::error!("Broadcast transaction failed (ignore this error if the transaction already sent by another seednode). {}", e),
};
})
}
}
inform_about_break(topic.as_str(), &topic);
},
None | Some(_) => (),
}
if to_propagate && i_am_relay {
propagate_message(&ctx, message_id, peer_id);
}
}
fn process_p2p_request(
ctx: MmArc,
_peer_id: PeerId,
request: Vec<u8>,
response_channel: mm2_libp2p::AdexResponseChannel,
) -> P2PRequestResult<()> {
let request = decode_message::<P2PRequest>(&request)?;
let result = match request {
P2PRequest::Ordermatch(req) => lp_ordermatch::process_peer_request(ctx.clone(), req),
P2PRequest::NetworkInfo(req) => lp_stats::process_info_request(ctx.clone(), req),
};
let res = match result {
Ok(Some(response)) => AdexResponse::Ok { response },
Ok(None) => AdexResponse::None,
Err(e) => AdexResponse::Err { error: e },
};
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);
let cmd = AdexBehaviourCmd::SendResponse { res, response_channel };
p2p_ctx
.cmd_tx
.lock()
.try_send(cmd)
.map_to_mm(|e| P2PRequestError::SendError(e.to_string()))?;
Ok(())
}
pub fn broadcast_p2p_msg(ctx: &MmArc, topic: String, msg: Vec<u8>, from: Option<PeerId>) {
let ctx = ctx.clone();
let cmd = match from {
Some(from) => AdexBehaviourCmd::PublishMsgFrom { topic, msg, from },
None => AdexBehaviourCmd::PublishMsg { topic, msg },
};
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);
if let Err(e) = p2p_ctx.cmd_tx.lock().try_send(cmd) {
log::error!("broadcast_p2p_msg cmd_tx.send error {:?}", e);
};
}
/// Subscribe to the given `topic`.
///
/// # Safety
///
/// The function locks the [`MmCtx::p2p_ctx`] mutex.
pub fn subscribe_to_topic(ctx: &MmArc, topic: String) {
let p2p_ctx = P2PContext::fetch_from_mm_arc(ctx);
let cmd = AdexBehaviourCmd::Subscribe { topic };
if let Err(e) = p2p_ctx.cmd_tx.lock().try_send(cmd) {
log::error!("subscribe_to_topic cmd_tx.send error {:?}", e);
};
}
pub async fn request_any_relay<T: de::DeserializeOwned>(
ctx: MmArc,
req: P2PRequest,
) -> P2PRequestResult<Option<(T, PeerId)>> {
let encoded = encode_message(&req)?;
let (response_tx, response_rx) = oneshot::channel();
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);
let cmd = AdexBehaviourCmd::RequestAnyRelay {
req: encoded,
response_tx,
};
p2p_ctx
.cmd_tx
.lock()
.try_send(cmd)
.map_to_mm(|e| P2PRequestError::SendError(e.to_string()))?;
match response_rx
.await
.map_to_mm(|e| P2PRequestError::ResponseError(e.to_string()))?
{
Some((from_peer, response)) => {
let response = decode_message::<T>(&response)?;
Ok(Some((response, from_peer)))
},
None => Ok(None),
}
}
pub enum PeerDecodedResponse<T> {
Ok(T),
None,
Err(String),
}
#[allow(dead_code)]
pub async fn request_relays<T: de::DeserializeOwned>(
ctx: MmArc,
req: P2PRequest,
) -> P2PRequestResult<Vec<(PeerId, PeerDecodedResponse<T>)>> {
let encoded = encode_message(&req)?;
let (response_tx, response_rx) = oneshot::channel();
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);
let cmd = AdexBehaviourCmd::RequestRelays {
req: encoded,
response_tx,
};
p2p_ctx
.cmd_tx
.lock()
.try_send(cmd)
.map_to_mm(|e| P2PRequestError::SendError(e.to_string()))?;
let responses = response_rx
.await
.map_to_mm(|e| P2PRequestError::ResponseError(e.to_string()))?;
Ok(parse_peers_responses(responses))
}
pub async fn request_peers<T: de::DeserializeOwned>(
ctx: MmArc,
req: P2PRequest,
peers: Vec<String>,
) -> P2PRequestResult<Vec<(PeerId, PeerDecodedResponse<T>)>> {
let encoded = encode_message(&req)?;
let (response_tx, response_rx) = oneshot::channel();
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);
let cmd = AdexBehaviourCmd::RequestPeers {
req: encoded,
peers,
response_tx,
};
p2p_ctx
.cmd_tx
.lock()
.try_send(cmd)
.map_to_mm(|e| P2PRequestError::SendError(e.to_string()))?;
let responses = response_rx
.await
.map_to_mm(|e| P2PRequestError::ResponseError(e.to_string()))?;
Ok(parse_peers_responses(responses))
}
pub async fn request_one_peer<T: de::DeserializeOwned>(
ctx: MmArc,
req: P2PRequest,
peer: String,
) -> P2PRequestResult<Option<T>> {
let start = Instant::now();
let mut responses = request_peers::<T>(ctx.clone(), req, vec![peer.clone()]).await?;
let elapsed = start.elapsed();
mm_timing!(ctx.metrics, "peer.outgoing_request.timing", elapsed, "peer" => peer);
if responses.len() != 1 {
return MmError::err(P2PRequestError::ExpectedSingleResponseError(responses.len()));
}
let (_, response) = responses.remove(0);
match response {
PeerDecodedResponse::Ok(response) => Ok(Some(response)),
PeerDecodedResponse::None => Ok(None),
PeerDecodedResponse::Err(e) => MmError::err(P2PRequestError::ResponseError(e)),
}
}
fn parse_peers_responses<T: de::DeserializeOwned>(
responses: Vec<(PeerId, AdexResponse)>,
) -> Vec<(PeerId, PeerDecodedResponse<T>)> {
responses
.into_iter()
.map(|(peer_id, res)| {
let res = match res {
AdexResponse::Ok { response } => match decode_message::<T>(&response) {
Ok(res) => PeerDecodedResponse::Ok(res),
Err(e) => PeerDecodedResponse::Err(ERRL!("{}", e)),
},
AdexResponse::None => PeerDecodedResponse::None,
AdexResponse::Err { error } => PeerDecodedResponse::Err(error),
};
(peer_id, res)
})
.collect()
}
pub fn propagate_message(ctx: &MmArc, message_id: MessageId, propagation_source: PeerId) {
let ctx = ctx.clone();
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);
let cmd = AdexBehaviourCmd::PropagateMessage {
message_id,
propagation_source,
};
if let Err(e) = p2p_ctx.cmd_tx.lock().try_send(cmd) {
log::error!("propagate_message cmd_tx.send error {:?}", e);
};
}
pub fn add_reserved_peer_addresses(ctx: &MmArc, peer: PeerId, addresses: PeerAddresses) {
let ctx = ctx.clone();
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);
let cmd = AdexBehaviourCmd::AddReservedPeer { peer, addresses };
if let Err(e) = p2p_ctx.cmd_tx.lock().try_send(cmd) {
log::error!("add_reserved_peer_addresses cmd_tx.send error {:?}", e);
};
}
#[derive(Debug, Display)]
pub enum ParseAddressError {
#[display(fmt = "Address/Seed {} resolved to IPv6 which is not supported", _0)]
UnsupportedIPv6Address(String),
#[display(fmt = "Address/Seed {} to_socket_addrs empty iter", _0)]
EmptyIterator(String),
#[display(fmt = "Couldn't resolve '{}' Address/Seed: {}", _0, _1)]
UnresolvedAddress(String, String),
}
#[cfg(not(target_arch = "wasm32"))]
pub fn addr_to_ipv4_string(address: &str) -> Result<String, MmError<ParseAddressError>> {
// Remove "https:// or http://" etc.. from address str
let formated_address = address.split("://").last().unwrap_or(address);
let address_with_port = if formated_address.contains(':') {
formated_address.to_string()
} else {
format!("{}:0", formated_address)
};
match address_with_port.as_str().to_socket_addrs() {
Ok(mut iter) => match iter.next() {
Some(addr) => {
if addr.is_ipv4() {
Ok(addr.ip().to_string())
} else {
log::warn!(
"Address/Seed {} resolved to IPv6 {} which is not supported",
address,
addr
);
MmError::err(ParseAddressError::UnsupportedIPv6Address(address.into()))
}
},
None => {
log::warn!("Address/Seed {} to_socket_addrs empty iter", address);
MmError::err(ParseAddressError::EmptyIterator(address.into()))
},
},
Err(e) => {
log::error!("Couldn't resolve '{}' seed: {}", address, e);
MmError::err(ParseAddressError::UnresolvedAddress(address.into(), e.to_string()))
},
}
}
#[derive(Clone, Debug, Display, Serialize)]
pub enum NetIdError {
#[display(fmt = "Netid {} is larger than max {}", netid, max_netid)]
LargerThanMax { netid: u16, max_netid: u16 },
}
pub fn lp_ports(netid: u16) -> Result<(u16, u16, u16), MmError<NetIdError>> {
const LP_RPCPORT: u16 = 7783;
let max_netid = (65535 - 40 - LP_RPCPORT) / 4;
if netid > max_netid {
return MmError::err(NetIdError::LargerThanMax { netid, max_netid });
}
let other_ports = if netid != 0 {
let net_mod = netid % 10;
let net_div = netid / 10;
(net_div * 40) + LP_RPCPORT + net_mod
} else {
LP_RPCPORT
};
Ok((other_ports + 10, other_ports + 20, other_ports + 30))
}
pub fn lp_network_ports(netid: u16) -> Result<NetworkPorts, MmError<NetIdError>> {
let (_, network_port, network_wss_port) = lp_ports(netid)?;
Ok(NetworkPorts {
tcp: network_port,
wss: network_wss_port,
})
}
pub fn peer_id_from_secp_public(secp_public: &[u8]) -> Result<PeerId, MmError<DecodingError>> {
let public_key = Libp2pSecpPublic::try_from_bytes(secp_public).unwrap();
Ok(PeerId::from_public_key(&Libp2pPublic::from(public_key)))
}