use crate::{marshal::Update, Block, Reporter}; use std::{ collections::BTreeMap, sync::{Arc, Mutex}, }; /// A mock application that stores finalized blocks. #[derive(Clone)] pub struct Application { blocks: Arc>>, #[allow(clippy::type_complexity)] tip: Arc>>, } impl Default for Application { fn default() -> Self { Self { blocks: Default::default(), tip: Default::default(), } } } impl Application { /// Returns the finalized blocks. pub fn blocks(&self) -> BTreeMap { self.blocks.lock().unwrap().clone() } /// Returns the tip. pub fn tip(&self) -> Option<(u64, B::Commitment)> { *self.tip.lock().unwrap() } } impl Reporter for Application { type Activity = Update; async fn report(&mut self, activity: Self::Activity) { match activity { Update::Block(block) => { self.blocks.lock().unwrap().insert(block.height(), block); } Update::Tip(height, commitment) => { *self.tip.lock().unwrap() = Some((height, commitment)); } } } }