fix: Add mutability to chainsync observer (#50)

This commit is contained in:
Santiago Carmuega 2022-02-16 19:08:51 -03:00 committed by GitHub
parent d14a7dc905
commit a77ec872ec
2 changed files with 13 additions and 9 deletions

View file

@ -12,14 +12,18 @@ use super::{BlockContent, HeaderContent, Message, SkippedContent, State, Tip};
/// An observer of chain-sync events sent by the state-machine /// An observer of chain-sync events sent by the state-machine
pub trait Observer<C> { pub trait Observer<C> {
fn on_roll_forward(&self, _content: C, tip: &Tip) -> Result<(), Box<dyn std::error::Error>> { fn on_roll_forward(
&mut self,
_content: C,
tip: &Tip,
) -> Result<(), Box<dyn std::error::Error>> {
log::debug!("asked to roll forward, tip at {:?}", tip); log::debug!("asked to roll forward, tip at {:?}", tip);
Ok(()) Ok(())
} }
fn on_intersect_found( fn on_intersect_found(
&self, &mut self,
point: &Point, point: &Point,
tip: &Tip, tip: &Tip,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
@ -27,11 +31,11 @@ pub trait Observer<C> {
Ok(()) Ok(())
} }
fn on_rollback(&self, point: &Point) -> Result<(), Box<dyn std::error::Error>> { fn on_rollback(&mut self, point: &Point) -> Result<(), Box<dyn std::error::Error>> {
log::debug!("asked to roll back {:?}", point); log::debug!("asked to roll back {:?}", point);
Ok(()) Ok(())
} }
fn on_tip_reached(&self) -> Result<(), Box<dyn std::error::Error>> { fn on_tip_reached(&mut self) -> Result<(), Box<dyn std::error::Error>> {
log::debug!("tip was reached"); log::debug!("tip was reached");
Ok(()) Ok(())
} }
@ -99,7 +103,7 @@ where
}) })
} }
fn on_intersect_found(self, point: Point, tip: Tip) -> Transition<Self> { fn on_intersect_found(mut self, point: Point, tip: Tip) -> Transition<Self> {
debug!("intersect found: {:?} (tip: {:?})", point, tip); debug!("intersect found: {:?} (tip: {:?})", point, tip);
self.observer.on_intersect_found(&point, &tip)?; self.observer.on_intersect_found(&point, &tip)?;
@ -123,7 +127,7 @@ where
}) })
} }
fn on_roll_forward(self, content: C, tip: Tip) -> Transition<Self> { fn on_roll_forward(mut self, content: C, tip: Tip) -> Transition<Self> {
debug!("rolling forward"); debug!("rolling forward");
self.observer.on_roll_forward(content, &tip)?; self.observer.on_roll_forward(content, &tip)?;
@ -135,7 +139,7 @@ where
}) })
} }
fn on_roll_backward(self, point: Point, tip: Tip) -> Transition<Self> { fn on_roll_backward(mut self, point: Point, tip: Tip) -> Transition<Self> {
debug!("rolling backward to point: {:?}", point); debug!("rolling backward to point: {:?}", point);
debug!("reporting rollback to observer"); debug!("reporting rollback to observer");
@ -149,7 +153,7 @@ where
}) })
} }
fn on_await_reply(self) -> Transition<Self> { fn on_await_reply(mut self) -> Transition<Self> {
debug!("reached tip, await reply"); debug!("reached tip, await reply");
self.observer.on_tip_reached()?; self.observer.on_tip_reached()?;

View file

@ -7,7 +7,7 @@ pub const TESTNET_MAGIC: u64 = 1097911063;
pub const MAINNET_MAGIC: u64 = 764824073; pub const MAINNET_MAGIC: u64 = 764824073;
/// A point within a chain /// A point within a chain
#[derive(Clone, Eq, PartialEq)] #[derive(Clone, Eq, PartialEq, Hash)]
pub struct Point(pub u64, pub Vec<u8>); pub struct Point(pub u64, pub Vec<u8>);
impl Debug for Point { impl Debug for Point {