diff --git a/pallas-alonzo/src/lib.rs b/pallas-alonzo/src/lib.rs index 10bed01..2b7e987 100644 --- a/pallas-alonzo/src/lib.rs +++ b/pallas-alonzo/src/lib.rs @@ -117,7 +117,7 @@ pub type Mint = Multiasset; pub type Coin = u64; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum Value { Coin(Coin), Multiasset(Coin, Multiasset), diff --git a/pallas-chainsync/examples/blocks.rs b/pallas-chainsync/examples/blocks.rs index 96e53da..0808590 100644 --- a/pallas-chainsync/examples/blocks.rs +++ b/pallas-chainsync/examples/blocks.rs @@ -1,4 +1,4 @@ -use pallas_chainsync::{ClientConsumer, NoopStorage}; +use pallas_chainsync::{ClientConsumer, NoopObserver}; use pallas_handshake::n2c::{Client, VersionTable}; use pallas_handshake::MAINNET_MAGIC; use pallas_machines::primitives::Point; @@ -27,7 +27,7 @@ fn main() { )]; let cs_channel = muxer.use_channel(5); - let cs = ClientConsumer::initial(known_points, NoopStorage { }); + let cs = ClientConsumer::initial(known_points, NoopObserver { }); let cs = run_agent(cs, cs_channel).unwrap(); println!("{:?}", cs); } diff --git a/pallas-chainsync/examples/headers.rs b/pallas-chainsync/examples/headers.rs index fedcda1..644c30d 100644 --- a/pallas-chainsync/examples/headers.rs +++ b/pallas-chainsync/examples/headers.rs @@ -2,7 +2,7 @@ use net2::TcpStreamExt; use pallas_machines::primitives::Point; use std::net::TcpStream; -use pallas_chainsync::{ClientConsumer, NoopStorage}; +use pallas_chainsync::{ClientConsumer, NoopObserver}; use pallas_handshake::n2n::{Client, VersionTable}; use pallas_handshake::MAINNET_MAGIC; use pallas_machines::run_agent; @@ -29,7 +29,7 @@ fn main() { let cs_channel = muxer.use_channel(2); - let cs = ClientConsumer::initial(known_points, NoopStorage {}); + let cs = ClientConsumer::initial(known_points, NoopObserver {}); let cs = run_agent(cs, cs_channel).unwrap(); println!("{:?}", cs); diff --git a/pallas-chainsync/src/lib.rs b/pallas-chainsync/src/lib.rs index d085a62..e5da871 100644 --- a/pallas-chainsync/src/lib.rs +++ b/pallas-chainsync/src/lib.rs @@ -189,52 +189,58 @@ where } } -/// An abstraction of a component in charge of block persistence -pub trait Storage { - fn save_block(&self, content: &C) -> Result<(), Box>; +/// An observer of chain-sync events sent by the state-machine +pub trait Observer { + fn on_block(&self, content: &C) -> Result<(), Box>; + fn on_rollback(&self, point: &Point) -> Result<(), Box>; } #[derive(Debug)] -pub struct NoopStorage {} +pub struct NoopObserver {} -impl Storage for NoopStorage +impl Observer for NoopObserver where C: Debug, { - fn save_block(&self, content: &C) -> Result<(), Box> { + fn on_block(&self, content: &C) -> Result<(), Box> { log::warn!("asked to save block {:?}", content); Ok(()) } + + fn on_rollback(&self, point: &Point) -> Result<(), Box> { + log::warn!("asked to roll back {:?}", point); + Ok(()) + } } #[derive(Debug)] -pub struct Consumer +pub struct Consumer where - S: Storage, + O: Observer, { pub state: State, pub known_points: Vec, pub cursor: Option, pub tip: Option, - storage: S, + observer: O, // as recommended here: https://doc.rust-lang.org/error-index.html#E0207 _phantom: Option, } -impl Consumer +impl Consumer where C: EncodePayload + DecodePayload + Debug, - S: Storage, + O: Observer, { - pub fn initial(known_points: Vec, storage: S) -> Self { + pub fn initial(known_points: Vec, observer: O) -> Self { Self { state: State::Idle, cursor: None, tip: None, known_points, - storage, + observer, _phantom: None, } @@ -291,8 +297,8 @@ where trace!("content: {:?}", content); } - debug!("saving block"); - self.storage.save_block(&content)?; + debug!("reporint block to observer"); + self.observer.on_block(&content)?; Ok(Self { tip: Some(tip), @@ -304,6 +310,9 @@ where fn on_roll_backward(self, point: Point, tip: Tip) -> Transition { debug!("rolling backward to point: {:?}", point); + debug!("reporting rollback to observer"); + self.observer.on_rollback(&point)?; + Ok(Self { tip: Some(tip), cursor: Some(point), @@ -322,10 +331,10 @@ where } } -impl Agent for Consumer +impl Agent for Consumer where C: EncodePayload + DecodePayload + Debug + 'static, - S: Storage, + O: Observer, { type Message = Message;