Compare commits
No commits in common. "25fcfed586c84d1624f3de128b72169977e3b734" and "9240f424e1bcf5a18b9ee29ed71e8a639e24e7ba" have entirely different histories.
25fcfed586
...
9240f424e1
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -1186,7 +1186,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "casper-runtime"
|
name = "casper-runtime"
|
||||||
version = "3.5.31"
|
version = "3.5.30"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"casper-runtime-constants",
|
"casper-runtime-constants",
|
||||||
"frame-benchmarking",
|
"frame-benchmarking",
|
||||||
@ -3649,14 +3649,13 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ghost-networks"
|
name = "ghost-networks"
|
||||||
version = "0.1.16"
|
version = "0.1.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"frame-benchmarking",
|
"frame-benchmarking",
|
||||||
"frame-support",
|
"frame-support",
|
||||||
"frame-system",
|
"frame-system",
|
||||||
"ghost-core-primitives",
|
"ghost-core-primitives",
|
||||||
"ghost-traits",
|
"ghost-traits",
|
||||||
"num-traits",
|
|
||||||
"pallet-balances",
|
"pallet-balances",
|
||||||
"pallet-staking",
|
"pallet-staking",
|
||||||
"pallet-staking-reward-curve",
|
"pallet-staking-reward-curve",
|
||||||
@ -3836,7 +3835,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ghost-slow-clap"
|
name = "ghost-slow-clap"
|
||||||
version = "0.3.40"
|
version = "0.3.39"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"frame-benchmarking",
|
"frame-benchmarking",
|
||||||
"frame-support",
|
"frame-support",
|
||||||
|
@ -69,7 +69,6 @@ bs58 = { version = "0.5.0" }
|
|||||||
prometheus-parse = { version = "0.2.2" }
|
prometheus-parse = { version = "0.2.2" }
|
||||||
rustc-hex = { version = "2.1.0", default-features = false }
|
rustc-hex = { version = "2.1.0", default-features = false }
|
||||||
log = { version = "0.4", default-features = false }
|
log = { version = "0.4", default-features = false }
|
||||||
num-traits = { version = "0.2.17", default-features = false }
|
|
||||||
libsecp256k1 = { version = "0.7", default-features = false }
|
libsecp256k1 = { version = "0.7", default-features = false }
|
||||||
bip39 = { package = "parity-bip39", version = "2.0.1" }
|
bip39 = { package = "parity-bip39", version = "2.0.1" }
|
||||||
sha3 = { version = "0.10", default-features = false }
|
sha3 = { version = "0.10", default-features = false }
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-networks"
|
name = "ghost-networks"
|
||||||
version = "0.1.16"
|
version = "0.1.13"
|
||||||
license.workspace = true
|
license.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
@ -10,7 +10,6 @@ repository.workspace = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
scale-info = { workspace = true, features = ["derive"] }
|
scale-info = { workspace = true, features = ["derive"] }
|
||||||
codec = { workspace = true, features = ["max-encoded-len"] }
|
codec = { workspace = true, features = ["max-encoded-len"] }
|
||||||
num-traits = { workspace = true }
|
|
||||||
|
|
||||||
frame-benchmarking = { workspace = true, optional = true }
|
frame-benchmarking = { workspace = true, optional = true }
|
||||||
frame-support = { workspace = true }
|
frame-support = { workspace = true }
|
||||||
@ -31,7 +30,6 @@ default = ["std"]
|
|||||||
std = [
|
std = [
|
||||||
"scale-info/std",
|
"scale-info/std",
|
||||||
"codec/std",
|
"codec/std",
|
||||||
"num-traits/std",
|
|
||||||
"frame-support/std",
|
"frame-support/std",
|
||||||
"frame-system/std",
|
"frame-system/std",
|
||||||
"frame-benchmarking?/std",
|
"frame-benchmarking?/std",
|
||||||
|
@ -21,11 +21,9 @@ pub use ghost_traits::networks::{
|
|||||||
NetworkDataBasicHandler, NetworkDataInspectHandler, NetworkDataMutateHandler,
|
NetworkDataBasicHandler, NetworkDataInspectHandler, NetworkDataMutateHandler,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod math;
|
|
||||||
mod weights;
|
mod weights;
|
||||||
|
|
||||||
pub use crate::weights::WeightInfo;
|
pub use crate::weights::WeightInfo;
|
||||||
use math::MulDiv;
|
|
||||||
pub use module::*;
|
pub use module::*;
|
||||||
|
|
||||||
#[cfg(any(feature = "runtime-benchmarks", test))]
|
#[cfg(any(feature = "runtime-benchmarks", test))]
|
||||||
@ -75,17 +73,7 @@ pub struct BridgedInflationCurve<RewardCurve, T>(core::marker::PhantomData<(Rewa
|
|||||||
impl<Balance, RewardCurve, T> pallet_staking::EraPayout<Balance>
|
impl<Balance, RewardCurve, T> pallet_staking::EraPayout<Balance>
|
||||||
for BridgedInflationCurve<RewardCurve, T>
|
for BridgedInflationCurve<RewardCurve, T>
|
||||||
where
|
where
|
||||||
Balance: Default
|
Balance: Default + AtLeast32BitUnsigned + Clone + Copy + From<u128>,
|
||||||
+ Copy
|
|
||||||
+ From<BalanceOf<T>>
|
|
||||||
+ AtLeast32BitUnsigned
|
|
||||||
+ num_traits::ops::wrapping::WrappingAdd
|
|
||||||
+ num_traits::ops::overflowing::OverflowingAdd
|
|
||||||
+ sp_std::ops::AddAssign
|
|
||||||
+ sp_std::ops::Not<Output = Balance>
|
|
||||||
+ sp_std::ops::Shl<Output = Balance>
|
|
||||||
+ sp_std::ops::Shr<Output = Balance>
|
|
||||||
+ sp_std::ops::BitAnd<Balance, Output = Balance>,
|
|
||||||
RewardCurve: Get<&'static PiecewiseLinear<'static>>,
|
RewardCurve: Get<&'static PiecewiseLinear<'static>>,
|
||||||
T: Config,
|
T: Config,
|
||||||
{
|
{
|
||||||
@ -94,27 +82,30 @@ where
|
|||||||
total_issuance: Balance,
|
total_issuance: Balance,
|
||||||
_era_duration_in_millis: u64,
|
_era_duration_in_millis: u64,
|
||||||
) -> (Balance, Balance) {
|
) -> (Balance, Balance) {
|
||||||
let reward_curve = RewardCurve::get();
|
let piecewise_linear = RewardCurve::get();
|
||||||
let bridged_imbalance = BridgedImbalance::<T>::get();
|
let bridge_adjustment = BridgedImbalance::<T>::get();
|
||||||
let accumulated_commission = AccumulatedCommission::<T>::get();
|
let accumulated_commission = AccumulatedCommission::<T>::get();
|
||||||
|
|
||||||
let accumulated_commission: Balance = accumulated_commission.into();
|
let bridged_out: u128 = bridge_adjustment.bridged_out.try_into().unwrap_or_default();
|
||||||
let adjusted_issuance: Balance = total_issuance
|
let bridged_in: u128 = bridge_adjustment.bridged_in.try_into().unwrap_or_default();
|
||||||
.saturating_add(bridged_imbalance.bridged_out.into())
|
let accumulated_commission: u128 = accumulated_commission.try_into().unwrap_or_default();
|
||||||
.saturating_sub(bridged_imbalance.bridged_in.into());
|
|
||||||
|
let accumulated_balance = Balance::from(accumulated_commission);
|
||||||
|
let adjusted_issuance = match bridged_out > bridged_in {
|
||||||
|
true => total_issuance.saturating_add(Balance::from(bridged_out - bridged_in)),
|
||||||
|
false => total_issuance.saturating_sub(Balance::from(bridged_in - bridged_out)),
|
||||||
|
};
|
||||||
|
|
||||||
NullifyNeeded::<T>::set(true);
|
NullifyNeeded::<T>::set(true);
|
||||||
|
|
||||||
let estimated_reward =
|
match piecewise_linear
|
||||||
reward_curve.calculate_for_fraction_times_denominator(total_staked, adjusted_issuance);
|
.calculate_for_fraction_times_denominator(total_staked, adjusted_issuance)
|
||||||
let payout = MulDiv::<Balance>::calculate(
|
.checked_mul(&accumulated_balance)
|
||||||
estimated_reward,
|
.and_then(|product| product.checked_div(&adjusted_issuance))
|
||||||
accumulated_commission,
|
{
|
||||||
adjusted_issuance,
|
Some(payout) => (payout, accumulated_balance.saturating_sub(payout)),
|
||||||
);
|
None => (Balance::default(), Balance::default()),
|
||||||
let rest_payout = accumulated_commission.saturating_sub(payout);
|
}
|
||||||
|
|
||||||
(payout, rest_payout)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -788,7 +779,6 @@ impl<T: Config> NetworkDataMutateHandler<NetworkData, BalanceOf<T>> for Pallet<T
|
|||||||
|
|
||||||
fn nullify_commission() {
|
fn nullify_commission() {
|
||||||
AccumulatedCommission::<T>::set(Default::default());
|
AccumulatedCommission::<T>::set(Default::default());
|
||||||
BridgedImbalance::<T>::set(Default::default());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trigger_nullification() {
|
fn trigger_nullification() {
|
||||||
|
@ -1,135 +0,0 @@
|
|||||||
use crate::AtLeast32BitUnsigned;
|
|
||||||
|
|
||||||
pub struct MulDiv<Balance>(core::marker::PhantomData<Balance>);
|
|
||||||
impl<Balance> MulDiv<Balance>
|
|
||||||
where
|
|
||||||
Balance: Copy
|
|
||||||
+ AtLeast32BitUnsigned
|
|
||||||
+ num_traits::ops::wrapping::WrappingAdd
|
|
||||||
+ num_traits::ops::overflowing::OverflowingAdd
|
|
||||||
+ sp_std::ops::AddAssign
|
|
||||||
+ sp_std::ops::Not<Output = Balance>
|
|
||||||
+ sp_std::ops::Shl<Output = Balance>
|
|
||||||
+ sp_std::ops::Shr<Output = Balance>
|
|
||||||
+ sp_std::ops::BitAnd<Balance, Output = Balance>,
|
|
||||||
{
|
|
||||||
|
|
||||||
fn zero(&self) -> Balance {
|
|
||||||
0u32.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn one(&self) -> Balance {
|
|
||||||
1u32.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bit_shift(&self) -> Balance {
|
|
||||||
let u32_shift: u32 = core::mem::size_of::<Balance>()
|
|
||||||
.saturating_mul(4)
|
|
||||||
.try_into()
|
|
||||||
.unwrap_or_default();
|
|
||||||
u32_shift.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn least_significant_bits(&self, a: Balance) -> Balance {
|
|
||||||
a & ((self.one() << self.bit_shift()) - self.one())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn most_significant_bits(&self, a: Balance) -> Balance {
|
|
||||||
a >> self.bit_shift()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn two_complement(&self, a: Balance) -> Balance {
|
|
||||||
(!a).wrapping_add(&self.one())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn adjusted_ratio(&self, a: Balance) -> Balance {
|
|
||||||
(self.two_complement(a) / a).wrapping_add(&self.one())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn modulo(&self, a: Balance) -> Balance {
|
|
||||||
self.two_complement(a) % a
|
|
||||||
}
|
|
||||||
|
|
||||||
fn overflow_resistant_addition(
|
|
||||||
&self,
|
|
||||||
a0: Balance,
|
|
||||||
a1: Balance,
|
|
||||||
b0: Balance,
|
|
||||||
b1: Balance,
|
|
||||||
) -> (Balance, Balance) {
|
|
||||||
let (r0, overflow) = a0.overflowing_add(&b0);
|
|
||||||
let overflow: Balance = overflow.then(|| 1u32).unwrap_or_default().into();
|
|
||||||
let r1 = a1.wrapping_add(&b1).wrapping_add(&overflow);
|
|
||||||
(r0, r1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn overflow_resistant_multiplication(&self, a: Balance, b: Balance) -> (Balance, Balance) {
|
|
||||||
let (a0, a1) = (
|
|
||||||
self.least_significant_bits(a),
|
|
||||||
self.most_significant_bits(a),
|
|
||||||
);
|
|
||||||
let (b0, b1) = (
|
|
||||||
self.least_significant_bits(b),
|
|
||||||
self.most_significant_bits(b),
|
|
||||||
);
|
|
||||||
let (x, y) = (a1 * b0, b1 * a0);
|
|
||||||
|
|
||||||
let (r0, r1) = (a0 * b0, a1 * b1);
|
|
||||||
let (r0, r1) = self.overflow_resistant_addition(
|
|
||||||
r0,
|
|
||||||
r1,
|
|
||||||
self.least_significant_bits(x) << self.bit_shift(),
|
|
||||||
self.most_significant_bits(x),
|
|
||||||
);
|
|
||||||
let (r0, r1) = self.overflow_resistant_addition(
|
|
||||||
r0,
|
|
||||||
r1,
|
|
||||||
self.least_significant_bits(y) << self.bit_shift(),
|
|
||||||
self.most_significant_bits(y),
|
|
||||||
);
|
|
||||||
|
|
||||||
(r0, r1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn overflow_resistant_division(
|
|
||||||
&self,
|
|
||||||
mut a0: Balance,
|
|
||||||
mut a1: Balance,
|
|
||||||
b: Balance,
|
|
||||||
) -> (Balance, Balance) {
|
|
||||||
if b == self.one() {
|
|
||||||
return (a0, a1);
|
|
||||||
}
|
|
||||||
|
|
||||||
let zero: Balance = 0u32.into();
|
|
||||||
let (q, r) = (self.adjusted_ratio(b), self.modulo(b));
|
|
||||||
let (mut x0, mut x1) = (zero, zero);
|
|
||||||
|
|
||||||
while a1 != zero {
|
|
||||||
let (t0, t1) = self.overflow_resistant_multiplication(a1, q);
|
|
||||||
let (new_x0, new_x1) = self.overflow_resistant_addition(x0, x1, t0, t1);
|
|
||||||
x0 = new_x0;
|
|
||||||
x1 = new_x1;
|
|
||||||
|
|
||||||
let (t0, t1) = self.overflow_resistant_multiplication(a1, r);
|
|
||||||
let (new_a0, new_a1) = self.overflow_resistant_addition(t0, t1, a0, zero);
|
|
||||||
a0 = new_a0;
|
|
||||||
a1 = new_a1;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.overflow_resistant_addition(x0, x1, a0 / b, zero)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mul_div(&self, a: Balance, b: Balance, c: Balance) -> Balance {
|
|
||||||
let (t0, t1) = self.overflow_resistant_multiplication(a, b);
|
|
||||||
self.overflow_resistant_division(t0, t1, c).0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn calculate(a: Balance, b: Balance, c: Balance) -> Balance {
|
|
||||||
let inner = MulDiv(core::marker::PhantomData);
|
|
||||||
if c == inner.zero() {
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
inner.mul_div(a, b, c)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1295,10 +1295,8 @@ fn accumulated_commission_could_be_nullified() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn bridged_inlation_reward_works() {
|
fn bridged_inlation_reward_works() {
|
||||||
ExtBuilder::build().execute_with(|| {
|
ExtBuilder::build().execute_with(|| {
|
||||||
let amount_full: u128 = 1337 * 1_000_000_000;
|
let amount: u128 = 1337 * 1_000_000_000;
|
||||||
let commission: u128 = amount_full / 100; // 1% commission
|
let commission: u128 = amount / 100; // 1% commission
|
||||||
let amount: u128 = amount_full - commission;
|
|
||||||
|
|
||||||
let total_staked_ideal: u128 = 69;
|
let total_staked_ideal: u128 = 69;
|
||||||
let total_staked_not_ideal: u128 = 68;
|
let total_staked_not_ideal: u128 = 68;
|
||||||
let total_issuance: u128 = 100;
|
let total_issuance: u128 = 100;
|
||||||
@ -1540,10 +1538,8 @@ fn bridged_inlation_reward_works() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn bridged_inflation_era_payout_triggers_need_of_nullification() {
|
fn bridged_inflation_era_payout_triggers_need_of_nullification() {
|
||||||
ExtBuilder::build().execute_with(|| {
|
ExtBuilder::build().execute_with(|| {
|
||||||
let amount_full: u128 = 1337 * 1_000_000_000;
|
let amount: u128 = 1337 * 1_000_000_000;
|
||||||
let commission: u128 = amount_full / 100; // 1% commission
|
let commission: u128 = amount / 100; // 1% commission
|
||||||
let amount: u128 = amount_full - commission;
|
|
||||||
|
|
||||||
let total_staked_ideal: u128 = 69;
|
let total_staked_ideal: u128 = 69;
|
||||||
let total_issuance: u128 = 100;
|
let total_issuance: u128 = 100;
|
||||||
|
|
||||||
@ -1567,333 +1563,10 @@ fn bridged_inflation_era_payout_triggers_need_of_nullification() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn trigger_nullification_works_as_expected() {
|
fn trigger_nullification_works_as_expected() {
|
||||||
ExtBuilder::build().execute_with(|| {
|
ExtBuilder::build().execute_with(|| {
|
||||||
let commission: u128 = 69;
|
|
||||||
let imbalance = BridgeAdjustment {
|
|
||||||
bridged_in: 1337u128,
|
|
||||||
bridged_out: 420u128,
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(AccumulatedCommission::<Test>::get(), 0);
|
|
||||||
assert_eq!(BridgedImbalance::<Test>::get(), BridgeAdjustment::default());
|
|
||||||
|
|
||||||
AccumulatedCommission::<Test>::set(commission);
|
|
||||||
BridgedImbalance::<Test>::set(imbalance.clone());
|
|
||||||
|
|
||||||
assert_eq!(AccumulatedCommission::<Test>::get(), commission);
|
|
||||||
assert_eq!(BridgedImbalance::<Test>::get(), imbalance);
|
|
||||||
|
|
||||||
assert_eq!(NullifyNeeded::<Test>::get(), false);
|
assert_eq!(NullifyNeeded::<Test>::get(), false);
|
||||||
GhostNetworks::trigger_nullification();
|
GhostNetworks::trigger_nullification();
|
||||||
assert_eq!(NullifyNeeded::<Test>::get(), true);
|
assert_eq!(NullifyNeeded::<Test>::get(), true);
|
||||||
GhostNetworks::trigger_nullification();
|
GhostNetworks::trigger_nullification();
|
||||||
assert_eq!(NullifyNeeded::<Test>::get(), false);
|
assert_eq!(NullifyNeeded::<Test>::get(), false);
|
||||||
assert_eq!(AccumulatedCommission::<Test>::get(), 0);
|
|
||||||
assert_eq!(BridgedImbalance::<Test>::get(), BridgeAdjustment::default());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_substrate_guarantees_not_to_overflow_u128() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let reward_curve = RewardCurve::get();
|
|
||||||
let mut n: u128 = 69;
|
|
||||||
let mut d: u128 = 100;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
n = match n.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
d = match d.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
assert_eq!(
|
|
||||||
reward_curve.calculate_for_fraction_times_denominator(n, d),
|
|
||||||
d
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_substrate_guarantees_not_to_overflow_u64() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let reward_curve = RewardCurve::get();
|
|
||||||
let mut n: u64 = 69;
|
|
||||||
let mut d: u64 = 100;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
n = match n.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
d = match d.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
assert_eq!(
|
|
||||||
reward_curve.calculate_for_fraction_times_denominator(n, d),
|
|
||||||
d
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_substrate_guarantees_not_to_overflow_u32() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let reward_curve = RewardCurve::get();
|
|
||||||
let mut n: u32 = 69;
|
|
||||||
let mut d: u32 = 100;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
n = match n.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
d = match d.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
assert_eq!(
|
|
||||||
reward_curve.calculate_for_fraction_times_denominator(n, d),
|
|
||||||
d
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_muldiv_guarantees_not_to_overflow_for_u128() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let mut a: u128 = 2;
|
|
||||||
let mut b: u128 = 3;
|
|
||||||
let mut c: u128 = 6;
|
|
||||||
let mut result: u128 = 1;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
a = match a.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
b = match b.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
c = match c.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
result = match result.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(MulDiv::<u128>::calculate(a, b, c), result);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
MulDiv::<u128>::calculate(u128::MAX, u128::MAX, u128::MAX),
|
|
||||||
u128::MAX
|
|
||||||
);
|
|
||||||
assert_eq!(MulDiv::<u128>::calculate(u128::MAX, 0, 0), 0);
|
|
||||||
assert_eq!(MulDiv::<u128>::calculate(0, u128::MAX, 0), 0);
|
|
||||||
assert_eq!(MulDiv::<u128>::calculate(0, 0, u128::MAX), 0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_muldiv_guarantees_not_to_overflow_for_u64() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let mut a: u64 = 2;
|
|
||||||
let mut b: u64 = 3;
|
|
||||||
let mut c: u64 = 6;
|
|
||||||
let mut result: u64 = 1;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
a = match a.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
b = match b.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
c = match c.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
result = match result.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(MulDiv::<u64>::calculate(a, b, c), result);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
MulDiv::<u64>::calculate(u64::MAX, u64::MAX, u64::MAX),
|
|
||||||
u64::MAX
|
|
||||||
);
|
|
||||||
assert_eq!(MulDiv::<u64>::calculate(u64::MAX, 0, 0), 0);
|
|
||||||
assert_eq!(MulDiv::<u64>::calculate(0, u64::MAX, 0), 0);
|
|
||||||
assert_eq!(MulDiv::<u64>::calculate(0, 0, u64::MAX), 0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_muldiv_guarantees_not_to_overflow_for_u32() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let mut a: u32 = 2;
|
|
||||||
let mut b: u32 = 3;
|
|
||||||
let mut c: u32 = 6;
|
|
||||||
let mut result: u32 = 1;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
a = match a.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
b = match b.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
c = match c.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
result = match result.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(MulDiv::<u32>::calculate(a, b, c), result);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
MulDiv::<u32>::calculate(u32::MAX, u32::MAX, u32::MAX),
|
|
||||||
u32::MAX
|
|
||||||
);
|
|
||||||
assert_eq!(MulDiv::<u32>::calculate(u32::MAX, 0, 0), 0);
|
|
||||||
assert_eq!(MulDiv::<u32>::calculate(0, u32::MAX, 0), 0);
|
|
||||||
assert_eq!(MulDiv::<u32>::calculate(0, 0, u32::MAX), 0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_bridged_inflation_curve_for_overflow() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let amount_full: u128 = 1337 * 1_000_000_000;
|
|
||||||
let commission: u128 = amount_full / 100; // 1% commission
|
|
||||||
let amount: u128 = amount_full - commission;
|
|
||||||
|
|
||||||
let tollerance: u128 = commission / 100; // 1% tollerance
|
|
||||||
let precomputed_payout: u128 = 13177568884;
|
|
||||||
let precomputed_rest: u128 = 192431116;
|
|
||||||
assert_eq!(precomputed_payout + precomputed_rest, commission);
|
|
||||||
|
|
||||||
let mut total_staked_ideal: u128 = 69_000;
|
|
||||||
let mut total_staked_not_ideal: u128 = 68_000;
|
|
||||||
let mut total_issuance: u128 = 100_000;
|
|
||||||
|
|
||||||
assert_ok!(GhostNetworks::accumulate_commission(&commission));
|
|
||||||
assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount));
|
|
||||||
|
|
||||||
loop {
|
|
||||||
total_staked_ideal = match total_staked_ideal.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
total_staked_not_ideal = match total_staked_not_ideal.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
total_issuance = match total_issuance.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
assert_eq!(
|
|
||||||
BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
|
||||||
total_staked_ideal,
|
|
||||||
total_issuance + amount,
|
|
||||||
0
|
|
||||||
),
|
|
||||||
(commission, 0)
|
|
||||||
);
|
|
||||||
|
|
||||||
let (payout, rest) = BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
|
||||||
total_staked_not_ideal,
|
|
||||||
total_issuance + amount,
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
|
|
||||||
let payout_deviation = if precomputed_payout > payout {
|
|
||||||
precomputed_payout - payout
|
|
||||||
} else {
|
|
||||||
payout - precomputed_payout
|
|
||||||
};
|
|
||||||
|
|
||||||
let rest_deviation = if precomputed_rest > rest {
|
|
||||||
precomputed_rest - rest
|
|
||||||
} else {
|
|
||||||
rest - precomputed_rest
|
|
||||||
};
|
|
||||||
|
|
||||||
assert!(payout_deviation < tollerance);
|
|
||||||
assert!(rest_deviation < tollerance);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn check_bridged_inflation_curve_for_big_commissions() {
|
|
||||||
ExtBuilder::build().execute_with(|| {
|
|
||||||
let mut amount_full: u128 = 1337;
|
|
||||||
|
|
||||||
let total_staked_ideal: u128 = 69_000_000;
|
|
||||||
let total_staked_gt_ideal: u128 = 100_000_000;
|
|
||||||
let total_staked_lt_ideal: u128 = 3_000_000;
|
|
||||||
let total_issuance: u128 = 100_000_000;
|
|
||||||
|
|
||||||
loop {
|
|
||||||
amount_full = match amount_full.checked_mul(1_000) {
|
|
||||||
Some(value) => value,
|
|
||||||
None => break,
|
|
||||||
};
|
|
||||||
let commission: u128 = amount_full / 100; // 1% commission
|
|
||||||
let amount: u128 = amount_full - commission;
|
|
||||||
|
|
||||||
AccumulatedCommission::<Test>::set(commission);
|
|
||||||
BridgedImbalance::<Test>::set(BridgeAdjustment {
|
|
||||||
bridged_in: amount,
|
|
||||||
bridged_out: 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
|
||||||
total_staked_ideal,
|
|
||||||
total_issuance + amount,
|
|
||||||
0
|
|
||||||
),
|
|
||||||
(commission, 0)
|
|
||||||
);
|
|
||||||
|
|
||||||
let (payout, rest) = BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
|
||||||
total_staked_gt_ideal,
|
|
||||||
total_issuance + amount,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
assert!(payout < commission);
|
|
||||||
assert!(rest < commission);
|
|
||||||
|
|
||||||
let (payout, rest) = BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
|
||||||
total_staked_lt_ideal,
|
|
||||||
total_issuance + amount,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
assert!(payout < commission);
|
|
||||||
assert!(rest < commission);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-slow-clap"
|
name = "ghost-slow-clap"
|
||||||
version = "0.3.40"
|
version = "0.3.39"
|
||||||
description = "Applause protocol for the EVM bridge"
|
description = "Applause protocol for the EVM bridge"
|
||||||
license.workspace = true
|
license.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
|
@ -905,9 +905,9 @@ fn should_nullify_commission_on_finalize() {
|
|||||||
BridgedInflationCurve::<RewardCurve, Runtime>::era_payout(
|
BridgedInflationCurve::<RewardCurve, Runtime>::era_payout(
|
||||||
total_staked,
|
total_staked,
|
||||||
total_issuance,
|
total_issuance,
|
||||||
0u64
|
0
|
||||||
),
|
),
|
||||||
(420000000000000u64, 0u64)
|
(420000000000000, 0)
|
||||||
); // precomputed values
|
); // precomputed values
|
||||||
assert_eq!(Networks::is_nullification_period(), true);
|
assert_eq!(Networks::is_nullification_period(), true);
|
||||||
Networks::on_finalize(System::block_number());
|
Networks::on_finalize(System::block_number());
|
||||||
@ -921,7 +921,6 @@ fn should_nullify_commission_on_finalize() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_avoid_applause_during_nullification_period() {
|
fn should_avoid_applause_during_nullification_period() {
|
||||||
let zero: u64 = 0u64;
|
|
||||||
let total_staked = 69_000_000;
|
let total_staked = 69_000_000;
|
||||||
let total_issuance = 100_000_000;
|
let total_issuance = 100_000_000;
|
||||||
|
|
||||||
@ -937,9 +936,9 @@ fn should_avoid_applause_during_nullification_period() {
|
|||||||
BridgedInflationCurve::<RewardCurve, Runtime>::era_payout(
|
BridgedInflationCurve::<RewardCurve, Runtime>::era_payout(
|
||||||
total_staked,
|
total_staked,
|
||||||
total_issuance,
|
total_issuance,
|
||||||
zero
|
0
|
||||||
),
|
),
|
||||||
(zero, zero)
|
(0, 0)
|
||||||
);
|
);
|
||||||
assert_eq!(Networks::is_nullification_period(), true);
|
assert_eq!(Networks::is_nullification_period(), true);
|
||||||
|
|
||||||
@ -957,7 +956,6 @@ fn should_avoid_applause_during_nullification_period() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn should_self_applause_if_enough_received_claps() {
|
fn should_self_applause_if_enough_received_claps() {
|
||||||
let zero: u64 = 0u64;
|
|
||||||
let (network_id, transaction_hash, unique_transaction_hash) =
|
let (network_id, transaction_hash, unique_transaction_hash) =
|
||||||
generate_unique_hash(None, None, None, None);
|
generate_unique_hash(None, None, None, None);
|
||||||
let (_, receiver, amount) = get_mocked_metadata();
|
let (_, receiver, amount) = get_mocked_metadata();
|
||||||
@ -983,10 +981,10 @@ fn should_self_applause_if_enough_received_claps() {
|
|||||||
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
assert_eq!(Balances::balance(&receiver), zero);
|
assert_eq!(Balances::balance(&receiver), 0);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
BridgedInflationCurve::<RewardCurve, Runtime>::era_payout(zero, zero, zero),
|
BridgedInflationCurve::<RewardCurve, Runtime>::era_payout(0, 0, 0),
|
||||||
(zero, zero)
|
(0, 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "casper-runtime"
|
name = "casper-runtime"
|
||||||
version = "3.5.31"
|
version = "3.5.30"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
description = "Runtime of the Casper Network"
|
description = "Runtime of the Casper Network"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
@ -117,7 +117,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
|||||||
spec_name: create_runtime_str!("casper"),
|
spec_name: create_runtime_str!("casper"),
|
||||||
impl_name: create_runtime_str!("casper-svengali"),
|
impl_name: create_runtime_str!("casper-svengali"),
|
||||||
authoring_version: 0,
|
authoring_version: 0,
|
||||||
spec_version: 2,
|
spec_version: 1,
|
||||||
impl_version: 0,
|
impl_version: 0,
|
||||||
apis: RUNTIME_API_VERSIONS,
|
apis: RUNTIME_API_VERSIONS,
|
||||||
transaction_version: 1,
|
transaction_version: 1,
|
||||||
|
Loading…
Reference in New Issue
Block a user