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
+73 -121
View File
@@ -36,6 +36,57 @@ pub type InnerOf<P> = <P as PerThing>::Inner;
/// Get the upper type of a `PerThing`.
pub type UpperOf<P> = <P as PerThing>::Upper;
pub trait RationalArg:
Clone
+ Ord
+ ops::Div<Self, Output = Self>
+ ops::Rem<Self, Output = Self>
+ ops::Add<Self, Output = Self>
+ ops::AddAssign<Self>
+ Unsigned
+ Zero
+ One
{
}
impl<
T: Clone
+ Ord
+ ops::Div<Self, Output = Self>
+ ops::Rem<Self, Output = Self>
+ ops::Add<Self, Output = Self>
+ ops::AddAssign<Self>
+ Unsigned
+ Zero
+ One,
> RationalArg for T
{
}
pub trait MultiplyArg:
Clone
+ ops::Rem<Self, Output = Self>
+ ops::Div<Self, Output = Self>
+ ops::Mul<Self, Output = Self>
+ ops::Add<Self, Output = Self>
+ Unsigned
{
}
impl<
T: Clone
+ ops::Rem<Self, Output = Self>
+ ops::Div<Self, Output = Self>
+ ops::Mul<Self, Output = Self>
+ ops::Add<Self, Output = Self>
+ Unsigned,
> MultiplyArg for T
{
}
pub trait ReciprocalArg: MultiplyArg + Saturating {}
impl<T: MultiplyArg + Saturating> ReciprocalArg for T {}
/// Something that implements a fixed point ration with an arbitrary granularity `X`, as _parts per
/// `X`_.
pub trait PerThing:
@@ -160,13 +211,7 @@ pub trait PerThing:
/// ```
fn mul_floor<N>(self, b: N) -> N
where
N: Clone
+ UniqueSaturatedInto<Self::Inner>
+ ops::Rem<N, Output = N>
+ ops::Div<N, Output = N>
+ ops::Mul<N, Output = N>
+ ops::Add<N, Output = N>
+ Unsigned,
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
@@ -189,13 +234,7 @@ pub trait PerThing:
/// ```
fn mul_ceil<N>(self, b: N) -> N
where
N: Clone
+ UniqueSaturatedInto<Self::Inner>
+ ops::Rem<N, Output = N>
+ ops::Div<N, Output = N>
+ ops::Mul<N, Output = N>
+ ops::Add<N, Output = N>
+ Unsigned,
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
@@ -212,14 +251,7 @@ pub trait PerThing:
/// ```
fn saturating_reciprocal_mul<N>(self, b: N) -> N
where
N: Clone
+ UniqueSaturatedInto<Self::Inner>
+ ops::Rem<N, Output = N>
+ ops::Div<N, Output = N>
+ ops::Mul<N, Output = N>
+ ops::Add<N, Output = N>
+ Saturating
+ Unsigned,
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::NearestPrefUp)
@@ -239,14 +271,7 @@ pub trait PerThing:
/// ```
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
where
N: Clone
+ UniqueSaturatedInto<Self::Inner>
+ ops::Rem<N, Output = N>
+ ops::Div<N, Output = N>
+ ops::Mul<N, Output = N>
+ ops::Add<N, Output = N>
+ Saturating
+ Unsigned,
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
@@ -266,14 +291,7 @@ pub trait PerThing:
/// ```
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
where
N: Clone
+ UniqueSaturatedInto<Self::Inner>
+ ops::Rem<N, Output = N>
+ ops::Div<N, Output = N>
+ ops::Mul<N, Output = N>
+ ops::Add<N, Output = N>
+ Saturating
+ Unsigned,
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
@@ -316,17 +334,7 @@ pub trait PerThing:
/// ```
fn from_rational<N>(p: N, q: N) -> Self
where
N: Clone
+ Ord
+ TryInto<Self::Inner>
+ TryInto<Self::Upper>
+ ops::Div<N, Output = N>
+ ops::Rem<N, Output = N>
+ ops::Add<N, Output = N>
+ ops::AddAssign<N>
+ Unsigned
+ Zero
+ One,
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
{
Self::from_rational_with_rounding(p, q, Rounding::Down).unwrap_or_else(|_| Self::one())
@@ -388,34 +396,14 @@ pub trait PerThing:
/// ```
fn from_rational_with_rounding<N>(p: N, q: N, rounding: Rounding) -> Result<Self, ()>
where
N: Clone
+ Ord
+ TryInto<Self::Inner>
+ TryInto<Self::Upper>
+ ops::Div<N, Output = N>
+ ops::Rem<N, Output = N>
+ ops::Add<N, Output = N>
+ ops::AddAssign<N>
+ Unsigned
+ Zero
+ One,
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>;
/// Same as `Self::from_rational`.
#[deprecated = "Use from_rational instead"]
fn from_rational_approximation<N>(p: N, q: N) -> Self
where
N: Clone
+ Ord
+ TryInto<Self::Inner>
+ TryInto<Self::Upper>
+ ops::Div<N, Output = N>
+ ops::Rem<N, Output = N>
+ ops::Add<N, Output = N>
+ ops::AddAssign<N>
+ Unsigned
+ Zero
+ One,
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
{
Self::from_rational(p, q)
@@ -495,13 +483,7 @@ where
/// Overflow-prune multiplication. Accurately multiply a value by `self` without overflowing.
fn overflow_prune_mul<N, P>(x: N, part: P::Inner, rounding: Rounding) -> N
where
N: Clone
+ UniqueSaturatedInto<P::Inner>
+ ops::Div<N, Output = N>
+ ops::Mul<N, Output = N>
+ ops::Add<N, Output = N>
+ ops::Rem<N, Output = N>
+ Unsigned,
N: MultiplyArg + UniqueSaturatedInto<P::Inner>,
P: PerThing,
P::Inner: Into<N>,
{
@@ -517,12 +499,7 @@ where
/// to `x / denom * numer` for an accurate result.
fn rational_mul_correction<N, P>(x: N, numer: P::Inner, denom: P::Inner, rounding: Rounding) -> N
where
N: UniqueSaturatedInto<P::Inner>
+ ops::Div<N, Output = N>
+ ops::Mul<N, Output = N>
+ ops::Add<N, Output = N>
+ ops::Rem<N, Output = N>
+ Unsigned,
N: MultiplyArg + UniqueSaturatedInto<P::Inner>,
P: PerThing,
P::Inner: Into<N>,
{
@@ -803,17 +780,7 @@ macro_rules! implement_per_thing {
#[deprecated = "Use `PerThing::from_rational` instead"]
pub fn from_rational_approximation<N>(p: N, q: N) -> Self
where
N: Clone
+ Ord
+ TryInto<$type>
+ TryInto<$upper_type>
+ ops::Div<N, Output = N>
+ ops::Rem<N, Output = N>
+ ops::Add<N, Output = N>
+ ops::AddAssign<N>
+ Unsigned
+ Zero
+ One,
N: RationalArg+ TryInto<$type> + TryInto<$upper_type>,
$type: Into<N>
{
<Self as PerThing>::from_rational(p, q)
@@ -822,17 +789,7 @@ macro_rules! implement_per_thing {
/// See [`PerThing::from_rational`].
pub fn from_rational<N>(p: N, q: N) -> Self
where
N: Clone
+ Ord
+ TryInto<$type>
+ TryInto<$upper_type>
+ ops::Div<N, Output = N>
+ ops::Rem<N, Output = N>
+ ops::Add<N, Output = N>
+ ops::AddAssign<N>
+ Unsigned
+ Zero
+ One,
N: RationalArg+ TryInto<$type> + TryInto<$upper_type>,
$type: Into<N>
{
<Self as PerThing>::from_rational(p, q)
@@ -851,9 +808,7 @@ macro_rules! implement_per_thing {
/// See [`PerThing::mul_floor`].
pub fn mul_floor<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<$type> +
ops::Rem<N, Output=N> + ops::Div<N, Output=N> + ops::Mul<N, Output=N> +
ops::Add<N, Output=N> + Unsigned,
N: MultiplyArg + UniqueSaturatedInto<$type>,
$type: Into<N>,
{
@@ -863,9 +818,7 @@ macro_rules! implement_per_thing {
/// See [`PerThing::mul_ceil`].
pub fn mul_ceil<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<$type> +
ops::Rem<N, Output=N> + ops::Div<N, Output=N> + ops::Mul<N, Output=N> +
ops::Add<N, Output=N> + Unsigned,
N: MultiplyArg + UniqueSaturatedInto<$type>,
$type: Into<N>,
{
PerThing::mul_ceil(self, b)
@@ -874,9 +827,7 @@ macro_rules! implement_per_thing {
/// See [`PerThing::saturating_reciprocal_mul`].
pub fn saturating_reciprocal_mul<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<$type> + ops::Rem<N, Output=N> +
ops::Div<N, Output=N> + ops::Mul<N, Output=N> + ops::Add<N, Output=N> +
Saturating + Unsigned,
N: ReciprocalArg + UniqueSaturatedInto<$type>,
$type: Into<N>,
{
PerThing::saturating_reciprocal_mul(self, b)
@@ -885,9 +836,7 @@ macro_rules! implement_per_thing {
/// See [`PerThing::saturating_reciprocal_mul_floor`].
pub fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<$type> + ops::Rem<N, Output=N> +
ops::Div<N, Output=N> + ops::Mul<N, Output=N> + ops::Add<N, Output=N> +
Saturating + Unsigned,
N: ReciprocalArg + UniqueSaturatedInto<$type>,
$type: Into<N>,
{
PerThing::saturating_reciprocal_mul_floor(self, b)
@@ -896,9 +845,7 @@ macro_rules! implement_per_thing {
/// See [`PerThing::saturating_reciprocal_mul_ceil`].
pub fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<$type> + ops::Rem<N, Output=N> +
ops::Div<N, Output=N> + ops::Mul<N, Output=N> + ops::Add<N, Output=N> +
Saturating + Unsigned,
N: ReciprocalArg + UniqueSaturatedInto<$type>,
$type: Into<N>,
{
PerThing::saturating_reciprocal_mul_ceil(self, b)
@@ -1133,6 +1080,11 @@ macro_rules! implement_per_thing {
}
}
impl $crate::traits::One for $name {
fn one() -> Self {
Self::one()
}
}
#[cfg(test)]
mod $test_mod {