Skip to content

Commit 4a80c63

Browse files
committed
api cleanup
1 parent e855da8 commit 4a80c63

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

toolkit/data-sources/dolos/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ figment = { workspace = true }
4242
tokio = { workspace = true }
4343
derive-new = { workspace = true }
4444
chrono = { workspace = true }
45+
bech32 = { workspace = true }
4546

4647
[features]
4748
default = []

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,10 @@ impl AuthoritySelectionDataSource for AuthoritySelectionDataSourceImpl {
107107
let candidates = self.get_registered_candidates(epoch, committee_candidate_address).await?;
108108
let pools = self.client.pools_extended().await?;
109109
let pred = |pool: PoolListExtendedInner| async move {
110-
let history = self.client.pools_history(&pool.pool_id).await?;
110+
let pool_id = mckeyhash_from_bech32(&pool.pool_id)?;
111+
let history = self.client.pools_history(pool_id).await?;
111112
Result::Ok(match history.into_iter().find(|h| h.epoch == epoch.0 as i32) {
112-
Some(e) => Some((
113-
MainchainKeyHash::decode_hex(&pool.pool_id)?,
114-
StakeDelegation(e.active_stake.parse::<u64>()?),
115-
)),
113+
Some(e) => Some((pool_id, StakeDelegation(e.active_stake.parse::<u64>()?))),
116114
None => None,
117115
})
118116
};
@@ -147,6 +145,11 @@ impl AuthoritySelectionDataSource for AuthoritySelectionDataSourceImpl {
147145
}
148146
}
149147

148+
fn mckeyhash_from_bech32(bech32_str: &str) -> Result<MainchainKeyHash> {
149+
let (_hrp, val) = bech32::decode(bech32_str).map_err(|e| e.to_string())?;
150+
Ok(MainchainKeyHash(val.try_into().map_err(|_| "failed to convert vec to array")?))
151+
}
152+
150153
#[derive(Debug)]
151154
struct RegisteredCandidate {
152155
stake_pool_pub_key: StakePoolPublicKey,

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,53 @@ use sidechain_domain::*;
1313
use crate::DataSourceError;
1414

1515
/// Mainchain block id, either a block hash or a block number
16-
pub enum McBlockId {
17-
/// Domain type Mainchain block hash
18-
McBlockHash(McBlockHash),
19-
/// Domain type Mainchain block number
20-
McBlockNumber(McBlockNumber),
21-
/// Mainchain block hash returned as string by Blockfrost API
22-
Raw(String),
23-
}
16+
pub struct McBlockId(String);
2417

2518
impl From<McBlockHash> for McBlockId {
2619
fn from(value: McBlockHash) -> Self {
27-
McBlockId::McBlockHash(value)
20+
McBlockId(value.to_string())
2821
}
2922
}
3023

3124
impl From<McBlockNumber> for McBlockId {
3225
fn from(value: McBlockNumber) -> Self {
33-
McBlockId::McBlockNumber(value)
26+
McBlockId(value.to_string())
3427
}
3528
}
3629

3730
impl From<String> for McBlockId {
3831
fn from(value: String) -> Self {
39-
McBlockId::Raw(value)
32+
McBlockId(value)
4033
}
4134
}
4235

4336
impl std::fmt::Display for McBlockId {
4437
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45-
match self {
46-
McBlockId::McBlockHash(mc_block_hash) => mc_block_hash.fmt(f),
47-
McBlockId::McBlockNumber(mc_block_number) => mc_block_number.fmt(f),
48-
McBlockId::Raw(str) => str.fmt(f),
49-
}
38+
self.0.fmt(f)
39+
}
40+
}
41+
42+
/// Mainchain pool id as a bech32 string
43+
pub struct McPoolId(String);
44+
45+
impl From<MainchainKeyHash> for McPoolId {
46+
fn from(value: MainchainKeyHash) -> Self {
47+
let pool_id =
48+
bech32::encode::<bech32::Bech32>(bech32::Hrp::parse_unchecked("pool"), &value.0)
49+
.expect("MainchainKeyHash is valid");
50+
McPoolId(pool_id)
51+
}
52+
}
53+
54+
impl From<String> for McPoolId {
55+
fn from(value: String) -> Self {
56+
McPoolId(value)
57+
}
58+
}
59+
60+
impl std::fmt::Display for McPoolId {
61+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62+
self.0.fmt(f)
5063
}
5164
}
5265

@@ -64,12 +77,12 @@ pub trait MiniBFApi {
6477
address: MainchainAddress,
6578
) -> Result<Vec<AddressTransactionsContentInner>, DataSourceError>;
6679

67-
/// List of a specific asset transactions.
80+
/// List of specific asset transactions.
6881
async fn assets_transactions(
6982
&self,
7083
asset_id: AssetId,
7184
) -> Result<Vec<AssetTransactionsInner>, DataSourceError>;
72-
/// List of a addresses containing a specific asset.
85+
/// List of addresses containing a specific asset.
7386
async fn assets_addresses(
7487
&self,
7588
asset_id: AssetId,
@@ -110,11 +123,14 @@ pub trait MiniBFApi {
110123
async fn epochs_stakes_by_pool(
111124
&self,
112125
epoch_number: McEpochNumber,
113-
pool_id: &str,
126+
pool_id: impl Into<McPoolId> + Send,
114127
) -> Result<Vec<EpochStakePoolContentInner>, DataSourceError>;
115128

116129
/// History of stake pool parameters over epochs.
117-
async fn pools_history(&self, pool_id: &str) -> Result<Vec<PoolHistoryInner>, DataSourceError>;
130+
async fn pools_history(
131+
&self,
132+
pool_id: impl Into<McPoolId> + Send,
133+
) -> Result<Vec<PoolHistoryInner>, DataSourceError>;
118134
/// List of registered stake pools with additional information.
119135
async fn pools_extended(&self) -> Result<Vec<PoolListExtendedInner>, DataSourceError>;
120136

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ureq::Agent;
1515

1616
use crate::{
1717
DataSourceError,
18-
client::api::{McBlockId, MiniBFApi},
18+
client::api::{McBlockId, McPoolId, MiniBFApi},
1919
};
2020

2121
/// Client implementing Dolos MiniBF
@@ -187,13 +187,18 @@ impl MiniBFApi for MiniBFClient {
187187
async fn epochs_stakes_by_pool(
188188
&self,
189189
epoch_number: McEpochNumber,
190-
pool_id: &str,
190+
pool_id: impl Into<McPoolId> + Send,
191191
) -> Result<Vec<EpochStakePoolContentInner>, DataSourceError> {
192+
let pool_id: McPoolId = pool_id.into();
192193
self.paginated_request_all(&format!("epochs/{epoch_number}/stakes/{pool_id}"))
193194
.await
194195
}
195196

196-
async fn pools_history(&self, pool_id: &str) -> Result<Vec<PoolHistoryInner>, DataSourceError> {
197+
async fn pools_history(
198+
&self,
199+
pool_id: impl Into<McPoolId> + Send,
200+
) -> Result<Vec<PoolHistoryInner>, DataSourceError> {
201+
let pool_id: McPoolId = pool_id.into();
197202
self.paginated_request_all(&format!("pools/{pool_id}/history")).await
198203
}
199204
async fn pools_extended(&self) -> Result<Vec<PoolListExtendedInner>, DataSourceError> {
@@ -223,7 +228,7 @@ impl MiniBFApi for MiniBFClient {
223228
}
224229
}
225230

226-
fn format_asset_id(asset_id: &AssetId) -> String {
231+
pub fn format_asset_id(asset_id: &AssetId) -> String {
227232
let AssetId { policy_id, asset_name } = asset_id;
228233
format!("{}{}", &policy_id.to_hex_string()[2..], &asset_name.to_hex_string()[2..])
229234
}

0 commit comments

Comments
 (0)