use crate::chaindef::BlockHash;
use crate::undo::blockundoer::BlockUndoer;
use anyhow::Result;
use async_trait::async_trait;

pub struct DummyBlockUndoer {
    allow_undo_calls: bool,
}

impl DummyBlockUndoer {
    pub fn new(allow_undo_calls: bool) -> Self {
        Self { allow_undo_calls }
    }
}

impl Default for DummyBlockUndoer {
    fn default() -> Self {
        Self::new(false)
    }
}

#[async_trait]
impl BlockUndoer for DummyBlockUndoer {
    async fn undo_block(
        &self,
        _blockhash: &BlockHash,
        _height: u64,
        _new_tip: &BlockHash,
    ) -> Result<()> {
        if !self.allow_undo_calls {
            panic!("undo_block called on DummyBlockUndoer")
        } else {
            Ok(())
        }
    }
}
