Skip to content

Commit

Permalink
Fix bug in SeignorageRecipientV1
Browse files Browse the repository at this point in the history
  • Loading branch information
darthsiroftardis committed Nov 23, 2024
1 parent a449464 commit 1adb141
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use casper_types::{
system::{
self,
auction::{
SeigniorageRecipientsSnapshotV1, SeigniorageRecipientsSnapshotV2, AUCTION_DELAY_KEY,
DEFAULT_SEIGNIORAGE_RECIPIENTS_SNAPSHOT_VERSION, LOCKED_FUNDS_PERIOD_KEY,
SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY, SEIGNIORAGE_RECIPIENTS_SNAPSHOT_VERSION_KEY,
UNBONDING_DELAY_KEY, VALIDATOR_SLOTS_KEY,
DelegatorKind, SeigniorageRecipientsSnapshotV1, SeigniorageRecipientsSnapshotV2,
AUCTION_DELAY_KEY, DEFAULT_SEIGNIORAGE_RECIPIENTS_SNAPSHOT_VERSION,
LOCKED_FUNDS_PERIOD_KEY, SEIGNIORAGE_RECIPIENTS_SNAPSHOT_KEY,
SEIGNIORAGE_RECIPIENTS_SNAPSHOT_VERSION_KEY, UNBONDING_DELAY_KEY, VALIDATOR_SLOTS_KEY,
},
mint::ROUND_SEIGNIORAGE_RATE_KEY,
},
Expand Down Expand Up @@ -882,10 +882,11 @@ fn should_migrate_seigniorage_snapshot_to_new_version() {
legacy_recipient.delegation_rate(),
new_recipient.delegation_rate()
);
assert_eq!(
legacy_recipient.delegator_stake(),
new_recipient.delegator_stake()
);
for pk in legacy_recipient.delegator_stake().keys() {
assert!(new_recipient
.delegator_stake()
.contains_key(&DelegatorKind::PublicKey(pk.clone())))
}
}
}
}
2 changes: 1 addition & 1 deletion storage/src/system/genesis/account_contract_installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ where
self.create_purse(delegator_delegated_amount.value())?;

let delegator_kind: DelegatorKind =
(*delegator_public_key).clone().into();
DelegatorKind::PublicKey((*delegator_public_key).clone());
let delegator = DelegatorBid::locked(
delegator_kind.clone(),
delegator_delegated_amount.value(),
Expand Down
5 changes: 5 additions & 0 deletions types/src/system/auction/delegator_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ impl DelegatorKind {
DelegatorKind::Purse(_) => DelegatorKindTag::Purse,
}
}

/// Returns true if the kind is a purse.
pub fn is_purse(&self) -> bool {
matches!(self, DelegatorKind::Purse(_))
}
}

impl ToBytes for DelegatorKind {
Expand Down
30 changes: 9 additions & 21 deletions types/src/system/auction/seigniorage_recipient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloc::{collections::BTreeMap, vec::Vec};
use crate::{
bytesrepr::{self, FromBytes, ToBytes},
system::auction::{Bid, DelegationRate, DelegatorKind},
CLType, CLTyped, U512,
CLType, CLTyped, PublicKey, U512,
};

/// The seigniorage recipient details.
Expand All @@ -15,15 +15,15 @@ pub struct SeigniorageRecipientV1 {
/// Delegation rate of a seigniorage recipient.
delegation_rate: DelegationRate,
/// Delegators and their bids.
delegator_stake: BTreeMap<DelegatorKind, U512>,
delegator_stake: BTreeMap<PublicKey, U512>,
}

impl SeigniorageRecipientV1 {
/// Creates a new SeigniorageRecipient
pub fn new(
stake: U512,
delegation_rate: DelegationRate,
delegator_stake: BTreeMap<DelegatorKind, U512>,
delegator_stake: BTreeMap<PublicKey, U512>,
) -> Self {
Self {
stake,
Expand All @@ -43,7 +43,7 @@ impl SeigniorageRecipientV1 {
}

/// Returns delegators of the provided recipient and their stake
pub fn delegator_stake(&self) -> &BTreeMap<DelegatorKind, U512> {
pub fn delegator_stake(&self) -> &BTreeMap<PublicKey, U512> {
&self.delegator_stake
}

Expand Down Expand Up @@ -106,10 +106,7 @@ impl From<&Bid> for SeigniorageRecipientV1 {
.delegators()
.iter()
.map(|(delegator_public_key, delegator)| {
(
DelegatorKind::PublicKey(delegator_public_key.clone()),
delegator.staked_amount(),
)
(delegator_public_key.clone(), delegator.staked_amount())
})
.collect();
Self {
Expand Down Expand Up @@ -251,7 +248,7 @@ impl From<SeigniorageRecipientV1> for SeigniorageRecipientV2 {
fn from(snapshot: SeigniorageRecipientV1) -> Self {
let mut delegator_stake = BTreeMap::new();
for (kind, amount) in snapshot.delegator_stake {
delegator_stake.insert(kind, amount);
delegator_stake.insert(DelegatorKind::PublicKey(kind), amount);
}

Self {
Expand Down Expand Up @@ -381,18 +378,9 @@ mod tests {
stake: U512::max_value(),
delegation_rate: DelegationRate::MAX,
delegator_stake: BTreeMap::from_iter(vec![
(
DelegatorKind::PublicKey(delegator_1_key.clone()),
U512::max_value(),
),
(
DelegatorKind::PublicKey(delegator_2_key.clone()),
U512::max_value(),
),
(
DelegatorKind::PublicKey(delegator_3_key.clone()),
U512::zero(),
),
(delegator_1_key.clone(), U512::max_value()),
(delegator_2_key.clone(), U512::max_value()),
(delegator_3_key.clone(), U512::zero()),
]),
};

Expand Down

0 comments on commit 1adb141

Please sign in to comment.