Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kpinter-iohk committed Feb 14, 2025
1 parent 3a5ba91 commit d01f780
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 65 deletions.
13 changes: 0 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,36 @@ impl StakeDistributionDataSource for StakeDistributionDataSourceImpl {

fn rows_to_distribution(rows: Vec<StakePoolDelegationOutputRow>) -> StakeDistribution {
let mut res = BTreeMap::<MainchainKeyHash, PoolDelegation>::new();
'row_loop: for row in rows {
let delegator_key = match &row.stake_address_hash_raw[..] {
[0xe0 | 0xe1, rest @ ..] => DelegatorKey::StakeKeyHash(
rest.try_into().expect("stake_address_hash_raw is 29 bytes"),
),
[0xf0 | 0xf1, rest @ ..] => DelegatorKey::ScriptKeyHash {
hash_raw: rest.try_into().expect("stake_address_hash_raw is 29 bytes"),
script_hash: row
.stake_address_script_hash
.expect("stake_address_script_hash must be present for script keys"),
for row in rows {
match get_delegator_key(&row) {
Ok(delegator_key) => {
let pool = res.entry(MainchainKeyHash(row.pool_hash_raw)).or_default();
pool.delegators
.entry(delegator_key)
.or_insert(DelegatorStakeAmount(row.epoch_stake_amount.0));
pool.total_stake.0 += row.epoch_stake_amount.0;
},
_ => {
log::warn!(
"invalid starting address byte on stake_address_hash_raw: {}",
hex::encode(row.stake_address_hash_raw)
);
continue 'row_loop;
Err(e) => {
log::warn!("Failed to parse StakePoolDelegationOutputRow: {}", e)
},
};
let pool = res.entry(MainchainKeyHash(row.pool_hash_raw)).or_default();
pool.delegators
.entry(delegator_key)
.or_insert(DelegatorStakeAmount(row.epoch_stake_amount.0));
pool.total_stake.0 += row.epoch_stake_amount.0;
}
}
StakeDistribution(res)
}

fn get_delegator_key(row: &StakePoolDelegationOutputRow) -> Result<DelegatorKey, String> {
match &row.stake_address_hash_raw[..] {
[0xe0 | 0xe1, rest @ ..] => Ok(DelegatorKey::StakeKeyHash(
rest.try_into().expect("infallible: stake_address_hash_raw is 29 bytes"),
)),
[0xf0 | 0xf1, rest @ ..] => Ok(DelegatorKey::ScriptKeyHash {
hash_raw: rest.try_into().expect("infallible: stake_address_hash_raw is 29 bytes"),
script_hash: row
.stake_address_script_hash
.ok_or("stake_address_script_hash must be present for script keys")?,
}),
_ => {
Err(format!("invalid stake address hash: {}", hex::encode(row.stake_address_hash_raw)))
},
}
}
31 changes: 1 addition & 30 deletions toolkit/primitives/stake-distribution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,12 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
async-trait = { workspace = true, optional = true }
envy = { workspace = true, optional = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sidechain-domain = { workspace = true }
sidechain-mc-hash = { workspace = true, optional = true }
sp-api = { workspace = true }
sp-blockchain = { workspace = true, optional = true }
sp-inherents = { workspace = true }
sp-runtime = { workspace = true }
thiserror = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
sp-version = { workspace = true, optional = true }
derive-new = { workspace = true }
derive_more = { workspace = true }

[dev-dependencies]
tokio = { workspace = true }

[features]
default = ["std"]
std = [
"async-trait",
"parity-scale-codec/std",
"scale-info/std",
"sidechain-domain/std",
"sidechain-mc-hash",
"sp-api/std",
"sp-blockchain",
"sp-inherents/std",
"sp-runtime/std",
"thiserror",
"envy",
"sp-version",
"sp-version/std",
"serde",
]
serde = ["dep:serde", "scale-info/serde", "sidechain-domain/serde"]
std = ["async-trait", "sidechain-domain/std"]
mock = []

0 comments on commit d01f780

Please sign in to comment.