Skip to content

Commit a87ddf0

Browse files
committed
better errors
1 parent 04827ca commit a87ddf0

File tree

4 files changed

+135
-74
lines changed

4 files changed

+135
-74
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::{
66
use chrono::{DateTime, NaiveDateTime, TimeDelta};
77
use derive_new::new;
88
use figment::{Figment, providers::Env};
9-
use futures::TryFutureExt;
109
use log::{debug, info};
1110
use serde::Deserialize;
1211
use sidechain_domain::mainchain_epoch::{MainchainEpochConfig, MainchainEpochDerivation};
@@ -46,9 +45,14 @@ pub struct BlockDataSourceImpl {
4645
impl BlockDataSourceImpl {
4746
/// Returns the latest _unstable_ Cardano block from Dolos
4847
pub async fn get_latest_block_info(&self) -> Result<MainchainBlock> {
49-
self.client.blocks_latest().await.map_err(|e| {
50-
DataSourceError::ExpectedDataNotFound(format!("No latest block on chain. {e}",)).into()
51-
}).and_then(from_block_content)
48+
self.client
49+
.blocks_latest()
50+
.await
51+
.map_err(|e| {
52+
DataSourceError::ExpectedDataNotFound(format!("No latest block on chain. {e}",))
53+
.into()
54+
})
55+
.and_then(from_block_content)
5256
}
5357

5458
/// Returns the latest _stable_ Cardano block from Dolos that is within
@@ -118,19 +122,14 @@ impl BlockDataSourceImpl {
118122
pub async fn new_from_env(
119123
client: MiniBFClient,
120124
) -> std::result::Result<Self, Box<dyn Error + Send + Sync + 'static>> {
121-
Self::from_config(
122-
client,
123-
DolosBlockDataSourceConfig::from_env()?,
124-
&read_mc_epoch_config()?,
125-
).await
125+
Self::from_config(client, DolosBlockDataSourceConfig::from_env()?, &read_mc_epoch_config()?)
126+
.await
126127
}
127128

128129
/// Creates a new instance of [BlockDataSourceImpl], using passed configuration.
129130
pub async fn from_config(
130131
client: MiniBFClient,
131-
DolosBlockDataSourceConfig {
132-
block_stability_margin,
133-
}: DolosBlockDataSourceConfig,
132+
DolosBlockDataSourceConfig { block_stability_margin }: DolosBlockDataSourceConfig,
134133
mc_epoch_config: &MainchainEpochConfig,
135134
) -> Result<BlockDataSourceImpl> {
136135
let genesis = client.genesis().await?;
@@ -142,15 +141,15 @@ impl BlockDataSourceImpl {
142141
let max_slot_boundary = 3 * min_slot_boundary;
143142
let cache_size = 100;
144143
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-
))
144+
client,
145+
security_parameter,
146+
TimeDelta::milliseconds(min_slot_boundary),
147+
TimeDelta::milliseconds(max_slot_boundary),
148+
mc_epoch_config.clone(),
149+
block_stability_margin,
150+
cache_size,
151+
BlocksCache::new_arc_mutex(),
152+
))
154153
}
155154
async fn get_latest_block(
156155
&self,
@@ -269,8 +268,8 @@ impl BlockDataSourceImpl {
269268
let futures = (from_block_no.0..=to_block_no.0).map(|block_no| async move {
270269
self.client
271270
.blocks_by_id(McBlockNumber(block_no))
272-
.map_err(|e| e.into())
273271
.await
272+
.map_err(|e| e.into())
274273
.and_then(from_block_content)
275274
});
276275
futures::future::try_join_all(futures).await?.into_iter().collect()

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
use async_trait::async_trait;
22
use blockfrost_openapi::models::{
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
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, genesis_content::GenesisContent,
8+
pool_history_inner::PoolHistoryInner, pool_list_extended_inner::PoolListExtendedInner,
9+
tx_content::TxContent, tx_content_utxo::TxContentUtxo,
410
};
511
use sidechain_domain::*;
612

13+
use crate::DataSourceError;
14+
715
/// Mainchain block id, either a block hash or a block number
816
pub enum McBlockId {
917
/// Domain type Mainchain block hash
@@ -49,63 +57,79 @@ pub trait MiniBFApi {
4957
async fn addresses_utxos(
5058
&self,
5159
address: MainchainAddress,
52-
) -> Result<Vec<AddressUtxoContentInner>, String>;
60+
) -> Result<Vec<AddressUtxoContentInner>, DataSourceError>;
5361
/// Transactions on the address.
5462
async fn addresses_transactions(
5563
&self,
5664
address: MainchainAddress,
57-
) -> Result<Vec<AddressTransactionsContentInner>, String>;
65+
) -> Result<Vec<AddressTransactionsContentInner>, DataSourceError>;
5866

5967
/// List of a specific asset transactions.
6068
async fn assets_transactions(
6169
&self,
6270
asset_id: AssetId,
63-
) -> Result<Vec<AssetTransactionsInner>, String>;
71+
) -> Result<Vec<AssetTransactionsInner>, DataSourceError>;
6472
/// List of a addresses containing a specific asset.
65-
async fn assets_addresses(&self, asset_id: AssetId)
66-
-> Result<Vec<AssetAddressesInner>, String>;
73+
async fn assets_addresses(
74+
&self,
75+
asset_id: AssetId,
76+
) -> Result<Vec<AssetAddressesInner>, DataSourceError>;
6777

6878
/// Return the latest block available to the backends, also known as the tip of the blockchain.
69-
async fn blocks_latest(&self) -> Result<BlockContent, String>;
79+
async fn blocks_latest(&self) -> Result<BlockContent, DataSourceError>;
7080
/// Return the content of a requested block.
71-
async fn blocks_by_id(&self, id: impl Into<McBlockId> + Send) -> Result<BlockContent, String>;
81+
async fn blocks_by_id(
82+
&self,
83+
id: impl Into<McBlockId> + Send,
84+
) -> Result<BlockContent, DataSourceError>;
7285
/// Return the content of a requested block for a specific slot.
73-
async fn blocks_slot(&self, slot_number: McSlotNumber) -> Result<BlockContent, String>;
86+
async fn blocks_slot(&self, slot_number: McSlotNumber)
87+
-> Result<BlockContent, DataSourceError>;
7488
/// Return the list of blocks following a specific block.
7589
async fn blocks_next(
7690
&self,
7791
hash: impl Into<McBlockId> + Send,
78-
) -> Result<Vec<BlockContent>, String>;
92+
) -> Result<Vec<BlockContent>, DataSourceError>;
7993
/// Return the transactions within the block.
80-
async fn blocks_txs(&self, id: impl Into<McBlockId> + Send) -> Result<Vec<String>, String>;
94+
async fn blocks_txs(
95+
&self,
96+
id: impl Into<McBlockId> + Send,
97+
) -> Result<Vec<String>, DataSourceError>;
8198

8299
/// Return the blocks minted for the epoch specified.
83-
async fn epochs_blocks(&self, epoch_number: McEpochNumber) -> Result<Vec<String>, String>;
100+
async fn epochs_blocks(
101+
&self,
102+
epoch_number: McEpochNumber,
103+
) -> Result<Vec<String>, DataSourceError>;
84104
/// Return the protocol parameters for the epoch specified.
85105
async fn epochs_parameters(
86106
&self,
87107
epoch_number: McEpochNumber,
88-
) -> Result<EpochParamContent, String>;
108+
) -> Result<EpochParamContent, DataSourceError>;
89109
/// Return the active stake distribution for the epoch specified by stake pool.
90110
async fn epochs_stakes_by_pool(
91111
&self,
92112
epoch_number: McEpochNumber,
93113
pool_id: &str,
94-
) -> Result<Vec<EpochStakePoolContentInner>, String>;
114+
) -> Result<Vec<EpochStakePoolContentInner>, DataSourceError>;
95115

96116
/// History of stake pool parameters over epochs.
97-
async fn pools_history(&self, pool_id: &str) -> Result<Vec<PoolHistoryInner>, String>;
117+
async fn pools_history(&self, pool_id: &str) -> Result<Vec<PoolHistoryInner>, DataSourceError>;
98118
/// List of registered stake pools with additional information.
99-
async fn pools_extended(&self) -> Result<Vec<PoolListExtendedInner>, String>;
119+
async fn pools_extended(&self) -> Result<Vec<PoolListExtendedInner>, DataSourceError>;
100120

101121
/// Query JSON value of a datum by its hash.
102-
async fn scripts_datum_hash(&self, datum_hash: &str) -> Result<Vec<serde_json::Value>, String>;
122+
async fn scripts_datum_hash(
123+
&self,
124+
datum_hash: &str,
125+
) -> Result<Vec<serde_json::Value>, DataSourceError>;
103126

104127
/// Return content of the requested transaction.
105-
async fn transaction_by_hash(&self, tx_hash: McTxHash) -> Result<TxContent, String>;
128+
async fn transaction_by_hash(&self, tx_hash: McTxHash) -> Result<TxContent, DataSourceError>;
106129
/// Return the inputs and UTXOs of the specific transaction.
107-
async fn transactions_utxos(&self, tx_hash: McTxHash) -> Result<TxContentUtxo, String>;
130+
async fn transactions_utxos(&self, tx_hash: McTxHash)
131+
-> Result<TxContentUtxo, DataSourceError>;
108132

109133
/// Return the information about blockchain genesis.
110-
async fn genesis(&self) -> Result<GenesisContent, String>;
134+
async fn genesis(&self) -> Result<GenesisContent, DataSourceError>;
111135
}

0 commit comments

Comments
 (0)