parent
7fe00c6e22
commit
43c3cbd457
33 changed files with 402 additions and 450 deletions
|
|
@ -1,10 +1,9 @@
|
|||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use log::debug;
|
||||
use pallas_codec::Fragment;
|
||||
|
||||
use crate::machines::{Agent, MachineError, MachineOutput, Transition};
|
||||
use crate::{DecodePayload, EncodePayload};
|
||||
|
||||
use crate::common::Point;
|
||||
|
||||
|
|
@ -64,7 +63,7 @@ where
|
|||
impl<C, O> Consumer<C, O>
|
||||
where
|
||||
O: Observer<C>,
|
||||
C: DecodePayload + EncodePayload,
|
||||
Message<C>: Fragment,
|
||||
{
|
||||
pub fn initial(known_points: Option<Vec<Point>>, observer: O) -> Self {
|
||||
Self {
|
||||
|
|
@ -78,7 +77,7 @@ where
|
|||
}
|
||||
|
||||
fn send_find_intersect(self, tx: &impl MachineOutput) -> Transition<Self> {
|
||||
debug!("requesting find intersect");
|
||||
log::debug!("requesting find intersect");
|
||||
|
||||
let points = match &self.known_points {
|
||||
Some(x) => x.clone(),
|
||||
|
|
@ -96,7 +95,7 @@ where
|
|||
}
|
||||
|
||||
fn send_request_next(self, tx: &impl MachineOutput) -> Transition<Self> {
|
||||
debug!("requesting next");
|
||||
log::debug!("requesting next");
|
||||
|
||||
let msg = Message::<C>::RequestNext;
|
||||
|
||||
|
|
@ -109,7 +108,7 @@ where
|
|||
}
|
||||
|
||||
fn on_intersect_found(mut self, point: Point, tip: Tip) -> Transition<Self> {
|
||||
debug!("intersect found: {:?} (tip: {:?})", point, tip);
|
||||
log::debug!("intersect found: {:?} (tip: {:?})", point, tip);
|
||||
|
||||
self.observer.on_intersect_found(&point, &tip)?;
|
||||
|
||||
|
|
@ -122,7 +121,7 @@ where
|
|||
}
|
||||
|
||||
fn on_intersect_not_found(self, tip: Tip) -> Transition<Self> {
|
||||
debug!("intersect not found (tip: {:?})", tip);
|
||||
log::debug!("intersect not found (tip: {:?})", tip);
|
||||
|
||||
Ok(Self {
|
||||
tip: Some(tip),
|
||||
|
|
@ -133,7 +132,7 @@ where
|
|||
}
|
||||
|
||||
fn on_roll_forward(mut self, content: C, tip: Tip) -> Transition<Self> {
|
||||
debug!("rolling forward");
|
||||
log::debug!("rolling forward");
|
||||
|
||||
self.observer.on_roll_forward(content, &tip)?;
|
||||
|
||||
|
|
@ -145,9 +144,9 @@ where
|
|||
}
|
||||
|
||||
fn on_roll_backward(mut self, point: Point, tip: Tip) -> Transition<Self> {
|
||||
debug!("rolling backward to point: {:?}", point);
|
||||
log::debug!("rolling backward to point: {:?}", point);
|
||||
|
||||
debug!("reporting rollback to observer");
|
||||
log::debug!("reporting rollback to observer");
|
||||
self.observer.on_rollback(&point)?;
|
||||
|
||||
Ok(Self {
|
||||
|
|
@ -159,7 +158,7 @@ where
|
|||
}
|
||||
|
||||
fn on_await_reply(mut self) -> Transition<Self> {
|
||||
debug!("reached tip, await reply");
|
||||
log::debug!("reached tip, await reply");
|
||||
|
||||
self.observer.on_tip_reached()?;
|
||||
|
||||
|
|
@ -172,8 +171,9 @@ where
|
|||
|
||||
impl<C, O> Agent for Consumer<C, O>
|
||||
where
|
||||
C: EncodePayload + DecodePayload + Debug + 'static,
|
||||
O: Observer<C>,
|
||||
C: Debug + 'static,
|
||||
Message<C>: Fragment,
|
||||
{
|
||||
type Message = Message<C>;
|
||||
|
||||
|
|
@ -259,7 +259,7 @@ impl TipFinder {
|
|||
}
|
||||
|
||||
fn on_intersect_found(self, tip: Tip) -> Transition<Self> {
|
||||
debug!("intersect found with tip: {:?}", tip);
|
||||
log::debug!("intersect found with tip: {:?}", tip);
|
||||
|
||||
Ok(Self {
|
||||
state: State::Done,
|
||||
|
|
@ -269,7 +269,7 @@ impl TipFinder {
|
|||
}
|
||||
|
||||
fn on_intersect_not_found(self, tip: Tip) -> Transition<Self> {
|
||||
debug!("intersect not found but still have a tip: {:?}", tip);
|
||||
log::debug!("intersect not found but still have a tip: {:?}", tip);
|
||||
|
||||
Ok(Self {
|
||||
state: State::Done,
|
||||
|
|
|
|||
|
|
@ -1,33 +1,36 @@
|
|||
use crate::common::Point;
|
||||
use crate::machines::{CodecError, DecodePayload, EncodePayload, PayloadDecoder, PayloadEncoder};
|
||||
use pallas_codec::{
|
||||
impl_fragment,
|
||||
minicbor::{decode, encode, Decode, Decoder, Encode, Encoder},
|
||||
};
|
||||
|
||||
use super::{BlockContent, HeaderContent, Message, SkippedContent, Tip};
|
||||
|
||||
impl EncodePayload for Tip {
|
||||
fn encode_payload(&self, e: &mut PayloadEncoder) -> Result<(), Box<dyn std::error::Error>> {
|
||||
impl Encode for Tip {
|
||||
fn encode<W: encode::Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
|
||||
e.array(2)?;
|
||||
self.0.encode_payload(e)?;
|
||||
self.0.encode(e)?;
|
||||
e.u64(self.1)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl DecodePayload for Tip {
|
||||
fn decode_payload(d: &mut PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
impl<'b> Decode<'b> for Tip {
|
||||
fn decode(d: &mut Decoder<'b>) -> Result<Self, decode::Error> {
|
||||
d.array()?;
|
||||
let point = Point::decode_payload(d)?;
|
||||
let point = Point::decode(d)?;
|
||||
let block_num = d.u64()?;
|
||||
|
||||
Ok(Tip(point, block_num))
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> EncodePayload for Message<C>
|
||||
impl<C> Encode for Message<C>
|
||||
where
|
||||
C: EncodePayload,
|
||||
C: Encode,
|
||||
{
|
||||
fn encode_payload(&self, e: &mut PayloadEncoder) -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn encode<W: encode::Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
|
||||
match self {
|
||||
Message::RequestNext => {
|
||||
e.array(1)?.u16(0)?;
|
||||
|
|
@ -39,33 +42,33 @@ where
|
|||
}
|
||||
Message::RollForward(content, tip) => {
|
||||
e.array(3)?.u16(2)?;
|
||||
content.encode_payload(e)?;
|
||||
tip.encode_payload(e)?;
|
||||
content.encode(e)?;
|
||||
tip.encode(e)?;
|
||||
Ok(())
|
||||
}
|
||||
Message::RollBackward(point, tip) => {
|
||||
e.array(3)?.u16(3)?;
|
||||
point.encode_payload(e)?;
|
||||
tip.encode_payload(e)?;
|
||||
point.encode(e)?;
|
||||
tip.encode(e)?;
|
||||
Ok(())
|
||||
}
|
||||
Message::FindIntersect(points) => {
|
||||
e.array(2)?.u16(4)?;
|
||||
e.array(points.len() as u64)?;
|
||||
for point in points.iter() {
|
||||
point.encode_payload(e)?;
|
||||
point.encode(e)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Message::IntersectFound(point, tip) => {
|
||||
e.array(3)?.u16(5)?;
|
||||
point.encode_payload(e)?;
|
||||
tip.encode_payload(e)?;
|
||||
point.encode(e)?;
|
||||
tip.encode(e)?;
|
||||
Ok(())
|
||||
}
|
||||
Message::IntersectNotFound(tip) => {
|
||||
e.array(1)?.u16(6)?;
|
||||
tip.encode_payload(e)?;
|
||||
tip.encode(e)?;
|
||||
Ok(())
|
||||
}
|
||||
Message::Done => {
|
||||
|
|
@ -76,11 +79,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<C> DecodePayload for Message<C>
|
||||
impl<'b, C> Decode<'b> for Message<C>
|
||||
where
|
||||
C: DecodePayload,
|
||||
C: Decode<'b>,
|
||||
{
|
||||
fn decode_payload(d: &mut PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
fn decode(d: &mut Decoder<'b>) -> Result<Self, decode::Error> {
|
||||
d.array()?;
|
||||
let label = d.u16()?;
|
||||
|
||||
|
|
@ -88,36 +91,38 @@ where
|
|||
0 => Ok(Message::RequestNext),
|
||||
1 => Ok(Message::AwaitReply),
|
||||
2 => {
|
||||
let content = C::decode_payload(d)?;
|
||||
let tip = Tip::decode_payload(d)?;
|
||||
let content = C::decode(d)?;
|
||||
let tip = Tip::decode(d)?;
|
||||
Ok(Message::RollForward(content, tip))
|
||||
}
|
||||
3 => {
|
||||
let point = Point::decode_payload(d)?;
|
||||
let tip = Tip::decode_payload(d)?;
|
||||
let point = Point::decode(d)?;
|
||||
let tip = Tip::decode(d)?;
|
||||
Ok(Message::RollBackward(point, tip))
|
||||
}
|
||||
4 => {
|
||||
let points = Vec::<Point>::decode_payload(d)?;
|
||||
let points = Vec::<Point>::decode(d)?;
|
||||
Ok(Message::FindIntersect(points))
|
||||
}
|
||||
5 => {
|
||||
let point = Point::decode_payload(d)?;
|
||||
let tip = Tip::decode_payload(d)?;
|
||||
let point = Point::decode(d)?;
|
||||
let tip = Tip::decode(d)?;
|
||||
Ok(Message::IntersectFound(point, tip))
|
||||
}
|
||||
6 => {
|
||||
let tip = Tip::decode_payload(d)?;
|
||||
let tip = Tip::decode(d)?;
|
||||
Ok(Message::IntersectNotFound(tip))
|
||||
}
|
||||
7 => Ok(Message::Done),
|
||||
x => Err(Box::new(CodecError::BadLabel(x))),
|
||||
_ => Err(decode::Error::message(
|
||||
"unknown variant for chainsync message",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DecodePayload for HeaderContent {
|
||||
fn decode_payload(d: &mut crate::PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
impl<'b> Decode<'b> for HeaderContent {
|
||||
fn decode(d: &mut Decoder<'b>) -> Result<Self, decode::Error> {
|
||||
d.array()?;
|
||||
let variant = d.u8()?; // era variant
|
||||
|
||||
|
|
@ -154,43 +159,41 @@ impl DecodePayload for HeaderContent {
|
|||
}
|
||||
}
|
||||
|
||||
impl EncodePayload for HeaderContent {
|
||||
fn encode_payload(
|
||||
&self,
|
||||
_e: &mut crate::PayloadEncoder,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
impl Encode for HeaderContent {
|
||||
fn encode<W: encode::Write>(&self, _e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl DecodePayload for BlockContent {
|
||||
fn decode_payload(d: &mut crate::PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
impl_fragment!(Message<HeaderContent>);
|
||||
|
||||
impl<'b> Decode<'b> for BlockContent {
|
||||
fn decode(d: &mut Decoder<'b>) -> Result<Self, decode::Error> {
|
||||
d.tag()?;
|
||||
let bytes = d.bytes()?;
|
||||
Ok(BlockContent(Vec::from(bytes)))
|
||||
}
|
||||
}
|
||||
|
||||
impl EncodePayload for BlockContent {
|
||||
fn encode_payload(
|
||||
&self,
|
||||
_e: &mut crate::PayloadEncoder,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
impl Encode for BlockContent {
|
||||
fn encode<W: encode::Write>(&self, _e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
impl DecodePayload for SkippedContent {
|
||||
fn decode_payload(d: &mut crate::PayloadDecoder) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
|
||||
impl_fragment!(Message<BlockContent>);
|
||||
|
||||
impl<'b> Decode<'b> for SkippedContent {
|
||||
fn decode(d: &mut Decoder<'b>) -> Result<Self, decode::Error> {
|
||||
d.skip()?;
|
||||
Ok(SkippedContent)
|
||||
}
|
||||
}
|
||||
|
||||
impl EncodePayload for SkippedContent {
|
||||
fn encode_payload(
|
||||
&self,
|
||||
_e: &mut crate::PayloadEncoder,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(())
|
||||
impl Encode for SkippedContent {
|
||||
fn encode<W: encode::Write>(&self, _e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl_fragment!(Message<SkippedContent>);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue