partner_chains_db_sync_data_sources/lib.rs
1//! Crate providing implementations of Partner Chain Data Sources that read from Db-Sync Postgres.
2//!
3//! # Usage
4//!
5//! ## Adding to the node
6//!
7//! All data sources defined in this crate require a Postgres connection pool [PgPool] to run
8//! queries, which should be shared between all data sources. For convenience, this crate provides
9//! a helper function [get_connection_from_env] that will create a connection pool based on
10//! configuration read from node environment.
11//!
12//! Each data source also accepts an optional Prometheus metrics client [McFollowerMetrics] for
13//! reporting metrics to the Substrate's Prometheus metrics service. This client can be obtained
14//! using the [register_metrics_warn_errors] function.
15//!
16//! In addition to these two common arguments, some data sources depend on [BlockDataSourceImpl]
17//! which provides basic queries about blocks, and additional configuration for their data cache
18//! size.
19//!
20//! An example node code that creates the data sources can look like the following:
21//!
22//! ```rust
23//! # use std::error::Error;
24//! # use std::sync::Arc;
25//! use partner_chains_db_sync_data_sources::*;
26//!
27//! pub const CANDIDATES_FOR_EPOCH_CACHE_SIZE: usize = 64;
28//! pub const STAKE_CACHE_SIZE: usize = 100;
29//! pub const GOVERNED_MAP_CACHE_SIZE: u16 = 100;
30//!
31//! async fn create_data_sources(
32//! metrics_registry_opt: Option<&substrate_prometheus_endpoint::Registry>
33//! ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
34//! let metrics = register_metrics_warn_errors(metrics_registry_opt);
35//! let pool = get_connection_from_env().await?;
36//!
37//! // Block data source is shared by others for cache reuse
38//! let block = Arc::new(BlockDataSourceImpl::new_from_env(pool.clone()).await?);
39//!
40//! let sidechain_rpc = SidechainRpcDataSourceImpl::new(block.clone(), metrics.clone());
41//!
42//! let mc_hash = Arc::new(McHashDataSourceImpl::new(block.clone(), metrics.clone()));
43//!
44//! let authority_selection =
45//! CandidatesDataSourceImpl::new(pool.clone(), metrics.clone())
46//! .await?
47//! .cached(CANDIDATES_FOR_EPOCH_CACHE_SIZE)?;
48//!
49//! let block_participation =
50//! StakeDistributionDataSourceImpl::new(pool.clone(), metrics.clone(), STAKE_CACHE_SIZE);
51//!
52//! let governed_map =
53//! GovernedMapDataSourceCachedImpl::new(pool, metrics.clone(), GOVERNED_MAP_CACHE_SIZE, block).await?;
54//! Ok(())
55//! }
56//! ```
57//!
58//! ## Cardano DB Sync configuration
59//!
60//! Partner Chains data sources require specific Db-Sync configuration to be set for them to
61//! operate correctly:
62//! - `insert_options.tx_out.value`: must be either `"enable"` (default) or `"consumed"`.
63//! The data sources in this crate that need to query transaction intputs automatically detect
64//! which option is used and adjust their queries accordingly. This requires the database to be
65//! already initialized by db-sync. When run for an uninitialized database, the data sources
66//! will default to the `"enable"` option.
67//! - `insert_options.tx_out.use_address_table`: must be `false` (default).
68//! - `insert_options.ledger`: must be `"enable"` (default).
69//! - `insert_options.multi_asset`: must be `true` (default).
70//! - `insert_options.governance`: must `"enable"` (default).
71//! - `insert_options.remove_jsonb_from_schema`: must be `"disable"` (default).
72//! - `insert_options.plutus`: must be `"enable"` (default).
73//!
74//! The default Cardano DB Sync configuration meets these requirements, so Partner Chain node
75//! operators that do not wish to use any custom configuration can use the defaults, otherwise
76//! they must preserve the values described above. See [Db-Sync configuration docs] for more
77//! information.
78//!
79//! ## Custom Indexes
80//!
81//! In addition to indexes automatically created by Db-Sync itself, data sources in this crate
82//! require additional ones to be created for some of the queries to execute efficiently. These
83//! indexes are:
84//! - `idx_ma_tx_out_ident ON ma_tx_out(ident)`
85//! - `idx_tx_out_address ON tx_out USING hash (address)`
86//!
87//! The data sources in this crate automatically create these indexes when needed at node startup.
88//!
89//! [PgPool]: sqlx::PgPool
90//! [BlockDataSourceImpl]: crate::block::BlockDataSourceImpl
91//! [McFollowerMetrics]: crate::metrics::McFollowerMetrics
92//! [get_connection_from_env]: crate::data_sources::get_connection_from_env
93//! [register_metrics_warn_errors]: crate::metrics::register_metrics_warn_errors
94//! [Db-Sync configuration docs]: https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/configuration.md
95#![deny(missing_docs)]
96#![allow(rustdoc::private_intra_doc_links)]
97
98pub use crate::{
99 data_sources::{ConnectionConfig, PgPool, get_connection_from_env},
100 metrics::{McFollowerMetrics, register_metrics_warn_errors},
101};
102
103#[cfg(feature = "block-source")]
104pub use crate::block::{BlockDataSourceImpl, DbSyncBlockDataSourceConfig};
105#[cfg(feature = "bridge")]
106pub use crate::bridge::{TokenBridgeDataSourceImpl, cache::CachedTokenBridgeDataSourceImpl};
107#[cfg(feature = "candidate-source")]
108pub use crate::candidates::CandidatesDataSourceImpl;
109#[cfg(feature = "governed-map")]
110pub use crate::governed_map::{GovernedMapDataSourceCachedImpl, GovernedMapDataSourceImpl};
111#[cfg(feature = "mc-hash")]
112pub use crate::mc_hash::McHashDataSourceImpl;
113#[cfg(feature = "sidechain-rpc")]
114pub use crate::sidechain_rpc::SidechainRpcDataSourceImpl;
115#[cfg(feature = "block-participation")]
116pub use crate::stake_distribution::StakeDistributionDataSourceImpl;
117
118mod data_sources;
119mod db_datum;
120mod db_model;
121mod metrics;
122
123#[cfg(feature = "block-source")]
124mod block;
125#[cfg(feature = "bridge")]
126mod bridge;
127#[cfg(feature = "candidate-source")]
128mod candidates;
129#[cfg(feature = "governed-map")]
130mod governed_map;
131#[cfg(feature = "mc-hash")]
132mod mc_hash;
133#[cfg(feature = "sidechain-rpc")]
134mod sidechain_rpc;
135#[cfg(feature = "block-participation")]
136mod stake_distribution;
137
138#[derive(Debug)]
139/// Wrapper error type for [sqlx::Error]
140pub struct SqlxError(sqlx::Error);
141
142impl From<sqlx::Error> for SqlxError {
143 fn from(value: sqlx::Error) -> Self {
144 SqlxError(value)
145 }
146}
147
148impl From<SqlxError> for DataSourceError {
149 fn from(e: SqlxError) -> Self {
150 DataSourceError::InternalDataSourceError(e.0.to_string())
151 }
152}
153
154impl From<SqlxError> for Box<dyn std::error::Error + Send + Sync> {
155 fn from(e: SqlxError) -> Self {
156 e.0.into()
157 }
158}
159
160/// Error type returned by Db-Sync based data sources
161#[derive(Debug, PartialEq, thiserror::Error)]
162pub enum DataSourceError {
163 /// Indicates that the Db-Sync database rejected a request as invalid
164 #[error("Bad request: `{0}`.")]
165 BadRequest(String),
166 /// Indicates that an internal error occured when querying the Db-Sync database
167 #[error("Internal error of data source: `{0}`.")]
168 InternalDataSourceError(String),
169 /// Indicates that expected data was not found when querying the Db-Sync database
170 #[error(
171 "'{0}' not found. Possible causes: data source configuration error, db-sync not synced fully, or data not set on the main chain."
172 )]
173 ExpectedDataNotFound(String),
174 /// Indicates that data returned by the Db-Sync database is invalid
175 #[error(
176 "Invalid data. {0} Possible cause is an error in Plutus scripts or data source is outdated."
177 )]
178 InvalidData(String),
179}
180
181/// Result type used by Db-Sync data sources
182pub(crate) type Result<T> = std::result::Result<T, DataSourceError>;
183
184#[cfg(test)]
185mod tests {
186 use ctor::{ctor, dtor};
187 use std::sync::{OnceLock, mpsc};
188 use testcontainers_modules::postgres::Postgres;
189 use testcontainers_modules::testcontainers::{
190 Container, ImageExt,
191 bollard::query_parameters::{RemoveContainerOptions, StopContainerOptions},
192 core::client::docker_client_instance,
193 runners::SyncRunner,
194 };
195
196 static POSTGRES: OnceLock<Container<Postgres>> = OnceLock::new();
197
198 fn init_postgres() -> Container<Postgres> {
199 Postgres::default().with_tag("17.2").start().unwrap()
200 }
201
202 #[ctor]
203 fn on_startup() {
204 let postgres = POSTGRES.get_or_init(init_postgres);
205 let database_url = &format!(
206 "postgres://postgres:postgres@127.0.0.1:{}/postgres",
207 postgres.get_host_port_ipv4(5432).unwrap()
208 );
209 // Needed for sqlx::test macro annotation
210 unsafe {
211 std::env::set_var("DATABASE_URL", database_url);
212 }
213 }
214
215 #[dtor]
216 fn on_shutdown() {
217 let (tx, rx) = mpsc::channel();
218 std::thread::spawn(move || {
219 let runtime =
220 tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
221 runtime.block_on(async {
222 let docker = docker_client_instance().await.unwrap();
223 let id = POSTGRES.get().unwrap().id();
224 docker.stop_container(id, None::<StopContainerOptions>).await.unwrap();
225 docker.remove_container(id, None::<RemoveContainerOptions>).await.unwrap();
226 tx.send(());
227 });
228 });
229 let _: () = rx.recv().unwrap();
230 }
231}