From 05af7a91308c9a07c05adc4bf83c1dc6d4da13c4 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Wed, 16 Feb 2022 19:08:51 -0300 Subject: [PATCH] fix: Add mutability to chainsync observer (#50) --- pallas-miniprotocols/src/chainsync/clients.rs | 20 +++++++++++-------- pallas-miniprotocols/src/common.rs | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pallas-miniprotocols/src/chainsync/clients.rs b/pallas-miniprotocols/src/chainsync/clients.rs index ad6f1df..bbbe538 100644 --- a/pallas-miniprotocols/src/chainsync/clients.rs +++ b/pallas-miniprotocols/src/chainsync/clients.rs @@ -12,14 +12,18 @@ use super::{BlockContent, HeaderContent, Message, SkippedContent, State, Tip}; /// An observer of chain-sync events sent by the state-machine pub trait Observer { - fn on_roll_forward(&self, _content: C, tip: &Tip) -> Result<(), Box> { + fn on_roll_forward( + &mut self, + _content: C, + tip: &Tip, + ) -> Result<(), Box> { log::debug!("asked to roll forward, tip at {:?}", tip); Ok(()) } fn on_intersect_found( - &self, + &mut self, point: &Point, tip: &Tip, ) -> Result<(), Box> { @@ -27,11 +31,11 @@ pub trait Observer { Ok(()) } - fn on_rollback(&self, point: &Point) -> Result<(), Box> { + fn on_rollback(&mut self, point: &Point) -> Result<(), Box> { log::debug!("asked to roll back {:?}", point); Ok(()) } - fn on_tip_reached(&self) -> Result<(), Box> { + fn on_tip_reached(&mut self) -> Result<(), Box> { log::debug!("tip was reached"); Ok(()) } @@ -99,7 +103,7 @@ where }) } - fn on_intersect_found(self, point: Point, tip: Tip) -> Transition { + fn on_intersect_found(mut self, point: Point, tip: Tip) -> Transition { debug!("intersect found: {:?} (tip: {:?})", point, tip); self.observer.on_intersect_found(&point, &tip)?; @@ -123,7 +127,7 @@ where }) } - fn on_roll_forward(self, content: C, tip: Tip) -> Transition { + fn on_roll_forward(mut self, content: C, tip: Tip) -> Transition { debug!("rolling forward"); self.observer.on_roll_forward(content, &tip)?; @@ -135,7 +139,7 @@ where }) } - fn on_roll_backward(self, point: Point, tip: Tip) -> Transition { + fn on_roll_backward(mut self, point: Point, tip: Tip) -> Transition { debug!("rolling backward to point: {:?}", point); debug!("reporting rollback to observer"); @@ -149,7 +153,7 @@ where }) } - fn on_await_reply(self) -> Transition { + fn on_await_reply(mut self) -> Transition { debug!("reached tip, await reply"); self.observer.on_tip_reached()?; diff --git a/pallas-miniprotocols/src/common.rs b/pallas-miniprotocols/src/common.rs index 9ddcaea..70ad4e9 100644 --- a/pallas-miniprotocols/src/common.rs +++ b/pallas-miniprotocols/src/common.rs @@ -7,7 +7,7 @@ pub const TESTNET_MAGIC: u64 = 1097911063; pub const MAINNET_MAGIC: u64 = 764824073; /// A point within a chain -#[derive(Clone, Eq, PartialEq)] +#[derive(Clone, Eq, PartialEq, Hash)] pub struct Point(pub u64, pub Vec); impl Debug for Point {