Non-Interactive Staking (#12610)

* Improve naming.

* More improvements to naming

* Fungible counterpart

* Shared pot instead of reserve

* Transferable receipts

* Better naming

* Use u128 for counterpart

* Partial thawing

* Docs

* Remove AdminOrigin

* Integrate into Kitchen Sink

* Thaw throttling

* Remove todo

* Docs

* Fix benchmarks

* Building

* Tests work

* New benchmarks

* Benchmarking tests

* Test new defensive_saturating_* functions

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Formatting

* Update frame/nis/src/lib.rs

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Apply suggestions from code review

Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Events added

* Fix kitchensink

* Update frame/nis/src/lib.rs

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>

* Review niggles

* Remove genesis build requirements

* Grumbles

* Fixes

* Fixes

* Fixes

* Update frame/nis/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Formatting

* Fixes

* Fix node genesis config

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix node chain specs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use free asset ID as counterpart

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Account for rounding errors in fund_deficit bench

Relaxes the check for the NIS account balance in the fund_deficit bench
from equality from to checking for 99.999% equality. The exact deviation
for the kitchensink runtime config is 1.24e-10 percent but could vary if
the config is changed.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* clippy

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fmt

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix

* Rename

* Fixes

* Fixes

* Formatting

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Gavin Wood
2022-12-05 13:37:52 +00:00
committed by GitHub
parent f9f1ac2515
commit 2a0e53d11a
25 changed files with 2311 additions and 1664 deletions
+182
View File
@@ -0,0 +1,182 @@
// This file is part of Substrate.
// Copyright (C) 2021-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.
//! Benchmarks for NIS Pallet
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_support::traits::{Currency, EnsureOrigin, Get};
use frame_system::RawOrigin;
use sp_arithmetic::Perquintill;
use sp_runtime::{
traits::{Bounded, One, Zero},
DispatchError, PerThing,
};
use sp_std::prelude::*;
use crate::Pallet as Nis;
const SEED: u32 = 0;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
fn fill_queues<T: Config>() -> Result<(), DispatchError> {
// filling queues involves filling the first queue entirely and placing a single item in all
// other queues.
let queues = T::QueueCount::get();
let bids = T::MaxQueueLen::get();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(
&caller,
T::MinBid::get() * BalanceOf::<T>::from(queues + bids),
);
for _ in 0..bids {
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
}
for d in 1..queues {
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1 + d)?;
}
Ok(())
}
benchmarks! {
place_bid {
let l in 0..(T::MaxQueueLen::get() - 1);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
for i in 0..l {
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
}
}: _(RawOrigin::Signed(caller.clone()), T::MinBid::get() * BalanceOf::<T>::from(2u32), 1)
verify {
assert_eq!(QueueTotals::<T>::get()[0], (l + 1, T::MinBid::get() * BalanceOf::<T>::from(l + 2)));
}
place_bid_max {
let caller: T::AccountId = whitelisted_caller();
let origin = RawOrigin::Signed(caller.clone());
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
for i in 0..T::MaxQueueLen::get() {
Nis::<T>::place_bid(origin.clone().into(), T::MinBid::get(), 1)?;
}
}: place_bid(origin, T::MinBid::get() * BalanceOf::<T>::from(2u32), 1)
verify {
assert_eq!(QueueTotals::<T>::get()[0], (
T::MaxQueueLen::get(),
T::MinBid::get() * BalanceOf::<T>::from(T::MaxQueueLen::get() + 1),
));
}
retract_bid {
let l in 1..T::MaxQueueLen::get();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
for i in 0..l {
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
}
}: _(RawOrigin::Signed(caller.clone()), T::MinBid::get(), 1)
verify {
assert_eq!(QueueTotals::<T>::get()[0], (l - 1, T::MinBid::get() * BalanceOf::<T>::from(l - 1)));
}
fund_deficit {
let origin = T::FundOrigin::successful_origin();
let caller: T::AccountId = whitelisted_caller();
let bid = T::MinBid::get().max(One::one());
T::Currency::make_free_balance_be(&caller, bid);
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
Nis::<T>::process_queues(Perquintill::one(), 1, 1, &mut WeightCounter::unlimited());
let original = T::Currency::free_balance(&Nis::<T>::account_id());
T::Currency::make_free_balance_be(&Nis::<T>::account_id(), BalanceOf::<T>::min_value());
}: _<T::RuntimeOrigin>(origin)
verify {
// Must fund at least 99.999% of the required amount.
let missing = Perquintill::from_rational(
T::Currency::free_balance(&Nis::<T>::account_id()), original).left_from_one();
assert!(missing <= Perquintill::one() / 100_000);
}
thaw {
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, T::MinBid::get() * BalanceOf::<T>::from(3u32));
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
Nis::<T>::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited());
Receipts::<T>::mutate(0, |m_g| if let Some(ref mut g) = m_g { g.expiry = Zero::zero() });
}: _(RawOrigin::Signed(caller.clone()), 0, None)
verify {
assert!(Receipts::<T>::get(0).is_none());
}
process_queues {
fill_queues::<T>()?;
}: {
Nis::<T>::process_queues(
Perquintill::one(),
Zero::zero(),
u32::max_value(),
&mut WeightCounter::unlimited(),
)
}
process_queue {
let our_account = Nis::<T>::account_id();
let issuance = Nis::<T>::issuance();
let mut summary = Summary::<T>::get();
}: {
Nis::<T>::process_queue(
1u32,
1u32.into(),
&our_account,
&issuance,
0,
&mut Bounded::max_value(),
&mut (T::MaxQueueLen::get(), Bounded::max_value()),
&mut summary,
&mut WeightCounter::unlimited(),
)
}
process_bid {
let who = account::<T::AccountId>("bidder", 0, SEED);
let bid = Bid {
amount: T::MinBid::get(),
who,
};
let our_account = Nis::<T>::account_id();
let issuance = Nis::<T>::issuance();
let mut summary = Summary::<T>::get();
}: {
Nis::<T>::process_bid(
bid,
2u32.into(),
&our_account,
&issuance,
&mut Bounded::max_value(),
&mut Bounded::max_value(),
&mut summary,
)
}
impl_benchmark_test_suite!(Nis, crate::mock::new_test_ext(), crate::mock::Test);
}