2024-01-13 00:07:35 +00:00
|
|
|
use std::sync::Arc;
|
2023-11-30 18:09:05 +00:00
|
|
|
|
|
|
|
use futures::{channel::mpsc, StreamExt};
|
2024-01-13 00:07:35 +00:00
|
|
|
use tokio::sync::{broadcast, Semaphore};
|
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;
|
2023-11-30 18:09:05 +00:00
|
|
|
use monero_wire::{common::PeerSupportFlags, BasicNodeData};
|
|
|
|
|
2023-12-08 15:03:01 +00:00
|
|
|
use monero_p2p::{
|
2023-11-30 18:09:05 +00:00
|
|
|
client::{ConnectRequest, Connector, DoHandshakeRequest, HandShaker},
|
|
|
|
network_zones::ClearNet,
|
|
|
|
ConnectionDirection,
|
|
|
|
};
|
|
|
|
|
2024-02-12 13:39:15 +00:00
|
|
|
use cuprate_test_utils::{
|
|
|
|
monerod::monerod,
|
|
|
|
test_netzone::{TestNetZone, TestNetZoneAddr},
|
|
|
|
};
|
2024-01-13 00:07:35 +00:00
|
|
|
use monero_p2p::client::InternalPeerID;
|
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 (broadcast_tx, _) = broadcast::channel(1); // this isn't actually used in this test.
|
|
|
|
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-01-30 16:09:54 +00:00
|
|
|
network_id: Network::Mainnet.network_id().into(),
|
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;
|
|
|
|
|
|
|
|
let mut handshaker_1 = HandShaker::<TestNetZone<true, true, true>, _, _, _>::new(
|
|
|
|
DummyAddressBook,
|
|
|
|
DummyCoreSyncSvc,
|
|
|
|
DummyPeerRequestHandlerSvc,
|
2024-01-13 00:07:35 +00:00
|
|
|
broadcast_tx.clone(),
|
2023-11-30 18:09:05 +00:00
|
|
|
our_basic_node_data_1,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut handshaker_2 = HandShaker::<TestNetZone<true, true, true>, _, _, _>::new(
|
|
|
|
DummyAddressBook,
|
|
|
|
DummyCoreSyncSvc,
|
|
|
|
DummyPeerRequestHandlerSvc,
|
2024-01-13 00:07:35 +00:00
|
|
|
broadcast_tx.clone(),
|
2023-11-30 18:09:05 +00:00
|
|
|
our_basic_node_data_2,
|
|
|
|
);
|
|
|
|
|
|
|
|
let (p1_sender, p2_receiver) = mpsc::channel(5);
|
|
|
|
let (p2_sender, p1_receiver) = mpsc::channel(5);
|
|
|
|
|
|
|
|
let p1_handshake_req = DoHandshakeRequest {
|
2024-01-13 00:07:35 +00:00
|
|
|
addr: InternalPeerID::KnownAddr(TestNetZoneAddr(888)),
|
2023-11-30 18:09:05 +00:00
|
|
|
peer_stream: p2_receiver.map(Ok).boxed(),
|
|
|
|
peer_sink: p2_sender.into(),
|
|
|
|
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)),
|
2023-11-30 18:09:05 +00:00
|
|
|
peer_stream: p1_receiver.boxed().map(Ok).boxed(),
|
|
|
|
peer_sink: p1_sender.into(),
|
|
|
|
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()
|
|
|
|
});
|
|
|
|
|
|
|
|
let (res1, res2) = futures::join!(p1, p2);
|
|
|
|
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 (broadcast_tx, _) = broadcast::channel(1); // this isn't actually used in this test.
|
|
|
|
let semaphore = Arc::new(Semaphore::new(10));
|
|
|
|
let permit = semaphore.acquire_owned().await.unwrap();
|
|
|
|
|
2024-02-12 13:39:15 +00:00
|
|
|
let (monerod, _) = monerod(
|
|
|
|
vec!["--fixed-difficulty=1".into(), "--out-peers=0".into()],
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.await;
|
2023-11-30 18:09:05 +00:00
|
|
|
|
|
|
|
let our_basic_node_data = BasicNodeData {
|
|
|
|
my_port: 0,
|
2024-01-30 16:09:54 +00:00
|
|
|
network_id: Network::Mainnet.network_id().into(),
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
let handshaker = HandShaker::<ClearNet, _, _, _>::new(
|
|
|
|
DummyAddressBook,
|
|
|
|
DummyCoreSyncSvc,
|
|
|
|
DummyPeerRequestHandlerSvc,
|
2024-01-13 00:07:35 +00:00
|
|
|
broadcast_tx,
|
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-12 13:39:15 +00:00
|
|
|
addr: monerod,
|
2024-01-13 00:07:35 +00:00
|
|
|
permit,
|
2023-11-30 18:09:05 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|