Skip to content

Commit

Permalink
Fix get_stake_weight Treatment of delegator Option
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay committed Sep 12, 2024
1 parent b86d2e2 commit 30d9e94
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions server/src/models/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ pub(crate) struct LedgerAccount {
pub(crate) delegate: Option<String>,
}

fn is_matching_public_key(delegate: &Option<String>, pk: &String) -> bool {
match delegate {
Some(v) => v == pk,
None => false,
}
}

impl Ledger {
pub(crate) async fn fetch(
hash: impl Into<String>,
Expand Down Expand Up @@ -90,15 +83,16 @@ impl Ledger {

match version {
ProposalVersion::V1 => {
if is_matching_public_key(&account.delegate, &public_key) {
if account.delegate.clone().unwrap_or(public_key.clone()) != public_key {
return Ok(Decimal::new(0, LEDGER_BALANCE_SCALE));
}

let delegators = self
.0
.iter()
.filter(|d| {
is_matching_public_key(&d.delegate, &public_key) && d.pk != public_key
d.delegate.clone().unwrap_or(d.pk.clone()) == public_key
&& d.pk != public_key
})
.collect::<Vec<&LedgerAccount>>();

Expand All @@ -123,7 +117,7 @@ impl Ledger {
.0
.iter()
.filter(|d| {
is_matching_public_key(&d.delegate, &public_key)
d.delegate.clone().unwrap_or(d.pk.clone()) == public_key
&& d.pk != public_key
&& !map.0.contains_key(&d.pk)
})
Expand Down Expand Up @@ -154,11 +148,11 @@ mod tests {
use super::*;

impl LedgerAccount {
pub fn new(pk: String, balance: String, delegate: String) -> LedgerAccount {
pub fn new(pk: String, balance: String, delegate: Option<String>) -> LedgerAccount {
LedgerAccount {
pk,
balance,
delegate: Some(delegate),
delegate,
}
}
}
Expand All @@ -170,13 +164,13 @@ mod tests {
LedgerAccount,
LedgerAccount,
) {
return (
LedgerAccount::new("A".to_string(), "1".to_string(), "A".to_string()),
LedgerAccount::new("B".to_string(), "1".to_string(), "B".to_string()),
LedgerAccount::new("C".to_string(), "1".to_string(), "A".to_string()),
LedgerAccount::new("D".to_string(), "1".to_string(), "A".to_string()),
LedgerAccount::new("E".to_string(), "1".to_string(), "B".to_string()),
);
(
LedgerAccount::new("A".to_string(), "1".to_string(), None),
LedgerAccount::new("B".to_string(), "1".to_string(), None),
LedgerAccount::new("C".to_string(), "1".to_string(), Some("A".to_string())),
LedgerAccount::new("D".to_string(), "1".to_string(), Some("A".to_string())),
LedgerAccount::new("E".to_string(), "1".to_string(), Some("B".to_string())),
)
}

fn get_votes() -> HashMap<String, MinaVote> {
Expand Down

0 comments on commit 30d9e94

Please sign in to comment.