mithril_common/chain_reader/
fake_chain_reader.rs1use std::{collections::VecDeque, sync::Mutex};
2
3use async_trait::async_trait;
4
5use crate::cardano_block_scanner::RawCardanoPoint;
6use crate::StdResult;
7
8use super::{ChainBlockNextAction, ChainBlockReader};
9
10pub struct FakeChainReader {
12 chain_block_next_actions: Mutex<VecDeque<ChainBlockNextAction>>,
13}
14
15impl FakeChainReader {
16 pub fn new(chain_block_next_actions: Vec<ChainBlockNextAction>) -> Self {
18 Self {
19 chain_block_next_actions: Mutex::new(chain_block_next_actions.into()),
20 }
21 }
22
23 pub fn get_total_remaining_next_actions(&self) -> usize {
25 self.chain_block_next_actions.lock().unwrap().len()
26 }
27}
28
29#[async_trait]
30impl ChainBlockReader for FakeChainReader {
31 async fn set_chain_point(&mut self, _point: &RawCardanoPoint) -> StdResult<()> {
32 Ok(())
33 }
34
35 async fn get_next_chain_block(&mut self) -> StdResult<Option<ChainBlockNextAction>> {
36 Ok(self.chain_block_next_actions.lock().unwrap().pop_front())
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use crate::cardano_block_scanner::ScannedBlock;
43 use crate::entities::{BlockNumber, SlotNumber};
44
45 use super::*;
46
47 #[tokio::test]
48 async fn test_get_next_chain_block() {
49 let expected_chain_block_next_actions = vec![
50 ChainBlockNextAction::RollForward {
51 parsed_block: ScannedBlock::new(
52 "hash-1",
53 BlockNumber(1),
54 SlotNumber(10),
55 Vec::<&str>::new(),
56 ),
57 },
58 ChainBlockNextAction::RollForward {
59 parsed_block: ScannedBlock::new(
60 "hash-2",
61 BlockNumber(2),
62 SlotNumber(11),
63 Vec::<&str>::new(),
64 ),
65 },
66 ChainBlockNextAction::RollBackward {
67 rollback_point: RawCardanoPoint::new(SlotNumber(1), "point-hash-1".as_bytes()),
68 },
69 ];
70
71 let mut chain_reader = FakeChainReader::new(expected_chain_block_next_actions.clone());
72
73 let mut chain_block_next_actions = vec![];
74 while let Some(chain_block_next_action) = chain_reader.get_next_chain_block().await.unwrap()
75 {
76 chain_block_next_actions.push(chain_block_next_action);
77 }
78
79 assert_eq!(expected_chain_block_next_actions, chain_block_next_actions);
80 }
81}