Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ impl ShelleyAddress {
ShelleyAddressPaymentPart::ScriptHash(data) => (data, 1),
};

// TODO: MH - make delegation hash an Option<Hash<28>> type
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do this in another PR!

let (delegation_hash, delegation_bits): (Vec<u8>, u8) = match &self.delegation {
ShelleyAddressDelegationPart::None => (Vec::new(), 3),
ShelleyAddressDelegationPart::StakeKeyHash(hash) => (hash.to_vec(), 0),
Expand Down
22 changes: 10 additions & 12 deletions common/src/queries/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;

use crate::{
DRepChoice, KeyHash, PoolId, PoolLiveStakeInfo, RewardType, StakeAddress, TxIdentifier,
};
use crate::{DRepChoice, PoolId, PoolLiveStakeInfo, RewardType, StakeAddress, TxIdentifier};

pub const DEFAULT_ACCOUNTS_QUERY_TOPIC: (&str, &str) =
("accounts-state-query-topic", "cardano.query.accounts");
Expand Down Expand Up @@ -59,17 +57,17 @@ pub enum AccountsStateQueryResponse {
AccountAssets(AccountAssets),
AccountAssetsTotals(AccountAssetsTotals),
AccountUTxOs(AccountUTxOs),
AccountsUtxoValuesMap(HashMap<KeyHash, u64>),
AccountsUtxoValuesMap(HashMap<StakeAddress, u64>),
AccountsUtxoValuesSum(u64),
AccountsBalancesMap(HashMap<KeyHash, u64>),
AccountsBalancesMap(HashMap<StakeAddress, u64>),
AccountsBalancesSum(u64),

// Epochs-related responses
ActiveStakes(u64),
/// Vec<(PoolId, StakeKey, ActiveStakeAmount)>
SPDDByEpoch(Vec<(PoolId, KeyHash, u64)>),
/// Vec<(StakeKey, ActiveStakeAmount)>
SPDDByEpochAndPool(Vec<(KeyHash, u64)>),
/// Vec<(PoolId, StakeAddress, ActiveStakeAmount)>
SPDDByEpoch(Vec<(PoolId, StakeAddress, u64)>),
/// Vec<(StakeAddress, ActiveStakeAmount)>
SPDDByEpochAndPool(Vec<(StakeAddress, u64)>),

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

// DReps-related responses
DrepDelegators(DrepDelegators),
AccountsDrepDelegationsMap(HashMap<KeyHash, Option<DRepChoice>>),
AccountsDrepDelegationsMap(HashMap<StakeAddress, Option<DRepChoice>>),

NotFound,
Error(String),
Expand Down Expand Up @@ -186,10 +184,10 @@ pub struct OptimalPoolSizing {

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

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DrepDelegators {
pub delegators: Vec<(KeyHash, u64)>,
pub delegators: Vec<(StakeAddress, u64)>,
}
6 changes: 3 additions & 3 deletions common/src/queries/pools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
queries::governance::VoteRecord, rational_number::RationalNumber, KeyHash, PoolEpochState,
PoolId, PoolMetadata, PoolRegistration, PoolRetirement, PoolUpdateEvent, Relay,
queries::governance::VoteRecord, rational_number::RationalNumber, PoolEpochState, PoolId,
PoolMetadata, PoolRegistration, PoolRetirement, PoolUpdateEvent, Relay, StakeAddress,
};

pub const DEFAULT_POOLS_QUERY_TOPIC: (&str, &str) =
Expand Down Expand Up @@ -94,5 +94,5 @@ pub struct PoolActiveStakeInfo {

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PoolDelegators {
pub delegators: Vec<(KeyHash, u64)>,
pub delegators: Vec<(StakeAddress, u64)>,
}
57 changes: 24 additions & 33 deletions common/src/stake_addresses.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
math::update_value_with_delta, messages::DRepDelegationDistribution, DRepChoice,
DRepCredential, DelegatedStake, KeyHash, Lovelace, PoolId, PoolLiveStakeInfo, StakeAddress,
DRepCredential, DelegatedStake, Lovelace, PoolId, PoolLiveStakeInfo, StakeAddress,
StakeAddressDelta, Withdrawal,
};
use anyhow::Result;
Expand Down Expand Up @@ -163,15 +163,15 @@ impl StakeAddressMap {
}

/// Get Pool Delegators with live_stakes
pub fn get_pool_delegators(&self, pool_operator: &PoolId) -> Vec<(KeyHash, u64)> {
pub fn get_pool_delegators(&self, pool_operator: &PoolId) -> Vec<(StakeAddress, u64)> {
// Find stake addresses delegated to pool_operator
let delegators: Vec<(KeyHash, u64)> = self
let delegators: Vec<(StakeAddress, u64)> = self
.inner
.iter()
.filter_map(|(stake_address, sas)| match sas.delegated_spo.as_ref() {
Some(delegated_spo) => {
if delegated_spo.eq(pool_operator) {
Some((*stake_address.get_hash(), sas.utxo_value + sas.rewards))
Some((stake_address.clone(), sas.utxo_value + sas.rewards))
} else {
None
}
Expand All @@ -184,15 +184,15 @@ impl StakeAddressMap {
}

/// Get DRep Delegators with live_stakes
pub fn get_drep_delegators(&self, drep: &DRepChoice) -> Vec<(KeyHash, u64)> {
pub fn get_drep_delegators(&self, drep: &DRepChoice) -> Vec<(StakeAddress, u64)> {
// Find stake addresses delegated to drep
let delegators: Vec<(KeyHash, u64)> = self
let delegators: Vec<(StakeAddress, u64)> = self
.inner
.iter()
.filter_map(|(stake_address, sas)| match sas.delegated_drep.as_ref() {
Some(delegated_drep) => {
if delegated_drep.eq(drep) {
Some((*stake_address.get_hash(), sas.utxo_value))
Some((stake_address.clone(), sas.utxo_value))
} else {
None
}
Expand All @@ -209,14 +209,13 @@ impl StakeAddressMap {
pub fn get_accounts_utxo_values_map(
&self,
stake_addresses: &[StakeAddress],
) -> Option<HashMap<KeyHash, u64>> {
) -> Option<HashMap<StakeAddress, u64>> {
let mut map = HashMap::new();

for stake_address in stake_addresses {
let account = self.get(stake_address)?;
let utxo_value = account.utxo_value;
let key_hash = stake_address.get_hash();
map.insert(*key_hash, utxo_value);
map.insert(stake_address.clone(), utxo_value);
}

Some(map)
Expand All @@ -227,14 +226,13 @@ impl StakeAddressMap {
pub fn get_accounts_balances_map(
&self,
stake_addresses: &[StakeAddress],
) -> Option<HashMap<KeyHash, u64>> {
) -> Option<HashMap<StakeAddress, u64>> {
let mut map = HashMap::new();

for stake_address in stake_addresses {
let account = self.get(stake_address)?;
let balance = account.utxo_value + account.rewards;
let key_hash = stake_address.get_hash();
map.insert(*key_hash, balance);
map.insert(stake_address.clone(), balance);
}

Some(map)
Expand All @@ -245,14 +243,13 @@ impl StakeAddressMap {
pub fn get_drep_delegations_map(
&self,
stake_addresses: &[StakeAddress],
) -> Option<HashMap<KeyHash, Option<DRepChoice>>> {
) -> Option<HashMap<StakeAddress, Option<DRepChoice>>> {
let mut map = HashMap::new();

for stake_address in stake_addresses {
let account = self.get(stake_address)?;
let maybe_drep = account.delegated_drep.clone();
let key_hash = stake_address.get_hash();
map.insert(*key_hash, maybe_drep);
map.insert(stake_address.clone(), maybe_drep);
}

Some(map)
Expand Down Expand Up @@ -323,7 +320,7 @@ impl StakeAddressMap {

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

let mut result: HashMap<PoolId, Vec<(KeyHash, u64)>> = HashMap::new();
let mut result: HashMap<PoolId, Vec<(StakeAddress, u64)>> = HashMap::new();
for (spo, entry) in entries {
result.entry(spo).or_default().push((entry.0.get_credential().get_hash(), entry.1));
result.entry(spo).or_default().push((entry.0, entry.1));
}
result
}
Expand Down Expand Up @@ -552,7 +549,7 @@ impl StakeAddressMap {
mod tests {
use super::*;
use crate::hash::Hash;
use crate::{NetworkId, StakeAddress, StakeCredential};
use crate::{KeyHash, NetworkId, StakeAddress, StakeCredential};

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

assert_eq!(map.len(), 2);
assert_eq!(map.get(addr1.get_hash()).copied().unwrap(), 1000);
assert_eq!(map.get(addr2.get_hash()).copied().unwrap(), 2000);
assert_eq!(map.get(&addr1).copied().unwrap(), 1000);
assert_eq!(map.get(&addr2).copied().unwrap(), 2000);
}

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

assert_eq!(map.len(), 2);
assert_eq!(map.get(addr1.get_hash()).copied().unwrap(), 1100);
assert_eq!(map.get(addr2.get_hash()).copied().unwrap(), 2000);
assert_eq!(map.get(&addr1).copied().unwrap(), 1100);
assert_eq!(map.get(&addr2).copied().unwrap(), 2000);
}

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

assert_eq!(map.len(), 3);
assert_eq!(
map.get(addr1.get_hash()).unwrap(),
&Some(DRepChoice::Abstain)
);
assert_eq!(
map.get(addr2.get_hash()).unwrap(),
&Some(DRepChoice::Key(DREP_HASH))
);
assert_eq!(map.get(addr3.get_hash()).unwrap(), &None);
assert_eq!(map.get(&addr1).unwrap(), &Some(DRepChoice::Abstain));
assert_eq!(map.get(&addr2).unwrap(), &Some(DRepChoice::Key(DREP_HASH)));
assert_eq!(map.get(&addr3).unwrap(), &None);
}

#[test]
Expand Down
Loading