From 1adb141414208b3442b6e6e0379c0d2833363cb7 Mon Sep 17 00:00:00 2001 From: Karan Dhareshwar Date: Sat, 23 Nov 2024 11:45:39 -0600 Subject: [PATCH] Fix bug in SeignorageRecipientV1 --- .../src/test/system_contracts/upgrade.rs | 17 ++++++----- .../genesis/account_contract_installer.rs | 2 +- types/src/system/auction/delegator_kind.rs | 5 ++++ .../system/auction/seigniorage_recipient.rs | 30 ++++++------------- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs b/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs index 91bad8889a..1d5a22e850 100644 --- a/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs +++ b/execution_engine_testing/tests/src/test/system_contracts/upgrade.rs @@ -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, }, @@ -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()))) + } } } } diff --git a/storage/src/system/genesis/account_contract_installer.rs b/storage/src/system/genesis/account_contract_installer.rs index bb0610c9ea..46dbffef64 100644 --- a/storage/src/system/genesis/account_contract_installer.rs +++ b/storage/src/system/genesis/account_contract_installer.rs @@ -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(), diff --git a/types/src/system/auction/delegator_kind.rs b/types/src/system/auction/delegator_kind.rs index 8bda114a72..47ef0915d0 100644 --- a/types/src/system/auction/delegator_kind.rs +++ b/types/src/system/auction/delegator_kind.rs @@ -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 { diff --git a/types/src/system/auction/seigniorage_recipient.rs b/types/src/system/auction/seigniorage_recipient.rs index 00594233f7..7596e21918 100644 --- a/types/src/system/auction/seigniorage_recipient.rs +++ b/types/src/system/auction/seigniorage_recipient.rs @@ -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. @@ -15,7 +15,7 @@ pub struct SeigniorageRecipientV1 { /// Delegation rate of a seigniorage recipient. delegation_rate: DelegationRate, /// Delegators and their bids. - delegator_stake: BTreeMap, + delegator_stake: BTreeMap, } impl SeigniorageRecipientV1 { @@ -23,7 +23,7 @@ impl SeigniorageRecipientV1 { pub fn new( stake: U512, delegation_rate: DelegationRate, - delegator_stake: BTreeMap, + delegator_stake: BTreeMap, ) -> Self { Self { stake, @@ -43,7 +43,7 @@ impl SeigniorageRecipientV1 { } /// Returns delegators of the provided recipient and their stake - pub fn delegator_stake(&self) -> &BTreeMap { + pub fn delegator_stake(&self) -> &BTreeMap { &self.delegator_stake } @@ -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 { @@ -251,7 +248,7 @@ impl From 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 { @@ -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()), ]), };