refactor: Merge multiplexer & miniprotocols into single crate (#244)
This commit is contained in:
parent
b63d2052cb
commit
d5f0c2fd80
80 changed files with 1694 additions and 3002 deletions
62
pallas-network/tests/plexer.rs
Normal file
62
pallas-network/tests/plexer.rs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
use std::net::{Ipv4Addr, SocketAddrV4};
|
||||
|
||||
use pallas_network::{bearer::Bearer, plexer::Plexer};
|
||||
use rand::{distributions::Uniform, Rng};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
async fn setup_passive_muxer<const P: u16>() -> Plexer {
|
||||
let server = TcpListener::bind(SocketAddrV4::new(Ipv4Addr::LOCALHOST, P))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("listening for connections on port {}", P);
|
||||
|
||||
let (bearer, _) = Bearer::accept_tcp(server).await.unwrap();
|
||||
|
||||
Plexer::new(bearer)
|
||||
}
|
||||
|
||||
async fn setup_active_muxer<const P: u16>() -> Plexer {
|
||||
let bearer = Bearer::connect_tcp(SocketAddrV4::new(Ipv4Addr::LOCALHOST, P))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("active plexer connected");
|
||||
|
||||
Plexer::new(bearer)
|
||||
}
|
||||
|
||||
fn random_payload(size: usize) -> Vec<u8> {
|
||||
let range = Uniform::from(0..255);
|
||||
rand::thread_rng().sample_iter(&range).take(size).collect()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn one_way_small_sequence_of_payloads() {
|
||||
let passive = tokio::spawn(setup_passive_muxer::<50301>());
|
||||
|
||||
// HACK: a small sleep seems to be required for Github actions runner to
|
||||
// formally expose the port
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
|
||||
let mut active = setup_active_muxer::<50301>().await;
|
||||
|
||||
let mut passive = passive.await.unwrap();
|
||||
|
||||
let mut sender_channel = active.subscribe_client(3);
|
||||
let mut receiver_channel = passive.subscribe_server(3);
|
||||
|
||||
let passive_run = tokio::spawn(async move { passive.run().await });
|
||||
let active_run = tokio::spawn(async move { active.run().await });
|
||||
|
||||
for _ in 0..100 {
|
||||
let payload = random_payload(50);
|
||||
println!("sending chunk");
|
||||
sender_channel.enqueue_chunk(payload.clone()).await.unwrap();
|
||||
let received_payload = receiver_channel.dequeue_chunk().await.unwrap();
|
||||
assert_eq!(payload, received_payload);
|
||||
}
|
||||
|
||||
passive_run.abort();
|
||||
active_run.abort();
|
||||
}
|
||||
146
pallas-network/tests/protocols.rs
Normal file
146
pallas-network/tests/protocols.rs
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
use pallas_network::facades::PeerClient;
|
||||
use pallas_network::miniprotocols::{
|
||||
blockfetch,
|
||||
chainsync::{self, NextResponse},
|
||||
Point,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
pub async fn chainsync_history_happy_path() {
|
||||
let mut peer = PeerClient::connect("preview-node.world.dev.cardano.org:30002", 2)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let client = peer.chainsync();
|
||||
|
||||
let known_point = Point::Specific(
|
||||
1654413,
|
||||
hex::decode("7de1f036df5a133ce68a82877d14354d0ba6de7625ab918e75f3e2ecb29771c2").unwrap(),
|
||||
);
|
||||
|
||||
let (point, _) = client
|
||||
.find_intersect(vec![known_point.clone()])
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("{:?}", point);
|
||||
|
||||
assert!(matches!(client.state(), chainsync::State::Idle));
|
||||
|
||||
match point {
|
||||
Some(point) => assert_eq!(point, known_point),
|
||||
None => panic!("expected point"),
|
||||
}
|
||||
|
||||
let next = client.request_next().await.unwrap();
|
||||
|
||||
match next {
|
||||
NextResponse::RollBackward(point, _) => assert_eq!(point, known_point),
|
||||
_ => panic!("expected rollback"),
|
||||
}
|
||||
|
||||
assert!(matches!(client.state(), chainsync::State::Idle));
|
||||
|
||||
for _ in 0..10 {
|
||||
let next = client.request_next().await.unwrap();
|
||||
|
||||
match next {
|
||||
NextResponse::RollForward(_, _) => (),
|
||||
_ => panic!("expected roll-forward"),
|
||||
}
|
||||
|
||||
assert!(matches!(client.state(), chainsync::State::Idle));
|
||||
}
|
||||
|
||||
client.send_done().await.unwrap();
|
||||
|
||||
assert!(matches!(client.state(), chainsync::State::Done));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
pub async fn chainsync_tip_happy_path() {
|
||||
let mut peer = PeerClient::connect("preview-node.world.dev.cardano.org:30002", 2)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let client = peer.chainsync();
|
||||
|
||||
client.intersect_tip().await.unwrap();
|
||||
|
||||
assert!(matches!(client.state(), chainsync::State::Idle));
|
||||
|
||||
let next = client.request_next().await.unwrap();
|
||||
|
||||
assert!(matches!(next, NextResponse::RollBackward(..)));
|
||||
|
||||
let mut await_count = 0;
|
||||
|
||||
for _ in 0..4 {
|
||||
let next = if client.has_agency() {
|
||||
client.request_next().await.unwrap()
|
||||
} else {
|
||||
await_count += 1;
|
||||
client.recv_while_must_reply().await.unwrap()
|
||||
};
|
||||
|
||||
match next {
|
||||
NextResponse::RollForward(_, _) => (),
|
||||
NextResponse::Await => (),
|
||||
_ => panic!("expected roll-forward or await"),
|
||||
}
|
||||
}
|
||||
|
||||
assert!(await_count > 0, "tip was never reached");
|
||||
|
||||
client.send_done().await.unwrap();
|
||||
|
||||
assert!(matches!(client.state(), chainsync::State::Done));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
pub async fn blockfetch_happy_path() {
|
||||
let mut peer = PeerClient::connect("preview-node.world.dev.cardano.org:30002", 2)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let client = peer.blockfetch();
|
||||
|
||||
let known_point = Point::Specific(
|
||||
1654413,
|
||||
hex::decode("7de1f036df5a133ce68a82877d14354d0ba6de7625ab918e75f3e2ecb29771c2").unwrap(),
|
||||
);
|
||||
|
||||
let range_ok = client
|
||||
.request_range((known_point.clone(), known_point))
|
||||
.await;
|
||||
|
||||
assert!(matches!(client.state(), blockfetch::State::Streaming));
|
||||
|
||||
println!("streaming...");
|
||||
|
||||
assert!(matches!(range_ok, Ok(_)));
|
||||
|
||||
for _ in 0..1 {
|
||||
let next = client.recv_while_streaming().await.unwrap();
|
||||
|
||||
match next {
|
||||
Some(body) => assert_eq!(body.len(), 3251),
|
||||
_ => panic!("expected block body"),
|
||||
}
|
||||
|
||||
assert!(matches!(client.state(), blockfetch::State::Streaming));
|
||||
}
|
||||
|
||||
let next = client.recv_while_streaming().await.unwrap();
|
||||
|
||||
assert!(matches!(next, None));
|
||||
|
||||
client.send_done().await.unwrap();
|
||||
|
||||
assert!(matches!(client.state(), blockfetch::State::Done));
|
||||
}
|
||||
|
||||
// TODO: redo txsubmission client test
|
||||
Loading…
Add table
Add a link
Reference in a new issue