@@ -6,6 +6,7 @@ use crate::{
66use chrono:: { DateTime , NaiveDateTime , TimeDelta } ;
77use derive_new:: new;
88use figment:: { Figment , providers:: Env } ;
9+ use futures:: TryFutureExt ;
910use log:: { debug, info} ;
1011use serde:: Deserialize ;
1112use sidechain_domain:: mainchain_epoch:: { MainchainEpochConfig , MainchainEpochDerivation } ;
@@ -34,8 +35,7 @@ pub struct BlockDataSourceImpl {
3435 mainchain_epoch_config : MainchainEpochConfig ,
3536 /// Additional offset applied when selecting the latest stable Cardano block
3637 ///
37- /// This parameter should be 0 by default and should only be increased to 1 in networks
38- /// struggling with frequent block rejections due to Db-Sync or Cardano node lag.
38+ /// This parameter should be 1 by default.
3939 block_stability_margin : u32 ,
4040 /// Number of contiguous Cardano blocks to be cached by this data source
4141 cache_size : u16 ,
@@ -44,14 +44,14 @@ pub struct BlockDataSourceImpl {
4444}
4545
4646impl BlockDataSourceImpl {
47- /// Returns the latest _unstable_ Cardano block from the Db-Sync database
47+ /// Returns the latest _unstable_ Cardano block from Dolos
4848 pub async fn get_latest_block_info ( & self ) -> Result < MainchainBlock > {
49- self . client . blocks_latest ( ) . await . and_then ( from_block_content ) . map_err ( |e| {
49+ self . client . blocks_latest ( ) . await . map_err ( |e| {
5050 DataSourceError :: ExpectedDataNotFound ( format ! ( "No latest block on chain. {e}" , ) ) . into ( )
51- } )
51+ } ) . and_then ( from_block_content )
5252 }
5353
54- /// Returns the latest _stable_ Cardano block from the Db-Sync database that is within
54+ /// Returns the latest _stable_ Cardano block from Dolos that is within
5555 /// acceptable bounds from `reference_timestamp`, accounting for the additional stability
5656 /// offset configured by [block_stability_margin][Self::block_stability_margin].
5757 pub async fn get_latest_stable_block_for (
@@ -94,22 +94,14 @@ impl BlockDataSourceImpl {
9494
9595/// Configuration for [BlockDataSourceImpl]
9696#[ derive( Debug , Clone , Deserialize ) ]
97- pub struct DbSyncBlockDataSourceConfig {
98- /// Cardano security parameter, ie. the number of confirmations needed to stabilize a block
99- pub cardano_security_parameter : u32 ,
100- /// Expected fraction of Cardano slots that will have a block produced
101- ///
102- /// This value can be found in `shelley-genesis.json` file used by the Cardano node,
103- /// example: `"activeSlotsCoeff": 0.05`.
104- pub cardano_active_slots_coeff : f64 ,
97+ pub struct DolosBlockDataSourceConfig {
10598 /// Additional offset applied when selecting the latest stable Cardano block
10699 ///
107- /// This parameter should be 0 by default and should only be increased to 1 in networks
108- /// struggling with frequent block rejections due to Db-Sync or Cardano node lag.
100+ /// This parameter should be 1 by default.
109101 pub block_stability_margin : u32 ,
110102}
111103
112- impl DbSyncBlockDataSourceConfig {
104+ impl DolosBlockDataSourceConfig {
113105 /// Reads the config from environment
114106 pub fn from_env ( ) -> std:: result:: Result < Self , Box < dyn Error + Send + Sync + ' static > > {
115107 let config: Self = Figment :: new ( )
@@ -126,38 +118,39 @@ impl BlockDataSourceImpl {
126118 pub async fn new_from_env (
127119 client : MiniBFClient ,
128120 ) -> std:: result:: Result < Self , Box < dyn Error + Send + Sync + ' static > > {
129- Ok ( Self :: from_config (
121+ Self :: from_config (
130122 client,
131- DbSyncBlockDataSourceConfig :: from_env ( ) ?,
123+ DolosBlockDataSourceConfig :: from_env ( ) ?,
132124 & read_mc_epoch_config ( ) ?,
133- ) )
125+ ) . await
134126 }
135127
136128 /// Creates a new instance of [BlockDataSourceImpl], using passed configuration.
137- pub fn from_config (
129+ pub async fn from_config (
138130 client : MiniBFClient ,
139- DbSyncBlockDataSourceConfig {
140- cardano_security_parameter,
141- cardano_active_slots_coeff,
131+ DolosBlockDataSourceConfig {
142132 block_stability_margin,
143- } : DbSyncBlockDataSourceConfig ,
133+ } : DolosBlockDataSourceConfig ,
144134 mc_epoch_config : & MainchainEpochConfig ,
145- ) -> BlockDataSourceImpl {
146- let k: f64 = cardano_security_parameter. into ( ) ;
135+ ) -> Result < BlockDataSourceImpl > {
136+ let genesis = client. genesis ( ) . await ?;
137+ let active_slots_coeff = genesis. active_slots_coefficient ;
138+ let security_parameter = genesis. security_param as u32 ;
139+ let k: f64 = security_parameter. into ( ) ;
147140 let slot_duration: f64 = mc_epoch_config. slot_duration_millis . millis ( ) as f64 ;
148- let min_slot_boundary = ( slot_duration * k / cardano_active_slots_coeff ) . round ( ) as i64 ;
141+ let min_slot_boundary = ( slot_duration * k / active_slots_coeff ) . round ( ) as i64 ;
149142 let max_slot_boundary = 3 * min_slot_boundary;
150143 let cache_size = 100 ;
151- BlockDataSourceImpl :: new (
152- client,
153- cardano_security_parameter ,
154- TimeDelta :: milliseconds ( min_slot_boundary) ,
155- TimeDelta :: milliseconds ( max_slot_boundary) ,
156- mc_epoch_config. clone ( ) ,
157- block_stability_margin,
158- cache_size,
159- BlocksCache :: new_arc_mutex ( ) ,
160- )
144+ Ok ( BlockDataSourceImpl :: new (
145+ client,
146+ security_parameter ,
147+ TimeDelta :: milliseconds ( min_slot_boundary) ,
148+ TimeDelta :: milliseconds ( max_slot_boundary) ,
149+ mc_epoch_config. clone ( ) ,
150+ block_stability_margin,
151+ cache_size,
152+ BlocksCache :: new_arc_mutex ( ) ,
153+ ) )
161154 }
162155 async fn get_latest_block (
163156 & self ,
@@ -221,7 +214,7 @@ impl BlockDataSourceImpl {
221214 debug ! ( "Block by hash: {hash} found in cache." ) ;
222215 Ok ( Some ( From :: from ( block) ) )
223216 } else {
224- debug ! ( "Block by hash: {hash}, not found in cache, serving from database ." ) ;
217+ debug ! ( "Block by hash: {hash}, not found in cache, serving from Dolos ." ) ;
225218 if let Some ( block_by_hash) =
226219 self . get_stable_block_by_hash_from_db ( hash, reference_timestamp) . await ?
227220 {
@@ -276,6 +269,7 @@ impl BlockDataSourceImpl {
276269 let futures = ( from_block_no. 0 ..=to_block_no. 0 ) . map ( |block_no| async move {
277270 self . client
278271 . blocks_by_id ( McBlockNumber ( block_no) )
272+ . map_err ( |e| e. into ( ) )
279273 . await
280274 . and_then ( from_block_content)
281275 } ) ;
0 commit comments