Skip to content

Commit 04827ca

Browse files
committed
address comments
1 parent 6db7cd0 commit 04827ca

File tree

5 files changed

+48
-58
lines changed

5 files changed

+48
-58
lines changed

toolkit/data-sources/dolos/src/block.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
use chrono::{DateTime, NaiveDateTime, TimeDelta};
77
use derive_new::new;
88
use figment::{Figment, providers::Env};
9+
use futures::TryFutureExt;
910
use log::{debug, info};
1011
use serde::Deserialize;
1112
use 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

4646
impl 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
});

toolkit/data-sources/dolos/src/client/api.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
use async_trait::async_trait;
22
use blockfrost_openapi::models::{
3-
address_transactions_content_inner::AddressTransactionsContentInner,
4-
address_utxo_content_inner::AddressUtxoContentInner,
5-
asset_addresses_inner::AssetAddressesInner, asset_transactions_inner::AssetTransactionsInner,
6-
block_content::BlockContent, epoch_param_content::EpochParamContent,
7-
epoch_stake_pool_content_inner::EpochStakePoolContentInner,
8-
pool_history_inner::PoolHistoryInner, pool_list_extended_inner::PoolListExtendedInner,
9-
tx_content::TxContent, tx_content_utxo::TxContentUtxo,
3+
address_transactions_content_inner::AddressTransactionsContentInner, address_utxo_content_inner::AddressUtxoContentInner, asset_addresses_inner::AssetAddressesInner, asset_transactions_inner::AssetTransactionsInner, block_content::BlockContent, epoch_param_content::EpochParamContent, epoch_stake_pool_content_inner::EpochStakePoolContentInner, genesis_content::GenesisContent, pool_history_inner::PoolHistoryInner, pool_list_extended_inner::PoolListExtendedInner, tx_content::TxContent, tx_content_utxo::TxContentUtxo
104
};
115
use sidechain_domain::*;
126

@@ -111,4 +105,7 @@ pub trait MiniBFApi {
111105
async fn transaction_by_hash(&self, tx_hash: McTxHash) -> Result<TxContent, String>;
112106
/// Return the inputs and UTXOs of the specific transaction.
113107
async fn transactions_utxos(&self, tx_hash: McTxHash) -> Result<TxContentUtxo, String>;
108+
109+
/// Return the information about blockchain genesis.
110+
async fn genesis(&self) -> Result<GenesisContent, String>;
114111
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
use blockfrost_openapi::models::block_content::BlockContent;
22
use sidechain_domain::*;
3+
use crate::{Result, DataSourceError};
34

4-
pub fn from_block_content(value: BlockContent) -> Result<MainchainBlock, String> {
5+
pub fn from_block_content(value: BlockContent) -> Result<MainchainBlock> {
56
Ok(MainchainBlock {
67
number: value
78
.height
89
.map(|n| sidechain_domain::McBlockNumber(n as u32))
9-
.ok_or("number missing")?,
10+
.ok_or(DataSourceError::InvalidData("number missing".to_string()))?,
1011
hash: McBlockHash::decode_hex(&value.hash)?,
1112
epoch: value
1213
.epoch
1314
.map(|n| sidechain_domain::McEpochNumber(n as u32))
14-
.ok_or("epoch missing")?,
15+
.ok_or(DataSourceError::InvalidData("epoch missing".to_string()))?,
1516
slot: value
1617
.slot
1718
.map(|n| sidechain_domain::McSlotNumber(n as u64))
18-
.ok_or("slot missing")?,
19+
.ok_or(DataSourceError::InvalidData("slot missing".to_string()))?,
1920
timestamp: value.time as u64,
2021
})
2122
}

toolkit/data-sources/dolos/src/client/minibf.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
use async_trait::async_trait;
22
use blockfrost_openapi::models::{
3-
address_transactions_content_inner::AddressTransactionsContentInner,
4-
address_utxo_content_inner::AddressUtxoContentInner,
5-
asset_addresses_inner::AssetAddressesInner, asset_transactions_inner::AssetTransactionsInner,
6-
block_content::BlockContent, epoch_param_content::EpochParamContent,
7-
epoch_stake_pool_content_inner::EpochStakePoolContentInner,
8-
pool_history_inner::PoolHistoryInner, pool_list_extended_inner::PoolListExtendedInner,
9-
tx_content::TxContent, tx_content_utxo::TxContentUtxo,
3+
address_transactions_content_inner::AddressTransactionsContentInner, address_utxo_content_inner::AddressUtxoContentInner, asset_addresses_inner::AssetAddressesInner, asset_transactions_inner::AssetTransactionsInner, block_content::BlockContent, epoch_param_content::EpochParamContent, epoch_stake_pool_content_inner::EpochStakePoolContentInner, genesis_content::GenesisContent, pool_history_inner::PoolHistoryInner, pool_list_extended_inner::PoolListExtendedInner, tx_content::TxContent, tx_content_utxo::TxContentUtxo
104
};
115
use serde::de::DeserializeOwned;
126
use sidechain_domain::*;
@@ -188,6 +182,10 @@ impl MiniBFApi for MiniBFClient {
188182
async fn transactions_utxos(&self, tx_hash: McTxHash) -> Result<TxContentUtxo, String> {
189183
self.request(&format!("txs/{tx_hash}/utxos")).await
190184
}
185+
186+
async fn genesis(&self) -> Result<GenesisContent, String> {
187+
self.request(&format!("genesis")).await
188+
}
191189
}
192190

193191
fn format_asset_id(asset_id: &AssetId) -> String {

toolkit/data-sources/dolos/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub enum DataSourceError {
4545
/// Indicates that Dolos rejected a request as invalid
4646
#[error("Bad request: `{0}`.")]
4747
BadRequest(String),
48-
/// Indicates that an internal error occured when querying Dolos
48+
/// Indicates that an internal error occurred when querying Dolos
4949
#[error("Internal error of data source: `{0}`.")]
5050
InternalDataSourceError(String),
5151
/// Indicates that expected data was not found when querying Dolos

0 commit comments

Comments
 (0)