mithril_cardano_node_chain/chain_observer/
builder.rs

1use serde::{Deserialize, Serialize};
2use std::{fmt::Display, path::PathBuf, sync::Arc};
3use thiserror::Error;
4
5use mithril_common::StdResult;
6use mithril_common::entities::CardanoNetwork;
7
8use crate::chain_observer::ChainObserver;
9use crate::test::double::FakeChainObserver;
10
11use super::{CardanoCliChainObserver, CardanoCliRunner, PallasChainObserver};
12
13/// Type of chain observers available
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
15#[serde(rename_all = "lowercase")]
16pub enum ChainObserverType {
17    /// Cardano Cli chain observer.
18    #[serde(rename = "cardano-cli")]
19    CardanoCli,
20    /// Pallas chain observer.
21    Pallas,
22    /// `TEST ONLY` Fake chain observer
23    Fake,
24}
25
26impl Display for ChainObserverType {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            Self::CardanoCli => write!(f, "cardano-cli"),
30            Self::Pallas => write!(f, "pallas"),
31            Self::Fake => write!(f, "fake"),
32        }
33    }
34}
35
36/// Error type for chain observer builder service.
37#[derive(Error, Debug)]
38pub enum ChainObserverBuilderError {
39    /// Missing cardano cli runner error.
40    #[error("cardano cli runner is missing")]
41    MissingCardanoCliRunner,
42}
43
44/// Chain observer builder
45pub struct ChainObserverBuilder {
46    chain_observer_type: ChainObserverType,
47    cardano_node_socket_path: PathBuf,
48    cardano_network: CardanoNetwork,
49    cardano_cli_runner: Option<Box<CardanoCliRunner>>,
50}
51
52impl ChainObserverBuilder {
53    /// Chain observer builder factory
54    pub fn new(
55        chain_observer_type: &ChainObserverType,
56        cardano_node_socket_path: &PathBuf,
57        cardano_node_network: &CardanoNetwork,
58        cardano_cli_runner: Option<&CardanoCliRunner>,
59    ) -> Self {
60        Self {
61            chain_observer_type: chain_observer_type.to_owned(),
62            cardano_node_socket_path: cardano_node_socket_path.to_owned(),
63            cardano_network: cardano_node_network.to_owned(),
64            cardano_cli_runner: cardano_cli_runner.map(|c| c.to_owned().into()),
65        }
66    }
67
68    /// Create chain observer
69    pub fn build(&self) -> StdResult<Arc<dyn ChainObserver>> {
70        match self.chain_observer_type {
71            ChainObserverType::CardanoCli => Ok(Arc::new(CardanoCliChainObserver::new(
72                self.cardano_cli_runner
73                    .as_ref()
74                    .ok_or(ChainObserverBuilderError::MissingCardanoCliRunner)?
75                    .to_owned(),
76            ))),
77            ChainObserverType::Pallas => {
78                let observer =
79                    PallasChainObserver::new(&self.cardano_node_socket_path, self.cardano_network);
80                Ok(Arc::new(observer))
81            }
82            ChainObserverType::Fake => Ok(Arc::new(FakeChainObserver::default())),
83        }
84    }
85}