partner_chains_dolos_data_sources/
lib.rs

1#[cfg(feature = "candidate-source")]
2mod candidate;
3#[cfg(feature = "candidate-source")]
4pub use candidate::AuthoritySelectionDataSourceImpl;
5
6#[cfg(feature = "governed-map")]
7mod governed_map;
8#[cfg(feature = "governed-map")]
9pub use governed_map::GovernedMapDataSourceImpl;
10
11#[cfg(feature = "mc-hash")]
12mod mc_hash;
13#[cfg(feature = "mc-hash")]
14pub use mc_hash::McHashDataSourceImpl;
15
16#[cfg(feature = "sidechain-rpc")]
17mod sidechain_rpc;
18#[cfg(feature = "sidechain-rpc")]
19pub use sidechain_rpc::SidechainRpcDataSourceImpl;
20
21#[cfg(feature = "block-participation")]
22mod stake_distribution;
23#[cfg(feature = "block-participation")]
24pub use stake_distribution::StakeDistributionDataSourceImpl;
25
26#[cfg(feature = "bridge")]
27mod bridge;
28#[cfg(feature = "bridge")]
29pub use bridge::TokenBridgeDataSourceImpl;
30
31mod block;
32pub use block::BlockDataSourceImpl;
33use sidechain_domain::mainchain_epoch::MainchainEpochConfig;
34
35use crate::client::MiniBFClient;
36
37pub mod client;
38
39type ResultErr = Box<dyn std::error::Error + Send + Sync>;
40type Result<T> = std::result::Result<T, ResultErr>;
41
42/// Error type returned by Dolos based data sources
43#[derive(Debug, PartialEq, thiserror::Error)]
44pub enum DataSourceError {
45	/// Indicates that Dolos rejected a request as invalid
46	#[error("Bad request: `{0}`.")]
47	BadRequest(String),
48	/// Indicates that Dolos client produced an error while calling endpoint
49	#[error("Dolos client call error: `{0}`.")]
50	DolosCallError(String),
51	/// Indicates that Dolos client produced an error while parsing response
52	#[error("Dolos client response parse error: `{0}`.")]
53	DolosResponseParseError(String),
54	/// Indicates that expected data was not found when querying Dolos
55	#[error(
56		"'{0}' not found. Possible causes: data source configuration error, Dolos not synced fully, or data not set on the main chain."
57	)]
58	ExpectedDataNotFound(String),
59	/// Indicates that data returned by Dolos is invalid
60	#[error(
61		"Invalid data. {0} Possible cause is an error in Plutus scripts or data source is outdated."
62	)]
63	InvalidData(String),
64}
65
66/// Returns a [MiniBFClient] constructed using configuration read from environment
67///
68/// # Environment variables read:
69/// - `DOLOS_MINIBF_URL`: Dolos MiniBF client, eg. `localhost:3000`
70pub fn get_connection_from_env() -> Result<MiniBFClient> {
71	log::warn!("Dolos data sources are still WIP and should not be used in production");
72	let config = ConnectionConfig::from_env()?;
73	Ok(MiniBFClient::new(config.dolos_minibf_url.as_str(), std::time::Duration::from_secs(30)))
74}
75
76/// Dolos connection config used when creating a [MiniBFClient].
77#[derive(Debug, Clone, serde::Deserialize)]
78pub struct ConnectionConfig {
79	/// Dolos MiniBF client, eg. `localhost:3000`
80	pub(crate) dolos_minibf_url: String,
81}
82
83impl ConnectionConfig {
84	/// Reads Dolos connection config from the environment
85	pub fn from_env() -> Result<Self> {
86		let config: Self = figment::Figment::new()
87			.merge(figment::providers::Env::raw())
88			.extract()
89			.map_err(|e| format!("Failed to read Dolos data source connection: {e}"))?;
90		Ok(config)
91	}
92}
93
94/// Reads Cardano main chain epoch configuration from the environment.
95///
96/// See documentation of [MainchainEpochConfig::read_from_env] for the list of environment variables read.
97#[cfg(feature = "block-source")]
98pub fn read_mc_epoch_config() -> Result<MainchainEpochConfig> {
99	Ok(MainchainEpochConfig::read_from_env()
100		.map_err(|e| format!("Failed to read main chain config: {}", e))?)
101}