Skip to content

Commit d34c487

Browse files
authored
Merge pull request #1423 from input-output-hk/jpraynaud/1396-chain-observer-builder-aggregator
Implement configurable Chain Observer in aggregator
2 parents 673a87d + 0fb579e commit d34c487

File tree

23 files changed

+230
-75
lines changed

23 files changed

+230
-75
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/website/root/manual/developer-docs/nodes/mithril-aggregator.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ Here is a list of the available parameters:
439439
| `snapshot_bucket_name` | - | - | `SNAPSHOT_BUCKET_NAME` | Name of the bucket where the snapshots are stored | - | `snapshot-bucket` | :heavy_check_mark: | Required if `snapshot_uploader_type` is `gcp`
440440
| `snapshot_use_cdn_domain` | - | - | `SNAPSHOT_USE_CDN_DOMAIN` | Use CDN domain for constructing snapshot url | `false` | - | - | To be used if `snapshot_uploader_type` is `gcp`
441441
| `run_interval` | - | - | `RUN_INTERVAL` | Interval between two runtime cycles in ms | - | `60000` | :heavy_check_mark: |
442+
| `chain_observer_type` | `--chain-observer-type` | - | `CHAIN_OBSERVER_TYPE` | Chain observer type that can be `cardano-cli`, `pallas` or `fake`. | `pallas` | - | - |
442443
| `era_reader_adapter_type` | `--era-reader-adapter-type` | - | `ERA_READER_ADAPTER_TYPE` | Era reader adapter type that can be `cardano-chain`, `file` or `bootstrap`. | `bootstrap` | - | - |
443444
| `era_reader_adapter_params` | `--era-reader-adapter-params` | - | `ERA_READER_ADAPTER_PARAMS` | Era reader adapter params that is an optional JSON encoded parameters structure that is expected depending on the `era_reader_adapter_type` parameter | - | - | - |
444445
| `signed_entity_types` | `--signed-entity-types` | - | `AGGREGATOR_SIGNED_ENTITY_TYPES` | Signed entity types parameters (discriminants names in an ordered comma separated list) | - | `MithrilStakeDistribution,CardanoImmutableFilesFull,CardanoStakeDistribution` | - |

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.4.23"
3+
version = "0.4.24"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

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
};

mithril-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-common"
3-
version = "0.2.149"
3+
version = "0.2.150"
44
description = "Common types, interfaces, and utilities for Mithril nodes."
55
authors = { workspace = true }
66
edition = { workspace = true }
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-infra/assets/docker/docker-compose-aggregator-auth-p2p.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ services:
6565
- CARDANO_NODE_SOCKET_PATH=/ipc/node.socket
6666
- CARDANO_NODE_VERSION=${CARDANO_IMAGE_ID}
6767
- CARDANO_CLI_PATH=/app/bin/cardano-cli
68+
- CHAIN_OBSERVER_TYPE=${CHAIN_OBSERVER_TYPE}
6869
- GENESIS_VERIFICATION_KEY=${GENESIS_VERIFICATION_KEY}
6970
- GENESIS_SECRET_KEY=${GENESIS_SECRET_KEY}
7071
- DB_DIRECTORY=/db

0 commit comments

Comments
 (0)