refactor: Merge multiplexer & miniprotocols into single crate (#244)

This commit is contained in:
Santiago Carmuega 2023-04-11 00:51:38 +02:00 committed by GitHub
parent b8ff4e9418
commit cb0348b47a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
80 changed files with 1694 additions and 3002 deletions

View file

@ -10,3 +10,6 @@ publish = false
pallas = { path = "../../pallas" }
net2 = "0.2.37"
hex = "0.4.3"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
tokio = { version = "1.27.0", features = ["rt-multi-thread"] }

View file

@ -1,38 +1,27 @@
use pallas::network::{
miniprotocols::{
blockfetch,
handshake::{self, n2n::VersionTable},
Point, MAINNET_MAGIC, PROTOCOL_N2N_BLOCK_FETCH, PROTOCOL_N2N_HANDSHAKE,
},
multiplexer::{bearers::Bearer, StdPlexer},
facades::PeerClient,
miniprotocols::{Point, MAINNET_MAGIC},
};
fn main() {
env_logger::init();
#[tokio::main]
async fn main() {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::TRACE)
.finish(),
)
.unwrap();
let bearer = Bearer::connect_tcp("relays-new.cardano-mainnet.iohk.io:3001").unwrap();
let mut plexer = StdPlexer::new(bearer);
let handshake = plexer.use_client_channel(PROTOCOL_N2N_HANDSHAKE);
let blockfetch = plexer.use_client_channel(PROTOCOL_N2N_BLOCK_FETCH);
plexer.muxer.spawn();
plexer.demuxer.spawn();
let versions = VersionTable::v4_and_above(MAINNET_MAGIC);
let mut hs_client = handshake::N2NClient::new(handshake);
let handshake = hs_client.handshake(versions).unwrap();
assert!(matches!(handshake, handshake::Confirmation::Accepted(..)));
let mut peer = PeerClient::connect("relays-new.cardano-mainnet.iohk.io:3001", MAINNET_MAGIC)
.await
.unwrap();
let point = Point::Specific(
49159253,
hex::decode("d034a2d0e4c3076f57368ed59319010c265718f0923057f8ff914a3b6bfd1314").unwrap(),
);
let mut bf_client = blockfetch::Client::new(blockfetch);
let block = bf_client.fetch_single(point).unwrap();
let block = peer.blockfetch().fetch_single(point).await.unwrap();
println!("downloaded block of size: {}", block.len());
println!("{}", hex::encode(&block));

View file

@ -11,3 +11,6 @@ pallas = { path = "../../pallas" }
net2 = "0.2.37"
hex = "0.4.3"
log = "0.4.16"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
tokio = { version = "1.27.0", features = ["rt-multi-thread"] }

View file

@ -1,56 +1,37 @@
use pallas::network::{
miniprotocols::{chainsync, handshake, localstate, Point, MAINNET_MAGIC},
multiplexer,
facades::NodeClient,
miniprotocols::{chainsync, localstate, Point, MAINNET_MAGIC},
};
use tracing::info;
#[derive(Debug)]
struct LoggingObserver;
#[allow(dead_code)]
fn do_handshake(channel: multiplexer::StdChannel) {
let mut client = handshake::N2CClient::new(channel);
let confirmation = client
.handshake(handshake::n2c::VersionTable::v1_and_above(MAINNET_MAGIC))
.unwrap();
match confirmation {
handshake::Confirmation::Accepted(v, _) => {
log::info!("hand-shake accepted, using version {}", v)
}
handshake::Confirmation::Rejected(x) => {
log::info!("hand-shake rejected with reason {:?}", x)
}
}
}
#[allow(dead_code)]
fn do_localstate_query(channel: multiplexer::StdChannel) {
let mut client = localstate::ClientV10::new(channel);
client.acquire(None).unwrap();
async fn do_localstate_query(client: &mut NodeClient) {
client.statequery().acquire(None).await.unwrap();
let result = client
.statequery()
.query(localstate::queries::RequestV10::GetSystemStart)
.await
.unwrap();
log::info!("system start result: {:?}", result);
info!("system start result: {:?}", result);
}
#[allow(dead_code)]
fn do_chainsync(channel: multiplexer::StdChannel) {
async fn do_chainsync(client: &mut NodeClient) {
let known_points = vec![Point::Specific(
43847831u64,
hex::decode("15b9eeee849dd6386d3770b0745e0450190f7560e5159b1b3ab13b14b2684a45").unwrap(),
)];
let mut client = chainsync::N2CClient::new(channel);
let (point, _) = client
.chainsync()
.find_intersect(known_points)
.await
.unwrap();
let (point, _) = client.find_intersect(known_points).unwrap();
log::info!("intersected point is {:?}", point);
info!("intersected point is {:?}", point);
for _ in 0..10 {
let next = client.request_next().unwrap();
let next = client.chainsync().request_next().await.unwrap();
match next {
chainsync::NextResponse::RollForward(h, _) => {
@ -62,44 +43,29 @@ fn do_chainsync(channel: multiplexer::StdChannel) {
}
}
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Trace)
.init();
#[tokio::main]
async fn main() {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::TRACE)
.finish(),
)
.unwrap();
#[cfg(not(target_family = "unix"))]
{
panic!("can't use n2c unix socket on non-unix systems");
}
// we connect to the unix socket of the local node. Make sure you have the right
// path for your environment
#[cfg(target_family = "unix")]
{
use pallas::network::{
miniprotocols::{
PROTOCOL_N2C_CHAIN_SYNC, PROTOCOL_N2C_HANDSHAKE, PROTOCOL_N2C_STATE_QUERY,
},
multiplexer::bearers::Bearer,
};
let bearer = Bearer::connect_unix("/tmp/node.socket").unwrap();
let mut client = NodeClient::connect("/tmp/node.socket", MAINNET_MAGIC)
.await
.unwrap();
// setup the multiplexer by specifying the bearer and the IDs of the
// miniprotocols to use
let mut plexer = multiplexer::StdPlexer::new(bearer);
let handshake = plexer.use_client_channel(PROTOCOL_N2C_HANDSHAKE);
let statequery = plexer.use_client_channel(PROTOCOL_N2C_STATE_QUERY);
let chainsync = plexer.use_client_channel(PROTOCOL_N2C_CHAIN_SYNC);
// execute an arbitrary "Local State" query against the node
do_localstate_query(&mut client).await;
plexer.muxer.spawn();
plexer.demuxer.spawn();
// execute the required handshake against the relay
do_handshake(handshake);
// execute an arbitrary "Local State" query against the node
do_localstate_query(statequery);
// execute the chainsync flow from an arbitrary point in the chain
do_chainsync(chainsync);
}
// execute the chainsync flow from an arbitrary point in the chain
do_chainsync(&mut client).await;
}
#[cfg(not(target_family = "unix"))]
fn main() {
panic!("can't use n2c unix socket on non-unix systems");
}

View file

@ -11,3 +11,6 @@ pallas = { path = "../../pallas" }
net2 = "0.2.37"
hex = "0.4.3"
log = "0.4.16"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
tokio = { version = "1.27.0", features = ["rt-multi-thread"] }

View file

@ -1,32 +1,10 @@
use pallas::network::{
miniprotocols::{
blockfetch, chainsync, handshake, Point, MAINNET_MAGIC, PROTOCOL_N2N_BLOCK_FETCH,
PROTOCOL_N2N_CHAIN_SYNC, PROTOCOL_N2N_HANDSHAKE,
},
multiplexer::{bearers::Bearer, StdChannel, StdPlexer},
facades::PeerClient,
miniprotocols::{chainsync, Point, MAINNET_MAGIC},
};
use tracing::info;
#[derive(Debug)]
struct LoggingObserver;
fn do_handshake(channel: StdChannel) {
let mut client = handshake::N2NClient::new(channel);
let confirmation = client
.handshake(handshake::n2n::VersionTable::v7_and_above(MAINNET_MAGIC))
.unwrap();
match confirmation {
handshake::Confirmation::Accepted(v, _) => {
log::info!("hand-shake accepted, using version {}", v)
}
handshake::Confirmation::Rejected(x) => {
log::info!("hand-shake rejected with reason {:?}", x)
}
}
}
fn do_blockfetch(channel: StdChannel) {
async fn do_blockfetch(peer: &mut PeerClient) {
let range = (
Point::Specific(
43847831,
@ -40,29 +18,25 @@ fn do_blockfetch(channel: StdChannel) {
),
);
let mut client = blockfetch::Client::new(channel);
let blocks = client.fetch_range(range).unwrap();
let blocks = peer.blockfetch().fetch_range(range).await.unwrap();
for block in blocks {
log::info!("received block of size: {}", block.len());
info!("received block of size: {}", block.len());
}
}
fn do_chainsync(channel: StdChannel) {
async fn do_chainsync(peer: &mut PeerClient) {
let known_points = vec![Point::Specific(
43847831u64,
hex::decode("15b9eeee849dd6386d3770b0745e0450190f7560e5159b1b3ab13b14b2684a45").unwrap(),
)];
let mut client = chainsync::N2NClient::new(channel);
let (point, _) = peer.chainsync().find_intersect(known_points).await.unwrap();
let (point, _) = client.find_intersect(known_points).unwrap();
log::info!("intersected point is {:?}", point);
info!("intersected point is {:?}", point);
for _ in 0..10 {
let next = client.request_next().unwrap();
let next = peer.chainsync().request_next().await.unwrap();
match next {
chainsync::NextResponse::RollForward(h, _) => {
@ -74,31 +48,24 @@ fn do_chainsync(channel: StdChannel) {
}
}
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.init();
#[tokio::main]
async fn main() {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::TRACE)
.finish(),
)
.unwrap();
// setup a TCP socket to act as data bearer between our agents and the remote
// relay.
let bearer = Bearer::connect_tcp("relays-new.cardano-mainnet.iohk.io:3001").unwrap();
// setup the multiplexer by specifying the bearer and the IDs of the
// miniprotocols to use
let mut plexer = StdPlexer::new(bearer);
let handshake = plexer.use_client_channel(PROTOCOL_N2N_HANDSHAKE);
let blockfetch = plexer.use_client_channel(PROTOCOL_N2N_BLOCK_FETCH);
let chainsync = plexer.use_client_channel(PROTOCOL_N2N_CHAIN_SYNC);
plexer.muxer.spawn();
plexer.demuxer.spawn();
// execute the required handshake against the relay
do_handshake(handshake);
let mut peer = PeerClient::connect("relays-new.cardano-mainnet.iohk.io:3001", MAINNET_MAGIC)
.await
.unwrap();
// fetch an arbitrary batch of block
do_blockfetch(blockfetch);
do_blockfetch(&mut peer).await;
// execute the chainsync flow from an arbitrary point in the chain
do_chainsync(chainsync);
do_chainsync(&mut peer).await;
}