From 32f332a71246a01429927d822448b405299e47d9 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Tue, 17 Jan 2023 11:37:59 -0300 Subject: [PATCH] NIS should retain funds in reserve (#12928) * Keep funds with receipt holder * Counterpart is optional * Use named reserves * Tests * Benchmarks * Fixes * Update frame/nis/src/lib.rs Co-authored-by: Keith Yeung * Update frame/nis/src/lib.rs Co-authored-by: Keith Yeung * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Update frame/nis/src/tests.rs * Update frame/nis/src/lib.rs Co-authored-by: Oliver Tale-Yazdi * Update frame/nis/src/lib.rs Co-authored-by: Oliver Tale-Yazdi * Update frame/nis/src/lib.rs Co-authored-by: Oliver Tale-Yazdi * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Update frame/nis/src/lib.rs * Formatting Co-authored-by: Keith Yeung Co-authored-by: Oliver Tale-Yazdi --- bin/node/runtime/src/lib.rs | 2 + frame/nis/src/benchmarking.rs | 41 +- frame/nis/src/lib.rs | 312 +++++++++++--- frame/nis/src/mock.rs | 4 +- frame/nis/src/tests.rs | 453 ++++++++++++++------ frame/nis/src/weights.rs | 320 ++++++++------ frame/support/src/traits/tokens/fungible.rs | 5 + 7 files changed, 828 insertions(+), 309 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index a9e93a16f0713..d34c04fcb5d3c 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -1499,6 +1499,7 @@ parameter_types! { pub const ThawThrottle: (Perquintill, BlockNumber) = (Perquintill::from_percent(25), 5); pub Target: Perquintill = Perquintill::zero(); pub const NisPalletId: PalletId = PalletId(*b"py/nis "); + pub const NisReserveId: [u8; 8] = *b"py/nis "; } impl pallet_nis::Config for Runtime { @@ -1522,6 +1523,7 @@ impl pallet_nis::Config for Runtime { type IntakePeriod = IntakePeriod; type MaxIntakeWeight = MaxIntakeWeight; type ThawThrottle = ThawThrottle; + type ReserveId = NisReserveId; } parameter_types! { diff --git a/frame/nis/src/benchmarking.rs b/frame/nis/src/benchmarking.rs index 606b1c484b1e8..b45c982bd150a 100644 --- a/frame/nis/src/benchmarking.rs +++ b/frame/nis/src/benchmarking.rs @@ -21,7 +21,7 @@ use super::*; use frame_benchmarking::{account, benchmarks, whitelisted_caller}; -use frame_support::traits::{Currency, EnsureOrigin, Get}; +use frame_support::traits::{nonfungible::Inspect, Currency, EnsureOrigin, Get}; use frame_system::RawOrigin; use sp_arithmetic::Perquintill; use sp_runtime::{ @@ -106,6 +106,7 @@ benchmarks! { T::Currency::make_free_balance_be(&caller, bid); Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?; Nis::::process_queues(Perquintill::one(), 1, 1, &mut WeightCounter::unlimited()); + Nis::::communify(RawOrigin::Signed(caller.clone()).into(), 0)?; let original = T::Currency::free_balance(&Nis::::account_id()); T::Currency::make_free_balance_be(&Nis::::account_id(), BalanceOf::::min_value()); }: _(origin) @@ -116,7 +117,7 @@ benchmarks! { assert!(missing <= Perquintill::one() / 100_000); } - thaw { + thaw_private { let caller: T::AccountId = whitelisted_caller(); T::Currency::make_free_balance_be(&caller, T::MinBid::get() * BalanceOf::::from(3u32)); Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?; @@ -128,6 +129,42 @@ benchmarks! { assert!(Receipts::::get(0).is_none()); } + thaw_communal { + let caller: T::AccountId = whitelisted_caller(); + T::Currency::make_free_balance_be(&caller, T::MinBid::get() * BalanceOf::::from(3u32)); + Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?; + Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?; + Nis::::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited()); + Receipts::::mutate(0, |m_g| if let Some(ref mut g) = m_g { g.expiry = Zero::zero() }); + Nis::::communify(RawOrigin::Signed(caller.clone()).into(), 0)?; + }: _(RawOrigin::Signed(caller.clone()), 0) + verify { + assert!(Receipts::::get(0).is_none()); + } + + privatize { + let caller: T::AccountId = whitelisted_caller(); + T::Currency::make_free_balance_be(&caller, T::MinBid::get() * BalanceOf::::from(3u32)); + Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?; + Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?; + Nis::::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited()); + Nis::::communify(RawOrigin::Signed(caller.clone()).into(), 0)?; + }: _(RawOrigin::Signed(caller.clone()), 0) + verify { + assert_eq!(Nis::::owner(&0), Some(caller)); + } + + communify { + let caller: T::AccountId = whitelisted_caller(); + T::Currency::make_free_balance_be(&caller, T::MinBid::get() * BalanceOf::::from(3u32)); + Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?; + Nis::::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?; + Nis::::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited()); + }: _(RawOrigin::Signed(caller.clone()), 0) + verify { + assert_eq!(Nis::::owner(&0), None); + } + process_queues { fill_queues::()?; }: { diff --git a/frame/nis/src/lib.rs b/frame/nis/src/lib.rs index dff64625a3654..fa0163fb60bfe 100644 --- a/frame/nis/src/lib.rs +++ b/frame/nis/src/lib.rs @@ -169,7 +169,7 @@ pub mod pallet { nonfungible::{Inspect as NonfungibleInspect, Transfer as NonfungibleTransfer}, Currency, Defensive, DefensiveSaturating, ExistenceRequirement::AllowDeath, - OnUnbalanced, ReservableCurrency, + NamedReservableCurrency, OnUnbalanced, }, PalletId, }; @@ -189,9 +189,10 @@ pub mod pallet { type ReceiptRecordOf = ReceiptRecord< ::AccountId, ::BlockNumber, + BalanceOf, >; type IssuanceInfoOf = IssuanceInfo>; - type SummaryRecordOf = SummaryRecord<::BlockNumber>; + type SummaryRecordOf = SummaryRecord<::BlockNumber, BalanceOf>; type BidOf = Bid, ::AccountId>; type QueueTotalsTypeOf = BoundedVec<(u32, BalanceOf), ::QueueCount>; @@ -208,7 +209,7 @@ pub mod pallet { type PalletId: Get; /// Currency type that this works on. - type Currency: ReservableCurrency; + type Currency: NamedReservableCurrency; /// Just the `Currency::Balance` type; we have this item to allow us to constrain it to /// `From`. @@ -300,6 +301,12 @@ pub mod pallet { /// The maximum proportion which may be thawed and the period over which it is reset. #[pallet::constant] type ThawThrottle: Get<(Perquintill, Self::BlockNumber)>; + + /// The name for the reserve ID. + #[pallet::constant] + type ReserveId: Get< + >::ReserveIdentifier, + >; } #[pallet::pallet] @@ -321,11 +328,13 @@ pub mod pallet { #[derive( Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen, )] - pub struct ReceiptRecord { + pub struct ReceiptRecord { /// The proportion of the effective total issuance. pub proportion: Perquintill, - /// The account to whom this receipt belongs. - pub who: AccountId, + /// The account to whom this receipt belongs and the amount of funds on hold in their + /// account for servicing this receipt. If `None`, then it is a communal receipt and + /// fungible counterparts have been issued. + pub owner: Option<(AccountId, Balance)>, /// The time after which this receipt can be thawed. pub expiry: BlockNumber, } @@ -344,7 +353,7 @@ pub mod pallet { #[derive( Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen, )] - pub struct SummaryRecord { + pub struct SummaryRecord { /// The total proportion over all outstanding receipts. pub proportion_owed: Perquintill, /// The total number of receipts created so far. @@ -353,6 +362,9 @@ pub mod pallet { pub thawed: Perquintill, /// The current thaw period's beginning. pub last_period: BlockNumber, + /// The total amount of funds on hold for receipts. This doesn't include the pot or funds + /// on hold for bids. + pub receipts_on_hold: Balance, } pub struct OnEmptyQueueTotals(sp_std::marker::PhantomData); @@ -440,24 +452,28 @@ pub mod pallet { /// The queue for the bid's duration is full and the amount bid is too low to get in /// through replacing an existing bid. BidTooLow, - /// Bond index is unknown. - Unknown, + /// Receipt index is unknown. + UnknownReceipt, /// Not the owner of the receipt. NotOwner, /// Bond not yet at expiry date. NotExpired, /// The given bid for retraction is not found. - NotFound, + UnknownBid, /// The portion supplied is beyond the value of the receipt. - TooMuch, + PortionTooBig, /// Not enough funds are held to pay out. Unfunded, /// There are enough funds for what is required. - Funded, + AlreadyFunded, /// The thaw throttle has been reached for this period. Throttled, /// The operation would result in a receipt worth an insignficant value. MakesDust, + /// The receipt is already communal. + AlreadyCommunal, + /// The receipt is already private. + AlreadyPrivate, } pub(crate) struct WeightCounter { @@ -539,13 +555,17 @@ pub mod pallet { |q| -> Result<(u32, BalanceOf), DispatchError> { let queue_full = q.len() == T::MaxQueueLen::get() as usize; ensure!(!queue_full || q[0].amount < amount, Error::::BidTooLow); - T::Currency::reserve(&who, amount)?; + T::Currency::reserve_named(&T::ReserveId::get(), &who, amount)?; // queue is let mut bid = Bid { amount, who: who.clone() }; let net = if queue_full { sp_std::mem::swap(&mut q[0], &mut bid); - let _ = T::Currency::unreserve(&bid.who, bid.amount); + let _ = T::Currency::unreserve_named( + &T::ReserveId::get(), + &bid.who, + bid.amount, + ); Self::deposit_event(Event::::BidDropped { who: bid.who, amount: bid.amount, @@ -597,7 +617,7 @@ pub mod pallet { let bid = Bid { amount, who }; let new_len = Queues::::try_mutate(duration, |q| -> Result { - let pos = q.iter().position(|i| i == &bid).ok_or(Error::::NotFound)?; + let pos = q.iter().position(|i| i == &bid).ok_or(Error::::UnknownBid)?; q.remove(pos); Ok(q.len() as u32) })?; @@ -608,7 +628,7 @@ pub mod pallet { qs[queue_index].1.saturating_reduce(bid.amount); }); - T::Currency::unreserve(&bid.who, bid.amount); + T::Currency::unreserve_named(&T::ReserveId::get(), &bid.who, bid.amount); Self::deposit_event(Event::BidRetracted { who: bid.who, amount: bid.amount, duration }); Ok(()) @@ -625,7 +645,7 @@ pub mod pallet { let our_account = Self::account_id(); let issuance = Self::issuance_with(&our_account, &summary); let deficit = issuance.required.saturating_sub(issuance.holdings); - ensure!(!deficit.is_zero(), Error::::Funded); + ensure!(!deficit.is_zero(), Error::::AlreadyFunded); T::Deficit::on_unbalanced(T::Currency::deposit_creating(&our_account, deficit)); Self::deposit_event(Event::::Funded { deficit }); Ok(()) @@ -640,27 +660,28 @@ pub mod pallet { /// - `portion`: If `Some`, then only the given portion of the receipt should be thawed. If /// `None`, then all of it should be. #[pallet::call_index(3)] - #[pallet::weight(T::WeightInfo::thaw())] - pub fn thaw( + #[pallet::weight(T::WeightInfo::thaw_private())] + pub fn thaw_private( origin: OriginFor, #[pallet::compact] index: ReceiptIndex, - portion: Option<>::Balance>, + maybe_proportion: Option, ) -> DispatchResult { let who = ensure_signed(origin)?; // Look for `index` let mut receipt: ReceiptRecordOf = - Receipts::::get(index).ok_or(Error::::Unknown)?; + Receipts::::get(index).ok_or(Error::::UnknownReceipt)?; // If found, check the owner is `who`. - ensure!(receipt.who == who, Error::::NotOwner); + let (owner, mut on_hold) = receipt.owner.ok_or(Error::::AlreadyCommunal)?; + ensure!(owner == who, Error::::NotOwner); + let now = frame_system::Pallet::::block_number(); ensure!(now >= receipt.expiry, Error::::NotExpired); let mut summary: SummaryRecordOf = Summary::::get(); - let proportion = if let Some(counterpart) = portion { - let proportion = T::CounterpartAmount::convert_back(counterpart); - ensure!(proportion <= receipt.proportion, Error::::TooMuch); + let proportion = if let Some(proportion) = maybe_proportion { + ensure!(proportion <= receipt.proportion, Error::::PortionTooBig); let remaining = receipt.proportion.saturating_sub(proportion); ensure!( remaining.is_zero() || remaining >= T::MinReceipt::get(), @@ -679,8 +700,6 @@ pub mod pallet { summary.thawed.saturating_accrue(proportion); ensure!(summary.thawed <= throttle, Error::::Throttled); - T::Counterpart::burn_from(&who, T::CounterpartAmount::convert(proportion))?; - // Multiply the proportion it is by the total issued. let our_account = Self::account_id(); let effective_issuance = Self::issuance_with(&our_account, &summary).effective; @@ -689,13 +708,55 @@ pub mod pallet { receipt.proportion.saturating_reduce(proportion); summary.proportion_owed.saturating_reduce(proportion); - T::Currency::transfer(&our_account, &who, amount, AllowDeath) - .map_err(|_| Error::::Unfunded)?; - let dropped = receipt.proportion.is_zero(); + + if amount > on_hold { + T::Currency::unreserve_named(&T::ReserveId::get(), &who, on_hold); + let deficit = amount - on_hold; + // Try to transfer deficit from pot to receipt owner. + summary.receipts_on_hold.saturating_reduce(on_hold); + on_hold = Zero::zero(); + T::Currency::transfer(&our_account, &who, deficit, AllowDeath) + .map_err(|_| Error::::Unfunded)?; + } else { + T::Currency::unreserve_named(&T::ReserveId::get(), &who, amount); + on_hold.saturating_reduce(amount); + summary.receipts_on_hold.saturating_reduce(amount); + if dropped && !on_hold.is_zero() { + // Reclaim any remainder: + // Transfer `excess` to the pot if we have now fully compensated for the + // receipt. + // + // This will legitimately fail if there is no pot account in existance. + // There's nothing we can do about this so we just swallow the error. + // This code is not ideal and could fail in the second phase leaving + // the system in an invalid state. It can be fixed properly with the + // new API in https://github.com/paritytech/substrate/pull/12951 + // + // Below is what it should look like then: + // let _ = T::Currency::repatriate_reserved_named( + // &T::ReserveId::get(), + // &who, + // &our_account, + // excess, + // BalanceStatus::Free, + // ).defensive(); + T::Currency::unreserve_named(&T::ReserveId::get(), &who, on_hold); + // It could theoretically be locked, so really we should be using a more + // forceful variant. But the alternative `repatriate_reserved_named` will + // fail if the destination account doesn't exist. This should be fixed when + // we move to the `fungible::*` traits, which should include a force + // transfer function to transfer the reserved balance into free balance in + // the destination regardless of locks and create it if it doesn't exist. + let _ = T::Currency::transfer(&who, &Self::account_id(), on_hold, AllowDeath); + summary.receipts_on_hold.saturating_reduce(on_hold); + } + } + if dropped { Receipts::::remove(index); } else { + receipt.owner = Some((owner, on_hold)); Receipts::::insert(index, &receipt); } Summary::::put(&summary); @@ -704,20 +765,159 @@ pub mod pallet { Ok(()) } + + /// Reduce or remove an outstanding receipt, placing the according proportion of funds into + /// the account of the owner. + /// + /// - `origin`: Must be Signed and the account must be the owner of the fungible counterpart + /// for receipt `index`. + /// - `index`: The index of the receipt. + #[pallet::call_index(4)] + #[pallet::weight(T::WeightInfo::thaw_communal())] + pub fn thaw_communal( + origin: OriginFor, + #[pallet::compact] index: ReceiptIndex, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + // Look for `index` + let receipt: ReceiptRecordOf = + Receipts::::get(index).ok_or(Error::::UnknownReceipt)?; + // If found, check it is actually communal. + ensure!(receipt.owner.is_none(), Error::::NotOwner); + let now = frame_system::Pallet::::block_number(); + ensure!(now >= receipt.expiry, Error::::NotExpired); + + let mut summary: SummaryRecordOf = Summary::::get(); + + let (throttle, throttle_period) = T::ThawThrottle::get(); + if now.saturating_sub(summary.last_period) >= throttle_period { + summary.thawed = Zero::zero(); + summary.last_period = now; + } + summary.thawed.saturating_accrue(receipt.proportion); + ensure!(summary.thawed <= throttle, Error::::Throttled); + + T::Counterpart::burn_from(&who, T::CounterpartAmount::convert(receipt.proportion))?; + + // Multiply the proportion it is by the total issued. + let our_account = Self::account_id(); + let effective_issuance = Self::issuance_with(&our_account, &summary).effective; + let amount = receipt.proportion * effective_issuance; + + summary.proportion_owed.saturating_reduce(receipt.proportion); + + // Try to transfer amount owed from pot to receipt owner. + T::Currency::transfer(&our_account, &who, amount, AllowDeath) + .map_err(|_| Error::::Unfunded)?; + + Receipts::::remove(index); + Summary::::put(&summary); + + let e = + Event::Thawed { index, who, amount, proportion: receipt.proportion, dropped: true }; + Self::deposit_event(e); + + Ok(()) + } + + /// Make a private receipt communal and create fungible counterparts for its owner. + #[pallet::call_index(5)] + #[pallet::weight(T::WeightInfo::communify())] + pub fn communify( + origin: OriginFor, + #[pallet::compact] index: ReceiptIndex, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + // Look for `index` + let mut receipt: ReceiptRecordOf = + Receipts::::get(index).ok_or(Error::::UnknownReceipt)?; + + // Check it's not already communal and make it so. + let (owner, on_hold) = receipt.owner.take().ok_or(Error::::AlreadyCommunal)?; + + // If found, check the owner is `who`. + ensure!(owner == who, Error::::NotOwner); + + // Unreserve and transfer the funds to the pot. + T::Currency::unreserve_named(&T::ReserveId::get(), &who, on_hold); + // Transfer `excess` to the pot if we have now fully compensated for the receipt. + T::Currency::transfer(&who, &Self::account_id(), on_hold, AllowDeath) + .map_err(|_| Error::::Unfunded)?; + // TODO #12951: ^^^ The above should be done in a single operation `transfer_on_hold`. + + // Record that we've moved the amount reserved. + let mut summary: SummaryRecordOf = Summary::::get(); + summary.receipts_on_hold.saturating_reduce(on_hold); + Summary::::put(&summary); + Receipts::::insert(index, &receipt); + + // Mint fungibles. + let fung_eq = T::CounterpartAmount::convert(receipt.proportion); + let _ = T::Counterpart::mint_into(&who, fung_eq).defensive(); + + Ok(()) + } + + /// Make a communal receipt private and burn fungible counterparts from its owner. + #[pallet::call_index(6)] + #[pallet::weight(T::WeightInfo::privatize())] + pub fn privatize( + origin: OriginFor, + #[pallet::compact] index: ReceiptIndex, + ) -> DispatchResult { + let who = ensure_signed(origin)?; + + // Look for `index` + let mut receipt: ReceiptRecordOf = + Receipts::::get(index).ok_or(Error::::UnknownReceipt)?; + + // If found, check there is no owner. + ensure!(receipt.owner.is_none(), Error::::AlreadyPrivate); + + // Multiply the proportion it is by the total issued. + let mut summary: SummaryRecordOf = Summary::::get(); + let our_account = Self::account_id(); + let effective_issuance = Self::issuance_with(&our_account, &summary).effective; + let max_amount = receipt.proportion * effective_issuance; + // Avoid trying to place more in the account's reserve than we have available in the pot + let amount = max_amount.min(T::Currency::free_balance(&our_account)); + + // Burn fungible counterparts. + T::Counterpart::burn_from(&who, T::CounterpartAmount::convert(receipt.proportion))?; + + // Transfer the funds from the pot to the owner and reserve + T::Currency::transfer(&Self::account_id(), &who, amount, AllowDeath) + .map_err(|_| Error::::Unfunded)?; + T::Currency::reserve_named(&T::ReserveId::get(), &who, amount)?; + // TODO: ^^^ The above should be done in a single operation `transfer_and_hold`. + + // Record that we've moved the amount reserved. + summary.receipts_on_hold.saturating_accrue(amount); + + receipt.owner = Some((who, amount)); + + Summary::::put(&summary); + Receipts::::insert(index, &receipt); + + Ok(()) + } } /// Issuance information returned by `issuance()`. - #[derive(RuntimeDebug)] + #[derive(Debug)] pub struct IssuanceInfo { - /// The balance held in reserve by this pallet instance. + /// The balance held by this pallet instance together with the balances on hold across + /// all receipt-owning accounts. pub holdings: Balance, /// The (non-ignored) issuance in the system, not including this pallet's account. pub other: Balance, /// The effective total issuance, hypothetically if all outstanding receipts were thawed at /// present. pub effective: Balance, - /// The amount needed to be the pallet instance's account in case all outstanding receipts - /// were thawed at present. + /// The amount needed to be accessible to this pallet in case all outstanding receipts were + /// thawed at present. If it is more than `holdings`, then the pallet will need funding. pub required: Balance, } @@ -725,7 +925,7 @@ pub mod pallet { type ItemId = ReceiptIndex; fn owner(item: &ReceiptIndex) -> Option { - Receipts::::get(item).map(|r| r.who) + Receipts::::get(item).and_then(|r| r.owner).map(|(who, _)| who) } fn attribute(item: &Self::ItemId, key: &[u8]) -> Option> { @@ -733,6 +933,8 @@ pub mod pallet { match key { b"proportion" => Some(item.proportion.encode()), b"expiry" => Some(item.expiry.encode()), + b"owner" => item.owner.as_ref().map(|x| x.0.encode()), + b"on_hold" => item.owner.as_ref().map(|x| x.1.encode()), _ => None, } } @@ -741,12 +943,28 @@ pub mod pallet { impl NonfungibleTransfer for Pallet { fn transfer(index: &ReceiptIndex, destination: &T::AccountId) -> DispatchResult { let mut item = Receipts::::get(index).ok_or(TokenError::UnknownAsset)?; - let from = item.who; - item.who = destination.clone(); + let (owner, on_hold) = item.owner.take().ok_or(Error::::AlreadyCommunal)?; + + // TODO: This should all be replaced by a single call `transfer_held`. + let shortfall = T::Currency::unreserve_named(&T::ReserveId::get(), &owner, on_hold); + if !shortfall.is_zero() { + let _ = + T::Currency::reserve_named(&T::ReserveId::get(), &owner, on_hold - shortfall); + return Err(TokenError::NoFunds.into()) + } + if let Err(e) = T::Currency::transfer(&owner, destination, on_hold, AllowDeath) { + let _ = T::Currency::reserve_named(&T::ReserveId::get(), &owner, on_hold); + return Err(e) + } + // This can never fail, and if it somehow does, then we can't handle this gracefully. + let _ = + T::Currency::reserve_named(&T::ReserveId::get(), destination, on_hold).defensive(); + + item.owner = Some((destination.clone(), on_hold)); Receipts::::insert(&index, &item); Pallet::::deposit_event(Event::::Transferred { - from, - to: item.who, + from: owner, + to: destination.clone(), index: *index, }); Ok(()) @@ -781,7 +999,8 @@ pub mod pallet { ) -> IssuanceInfo> { let total_issuance = T::Currency::total_issuance().saturating_sub(T::IgnoredIssuance::get()); - let holdings = T::Currency::free_balance(our_account); + let holdings = + T::Currency::free_balance(our_account).saturating_add(summary.receipts_on_hold); let other = total_issuance.saturating_sub(holdings); let effective = summary.proportion_owed.left_from_one().saturating_reciprocal_mul(other); @@ -893,7 +1112,7 @@ pub mod pallet { pub(crate) fn process_bid( mut bid: BidOf, expiry: T::BlockNumber, - our_account: &T::AccountId, + _our_account: &T::AccountId, issuance: &IssuanceInfo>, remaining: &mut BalanceOf, queue_amount: &mut BalanceOf, @@ -906,10 +1125,8 @@ pub mod pallet { } else { None }; - let amount = bid.amount.saturating_sub(T::Currency::unreserve(&bid.who, bid.amount)); - if T::Currency::transfer(&bid.who, &our_account, amount, AllowDeath).is_err() { - return result - } + let amount = bid.amount; + summary.receipts_on_hold.saturating_accrue(amount); // Can never overflow due to block above. remaining.saturating_reduce(amount); @@ -928,12 +1145,9 @@ pub mod pallet { let e = Event::Issued { index, expiry, who: who.clone(), amount, proportion }; Self::deposit_event(e); - let receipt = ReceiptRecord { proportion, who: who.clone(), expiry }; + let receipt = ReceiptRecord { proportion, owner: Some((who, amount)), expiry }; Receipts::::insert(index, receipt); - // issue the fungible counterpart - let fung_eq = T::CounterpartAmount::convert(proportion); - let _ = T::Counterpart::mint_into(&who, fung_eq).defensive(); result } } diff --git a/frame/nis/src/mock.rs b/frame/nis/src/mock.rs index ebe073d683309..585ed2d8b278f 100644 --- a/frame/nis/src/mock.rs +++ b/frame/nis/src/mock.rs @@ -84,7 +84,7 @@ impl pallet_balances::Config for Test { type AccountStore = System; type WeightInfo = (); type MaxLocks = (); - type MaxReserves = (); + type MaxReserves = ConstU32<1>; type ReserveIdentifier = [u8; 8]; } @@ -112,6 +112,7 @@ parameter_types! { pub const MinReceipt: Perquintill = Perquintill::from_percent(1); pub const ThawThrottle: (Perquintill, u64) = (Perquintill::from_percent(25), 5); pub static MaxIntakeWeight: Weight = Weight::from_ref_time(2_000_000_000_000); + pub const ReserveId: [u8; 8] = *b"py/nis "; } ord_parameter_types! { @@ -139,6 +140,7 @@ impl pallet_nis::Config for Test { type MaxIntakeWeight = MaxIntakeWeight; type MinReceipt = MinReceipt; type ThawThrottle = ThawThrottle; + type ReserveId = ReserveId; } // This function basically just builds a genesis storage key/value store according to diff --git a/frame/nis/src/tests.rs b/frame/nis/src/tests.rs index f0c45cc80b0e5..b5feb4df16aab 100644 --- a/frame/nis/src/tests.rs +++ b/frame/nis/src/tests.rs @@ -34,8 +34,16 @@ fn pot() -> u64 { Balances::free_balance(&Nis::account_id()) } +fn holdings() -> u64 { + Nis::issuance().holdings +} + +fn signed(who: u64) -> RuntimeOrigin { + RuntimeOrigin::signed(who) +} + fn enlarge(amount: u64, max_bids: u32) { - let summary: SummaryRecord = Summary::::get(); + let summary: SummaryRecord = Summary::::get(); let increase_in_proportion_owed = Perquintill::from_rational(amount, Nis::issuance().effective); let target = summary.proportion_owed.saturating_add(increase_in_proportion_owed); Nis::process_queues(target, u32::max_value(), max_bids, &mut WeightCounter::unlimited()); @@ -55,7 +63,8 @@ fn basic_setup_works() { proportion_owed: Perquintill::zero(), index: 0, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 0, } ); }); @@ -65,16 +74,13 @@ fn basic_setup_works() { fn place_bid_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_noop!(Nis::place_bid(RuntimeOrigin::signed(1), 1, 2), Error::::AmountTooSmall); + assert_noop!(Nis::place_bid(signed(1), 1, 2), Error::::AmountTooSmall); assert_noop!( - Nis::place_bid(RuntimeOrigin::signed(1), 101, 2), + Nis::place_bid(signed(1), 101, 2), BalancesError::::InsufficientBalance ); - assert_noop!( - Nis::place_bid(RuntimeOrigin::signed(1), 10, 4), - Error::::DurationTooBig - ); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); + assert_noop!(Nis::place_bid(signed(1), 10, 4), Error::::DurationTooBig); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); assert_eq!(Balances::reserved_balance(1), 10); assert_eq!(Queues::::get(2), vec![Bid { amount: 10, who: 1 }]); assert_eq!(QueueTotals::::get(), vec![(0, 0), (1, 10), (0, 0)]); @@ -85,16 +91,16 @@ fn place_bid_works() { fn place_bid_queuing_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 20, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 5, 2)); - assert_noop!(Nis::place_bid(RuntimeOrigin::signed(1), 5, 2), Error::::BidTooLow); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 15, 2)); + assert_ok!(Nis::place_bid(signed(1), 20, 2)); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); + assert_ok!(Nis::place_bid(signed(1), 5, 2)); + assert_noop!(Nis::place_bid(signed(1), 5, 2), Error::::BidTooLow); + assert_ok!(Nis::place_bid(signed(1), 15, 2)); assert_eq!(Balances::reserved_balance(1), 45); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 25, 2)); + assert_ok!(Nis::place_bid(signed(1), 25, 2)); assert_eq!(Balances::reserved_balance(1), 60); - assert_noop!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2), Error::::BidTooLow); + assert_noop!(Nis::place_bid(signed(1), 10, 2), Error::::BidTooLow); assert_eq!( Queues::::get(2), vec![ @@ -111,11 +117,11 @@ fn place_bid_queuing_works() { fn place_bid_fails_when_queue_full() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 10, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(3), 10, 2)); - assert_noop!(Nis::place_bid(RuntimeOrigin::signed(4), 10, 2), Error::::BidTooLow); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(4), 10, 3)); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); + assert_ok!(Nis::place_bid(signed(2), 10, 2)); + assert_ok!(Nis::place_bid(signed(3), 10, 2)); + assert_noop!(Nis::place_bid(signed(4), 10, 2), Error::::BidTooLow); + assert_ok!(Nis::place_bid(signed(4), 10, 3)); }); } @@ -123,11 +129,11 @@ fn place_bid_fails_when_queue_full() { fn multiple_place_bids_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 3)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 10, 2)); + assert_ok!(Nis::place_bid(signed(1), 10, 1)); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); + assert_ok!(Nis::place_bid(signed(1), 10, 3)); + assert_ok!(Nis::place_bid(signed(2), 10, 2)); assert_eq!(Balances::reserved_balance(1), 40); assert_eq!(Balances::reserved_balance(2), 10); @@ -149,9 +155,9 @@ fn multiple_place_bids_works() { fn retract_single_item_queue_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); - assert_ok!(Nis::retract_bid(RuntimeOrigin::signed(1), 10, 1)); + assert_ok!(Nis::place_bid(signed(1), 10, 1)); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); + assert_ok!(Nis::retract_bid(signed(1), 10, 1)); assert_eq!(Balances::reserved_balance(1), 10); assert_eq!(Queues::::get(1), vec![]); @@ -164,12 +170,12 @@ fn retract_single_item_queue_works() { fn retract_with_other_and_duplicate_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 10, 2)); + assert_ok!(Nis::place_bid(signed(1), 10, 1)); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); + assert_ok!(Nis::place_bid(signed(1), 10, 2)); + assert_ok!(Nis::place_bid(signed(2), 10, 2)); - assert_ok!(Nis::retract_bid(RuntimeOrigin::signed(1), 10, 2)); + assert_ok!(Nis::retract_bid(signed(1), 10, 2)); assert_eq!(Balances::reserved_balance(1), 20); assert_eq!(Balances::reserved_balance(2), 10); assert_eq!(Queues::::get(1), vec![Bid { amount: 10, who: 1 },]); @@ -185,11 +191,11 @@ fn retract_with_other_and_duplicate_works() { fn retract_non_existent_item_fails() { new_test_ext().execute_with(|| { run_to_block(1); - assert_noop!(Nis::retract_bid(RuntimeOrigin::signed(1), 10, 1), Error::::NotFound); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 10, 1)); - assert_noop!(Nis::retract_bid(RuntimeOrigin::signed(1), 20, 1), Error::::NotFound); - assert_noop!(Nis::retract_bid(RuntimeOrigin::signed(1), 10, 2), Error::::NotFound); - assert_noop!(Nis::retract_bid(RuntimeOrigin::signed(2), 10, 1), Error::::NotFound); + assert_noop!(Nis::retract_bid(signed(1), 10, 1), Error::::UnknownBid); + assert_ok!(Nis::place_bid(signed(1), 10, 1)); + assert_noop!(Nis::retract_bid(signed(1), 20, 1), Error::::UnknownBid); + assert_noop!(Nis::retract_bid(signed(1), 10, 2), Error::::UnknownBid); + assert_noop!(Nis::retract_bid(signed(2), 10, 1), Error::::UnknownBid); }); } @@ -197,14 +203,14 @@ fn retract_non_existent_item_fails() { fn basic_enlarge_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 40, 2)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(2), 40, 2)); enlarge(40, 2); // Takes 2/2, then stopped because it reaches its max amount assert_eq!(Balances::reserved_balance(1), 40); - assert_eq!(Balances::reserved_balance(2), 0); - assert_eq!(pot(), 40); + assert_eq!(Balances::reserved_balance(2), 40); + assert_eq!(holdings(), 40); assert_eq!(Queues::::get(1), vec![Bid { amount: 40, who: 1 }]); assert_eq!(Queues::::get(2), vec![]); @@ -216,12 +222,17 @@ fn basic_enlarge_works() { proportion_owed: Perquintill::from_percent(10), index: 1, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 40, } ); assert_eq!( Receipts::::get(0).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 2, expiry: 7 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((2, 40)), + expiry: 7 + } ); }); } @@ -230,10 +241,10 @@ fn basic_enlarge_works() { fn enlarge_respects_bids_limit() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 40, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(3), 40, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(4), 40, 3)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(2), 40, 2)); + assert_ok!(Nis::place_bid(signed(3), 40, 2)); + assert_ok!(Nis::place_bid(signed(4), 40, 3)); enlarge(100, 2); // Should have taken 4/3 and 2/2, then stopped because it's only allowed 2. @@ -244,11 +255,19 @@ fn enlarge_respects_bids_limit() { assert_eq!( Receipts::::get(0).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 4, expiry: 10 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((4, 40)), + expiry: 10 + } ); assert_eq!( Receipts::::get(1).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 2, expiry: 7 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((2, 40)), + expiry: 7 + } ); assert_eq!( Summary::::get(), @@ -256,7 +275,8 @@ fn enlarge_respects_bids_limit() { proportion_owed: Perquintill::from_percent(20), index: 2, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 80, } ); }); @@ -266,7 +286,7 @@ fn enlarge_respects_bids_limit() { fn enlarge_respects_amount_limit_and_will_split() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 80, 1)); + assert_ok!(Nis::place_bid(signed(1), 80, 1)); enlarge(40, 2); // Takes 2/2, then stopped because it reaches its max amount @@ -275,7 +295,11 @@ fn enlarge_respects_amount_limit_and_will_split() { assert_eq!( Receipts::::get(0).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 1, expiry: 4 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((1, 40)), + expiry: 4 + } ); assert_eq!( Summary::::get(), @@ -283,7 +307,8 @@ fn enlarge_respects_amount_limit_and_will_split() { proportion_owed: Perquintill::from_percent(10), index: 1, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 40, } ); }); @@ -293,25 +318,25 @@ fn enlarge_respects_amount_limit_and_will_split() { fn basic_thaw_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); assert_eq!(Nis::issuance().effective, 400); assert_eq!(Balances::free_balance(1), 60); assert_eq!(Balances::reserved_balance(1), 40); - assert_eq!(pot(), 0); + assert_eq!(holdings(), 0); enlarge(40, 1); assert_eq!(Nis::issuance().effective, 400); assert_eq!(Balances::free_balance(1), 60); - assert_eq!(Balances::reserved_balance(1), 0); - assert_eq!(pot(), 40); + assert_eq!(Balances::reserved_balance(1), 40); + assert_eq!(holdings(), 40); run_to_block(3); - assert_noop!(Nis::thaw(RuntimeOrigin::signed(1), 0, None), Error::::NotExpired); + assert_noop!(Nis::thaw_private(signed(1), 0, None), Error::::NotExpired); run_to_block(4); - assert_noop!(Nis::thaw(RuntimeOrigin::signed(1), 1, None), Error::::Unknown); - assert_noop!(Nis::thaw(RuntimeOrigin::signed(2), 0, None), Error::::NotOwner); + assert_noop!(Nis::thaw_private(signed(1), 1, None), Error::::UnknownReceipt); + assert_noop!(Nis::thaw_private(signed(2), 0, None), Error::::NotOwner); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, None)); + assert_ok!(Nis::thaw_private(signed(1), 0, None)); assert_eq!(NisBalances::free_balance(1), 0); assert_eq!(Nis::typed_attribute::<_, Perquintill>(&0, b"proportion"), None); assert_eq!(Nis::issuance().effective, 400); @@ -323,7 +348,8 @@ fn basic_thaw_works() { proportion_owed: Perquintill::zero(), index: 1, last_period: 0, - thawed: Perquintill::from_percent(10) + thawed: Perquintill::from_percent(10), + receipts_on_hold: 0, } ); assert_eq!(Receipts::::get(0), None); @@ -334,18 +360,16 @@ fn basic_thaw_works() { fn partial_thaw_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 80, 1)); + assert_ok!(Nis::place_bid(signed(1), 80, 1)); enlarge(80, 1); - assert_eq!(pot(), 80); + assert_eq!(holdings(), 80); run_to_block(4); - assert_noop!( - Nis::thaw(RuntimeOrigin::signed(1), 0, Some(4_100_000)), - Error::::MakesDust - ); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, Some(1_050_000))); + let prop = Perquintill::from_rational(4_100_000, 21_000_000u64); + assert_noop!(Nis::thaw_private(signed(1), 0, Some(prop)), Error::::MakesDust); + let prop = Perquintill::from_rational(1_050_000, 21_000_000u64); + assert_ok!(Nis::thaw_private(signed(1), 0, Some(prop))); - assert_eq!(NisBalances::free_balance(1), 3_150_000); assert_eq!( Nis::typed_attribute::<_, Perquintill>(&0, b"proportion"), Some(Perquintill::from_rational(3_150_000u64, 21_000_000u64)), @@ -353,9 +377,9 @@ fn partial_thaw_works() { assert_eq!(Nis::issuance().effective, 400); assert_eq!(Balances::free_balance(1), 40); - assert_eq!(pot(), 60); + assert_eq!(holdings(), 60); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, None)); + assert_ok!(Nis::thaw_private(signed(1), 0, None)); assert_eq!(Nis::issuance().effective, 400); assert_eq!(Balances::free_balance(1), 100); @@ -367,7 +391,8 @@ fn partial_thaw_works() { proportion_owed: Perquintill::zero(), index: 1, last_period: 0, - thawed: Perquintill::from_percent(20) + thawed: Perquintill::from_percent(20), + receipts_on_hold: 0, } ); assert_eq!(Receipts::::get(0), None); @@ -378,35 +403,144 @@ fn partial_thaw_works() { fn thaw_respects_transfers() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); enlarge(40, 1); run_to_block(4); assert_eq!(Nis::owner(&0), Some(1)); + assert_eq!(Balances::reserved_balance(&1), 40); + assert_eq!(Balances::reserved_balance(&2), 0); assert_ok!(Nis::transfer(&0, &2)); + assert_eq!(Balances::reserved_balance(&1), 0); + assert_eq!(Balances::reserved_balance(&2), 40); // Transfering the receipt... - assert_noop!(Nis::thaw(RuntimeOrigin::signed(1), 0, None), Error::::NotOwner); - // ...can't be thawed due to missing counterpart - assert_noop!(Nis::thaw(RuntimeOrigin::signed(2), 0, None), TokenError::NoFunds); + assert_noop!(Nis::thaw_private(signed(1), 0, None), Error::::NotOwner); - // Transfer the counterpart also... - assert_ok!(NisBalances::transfer(RuntimeOrigin::signed(1), 2, 2100000)); // ...and thawing is possible. - assert_ok!(Nis::thaw(RuntimeOrigin::signed(2), 0, None)); + assert_ok!(Nis::thaw_private(signed(2), 0, None)); - assert_eq!(Balances::free_balance(2), 140); - assert_eq!(Balances::free_balance(1), 60); + assert_eq!(Balances::total_balance(&2), 140); + assert_eq!(Balances::total_balance(&1), 60); + }); +} + +#[test] +fn communify_works() { + new_test_ext().execute_with(|| { + run_to_block(1); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + enlarge(40, 1); + run_to_block(4); + + assert_eq!(Nis::owner(&0), Some(1)); + assert_eq!(Balances::reserved_balance(&1), 40); + assert_eq!(pot(), 0); + assert_eq!(NisBalances::free_balance(&1), 0); + assert_ok!(Nis::communify(signed(1), 0)); + assert_eq!(Nis::owner(&0), None); + assert_eq!(Balances::reserved_balance(&1), 0); + assert_eq!(pot(), 40); + // We now have fungibles. + assert_eq!(NisBalances::free_balance(&1), 2_100_000); + + // Can't transfer the NFT or thaw it as a private. + assert_noop!(Nis::thaw_private(signed(1), 0, None), Error::::AlreadyCommunal); + assert_noop!(Nis::transfer(&0, &2), Error::::AlreadyCommunal); + // Communal thawing would be possible, except it's the wrong receipt. + assert_noop!(Nis::thaw_communal(signed(1), 1), Error::::UnknownReceipt); + + // Transfer some of the fungibles away. + assert_ok!(NisBalances::transfer(signed(1), 2, 100_000)); + assert_eq!(NisBalances::free_balance(&1), 2_000_000); + assert_eq!(NisBalances::free_balance(&2), 100_000); + + // Communal thawing with the correct index is not possible now. + assert_noop!(Nis::thaw_communal(signed(1), 0), TokenError::NoFunds); + assert_noop!(Nis::thaw_communal(signed(2), 0), TokenError::NoFunds); + + // Transfer the rest to 2... + assert_ok!(NisBalances::transfer(signed(1), 2, 2_000_000)); + assert_eq!(NisBalances::free_balance(&1), 0); + assert_eq!(NisBalances::free_balance(&2), 2_100_000); + + // ...and thawing becomes possible. + assert_ok!(Nis::thaw_communal(signed(2), 0)); + assert_eq!(NisBalances::free_balance(&1), 0); + assert_eq!(NisBalances::free_balance(&2), 0); + assert_eq!(pot(), 0); + assert_eq!(Balances::total_balance(&1), 60); + assert_eq!(Balances::total_balance(&2), 140); + + assert_noop!(Nis::thaw_communal(signed(2), 0), Error::::UnknownReceipt); + }); +} + +#[test] +fn privatize_works() { + new_test_ext().execute_with(|| { + run_to_block(1); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + enlarge(40, 1); + run_to_block(4); + assert_noop!(Nis::privatize(signed(2), 0), Error::::AlreadyPrivate); + assert_ok!(Nis::communify(signed(1), 0)); + + // Transfer the fungibles to #2 + assert_ok!(NisBalances::transfer(signed(1), 2, 2_100_000)); + assert_noop!(Nis::privatize(signed(1), 0), TokenError::NoFunds); + + // Privatize + assert_ok!(Nis::privatize(signed(2), 0)); + assert_noop!(Nis::privatize(signed(2), 0), Error::::AlreadyPrivate); + assert_eq!(NisBalances::free_balance(&2), 0); + assert_eq!(Nis::owner(&0), Some(2)); + assert_eq!(Balances::reserved_balance(&2), 40); + assert_eq!(pot(), 0); }); } #[test] -fn thaw_when_issuance_higher_works() { +fn privatize_and_thaw_with_another_receipt_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 100, 1)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(2), 40, 1)); + enlarge(80, 2); + run_to_block(4); + + assert_ok!(Nis::communify(signed(1), 0)); + assert_ok!(Nis::communify(signed(2), 1)); + + // Transfer half of fungibles to #3 from each of #1 and #2, and the other half from #2 to #4 + assert_ok!(NisBalances::transfer(signed(1), 3, 1_050_000)); + assert_ok!(NisBalances::transfer(signed(2), 3, 1_050_000)); + assert_ok!(NisBalances::transfer(signed(2), 4, 1_050_000)); + + // #3 privatizes, partially thaws, then re-communifies with #0, then transfers the fungibles + // to #2 + assert_ok!(Nis::privatize(signed(3), 0)); + assert_ok!(Nis::thaw_private(signed(3), 0, Some(Perquintill::from_percent(5)))); + assert_ok!(Nis::communify(signed(3), 0)); + assert_ok!(NisBalances::transfer(signed(3), 1, 1_050_000)); + + // #1 now has enough to thaw using receipt 1 + assert_ok!(Nis::thaw_communal(signed(1), 1)); + + // #4 now has enough to thaw using receipt 0 + assert_ok!(Nis::thaw_communal(signed(4), 0)); + }); +} + +#[test] +fn communal_thaw_when_issuance_higher_works() { + new_test_ext().execute_with(|| { + run_to_block(1); + assert_ok!(Nis::place_bid(signed(1), 100, 1)); enlarge(100, 1); + assert_ok!(Nis::communify(signed(1), 0)); + assert_eq!(NisBalances::free_balance(1), 5_250_000); // (25% of 21m) // Everybody else's balances goes up by 50% @@ -417,19 +551,45 @@ fn thaw_when_issuance_higher_works() { run_to_block(4); // Unfunded initially... - assert_noop!(Nis::thaw(RuntimeOrigin::signed(1), 0, None), Error::::Unfunded); + assert_noop!(Nis::thaw_communal(signed(1), 0), Error::::Unfunded); // ...so we fund. - assert_ok!(Nis::fund_deficit(RuntimeOrigin::signed(1))); + assert_ok!(Nis::fund_deficit(signed(1))); - // Transfer counterpart away... - assert_ok!(NisBalances::transfer(RuntimeOrigin::signed(1), 2, 250_000)); + // Transfer counterparts away... + assert_ok!(NisBalances::transfer(signed(1), 2, 250_000)); // ...and it's not thawable. - assert_noop!(Nis::thaw(RuntimeOrigin::signed(1), 0, None), TokenError::NoFunds); + assert_noop!(Nis::thaw_communal(signed(1), 0), TokenError::NoFunds); - // Transfer counterpart back... - assert_ok!(NisBalances::transfer(RuntimeOrigin::signed(2), 1, 250_000)); + // Transfer counterparts back... + assert_ok!(NisBalances::transfer(signed(2), 1, 250_000)); // ...and it is. - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, None)); + assert_ok!(Nis::thaw_communal(signed(1), 0)); + + assert_eq!(Balances::free_balance(1), 150); + assert_eq!(Balances::reserved_balance(1), 0); + }); +} + +#[test] +fn private_thaw_when_issuance_higher_works() { + new_test_ext().execute_with(|| { + run_to_block(1); + assert_ok!(Nis::place_bid(signed(1), 100, 1)); + enlarge(100, 1); + + // Everybody else's balances goes up by 50% + Balances::make_free_balance_be(&2, 150); + Balances::make_free_balance_be(&3, 150); + Balances::make_free_balance_be(&4, 150); + + run_to_block(4); + + // Unfunded initially... + assert_noop!(Nis::thaw_private(signed(1), 0, None), Error::::Unfunded); + // ...so we fund. + assert_ok!(Nis::fund_deficit(signed(1))); + + assert_ok!(Nis::thaw_private(signed(1), 0, None)); assert_eq!(Balances::free_balance(1), 150); assert_eq!(Balances::reserved_balance(1), 0); @@ -443,21 +603,21 @@ fn thaw_with_ignored_issuance_works() { // Give account zero some balance. Balances::make_free_balance_be(&0, 200); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 100, 1)); + assert_ok!(Nis::place_bid(signed(1), 100, 1)); enlarge(100, 1); // Account zero transfers 50 into everyone else's accounts. - assert_ok!(Balances::transfer(RuntimeOrigin::signed(0), 2, 50)); - assert_ok!(Balances::transfer(RuntimeOrigin::signed(0), 3, 50)); - assert_ok!(Balances::transfer(RuntimeOrigin::signed(0), 4, 50)); + assert_ok!(Balances::transfer(signed(0), 2, 50)); + assert_ok!(Balances::transfer(signed(0), 3, 50)); + assert_ok!(Balances::transfer(signed(0), 4, 50)); run_to_block(4); // Unfunded initially... - assert_noop!(Nis::thaw(RuntimeOrigin::signed(1), 0, None), Error::::Unfunded); + assert_noop!(Nis::thaw_private(signed(1), 0, None), Error::::Unfunded); // ...so we fund... - assert_ok!(Nis::fund_deficit(RuntimeOrigin::signed(1))); + assert_ok!(Nis::fund_deficit(signed(1))); // ...and then it's ok. - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, None)); + assert_ok!(Nis::thaw_private(signed(1), 0, None)); // Account zero changes have been ignored. assert_eq!(Balances::free_balance(1), 150); @@ -469,7 +629,7 @@ fn thaw_with_ignored_issuance_works() { fn thaw_when_issuance_lower_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 100, 1)); + assert_ok!(Nis::place_bid(signed(1), 100, 1)); enlarge(100, 1); // Everybody else's balances goes down by 25% @@ -478,7 +638,7 @@ fn thaw_when_issuance_lower_works() { Balances::make_free_balance_be(&4, 75); run_to_block(4); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, None)); + assert_ok!(Nis::thaw_private(signed(1), 0, None)); assert_eq!(Balances::free_balance(1), 75); assert_eq!(Balances::reserved_balance(1), 0); @@ -489,23 +649,23 @@ fn thaw_when_issuance_lower_works() { fn multiple_thaws_works() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 60, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 50, 1)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(1), 60, 1)); + assert_ok!(Nis::place_bid(signed(2), 50, 1)); enlarge(200, 3); // Double everyone's free balances. Balances::make_free_balance_be(&2, 100); Balances::make_free_balance_be(&3, 200); Balances::make_free_balance_be(&4, 200); - assert_ok!(Nis::fund_deficit(RuntimeOrigin::signed(1))); + assert_ok!(Nis::fund_deficit(signed(1))); run_to_block(4); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, None)); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 1, None)); - assert_noop!(Nis::thaw(RuntimeOrigin::signed(2), 2, None), Error::::Throttled); + assert_ok!(Nis::thaw_private(signed(1), 0, None)); + assert_ok!(Nis::thaw_private(signed(1), 1, None)); + assert_noop!(Nis::thaw_private(signed(2), 2, None), Error::::Throttled); run_to_block(5); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(2), 2, None)); + assert_ok!(Nis::thaw_private(signed(2), 2, None)); assert_eq!(Balances::free_balance(1), 200); assert_eq!(Balances::free_balance(2), 200); @@ -516,24 +676,24 @@ fn multiple_thaws_works() { fn multiple_thaws_works_in_alternative_thaw_order() { new_test_ext().execute_with(|| { run_to_block(1); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 60, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 50, 1)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(1), 60, 1)); + assert_ok!(Nis::place_bid(signed(2), 50, 1)); enlarge(200, 3); // Double everyone's free balances. Balances::make_free_balance_be(&2, 100); Balances::make_free_balance_be(&3, 200); Balances::make_free_balance_be(&4, 200); - assert_ok!(Nis::fund_deficit(RuntimeOrigin::signed(1))); + assert_ok!(Nis::fund_deficit(signed(1))); run_to_block(4); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(2), 2, None)); - assert_noop!(Nis::thaw(RuntimeOrigin::signed(1), 1, None), Error::::Throttled); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 0, None)); + assert_ok!(Nis::thaw_private(signed(2), 2, None)); + assert_noop!(Nis::thaw_private(signed(1), 1, None), Error::::Throttled); + assert_ok!(Nis::thaw_private(signed(1), 0, None)); run_to_block(5); - assert_ok!(Nis::thaw(RuntimeOrigin::signed(1), 1, None)); + assert_ok!(Nis::thaw_private(signed(1), 1, None)); assert_eq!(Balances::free_balance(1), 200); assert_eq!(Balances::free_balance(2), 200); @@ -548,11 +708,11 @@ fn enlargement_to_target_works() { <() as WeightInfo>::process_queue() + (<() as WeightInfo>::process_bid() * 2); super::mock::MaxIntakeWeight::set(w); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 1)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(1), 40, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 40, 2)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(2), 40, 3)); - assert_ok!(Nis::place_bid(RuntimeOrigin::signed(3), 40, 3)); + assert_ok!(Nis::place_bid(signed(1), 40, 1)); + assert_ok!(Nis::place_bid(signed(1), 40, 2)); + assert_ok!(Nis::place_bid(signed(2), 40, 2)); + assert_ok!(Nis::place_bid(signed(2), 40, 3)); + assert_ok!(Nis::place_bid(signed(3), 40, 3)); Target::set(Perquintill::from_percent(40)); run_to_block(3); @@ -571,11 +731,19 @@ fn enlargement_to_target_works() { // Two new items should have been issued to 2 & 3 for 40 each & duration of 3. assert_eq!( Receipts::::get(0).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 2, expiry: 13 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((2, 40)), + expiry: 13 + } ); assert_eq!( Receipts::::get(1).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 3, expiry: 13 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((3, 40)), + expiry: 13 + } ); assert_eq!( Summary::::get(), @@ -584,6 +752,7 @@ fn enlargement_to_target_works() { index: 2, last_period: 0, thawed: Perquintill::zero(), + receipts_on_hold: 80, } ); @@ -595,7 +764,8 @@ fn enlargement_to_target_works() { proportion_owed: Perquintill::from_percent(20), index: 2, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 80, } ); @@ -603,11 +773,19 @@ fn enlargement_to_target_works() { // Two new items should have been issued to 1 & 2 for 40 each & duration of 2. assert_eq!( Receipts::::get(2).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 1, expiry: 12 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((1, 40)), + expiry: 12 + } ); assert_eq!( Receipts::::get(3).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 2, expiry: 12 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((2, 40)), + expiry: 12 + } ); assert_eq!( Summary::::get(), @@ -615,7 +793,8 @@ fn enlargement_to_target_works() { proportion_owed: Perquintill::from_percent(40), index: 4, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 160, } ); @@ -627,7 +806,8 @@ fn enlargement_to_target_works() { proportion_owed: Perquintill::from_percent(40), index: 4, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 160, } ); @@ -635,10 +815,14 @@ fn enlargement_to_target_works() { Target::set(Perquintill::from_percent(60)); run_to_block(10); - // Two new items should have been issued to 1 & 2 for 40 each & duration of 2. + // One new item should have been issued to 1 for 40 each & duration of 2. assert_eq!( Receipts::::get(4).unwrap(), - ReceiptRecord { proportion: Perquintill::from_percent(10), who: 1, expiry: 13 } + ReceiptRecord { + proportion: Perquintill::from_percent(10), + owner: Some((1, 40)), + expiry: 13 + } ); assert_eq!( @@ -647,7 +831,8 @@ fn enlargement_to_target_works() { proportion_owed: Perquintill::from_percent(50), index: 5, last_period: 0, - thawed: Perquintill::zero() + thawed: Perquintill::zero(), + receipts_on_hold: 200, } ); }); diff --git a/frame/nis/src/weights.rs b/frame/nis/src/weights.rs index 71577075ada91..769ff795514c6 100644 --- a/frame/nis/src/weights.rs +++ b/frame/nis/src/weights.rs @@ -1,40 +1,31 @@ -// This file is part of Substrate. - -// Copyright (C) 2022 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. //! Autogenerated weights for pallet_nis //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2022-11-07, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! HOSTNAME: `bm2`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` +//! DATE: 2022-12-15, STEPS: `5`, REPEAT: 2, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `Workhorse.local`, CPU: `` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 // Executed Command: -// ./target/production/substrate +// ../../../target/release/substrate // benchmark // pallet -// --chain=dev -// --steps=50 -// --repeat=20 -// --pallet=pallet_nis -// --extrinsic=* +// --chain +// dev +// --steps +// 5 +// --repeat +// 2 +// --pallet +// pallet_nis +// --extrinsic +// * // --execution=wasm // --wasm-execution=compiled -// --template=./.maintain/frame-weight-template.hbs -// --output=./frame/nis/src/weights.rs +// --output +// ../../../frame/nis/src/weights.rs +// --template +// ../../../.maintain/frame-weight-template.hbs #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] @@ -48,8 +39,11 @@ pub trait WeightInfo { fn place_bid(l: u32, ) -> Weight; fn place_bid_max() -> Weight; fn retract_bid(l: u32, ) -> Weight; - fn thaw() -> Weight; fn fund_deficit() -> Weight; + fn thaw_private() -> Weight; + fn thaw_communal() -> Weight; + fn privatize() -> Weight; + fn communify() -> Weight; fn process_queues() -> Weight; fn process_queue() -> Weight; fn process_bid() -> Weight; @@ -59,143 +53,223 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { // Storage: Nis Queues (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) // Storage: Nis QueueTotals (r:1 w:1) + /// The range of component `l` is `[0, 999]`. fn place_bid(l: u32, ) -> Weight { - // Minimum execution time: 42_332 nanoseconds. - Weight::from_ref_time(45_584_514 as u64) - // Standard Error: 129 - .saturating_add(Weight::from_ref_time(45_727 as u64).saturating_mul(l as u64)) - .saturating_add(T::DbWeight::get().reads(2 as u64)) - .saturating_add(T::DbWeight::get().writes(2 as u64)) + // Minimum execution time: 47_000 nanoseconds. + Weight::from_ref_time(53_822_030) + // Standard Error: 4_869 + .saturating_add(Weight::from_ref_time(47_431).saturating_mul(l.into())) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(3)) } // Storage: Nis Queues (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) // Storage: Nis QueueTotals (r:1 w:1) fn place_bid_max() -> Weight { - // Minimum execution time: 85_866 nanoseconds. - Weight::from_ref_time(87_171_000 as u64) - .saturating_add(T::DbWeight::get().reads(2 as u64)) - .saturating_add(T::DbWeight::get().writes(2 as u64)) + // Minimum execution time: 109_000 nanoseconds. + Weight::from_ref_time(109_000_000) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(3)) } // Storage: Nis Queues (r:1 w:1) // Storage: Nis QueueTotals (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) + /// The range of component `l` is `[1, 1000]`. fn retract_bid(l: u32, ) -> Weight { - // Minimum execution time: 44_605 nanoseconds. - Weight::from_ref_time(46_850_108 as u64) - // Standard Error: 135 - .saturating_add(Weight::from_ref_time(34_178 as u64).saturating_mul(l as u64)) - .saturating_add(T::DbWeight::get().reads(2 as u64)) - .saturating_add(T::DbWeight::get().writes(2 as u64)) - } - // Storage: Nis Active (r:1 w:1) - // Storage: Nis ActiveTotal (r:1 w:1) - fn thaw() -> Weight { - // Minimum execution time: 55_143 nanoseconds. - Weight::from_ref_time(55_845_000 as u64) - .saturating_add(T::DbWeight::get().reads(2 as u64)) - .saturating_add(T::DbWeight::get().writes(2 as u64)) - } - // Storage: Nis Active (r:1 w:1) - // Storage: Nis ActiveTotal (r:1 w:1) + // Minimum execution time: 50_000 nanoseconds. + Weight::from_ref_time(54_479_879) + // Standard Error: 4_891 + .saturating_add(Weight::from_ref_time(38_224).saturating_mul(l.into())) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(3)) + } + // Storage: Nis Summary (r:1 w:0) + // Storage: System Account (r:1 w:1) fn fund_deficit() -> Weight { - Weight::from_ref_time(47_753_000 as u64) - .saturating_add(T::DbWeight::get().reads(2 as u64)) - .saturating_add(T::DbWeight::get().writes(2 as u64)) + // Minimum execution time: 57_000 nanoseconds. + Weight::from_ref_time(62_000_000) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(1)) } - // Storage: Nis ActiveTotal (r:1 w:0) - fn process_queues() -> Weight { - Weight::from_ref_time(1_663_000 as u64) - .saturating_add(T::DbWeight::get().reads(1 as u64)) + // Storage: Nis Receipts (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: System Account (r:1 w:0) + // Storage: Balances Reserves (r:1 w:1) + fn thaw_private() -> Weight { + // Minimum execution time: 84_000 nanoseconds. + Weight::from_ref_time(85_000_000) + .saturating_add(T::DbWeight::get().reads(4)) + .saturating_add(T::DbWeight::get().writes(3)) + } + // Storage: Nis Receipts (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: Assets Asset (r:1 w:1) + // Storage: Assets Account (r:1 w:1) + // Storage: System Account (r:1 w:1) + fn thaw_communal() -> Weight { + // Minimum execution time: 108_000 nanoseconds. + Weight::from_ref_time(115_000_000) + .saturating_add(T::DbWeight::get().reads(5)) + .saturating_add(T::DbWeight::get().writes(5)) } - // Storage: Nis ActiveTotal (r:1 w:1) + // Storage: Nis Receipts (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: System Account (r:1 w:1) + // Storage: Assets Asset (r:1 w:1) + // Storage: Assets Account (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) + fn privatize() -> Weight { + // Minimum execution time: 107_000 nanoseconds. + Weight::from_ref_time(110_000_000) + .saturating_add(T::DbWeight::get().reads(6)) + .saturating_add(T::DbWeight::get().writes(6)) + } + // Storage: Nis Receipts (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) + // Storage: System Account (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: Assets Asset (r:1 w:1) + // Storage: Assets Account (r:1 w:1) + fn communify() -> Weight { + // Minimum execution time: 89_000 nanoseconds. + Weight::from_ref_time(89_000_000) + .saturating_add(T::DbWeight::get().reads(6)) + .saturating_add(T::DbWeight::get().writes(6)) + } + // Storage: Nis Summary (r:1 w:1) + // Storage: System Account (r:1 w:0) // Storage: Nis QueueTotals (r:1 w:1) + fn process_queues() -> Weight { + // Minimum execution time: 34_000 nanoseconds. + Weight::from_ref_time(38_000_000) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(2)) + } // Storage: Nis Queues (r:1 w:1) - // Storage: Nis Active (r:0 w:1) fn process_queue() -> Weight { - Weight::from_ref_time(40_797_000 as u64) - // Standard Error: 1_000 - .saturating_add(T::DbWeight::get().reads(3 as u64)) - .saturating_add(T::DbWeight::get().writes(3 as u64)) + // Minimum execution time: 6_000 nanoseconds. + Weight::from_ref_time(7_000_000) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) } - // Storage: Nis ActiveTotal (r:1 w:1) - // Storage: Nis QueueTotals (r:1 w:1) - // Storage: Nis Queues (r:1 w:1) - // Storage: Nis Active (r:0 w:1) + // Storage: Nis Receipts (r:0 w:1) fn process_bid() -> Weight { - Weight::from_ref_time(14_944_000 as u64) - // Standard Error: 6_000 - .saturating_add(T::DbWeight::get().reads(2 as u64)) - .saturating_add(T::DbWeight::get().writes(2 as u64)) + // Minimum execution time: 14_000 nanoseconds. + Weight::from_ref_time(15_000_000) + .saturating_add(T::DbWeight::get().writes(1)) } } // For backwards compatibility and tests impl WeightInfo for () { // Storage: Nis Queues (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) // Storage: Nis QueueTotals (r:1 w:1) + /// The range of component `l` is `[0, 999]`. fn place_bid(l: u32, ) -> Weight { - // Minimum execution time: 42_332 nanoseconds. - Weight::from_ref_time(45_584_514 as u64) - // Standard Error: 129 - .saturating_add(Weight::from_ref_time(45_727 as u64).saturating_mul(l as u64)) - .saturating_add(RocksDbWeight::get().reads(2 as u64)) - .saturating_add(RocksDbWeight::get().writes(2 as u64)) + // Minimum execution time: 47_000 nanoseconds. + Weight::from_ref_time(53_822_030) + // Standard Error: 4_869 + .saturating_add(Weight::from_ref_time(47_431).saturating_mul(l.into())) + .saturating_add(RocksDbWeight::get().reads(3)) + .saturating_add(RocksDbWeight::get().writes(3)) } // Storage: Nis Queues (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) // Storage: Nis QueueTotals (r:1 w:1) fn place_bid_max() -> Weight { - // Minimum execution time: 85_866 nanoseconds. - Weight::from_ref_time(87_171_000 as u64) - .saturating_add(RocksDbWeight::get().reads(2 as u64)) - .saturating_add(RocksDbWeight::get().writes(2 as u64)) + // Minimum execution time: 109_000 nanoseconds. + Weight::from_ref_time(109_000_000) + .saturating_add(RocksDbWeight::get().reads(3)) + .saturating_add(RocksDbWeight::get().writes(3)) } // Storage: Nis Queues (r:1 w:1) // Storage: Nis QueueTotals (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) + /// The range of component `l` is `[1, 1000]`. fn retract_bid(l: u32, ) -> Weight { - // Minimum execution time: 44_605 nanoseconds. - Weight::from_ref_time(46_850_108 as u64) - // Standard Error: 135 - .saturating_add(Weight::from_ref_time(34_178 as u64).saturating_mul(l as u64)) - .saturating_add(RocksDbWeight::get().reads(2 as u64)) - .saturating_add(RocksDbWeight::get().writes(2 as u64)) - } - // Storage: Nis Active (r:1 w:1) - // Storage: Nis ActiveTotal (r:1 w:1) - fn thaw() -> Weight { - // Minimum execution time: 55_143 nanoseconds. - Weight::from_ref_time(55_845_000 as u64) - .saturating_add(RocksDbWeight::get().reads(2 as u64)) - .saturating_add(RocksDbWeight::get().writes(2 as u64)) - } - // Storage: Nis Active (r:1 w:1) - // Storage: Nis ActiveTotal (r:1 w:1) + // Minimum execution time: 50_000 nanoseconds. + Weight::from_ref_time(54_479_879) + // Standard Error: 4_891 + .saturating_add(Weight::from_ref_time(38_224).saturating_mul(l.into())) + .saturating_add(RocksDbWeight::get().reads(3)) + .saturating_add(RocksDbWeight::get().writes(3)) + } + // Storage: Nis Summary (r:1 w:0) + // Storage: System Account (r:1 w:1) fn fund_deficit() -> Weight { - Weight::from_ref_time(47_753_000 as u64) - .saturating_add(RocksDbWeight::get().reads(2 as u64)) - .saturating_add(RocksDbWeight::get().writes(2 as u64)) + // Minimum execution time: 57_000 nanoseconds. + Weight::from_ref_time(62_000_000) + .saturating_add(RocksDbWeight::get().reads(2)) + .saturating_add(RocksDbWeight::get().writes(1)) } - // Storage: Nis ActiveTotal (r:1 w:0) - fn process_queues() -> Weight { - Weight::from_ref_time(1_663_000 as u64) - .saturating_add(RocksDbWeight::get().reads(1 as u64)) + // Storage: Nis Receipts (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: System Account (r:1 w:0) + // Storage: Balances Reserves (r:1 w:1) + fn thaw_private() -> Weight { + // Minimum execution time: 84_000 nanoseconds. + Weight::from_ref_time(85_000_000) + .saturating_add(RocksDbWeight::get().reads(4)) + .saturating_add(RocksDbWeight::get().writes(3)) + } + // Storage: Nis Receipts (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: Assets Asset (r:1 w:1) + // Storage: Assets Account (r:1 w:1) + // Storage: System Account (r:1 w:1) + fn thaw_communal() -> Weight { + // Minimum execution time: 108_000 nanoseconds. + Weight::from_ref_time(115_000_000) + .saturating_add(RocksDbWeight::get().reads(5)) + .saturating_add(RocksDbWeight::get().writes(5)) + } + // Storage: Nis Receipts (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: System Account (r:1 w:1) + // Storage: Assets Asset (r:1 w:1) + // Storage: Assets Account (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) + fn privatize() -> Weight { + // Minimum execution time: 107_000 nanoseconds. + Weight::from_ref_time(110_000_000) + .saturating_add(RocksDbWeight::get().reads(6)) + .saturating_add(RocksDbWeight::get().writes(6)) } - // Storage: Nis ActiveTotal (r:1 w:1) + // Storage: Nis Receipts (r:1 w:1) + // Storage: Balances Reserves (r:1 w:1) + // Storage: System Account (r:1 w:1) + // Storage: Nis Summary (r:1 w:1) + // Storage: Assets Asset (r:1 w:1) + // Storage: Assets Account (r:1 w:1) + fn communify() -> Weight { + // Minimum execution time: 89_000 nanoseconds. + Weight::from_ref_time(89_000_000) + .saturating_add(RocksDbWeight::get().reads(6)) + .saturating_add(RocksDbWeight::get().writes(6)) + } + // Storage: Nis Summary (r:1 w:1) + // Storage: System Account (r:1 w:0) // Storage: Nis QueueTotals (r:1 w:1) + fn process_queues() -> Weight { + // Minimum execution time: 34_000 nanoseconds. + Weight::from_ref_time(38_000_000) + .saturating_add(RocksDbWeight::get().reads(3)) + .saturating_add(RocksDbWeight::get().writes(2)) + } // Storage: Nis Queues (r:1 w:1) - // Storage: Nis Active (r:0 w:1) fn process_queue() -> Weight { - Weight::from_ref_time(40_797_000 as u64) - // Standard Error: 1_000 - .saturating_add(RocksDbWeight::get().reads(3 as u64)) - .saturating_add(RocksDbWeight::get().writes(3 as u64)) + // Minimum execution time: 6_000 nanoseconds. + Weight::from_ref_time(7_000_000) + .saturating_add(RocksDbWeight::get().reads(1)) + .saturating_add(RocksDbWeight::get().writes(1)) } - // Storage: Nis ActiveTotal (r:1 w:1) - // Storage: Nis QueueTotals (r:1 w:1) - // Storage: Nis Queues (r:1 w:1) - // Storage: Nis Active (r:0 w:1) + // Storage: Nis Receipts (r:0 w:1) fn process_bid() -> Weight { - Weight::from_ref_time(14_944_000 as u64) - // Standard Error: 6_000 - .saturating_add(RocksDbWeight::get().reads(2 as u64)) - .saturating_add(RocksDbWeight::get().writes(2 as u64)) + // Minimum execution time: 14_000 nanoseconds. + Weight::from_ref_time(15_000_000) + .saturating_add(RocksDbWeight::get().writes(1)) } } diff --git a/frame/support/src/traits/tokens/fungible.rs b/frame/support/src/traits/tokens/fungible.rs index 05e109b870ec0..a36e63e717f94 100644 --- a/frame/support/src/traits/tokens/fungible.rs +++ b/frame/support/src/traits/tokens/fungible.rs @@ -78,6 +78,7 @@ pub trait Mutate: Inspect { /// returned and nothing is changed. If successful, the amount of tokens reduced is returned. fn burn_from(who: &AccountId, amount: Self::Balance) -> Result; + // TODO: Remove. /// Attempt to reduce the balance of `who` by as much as possible up to `amount`, and possibly /// slightly more due to minimum_balance requirements. If no decrease is possible then an `Err` /// is returned and nothing is changed. If successful, the amount of tokens reduced is returned. @@ -143,6 +144,7 @@ pub trait InspectHold: Inspect { fn can_hold(who: &AccountId, amount: Self::Balance) -> bool; } +// TODO: Introduce `HoldReason`. /// Trait for mutating a fungible asset which can be reserved. pub trait MutateHold: InspectHold + Transfer { /// Hold some funds in an account. @@ -160,6 +162,8 @@ pub trait MutateHold: InspectHold + Transfer { best_effort: bool, ) -> Result; + // TODO: Introduce repatriate_held + /// Transfer held funds into a destination account. /// /// If `on_hold` is `true`, then the destination account must already exist and the assets @@ -195,6 +199,7 @@ pub trait BalancedHold: Balanced + MutateHold { } impl + MutateHold> BalancedHold for T { + // TODO: This should be implemented properly, and `slash` should be removed. fn slash_held( who: &AccountId, amount: Self::Balance,