Skip to content

Commit cc6c0ee

Browse files
committed
Implement Chain Observer Builder
1 parent 673a87d commit cc6c0ee

File tree

6 files changed

+144
-26
lines changed

6 files changed

+144
-26
lines changed

mithril-aggregator/src/configuration.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::anyhow;
22
use config::{ConfigError, Map, Source, Value, ValueKind};
3+
use mithril_common::chain_observer::ChainObserverType;
34
use mithril_common::crypto_helper::ProtocolGenesisSigner;
45
use mithril_common::era::adapters::EraReaderAdapterType;
56
use serde::{Deserialize, Serialize};
@@ -64,6 +65,9 @@ pub struct Configuration {
6465
/// Cardano network
6566
pub network: String,
6667

68+
/// Cardano chain observer type
69+
pub chain_observer_type: ChainObserverType,
70+
6771
/// Protocol parameters
6872
pub protocol_parameters: ProtocolParameters,
6973

@@ -175,6 +179,7 @@ impl Configuration {
175179
cardano_node_version: "0.0.1".to_string(),
176180
network_magic: Some(42),
177181
network: "devnet".to_string(),
182+
chain_observer_type: ChainObserverType::Fake,
178183
protocol_parameters: ProtocolParameters {
179184
k: 5,
180185
m: 100,
@@ -263,6 +268,9 @@ pub struct DefaultConfiguration {
263268
/// Era reader adapter type
264269
pub era_reader_adapter_type: String,
265270

271+
/// Chain observer type
272+
pub chain_observer_type: String,
273+
266274
/// ImmutableDigesterCacheProvider default setting
267275
pub reset_digests_cache: String,
268276

@@ -290,6 +298,7 @@ impl Default for DefaultConfiguration {
290298
snapshot_store_type: "local".to_string(),
291299
snapshot_uploader_type: "gcp".to_string(),
292300
era_reader_adapter_type: "bootstrap".to_string(),
301+
chain_observer_type: "pallas".to_string(),
293302
reset_digests_cache: "false".to_string(),
294303
disable_digests_cache: "false".to_string(),
295304
snapshot_compression_algorithm: "zstandard".to_string(),

mithril-aggregator/src/dependency_injection/builder.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ use warp::Filter;
1717
use mithril_common::{
1818
api_version::APIVersionProvider,
1919
certificate_chain::{CertificateVerifier, MithrilCertificateVerifier},
20-
chain_observer::{
21-
CardanoCliChainObserver, CardanoCliRunner, ChainObserver, FakeObserver, PallasChainObserver,
22-
},
20+
chain_observer::{CardanoCliRunner, ChainObserver, ChainObserverBuilder, FakeObserver},
2321
crypto_helper::{
2422
ProtocolGenesisSigner, ProtocolGenesisVerificationKey, ProtocolGenesisVerifier,
2523
},
@@ -487,15 +485,23 @@ impl DependenciesBuilder {
487485
async fn build_chain_observer(&mut self) -> Result<Arc<dyn ChainObserver>> {
488486
let chain_observer: Arc<dyn ChainObserver> = match self.configuration.environment {
489487
ExecutionEnvironment::Production => {
490-
let fallback = CardanoCliChainObserver::new(self.get_cardano_cli_runner().await?);
491-
let observer = PallasChainObserver::new(
492-
&self.configuration.cardano_node_socket_path,
493-
self.configuration.get_network().with_context(|| {
494-
"Dependencies Builder can not get Cardano network while building cardano cli runner"
495-
})?,
496-
fallback,
488+
let cardano_cli_runner = &self.get_cardano_cli_runner().await?;
489+
let chain_observer_type = &self.configuration.chain_observer_type;
490+
let cardano_node_socket_path = &self.configuration.cardano_node_socket_path;
491+
let cardano_network = &self
492+
.configuration
493+
.get_network()
494+
.with_context(|| "Dependencies Builder can not get Cardano network while building the chain observer")?;
495+
let chain_observer_builder = ChainObserverBuilder::new(
496+
chain_observer_type,
497+
cardano_node_socket_path,
498+
cardano_network,
499+
Some(cardano_cli_runner),
497500
);
498-
Arc::new(observer)
501+
502+
chain_observer_builder
503+
.build()
504+
.with_context(|| "Dependencies Builder can not build chain observer")?
499505
}
500506
_ => Arc::new(FakeObserver::default()),
501507
};
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use serde::{Deserialize, Serialize};
2+
use std::{fmt::Display, path::PathBuf, sync::Arc};
3+
use thiserror::Error;
4+
5+
use crate::{chain_observer::ChainObserver, CardanoNetwork, StdResult};
6+
7+
use super::{CardanoCliChainObserver, CardanoCliRunner, FakeObserver, PallasChainObserver};
8+
9+
/// Type of chain observers available
10+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11+
#[serde(rename_all = "lowercase")]
12+
pub enum ChainObserverType {
13+
/// Cardano Cli chain observer.
14+
#[serde(rename = "cardano-cli")]
15+
CardanoCli,
16+
/// Pallas chain observer.
17+
Pallas,
18+
/// Fake chain observer.
19+
#[cfg(feature = "test_tools")]
20+
Fake,
21+
}
22+
23+
impl Display for ChainObserverType {
24+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25+
match self {
26+
Self::CardanoCli => write!(f, "cardano-cli"),
27+
Self::Pallas => write!(f, "pallas"),
28+
Self::Fake => write!(f, "fake"),
29+
}
30+
}
31+
}
32+
33+
/// Error type for chain observer builder service.
34+
#[derive(Error, Debug)]
35+
pub enum ChainObserverBuilderError {
36+
/// Missing cardano cli runner error.
37+
#[error("cardano cli runner is missing")]
38+
MissingCardanoCliRunner,
39+
}
40+
41+
/// Chain observer builder
42+
pub struct ChainObserverBuilder {
43+
chain_observer_type: ChainObserverType,
44+
cardano_node_socket_path: PathBuf,
45+
cardano_network: CardanoNetwork,
46+
cardano_cli_runner: Option<Box<CardanoCliRunner>>,
47+
}
48+
49+
impl ChainObserverBuilder {
50+
/// Chain observer builder factory
51+
pub fn new(
52+
chain_observer_type: &ChainObserverType,
53+
cardano_node_socket_path: &PathBuf,
54+
cardano_node_network: &CardanoNetwork,
55+
cardano_cli_runner: Option<&CardanoCliRunner>,
56+
) -> Self {
57+
Self {
58+
chain_observer_type: chain_observer_type.to_owned(),
59+
cardano_node_socket_path: cardano_node_socket_path.to_owned(),
60+
cardano_network: cardano_node_network.to_owned(),
61+
cardano_cli_runner: cardano_cli_runner.map(|c| c.to_owned().into()),
62+
}
63+
}
64+
65+
/// Create chain observer
66+
pub fn build(&self) -> StdResult<Arc<dyn ChainObserver>> {
67+
match self.chain_observer_type {
68+
ChainObserverType::CardanoCli => Ok(Arc::new(CardanoCliChainObserver::new(
69+
self.cardano_cli_runner
70+
.as_ref()
71+
.ok_or(ChainObserverBuilderError::MissingCardanoCliRunner)?
72+
.to_owned(),
73+
))),
74+
ChainObserverType::Pallas => {
75+
let fallback = CardanoCliChainObserver::new(
76+
self.cardano_cli_runner
77+
.as_ref()
78+
.ok_or(ChainObserverBuilderError::MissingCardanoCliRunner)?
79+
.to_owned(),
80+
);
81+
let observer = PallasChainObserver::new(
82+
&self.cardano_node_socket_path,
83+
self.cardano_network,
84+
fallback,
85+
);
86+
Ok(Arc::new(observer))
87+
}
88+
#[cfg(feature = "test_tools")]
89+
ChainObserverType::Fake => Ok(Arc::new(FakeObserver::default())),
90+
}
91+
}
92+
}

mithril-common/src/chain_observer/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Tools to request metadata, like the current epoch or the stake distribution, from the Cardano
22
3+
#[cfg(all(feature = "fs", feature = "random"))]
4+
mod builder;
35
#[cfg(all(feature = "fs", feature = "random"))]
46
mod cli_observer;
57
#[cfg(feature = "test_tools")]
@@ -15,6 +17,8 @@ mod test_cli_runner;
1517
#[cfg(test)]
1618
pub use cli_observer::CliRunner;
1719

20+
#[cfg(all(feature = "fs", feature = "random"))]
21+
pub use builder::{ChainObserverBuilder, ChainObserverType};
1822
#[cfg(all(feature = "fs", feature = "random"))]
1923
pub use cli_observer::{CardanoCliChainObserver, CardanoCliRunner};
2024
#[cfg(feature = "test_tools")]

mithril-common/src/era/adapters/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
StdError,
1616
};
1717

18-
/// Type of era reader adapaters available
18+
/// Type of era reader adapters available
1919
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
2020
#[serde(rename_all = "lowercase")]
2121
pub enum AdapterType {

mithril-signer/src/runtime/signer_services.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use std::{fs, sync::Arc, time::Duration};
55

66
use mithril_common::{
77
api_version::APIVersionProvider,
8-
chain_observer::{
9-
CardanoCliChainObserver, CardanoCliRunner, ChainObserver, PallasChainObserver,
10-
},
8+
chain_observer::{CardanoCliRunner, ChainObserver, ChainObserverBuilder, ChainObserverType},
119
crypto_helper::{OpCert, ProtocolPartyId, SerDeShelleyFileFormat},
1210
database::{ApplicationNodeType, DatabaseVersionChecker},
1311
digesters::{
@@ -60,19 +58,28 @@ impl<'a> ProductionServiceBuilder<'a> {
6058
pub fn new(config: &'a Configuration) -> Self {
6159
let chain_observer_builder: fn(&Configuration) -> StdResult<ChainObserverService> =
6260
|config: &Configuration| {
63-
let fallback = CardanoCliChainObserver::new(Box::new(CardanoCliRunner::new(
64-
config.cardano_cli_path.clone(),
65-
config.cardano_node_socket_path.clone(),
66-
config.get_network()?,
67-
)));
68-
69-
let observer = PallasChainObserver::new(
70-
&config.cardano_node_socket_path,
71-
config.get_network()?,
72-
fallback,
61+
let chain_observer_type = ChainObserverType::Pallas;
62+
let cardano_cli_path = &config.cardano_cli_path;
63+
let cardano_node_socket_path = &config.cardano_node_socket_path;
64+
let cardano_network = &config.get_network().with_context(|| {
65+
"Production Service Builder can not get Cardano network while building the chain observer"
66+
})?;
67+
let cardano_cli_runner = &CardanoCliRunner::new(
68+
cardano_cli_path.to_owned(),
69+
cardano_node_socket_path.to_owned(),
70+
cardano_network.to_owned(),
71+
);
72+
73+
let chain_observer_builder = ChainObserverBuilder::new(
74+
&chain_observer_type,
75+
cardano_node_socket_path,
76+
cardano_network,
77+
Some(cardano_cli_runner),
7378
);
7479

75-
Ok(Arc::new(observer))
80+
chain_observer_builder
81+
.build()
82+
.with_context(|| "Dependencies Builder can not build chain observer")
7683
};
7784

7885
let immutable_file_observer_builder: fn(

0 commit comments

Comments
 (0)