2024-02-25 21:21:25 +00:00
|
|
|
use std::{sync::Arc, time::Duration};
|
2023-11-30 18:09:05 +00:00
|
|
|
|
2024-03-05 01:29:57 +00:00
|
|
|
use futures::StreamExt;
|
2024-02-25 21:21:25 +00:00
|
|
|
use tokio::{
|
2024-03-05 01:29:57 +00:00
|
|
|
io::{duplex, split},
|
2024-05-02 22:58:22 +00:00
|
|
|
sync::Semaphore,
|
2024-02-25 21:21:25 +00:00
|
|
|
time::timeout,
|
|
|
|
};
|
2024-03-05 01:29:57 +00:00
|
|
|
use tokio_util::codec::{FramedRead, FramedWrite};
|
2023-11-30 18:09:05 +00:00
|
|
|
use tower::{Service, ServiceExt};
|
|
|
|
|
2024-01-22 01:56:34 +00:00
|
|
|
use cuprate_helper::network::Network;
|
2024-06-24 01:30:47 +00:00
|
|
|
use cuprate_wire::{common::PeerSupportFlags, BasicNodeData, MoneroWireCodec};
|
2023-11-30 18:09:05 +00:00
|
|
|
|
2024-06-24 01:30:47 +00:00
|
|
|
use cuprate_p2p_core::{
|
2024-02-25 21:21:25 +00:00
|
|
|
client::{ConnectRequest, Connector, DoHandshakeRequest, HandShaker, InternalPeerID},
|
|
|
|
network_zones::{ClearNet, ClearNetServerCfg},
|
|
|
|
ConnectionDirection, NetworkZone,
|
2023-11-30 18:09:05 +00:00
|
|
|
};
|
|
|
|
|
2024-02-12 13:39:15 +00:00
|
|
|
use cuprate_test_utils::{
|
|
|
|
monerod::monerod,
|
|
|
|
test_netzone::{TestNetZone, TestNetZoneAddr},
|
|
|
|
};
|
2023-11-30 18:09:05 +00:00
|
|
|
|
|
|
|
mod utils;
|
|
|
|
use utils::*;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn handshake_cuprate_to_cuprate() {
|
|
|
|
// Tests a Cuprate <-> Cuprate handshake by making 2 handshake services and making them talk to
|
|
|
|
// each other.
|
|
|
|
|
2024-01-13 00:07:35 +00:00
|
|
|
let semaphore = Arc::new(Semaphore::new(10));
|
|
|
|
let permit_1 = semaphore.clone().acquire_owned().await.unwrap();
|
|
|
|
let permit_2 = semaphore.acquire_owned().await.unwrap();
|
|
|
|
|
2023-11-30 18:09:05 +00:00
|
|
|
let our_basic_node_data_1 = BasicNodeData {
|
|
|
|
my_port: 0,
|
2024-05-02 22:58:22 +00:00
|
|
|
network_id: Network::Mainnet.network_id(),
|
2023-11-30 18:09:05 +00:00
|
|
|
peer_id: 87980,
|
|
|
|
// TODO: This fails if the support flags are empty (0)
|
|
|
|
support_flags: PeerSupportFlags::from(1_u32),
|
|
|
|
rpc_port: 0,
|
|
|
|
rpc_credits_per_hash: 0,
|
|
|
|
};
|
|
|
|
// make sure both node IDs are different
|
|
|
|
let mut our_basic_node_data_2 = our_basic_node_data_1.clone();
|
|
|
|
our_basic_node_data_2.peer_id = 2344;
|
|
|
|
|
2024-05-02 22:58:22 +00:00
|
|
|
let mut handshaker_1 = HandShaker::<TestNetZone<true, true, true>, _, _, _, _, _>::new(
|
2023-11-30 18:09:05 +00:00
|
|
|
DummyAddressBook,
|
2024-05-02 22:58:22 +00:00
|
|
|
DummyPeerSyncSvc,
|
2023-11-30 18:09:05 +00:00
|
|
|
DummyCoreSyncSvc,
|
|
|
|
DummyPeerRequestHandlerSvc,
|
2024-05-02 22:58:22 +00:00
|
|
|
|_| futures::stream::pending(),
|
2023-11-30 18:09:05 +00:00
|
|
|
our_basic_node_data_1,
|
|
|
|
);
|
|
|
|
|
2024-05-02 22:58:22 +00:00
|
|
|
let mut handshaker_2 = HandShaker::<TestNetZone<true, true, true>, _, _, _, _, _>::new(
|
2023-11-30 18:09:05 +00:00
|
|
|
DummyAddressBook,
|
2024-05-02 22:58:22 +00:00
|
|
|
DummyPeerSyncSvc,
|
2023-11-30 18:09:05 +00:00
|
|
|
DummyCoreSyncSvc,
|
|
|
|
DummyPeerRequestHandlerSvc,
|
2024-05-02 22:58:22 +00:00
|
|
|
|_| futures::stream::pending(),
|
2023-11-30 18:09:05 +00:00
|
|
|
our_basic_node_data_2,
|
|
|
|
);
|
|
|
|
|
2024-03-05 01:29:57 +00:00
|
|
|
let (p1, p2) = duplex(50_000);
|
|
|
|
|
|
|
|
let (p1_receiver, p1_sender) = split(p1);
|
|
|
|
let (p2_receiver, p2_sender) = split(p2);
|
2023-11-30 18:09:05 +00:00
|
|
|
|
|
|
|
let p1_handshake_req = DoHandshakeRequest {
|
2024-01-13 00:07:35 +00:00
|
|
|
addr: InternalPeerID::KnownAddr(TestNetZoneAddr(888)),
|
2024-03-05 01:29:57 +00:00
|
|
|
peer_stream: FramedRead::new(p2_receiver, MoneroWireCodec::default()),
|
|
|
|
peer_sink: FramedWrite::new(p2_sender, MoneroWireCodec::default()),
|
2023-11-30 18:09:05 +00:00
|
|
|
direction: ConnectionDirection::OutBound,
|
2024-01-13 00:07:35 +00:00
|
|
|
permit: permit_1,
|
2023-11-30 18:09:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let p2_handshake_req = DoHandshakeRequest {
|
2024-01-13 00:07:35 +00:00
|
|
|
addr: InternalPeerID::KnownAddr(TestNetZoneAddr(444)),
|
2024-03-05 01:29:57 +00:00
|
|
|
peer_stream: FramedRead::new(p1_receiver, MoneroWireCodec::default()),
|
|
|
|
peer_sink: FramedWrite::new(p1_sender, MoneroWireCodec::default()),
|
2023-11-30 18:09:05 +00:00
|
|
|
direction: ConnectionDirection::InBound,
|
2024-01-13 00:07:35 +00:00
|
|
|
permit: permit_2,
|
2023-11-30 18:09:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let p1 = tokio::spawn(async move {
|
|
|
|
handshaker_1
|
|
|
|
.ready()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.call(p1_handshake_req)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
});
|
|
|
|
|
|
|
|
let p2 = tokio::spawn(async move {
|
|
|
|
handshaker_2
|
|
|
|
.ready()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.call(p2_handshake_req)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
});
|
|
|
|
|
2024-05-02 22:58:22 +00:00
|
|
|
let (res1, res2) = tokio::join!(p1, p2);
|
2023-11-30 18:09:05 +00:00
|
|
|
res1.unwrap();
|
|
|
|
res2.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2024-02-12 13:39:15 +00:00
|
|
|
async fn handshake_cuprate_to_monerod() {
|
2024-01-13 00:07:35 +00:00
|
|
|
let semaphore = Arc::new(Semaphore::new(10));
|
|
|
|
let permit = semaphore.acquire_owned().await.unwrap();
|
|
|
|
|
2024-02-16 22:47:50 +00:00
|
|
|
let monerod = monerod(["--fixed-difficulty=1", "--out-peers=0"]).await;
|
2023-11-30 18:09:05 +00:00
|
|
|
|
|
|
|
let our_basic_node_data = BasicNodeData {
|
|
|
|
my_port: 0,
|
2024-05-02 22:58:22 +00:00
|
|
|
network_id: Network::Mainnet.network_id(),
|
2023-11-30 18:09:05 +00:00
|
|
|
peer_id: 87980,
|
|
|
|
support_flags: PeerSupportFlags::from(1_u32),
|
|
|
|
rpc_port: 0,
|
|
|
|
rpc_credits_per_hash: 0,
|
|
|
|
};
|
|
|
|
|
2024-05-02 22:58:22 +00:00
|
|
|
let handshaker = HandShaker::<ClearNet, _, _, _, _, _>::new(
|
2023-11-30 18:09:05 +00:00
|
|
|
DummyAddressBook,
|
2024-05-02 22:58:22 +00:00
|
|
|
DummyPeerSyncSvc,
|
2023-11-30 18:09:05 +00:00
|
|
|
DummyCoreSyncSvc,
|
|
|
|
DummyPeerRequestHandlerSvc,
|
2024-05-02 22:58:22 +00:00
|
|
|
|_| futures::stream::pending(),
|
2023-11-30 18:09:05 +00:00
|
|
|
our_basic_node_data,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut connector = Connector::new(handshaker);
|
|
|
|
|
|
|
|
connector
|
|
|
|
.ready()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.call(ConnectRequest {
|
2024-02-16 22:47:50 +00:00
|
|
|
addr: monerod.p2p_addr(),
|
2024-01-13 00:07:35 +00:00
|
|
|
permit,
|
2023-11-30 18:09:05 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
2024-02-25 21:21:25 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn handshake_monerod_to_cuprate() {
|
|
|
|
let semaphore = Arc::new(Semaphore::new(10));
|
|
|
|
let permit = semaphore.acquire_owned().await.unwrap();
|
|
|
|
|
|
|
|
let our_basic_node_data = BasicNodeData {
|
|
|
|
my_port: 18081,
|
2024-05-02 22:58:22 +00:00
|
|
|
network_id: Network::Mainnet.network_id(),
|
2024-02-25 21:21:25 +00:00
|
|
|
peer_id: 87980,
|
|
|
|
support_flags: PeerSupportFlags::from(1_u32),
|
|
|
|
rpc_port: 0,
|
|
|
|
rpc_credits_per_hash: 0,
|
|
|
|
};
|
|
|
|
|
2024-05-02 22:58:22 +00:00
|
|
|
let mut handshaker = HandShaker::<ClearNet, _, _, _, _, _>::new(
|
2024-02-25 21:21:25 +00:00
|
|
|
DummyAddressBook,
|
2024-05-02 22:58:22 +00:00
|
|
|
DummyPeerSyncSvc,
|
2024-02-25 21:21:25 +00:00
|
|
|
DummyCoreSyncSvc,
|
|
|
|
DummyPeerRequestHandlerSvc,
|
2024-05-02 22:58:22 +00:00
|
|
|
|_| futures::stream::pending(),
|
2024-02-25 21:21:25 +00:00
|
|
|
our_basic_node_data,
|
|
|
|
);
|
|
|
|
|
2024-06-04 17:19:25 +00:00
|
|
|
let ip = "127.0.0.1".parse().unwrap();
|
2024-02-25 21:21:25 +00:00
|
|
|
|
2024-06-04 17:19:25 +00:00
|
|
|
let mut listener = ClearNet::incoming_connection_listener(ClearNetServerCfg { ip }, 18081)
|
2024-02-25 21:21:25 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let _monerod = monerod(["--add-exclusive-node=127.0.0.1:18081"]).await;
|
|
|
|
|
|
|
|
// Put a timeout on this just in case monerod doesn't make the connection to us.
|
|
|
|
let next_connection_fut = timeout(Duration::from_secs(30), listener.next());
|
|
|
|
|
|
|
|
if let Some(Ok((addr, stream, sink))) = next_connection_fut.await.unwrap() {
|
|
|
|
let _ = handshaker
|
|
|
|
.ready()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.call(DoHandshakeRequest {
|
|
|
|
addr: InternalPeerID::KnownAddr(addr.unwrap()), // This is clear net all addresses are known.
|
|
|
|
peer_stream: stream,
|
|
|
|
peer_sink: sink,
|
|
|
|
direction: ConnectionDirection::InBound,
|
|
|
|
permit,
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
} else {
|
|
|
|
panic!("Failed to receive connection from monerod.");
|
|
|
|
};
|
|
|
|
}
|