Skip to content

Commit f652276

Browse files
authored
Merge pull request #326 from input-output-hk/lowhung/refactor-key-hash-to-stake-address
Refactor: SPO distribution store refactored to use StakeAddress
2 parents 4586648 + 6511204 commit f652276

File tree

13 files changed

+206
-253
lines changed

13 files changed

+206
-253
lines changed

Cargo.lock

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

common/src/address.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ impl ShelleyAddress {
274274
ShelleyAddressPaymentPart::ScriptHash(data) => (data, 1),
275275
};
276276

277+
// TODO: MH - make delegation hash an Option<Hash<28>> type
277278
let (delegation_hash, delegation_bits): (Vec<u8>, u8) = match &self.delegation {
278279
ShelleyAddressDelegationPart::None => (Vec::new(), 3),
279280
ShelleyAddressDelegationPart::StakeKeyHash(hash) => (hash.to_vec(), 0),

common/src/queries/accounts.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::collections::HashMap;
22

3-
use crate::{
4-
DRepChoice, KeyHash, PoolId, PoolLiveStakeInfo, RewardType, StakeAddress, TxIdentifier,
5-
};
3+
use crate::{DRepChoice, PoolId, PoolLiveStakeInfo, RewardType, StakeAddress, TxIdentifier};
64

75
pub const DEFAULT_ACCOUNTS_QUERY_TOPIC: (&str, &str) =
86
("accounts-state-query-topic", "cardano.query.accounts");
@@ -59,17 +57,17 @@ pub enum AccountsStateQueryResponse {
5957
AccountAssets(AccountAssets),
6058
AccountAssetsTotals(AccountAssetsTotals),
6159
AccountUTxOs(AccountUTxOs),
62-
AccountsUtxoValuesMap(HashMap<KeyHash, u64>),
60+
AccountsUtxoValuesMap(HashMap<StakeAddress, u64>),
6361
AccountsUtxoValuesSum(u64),
64-
AccountsBalancesMap(HashMap<KeyHash, u64>),
62+
AccountsBalancesMap(HashMap<StakeAddress, u64>),
6563
AccountsBalancesSum(u64),
6664

6765
// Epochs-related responses
6866
ActiveStakes(u64),
69-
/// Vec<(PoolId, StakeKey, ActiveStakeAmount)>
70-
SPDDByEpoch(Vec<(PoolId, KeyHash, u64)>),
71-
/// Vec<(StakeKey, ActiveStakeAmount)>
72-
SPDDByEpochAndPool(Vec<(KeyHash, u64)>),
67+
/// Vec<(PoolId, StakeAddress, ActiveStakeAmount)>
68+
SPDDByEpoch(Vec<(PoolId, StakeAddress, u64)>),
69+
/// Vec<(StakeAddress, ActiveStakeAmount)>
70+
SPDDByEpochAndPool(Vec<(StakeAddress, u64)>),
7371

7472
// Pools-related responses
7573
OptimalPoolSizing(Option<OptimalPoolSizing>),
@@ -79,7 +77,7 @@ pub enum AccountsStateQueryResponse {
7977

8078
// DReps-related responses
8179
DrepDelegators(DrepDelegators),
82-
AccountsDrepDelegationsMap(HashMap<KeyHash, Option<DRepChoice>>),
80+
AccountsDrepDelegationsMap(HashMap<StakeAddress, Option<DRepChoice>>),
8381

8482
NotFound,
8583
Error(String),
@@ -186,10 +184,10 @@ pub struct OptimalPoolSizing {
186184

187185
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
188186
pub struct PoolDelegators {
189-
pub delegators: Vec<(KeyHash, u64)>,
187+
pub delegators: Vec<(StakeAddress, u64)>,
190188
}
191189

192190
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
193191
pub struct DrepDelegators {
194-
pub delegators: Vec<(KeyHash, u64)>,
192+
pub delegators: Vec<(StakeAddress, u64)>,
195193
}

common/src/queries/pools.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
queries::governance::VoteRecord, rational_number::RationalNumber, KeyHash, PoolEpochState,
3-
PoolId, PoolMetadata, PoolRegistration, PoolRetirement, PoolUpdateEvent, Relay,
2+
queries::governance::VoteRecord, rational_number::RationalNumber, PoolEpochState, PoolId,
3+
PoolMetadata, PoolRegistration, PoolRetirement, PoolUpdateEvent, Relay, StakeAddress,
44
};
55

66
pub const DEFAULT_POOLS_QUERY_TOPIC: (&str, &str) =
@@ -94,5 +94,5 @@ pub struct PoolActiveStakeInfo {
9494

9595
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9696
pub struct PoolDelegators {
97-
pub delegators: Vec<(KeyHash, u64)>,
97+
pub delegators: Vec<(StakeAddress, u64)>,
9898
}

common/src/stake_addresses.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
math::update_value_with_delta, messages::DRepDelegationDistribution, DRepChoice,
3-
DRepCredential, DelegatedStake, KeyHash, Lovelace, PoolId, PoolLiveStakeInfo, StakeAddress,
3+
DRepCredential, DelegatedStake, Lovelace, PoolId, PoolLiveStakeInfo, StakeAddress,
44
StakeAddressDelta, Withdrawal,
55
};
66
use anyhow::Result;
@@ -163,15 +163,15 @@ impl StakeAddressMap {
163163
}
164164

165165
/// Get Pool Delegators with live_stakes
166-
pub fn get_pool_delegators(&self, pool_operator: &PoolId) -> Vec<(KeyHash, u64)> {
166+
pub fn get_pool_delegators(&self, pool_operator: &PoolId) -> Vec<(StakeAddress, u64)> {
167167
// Find stake addresses delegated to pool_operator
168-
let delegators: Vec<(KeyHash, u64)> = self
168+
let delegators: Vec<(StakeAddress, u64)> = self
169169
.inner
170170
.iter()
171171
.filter_map(|(stake_address, sas)| match sas.delegated_spo.as_ref() {
172172
Some(delegated_spo) => {
173173
if delegated_spo.eq(pool_operator) {
174-
Some((*stake_address.get_hash(), sas.utxo_value + sas.rewards))
174+
Some((stake_address.clone(), sas.utxo_value + sas.rewards))
175175
} else {
176176
None
177177
}
@@ -184,15 +184,15 @@ impl StakeAddressMap {
184184
}
185185

186186
/// Get DRep Delegators with live_stakes
187-
pub fn get_drep_delegators(&self, drep: &DRepChoice) -> Vec<(KeyHash, u64)> {
187+
pub fn get_drep_delegators(&self, drep: &DRepChoice) -> Vec<(StakeAddress, u64)> {
188188
// Find stake addresses delegated to drep
189-
let delegators: Vec<(KeyHash, u64)> = self
189+
let delegators: Vec<(StakeAddress, u64)> = self
190190
.inner
191191
.iter()
192192
.filter_map(|(stake_address, sas)| match sas.delegated_drep.as_ref() {
193193
Some(delegated_drep) => {
194194
if delegated_drep.eq(drep) {
195-
Some((*stake_address.get_hash(), sas.utxo_value))
195+
Some((stake_address.clone(), sas.utxo_value))
196196
} else {
197197
None
198198
}
@@ -209,14 +209,13 @@ impl StakeAddressMap {
209209
pub fn get_accounts_utxo_values_map(
210210
&self,
211211
stake_addresses: &[StakeAddress],
212-
) -> Option<HashMap<KeyHash, u64>> {
212+
) -> Option<HashMap<StakeAddress, u64>> {
213213
let mut map = HashMap::new();
214214

215215
for stake_address in stake_addresses {
216216
let account = self.get(stake_address)?;
217217
let utxo_value = account.utxo_value;
218-
let key_hash = stake_address.get_hash();
219-
map.insert(*key_hash, utxo_value);
218+
map.insert(stake_address.clone(), utxo_value);
220219
}
221220

222221
Some(map)
@@ -227,14 +226,13 @@ impl StakeAddressMap {
227226
pub fn get_accounts_balances_map(
228227
&self,
229228
stake_addresses: &[StakeAddress],
230-
) -> Option<HashMap<KeyHash, u64>> {
229+
) -> Option<HashMap<StakeAddress, u64>> {
231230
let mut map = HashMap::new();
232231

233232
for stake_address in stake_addresses {
234233
let account = self.get(stake_address)?;
235234
let balance = account.utxo_value + account.rewards;
236-
let key_hash = stake_address.get_hash();
237-
map.insert(*key_hash, balance);
235+
map.insert(stake_address.clone(), balance);
238236
}
239237

240238
Some(map)
@@ -245,14 +243,13 @@ impl StakeAddressMap {
245243
pub fn get_drep_delegations_map(
246244
&self,
247245
stake_addresses: &[StakeAddress],
248-
) -> Option<HashMap<KeyHash, Option<DRepChoice>>> {
246+
) -> Option<HashMap<StakeAddress, Option<DRepChoice>>> {
249247
let mut map = HashMap::new();
250248

251249
for stake_address in stake_addresses {
252250
let account = self.get(stake_address)?;
253251
let maybe_drep = account.delegated_drep.clone();
254-
let key_hash = stake_address.get_hash();
255-
map.insert(*key_hash, maybe_drep);
252+
map.insert(stake_address.clone(), maybe_drep);
256253
}
257254

258255
Some(map)
@@ -323,7 +320,7 @@ impl StakeAddressMap {
323320

324321
/// Dump current Stake Pool Delegation Distribution State
325322
/// <PoolId -> (Stake Key, Active Stakes Amount)>
326-
pub fn dump_spdd_state(&self) -> HashMap<PoolId, Vec<(KeyHash, u64)>> {
323+
pub fn dump_spdd_state(&self) -> HashMap<PoolId, Vec<(StakeAddress, u64)>> {
327324
let entries: Vec<_> = self
328325
.inner
329326
.par_iter()
@@ -332,9 +329,9 @@ impl StakeAddressMap {
332329
})
333330
.collect();
334331

335-
let mut result: HashMap<PoolId, Vec<(KeyHash, u64)>> = HashMap::new();
332+
let mut result: HashMap<PoolId, Vec<(StakeAddress, u64)>> = HashMap::new();
336333
for (spo, entry) in entries {
337-
result.entry(spo).or_default().push((entry.0.get_credential().get_hash(), entry.1));
334+
result.entry(spo).or_default().push((entry.0, entry.1));
338335
}
339336
result
340337
}
@@ -552,7 +549,7 @@ impl StakeAddressMap {
552549
mod tests {
553550
use super::*;
554551
use crate::hash::Hash;
555-
use crate::{NetworkId, StakeAddress, StakeCredential};
552+
use crate::{KeyHash, NetworkId, StakeAddress, StakeCredential};
556553

557554
const STAKE_KEY_HASH: KeyHash = KeyHash::new([0x99; 28]);
558555
const STAKE_KEY_HASH_2: KeyHash = KeyHash::new([0xaa; 28]);
@@ -1248,8 +1245,8 @@ mod tests {
12481245
let map = stake_addresses.get_accounts_utxo_values_map(&keys).unwrap();
12491246

12501247
assert_eq!(map.len(), 2);
1251-
assert_eq!(map.get(addr1.get_hash()).copied().unwrap(), 1000);
1252-
assert_eq!(map.get(addr2.get_hash()).copied().unwrap(), 2000);
1248+
assert_eq!(map.get(&addr1).copied().unwrap(), 1000);
1249+
assert_eq!(map.get(&addr2).copied().unwrap(), 2000);
12531250
}
12541251

12551252
#[test]
@@ -1362,8 +1359,8 @@ mod tests {
13621359
let map = stake_addresses.get_accounts_balances_map(&addresses).unwrap();
13631360

13641361
assert_eq!(map.len(), 2);
1365-
assert_eq!(map.get(addr1.get_hash()).copied().unwrap(), 1100);
1366-
assert_eq!(map.get(addr2.get_hash()).copied().unwrap(), 2000);
1362+
assert_eq!(map.get(&addr1).copied().unwrap(), 1100);
1363+
assert_eq!(map.get(&addr2).copied().unwrap(), 2000);
13671364
}
13681365

13691366
#[test]
@@ -1470,15 +1467,9 @@ mod tests {
14701467
let map = stake_addresses.get_drep_delegations_map(&addresses).unwrap();
14711468

14721469
assert_eq!(map.len(), 3);
1473-
assert_eq!(
1474-
map.get(addr1.get_hash()).unwrap(),
1475-
&Some(DRepChoice::Abstain)
1476-
);
1477-
assert_eq!(
1478-
map.get(addr2.get_hash()).unwrap(),
1479-
&Some(DRepChoice::Key(DREP_HASH))
1480-
);
1481-
assert_eq!(map.get(addr3.get_hash()).unwrap(), &None);
1470+
assert_eq!(map.get(&addr1).unwrap(), &Some(DRepChoice::Abstain));
1471+
assert_eq!(map.get(&addr2).unwrap(), &Some(DRepChoice::Key(DREP_HASH)));
1472+
assert_eq!(map.get(&addr3).unwrap(), &None);
14821473
}
14831474

14841475
#[test]

modules/accounts_state/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ anyhow = { workspace = true }
1717
bigdecimal = "0.4.8"
1818
chrono = { workspace = true }
1919
config = { workspace = true }
20-
dashmap = { workspace = true }
2120
hex = { workspace = true }
2221
imbl = { workspace = true }
2322
serde = { workspace = true }
24-
serde_json = { workspace = true }
25-
serde_with = { workspace = true }
2623
tokio = { workspace = true }
2724
tracing = { workspace = true }
2825
fjall = "2.11.2"
29-
rayon = "1.10.0"
3026
csv = "1.3.1"
3127
itertools = "0.14.0"
3228

29+
[dev-dependencies]
30+
tempfile = "3"
31+
3332
[lib]
3433
path = "src/accounts_state.rs"

0 commit comments

Comments
 (0)