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