feat: Migrate to dumb agents (#198)

BREAKING CHANGE: handshake, chainsync, localstate and blockfetch mini-protocols changed the API surface

Co-authored-by: jmhrpr <harper.jme@gmail.com>
This commit is contained in:
Santiago Carmuega 2022-10-25 14:42:34 -03:00 committed by GitHub
parent f4b278aa23
commit a129d77608
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 1088 additions and 1164 deletions

View file

@ -1,53 +1,39 @@
use pallas::network::{
miniprotocols::{
handshake::{n2n::VersionTable, Initiator},
run_agent, Point, TESTNET_MAGIC,
blockfetch,
handshake::{self, n2n::VersionTable},
Point, TESTNET_MAGIC,
},
multiplexer::{bearers::Bearer, StdPlexer},
};
use pallas::network::miniprotocols::blockfetch::{BatchClient, Observer};
#[derive(Debug)]
struct BlockPrinter;
impl Observer for BlockPrinter {
fn on_block_received(&mut self, body: Vec<u8>) -> Result<(), Box<dyn std::error::Error>> {
println!("{}", hex::encode(&body));
println!("----------");
Ok(())
}
}
fn main() {
env_logger::init();
let bearer = Bearer::connect_tcp("relays-new.cardano-testnet.iohkdev.io:3001").unwrap();
let mut plexer = StdPlexer::new(bearer);
let mut channel0 = plexer.use_channel(0).into();
let mut channel3 = plexer.use_channel(3).into();
let channel0 = plexer.use_channel(0);
let channel3 = plexer.use_channel(3);
plexer.muxer.spawn();
plexer.demuxer.spawn();
let versions = VersionTable::v4_and_above(TESTNET_MAGIC);
let _last = run_agent(Initiator::initial(versions), &mut channel0).unwrap();
let mut hs_client = handshake::N2NClient::new(channel0);
let handshake = hs_client.handshake(versions).unwrap();
let range = (
Point::Specific(
63528597,
hex::decode("3f3d81c7b88f0fa28867541c5fea8794125cccf6d6c9ee0037a1dbb064130dfd")
.unwrap(),
),
Point::Specific(
63528597,
hex::decode("3f3d81c7b88f0fa28867541c5fea8794125cccf6d6c9ee0037a1dbb064130dfd")
.unwrap(),
),
assert!(matches!(handshake, handshake::Confirmation::Accepted(..)));
let point = Point::Specific(
63528597,
hex::decode("3f3d81c7b88f0fa28867541c5fea8794125cccf6d6c9ee0037a1dbb064130dfd").unwrap(),
);
let bf = BatchClient::initial(range, BlockPrinter {});
let bf_last = run_agent(bf, &mut channel3);
println!("{:?}", bf_last);
let mut bf_client = blockfetch::Client::new(channel3);
let block = bf_client.fetch_single(point).unwrap();
println!("downloaded block of size: {}", block.len());
println!("{}", hex::encode(&block));
}

View file

@ -1,80 +1,62 @@
use pallas::network::{
miniprotocols::{chainsync, handshake, localstate, run_agent, Point, MAINNET_MAGIC},
miniprotocols::{chainsync, handshake, localstate, Point, MAINNET_MAGIC},
multiplexer::{self, bearers::Bearer},
};
#[derive(Debug)]
struct LoggingObserver;
impl chainsync::Observer<chainsync::HeaderContent> for LoggingObserver {
fn on_roll_forward(
&mut self,
_content: chainsync::HeaderContent,
tip: &chainsync::Tip,
) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::debug!("asked to roll forward, tip at {:?}", tip);
fn do_handshake(channel: multiplexer::StdChannel) {
let mut client = handshake::N2CClient::new(channel);
Ok(chainsync::Continuation::Proceed)
}
let confirmation = client
.handshake(handshake::n2c::VersionTable::v1_and_above(MAINNET_MAGIC))
.unwrap();
fn on_intersect_found(
&mut self,
point: &Point,
tip: &chainsync::Tip,
) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::debug!("intersect was found {:?} (tip: {:?})", point, tip);
Ok(chainsync::Continuation::Proceed)
}
fn on_rollback(
&mut self,
point: &Point,
) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::debug!("asked to roll back {:?}", point);
Ok(chainsync::Continuation::Proceed)
}
fn on_tip_reached(&mut self) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::debug!("tip was reached");
Ok(chainsync::Continuation::Proceed)
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_handshake(mut channel: multiplexer::StdChannelBuffer) {
let versions = handshake::n2c::VersionTable::v1_and_above(MAINNET_MAGIC);
let _last = run_agent(handshake::Initiator::initial(versions), &mut channel).unwrap();
fn do_localstate_query(channel: multiplexer::StdChannel) {
let mut client = localstate::ClientV10::new(channel);
client.acquire(None).unwrap();
let result = client
.query(localstate::queries::RequestV10::GetSystemStart)
.unwrap();
log::info!("system start result: {:?}", result);
}
fn do_localstate_query(mut channel: multiplexer::StdChannelBuffer) {
let agent = run_agent(
localstate::OneShotClient::<localstate::queries::QueryV10>::initial(
None,
localstate::queries::RequestV10::GetChainPoint,
),
&mut channel,
);
log::info!("state query result: {:?}", agent);
}
fn do_chainsync(mut channel: multiplexer::StdChannelBuffer) {
fn do_chainsync(channel: multiplexer::StdChannel) {
let known_points = vec![Point::Specific(
43847831u64,
hex::decode("15b9eeee849dd6386d3770b0745e0450190f7560e5159b1b3ab13b14b2684a45").unwrap(),
)];
let agent = run_agent(
chainsync::Consumer::<chainsync::HeaderContent, _>::initial(
Some(known_points),
LoggingObserver {},
),
&mut channel,
);
let mut client = chainsync::N2CClient::new(channel);
println!("{:?}", agent);
let (point, _) = client.find_intersect(known_points).unwrap();
log::info!("intersected point is {:?}", point);
for _ in 0..10 {
let next = client.request_next().unwrap();
match next {
chainsync::NextResponse::RollForward(h, _) => {
log::info!("rolling forward, block size: {}", h.len())
}
chainsync::NextResponse::RollBackward(x, _) => log::info!("rollback to {:?}", x),
chainsync::NextResponse::Await => log::info!("tip of chaing reached"),
};
}
}
fn main() {
@ -89,9 +71,9 @@ fn main() {
// setup the multiplexer by specifying the bearer and the IDs of the
// miniprotocols to use
let mut plexer = multiplexer::StdPlexer::new(bearer);
let channel0 = plexer.use_channel(0).into();
let channel7 = plexer.use_channel(7).into();
let channel5 = plexer.use_channel(5).into();
let channel0 = plexer.use_channel(0);
let channel7 = plexer.use_channel(7);
let channel5 = plexer.use_channel(5);
plexer.muxer.spawn();
plexer.demuxer.spawn();

View file

@ -1,61 +1,29 @@
use pallas::network::{
miniprotocols::{blockfetch, chainsync, handshake, run_agent, Point, MAINNET_MAGIC},
multiplexer::{agents::ChannelBuffer, bearers::Bearer, StdChannel, StdPlexer},
miniprotocols::{blockfetch, chainsync, handshake, Point, MAINNET_MAGIC},
multiplexer::{bearers::Bearer, StdChannel, StdPlexer},
};
#[derive(Debug)]
struct LoggingObserver;
impl blockfetch::Observer for LoggingObserver {
fn on_block_received(&mut self, body: Vec<u8>) -> Result<(), Box<dyn std::error::Error>> {
log::trace!("block received: {}", hex::encode(&body));
Ok(())
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)
}
}
}
impl chainsync::Observer<chainsync::HeaderContent> for LoggingObserver {
fn on_roll_forward(
&mut self,
_content: chainsync::HeaderContent,
tip: &chainsync::Tip,
) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::info!("asked to roll forward, tip at {:?}", tip);
Ok(chainsync::Continuation::Proceed)
}
fn on_intersect_found(
&mut self,
point: &Point,
tip: &chainsync::Tip,
) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::debug!("intersect was found {:?} (tip: {:?})", point, tip);
Ok(chainsync::Continuation::Proceed)
}
fn on_rollback(
&mut self,
point: &Point,
) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::debug!("asked to roll back {:?}", point);
Ok(chainsync::Continuation::Proceed)
}
fn on_tip_reached(&mut self) -> Result<chainsync::Continuation, Box<dyn std::error::Error>> {
log::debug!("tip was reached");
Ok(chainsync::Continuation::Proceed)
}
}
fn do_handshake(mut channel: ChannelBuffer<StdChannel>) {
let versions = handshake::n2n::VersionTable::v4_and_above(MAINNET_MAGIC);
let _last = run_agent(handshake::Initiator::initial(versions), &mut channel).unwrap();
}
fn do_blockfetch(mut channel: ChannelBuffer<StdChannel>) {
fn do_blockfetch(channel: StdChannel) {
let range = (
Point::Specific(
43847831,
@ -69,29 +37,38 @@ fn do_blockfetch(mut channel: ChannelBuffer<StdChannel>) {
),
);
let agent = run_agent(
blockfetch::BatchClient::initial(range, LoggingObserver {}),
&mut channel,
);
let mut client = blockfetch::Client::new(channel);
println!("{:?}", agent);
let blocks = client.fetch_range(range).unwrap();
for block in blocks {
log::info!("received block of size: {}", block.len());
}
}
fn do_chainsync(mut channel: ChannelBuffer<StdChannel>) {
fn do_chainsync(channel: StdChannel) {
let known_points = vec![Point::Specific(
43847831u64,
hex::decode("15b9eeee849dd6386d3770b0745e0450190f7560e5159b1b3ab13b14b2684a45").unwrap(),
)];
let agent = run_agent(
chainsync::Consumer::<chainsync::HeaderContent, _>::initial(
Some(known_points),
LoggingObserver {},
),
&mut channel,
);
let mut client = chainsync::N2NClient::new(channel);
println!("{:?}", agent);
let (point, _) = client.find_intersect(known_points).unwrap();
log::info!("intersected point is {:?}", point);
for _ in 0..10 {
let next = client.request_next().unwrap();
match next {
chainsync::NextResponse::RollForward(h, _) => {
log::info!("rolling forward, header size: {}", h.cbor.len())
}
chainsync::NextResponse::RollBackward(x, _) => log::info!("rollback to {:?}", x),
chainsync::NextResponse::Await => log::info!("tip of chaing reached"),
};
}
}
fn main() {
@ -106,9 +83,9 @@ fn main() {
// setup the multiplexer by specifying the bearer and the IDs of the
// miniprotocols to use
let mut plexer = StdPlexer::new(bearer);
let channel0 = plexer.use_channel(0).into();
let channel3 = plexer.use_channel(3).into();
let channel2 = plexer.use_channel(2).into();
let channel0 = plexer.use_channel(0);
let channel3 = plexer.use_channel(3);
let channel2 = plexer.use_channel(2);
plexer.muxer.spawn();
plexer.demuxer.spawn();