refactor: make chainsync machine agnostic of content
This commit is contained in:
parent
e10575f2da
commit
20d9019375
18 changed files with 148 additions and 94 deletions
|
|
@ -1,13 +1,39 @@
|
|||
use pallas_chainsync::{ClientConsumer, NoopObserver};
|
||||
use pallas_handshake::{
|
||||
n2c::{Client, VersionTable},
|
||||
MAINNET_MAGIC,
|
||||
};
|
||||
use pallas_machines::primitives::Point;
|
||||
use pallas_alonzo::{crypto, Block, BlockWrapper, Fragment};
|
||||
use pallas_chainsync::{BlockLike, Consumer, NoopObserver};
|
||||
use pallas_handshake::n2c::{Client, VersionTable};
|
||||
use pallas_handshake::MAINNET_MAGIC;
|
||||
use pallas_machines::run_agent;
|
||||
use pallas_machines::{
|
||||
primitives::Point, DecodePayload, EncodePayload, PayloadDecoder, PayloadEncoder,
|
||||
};
|
||||
use pallas_multiplexer::Multiplexer;
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Content(Block);
|
||||
|
||||
impl EncodePayload for Content {
|
||||
fn encode_payload(&self, _e: &mut PayloadEncoder) -> Result<(), Box<dyn std::error::Error>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl DecodePayload for Content {
|
||||
fn decode_payload(d: &mut PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
d.tag()?;
|
||||
let bytes = d.bytes()?;
|
||||
let BlockWrapper(_, block) = BlockWrapper::decode_fragment(bytes)?;
|
||||
Ok(Content(block))
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockLike for Content {
|
||||
fn block_point(&self) -> Result<Point, Box<dyn std::error::Error>> {
|
||||
let hash = crypto::hash_block_header(&self.0.header)?;
|
||||
Ok(Point(self.0.header.header_body.slot, Vec::from(hash)))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
|
|
@ -24,12 +50,12 @@ fn main() {
|
|||
|
||||
// some random known-point in the chain to use as starting point for the sync
|
||||
let known_points = vec![Point(
|
||||
43847831u64,
|
||||
hex::decode("15b9eeee849dd6386d3770b0745e0450190f7560e5159b1b3ab13b14b2684a45").unwrap(),
|
||||
45147459,
|
||||
hex::decode("bee16ef28ac02abb50c340a7deff085a77f3a7b84c66250b3318dcb125c19a10").unwrap(),
|
||||
)];
|
||||
|
||||
let mut cs_channel = muxer.use_channel(5);
|
||||
let cs = ClientConsumer::initial(known_points, NoopObserver {});
|
||||
let cs = Consumer::<Content, _>::initial(known_points, NoopObserver {});
|
||||
let cs = run_agent(cs, &mut cs_channel).unwrap();
|
||||
println!("{:?}", cs);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,47 @@
|
|||
use minicbor::data::Tag;
|
||||
use net2::TcpStreamExt;
|
||||
use pallas_alonzo::{crypto, Fragment, Header};
|
||||
use pallas_machines::primitives::Point;
|
||||
use std::net::TcpStream;
|
||||
|
||||
use pallas_chainsync::{ClientConsumer, NoopObserver};
|
||||
use pallas_chainsync::{BlockLike, Consumer, NoopObserver};
|
||||
use pallas_handshake::n2n::{Client, VersionTable};
|
||||
use pallas_handshake::MAINNET_MAGIC;
|
||||
use pallas_machines::run_agent;
|
||||
use pallas_machines::{run_agent, DecodePayload, EncodePayload, PayloadDecoder, PayloadEncoder};
|
||||
use pallas_multiplexer::Multiplexer;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Content(u32, Header);
|
||||
|
||||
impl EncodePayload for Content {
|
||||
fn encode_payload(&self, e: &mut PayloadEncoder) -> Result<(), Box<dyn std::error::Error>> {
|
||||
e.array(2)?;
|
||||
e.u32(self.0)?;
|
||||
e.tag(Tag::Cbor)?;
|
||||
e.bytes(&self.1.encode_fragment()?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl DecodePayload for Content {
|
||||
fn decode_payload(d: &mut PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
d.array()?;
|
||||
let unknown = d.u32()?; // WTF is this value?
|
||||
d.tag()?;
|
||||
let bytes = d.bytes()?;
|
||||
let header = Header::decode_fragment(bytes)?;
|
||||
Ok(Content(unknown, header))
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockLike for Content {
|
||||
fn block_point(&self) -> Result<Point, Box<dyn std::error::Error>> {
|
||||
let hash = crypto::hash_block_header(&self.1)?;
|
||||
Ok(Point(self.1.header_body.slot, Vec::from(hash)))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
||||
|
|
@ -29,7 +63,7 @@ fn main() {
|
|||
|
||||
let mut cs_channel = muxer.use_channel(2);
|
||||
|
||||
let cs = ClientConsumer::initial(known_points, NoopObserver {});
|
||||
let cs = Consumer::<Content, _>::initial(known_points, NoopObserver {});
|
||||
let cs = run_agent(cs, &mut cs_channel).unwrap();
|
||||
|
||||
println!("{:?}", cs);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue