make MulDiv to be used with any 32 bit unsigned

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2025-08-10 22:07:53 +03:00
parent f7f25bd087
commit 60b887f812
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
4 changed files with 190 additions and 29 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ghost-networks" name = "ghost-networks"
version = "0.1.15" version = "0.1.16"
license.workspace = true license.workspace = true
authors.workspace = true authors.workspace = true
edition.workspace = true edition.workspace = true

View File

@ -83,7 +83,9 @@ where
+ num_traits::ops::overflowing::OverflowingAdd + num_traits::ops::overflowing::OverflowingAdd
+ sp_std::ops::AddAssign + sp_std::ops::AddAssign
+ sp_std::ops::Not<Output = Balance> + sp_std::ops::Not<Output = Balance>
+ sp_std::ops::BitAnd<u128, 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,
{ {

View File

@ -9,24 +9,41 @@ where
+ num_traits::ops::overflowing::OverflowingAdd + num_traits::ops::overflowing::OverflowingAdd
+ sp_std::ops::AddAssign + sp_std::ops::AddAssign
+ sp_std::ops::Not<Output = Balance> + sp_std::ops::Not<Output = Balance>
+ sp_std::ops::BitAnd<u128, 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 { fn least_significant_bits(&self, a: Balance) -> Balance {
a & ((1 << 64) - 1) a & ((self.one() << self.bit_shift()) - self.one())
} }
fn most_significant_bits(&self, a: Balance) -> Balance { fn most_significant_bits(&self, a: Balance) -> Balance {
a >> 64 a >> self.bit_shift()
} }
fn two_complement(&self, a: Balance) -> Balance { fn two_complement(&self, a: Balance) -> Balance {
let one: Balance = 1u32.into(); (!a).wrapping_add(&self.one())
(!a).wrapping_add(&one)
} }
fn adjusted_ratio(&self, a: Balance) -> Balance { fn adjusted_ratio(&self, a: Balance) -> Balance {
let one: Balance = 1u32.into(); (self.two_complement(a) / a).wrapping_add(&self.one())
(self.two_complement(a) / a).wrapping_add(&one)
} }
fn modulo(&self, a: Balance) -> Balance { fn modulo(&self, a: Balance) -> Balance {
@ -61,13 +78,13 @@ where
let (r0, r1) = self.overflow_resistant_addition( let (r0, r1) = self.overflow_resistant_addition(
r0, r0,
r1, r1,
self.least_significant_bits(x) << 64, self.least_significant_bits(x) << self.bit_shift(),
self.most_significant_bits(x), self.most_significant_bits(x),
); );
let (r0, r1) = self.overflow_resistant_addition( let (r0, r1) = self.overflow_resistant_addition(
r0, r0,
r1, r1,
self.least_significant_bits(y) << 64, self.least_significant_bits(y) << self.bit_shift(),
self.most_significant_bits(y), self.most_significant_bits(y),
); );
@ -80,7 +97,7 @@ where
mut a1: Balance, mut a1: Balance,
b: Balance, b: Balance,
) -> (Balance, Balance) { ) -> (Balance, Balance) {
if b == 1u32.into() { if b == self.one() {
return (a0, a1); return (a0, a1);
} }
@ -109,12 +126,10 @@ where
} }
pub fn calculate(a: Balance, b: Balance, c: Balance) -> Balance { pub fn calculate(a: Balance, b: Balance, c: Balance) -> Balance {
let zero: Balance = 0u32.into();
if a == zero || b == zero || c == zero {
return zero;
}
let inner = MulDiv(core::marker::PhantomData); let inner = MulDiv(core::marker::PhantomData);
if c == inner.zero() {
return c;
}
inner.mul_div(a, b, c) inner.mul_div(a, b, c)
} }
} }

View File

@ -1593,18 +1593,18 @@ fn trigger_nullification_works_as_expected() {
} }
#[test] #[test]
fn check_substrate_guarantees_not_to_overflow() { fn check_substrate_guarantees_not_to_overflow_u128() {
ExtBuilder::build().execute_with(|| { ExtBuilder::build().execute_with(|| {
let reward_curve = RewardCurve::get(); let reward_curve = RewardCurve::get();
let mut n: u128 = 69; let mut n: u128 = 69;
let mut d: u128 = 100; let mut d: u128 = 100;
loop { loop {
n = match n.checked_mul(1_000_000) { n = match n.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
d = match d.checked_mul(1_000_000) { d = match d.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
@ -1617,7 +1617,55 @@ fn check_substrate_guarantees_not_to_overflow() {
} }
#[test] #[test]
fn check_muldiv_guarantees_not_to_overflow() { 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(|| { ExtBuilder::build().execute_with(|| {
let mut a: u128 = 2; let mut a: u128 = 2;
let mut b: u128 = 3; let mut b: u128 = 3;
@ -1625,19 +1673,19 @@ fn check_muldiv_guarantees_not_to_overflow() {
let mut result: u128 = 1; let mut result: u128 = 1;
loop { loop {
a = match a.checked_mul(1_000_000) { a = match a.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
b = match b.checked_mul(1_000_000) { b = match b.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
c = match c.checked_mul(1_000_000) { c = match c.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
result = match result.checked_mul(1_000_000) { result = match result.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
@ -1655,6 +1703,84 @@ fn check_muldiv_guarantees_not_to_overflow() {
}); });
} }
#[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] #[test]
fn check_bridged_inflation_curve_for_overflow() { fn check_bridged_inflation_curve_for_overflow() {
ExtBuilder::build().execute_with(|| { ExtBuilder::build().execute_with(|| {
@ -1675,15 +1801,15 @@ fn check_bridged_inflation_curve_for_overflow() {
assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount)); assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount));
loop { loop {
total_staked_ideal = match total_staked_ideal.checked_mul(1_000_000) { total_staked_ideal = match total_staked_ideal.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
total_staked_not_ideal = match total_staked_not_ideal.checked_mul(1_000_000) { total_staked_not_ideal = match total_staked_not_ideal.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
total_issuance = match total_issuance.checked_mul(1_000_000) { total_issuance = match total_issuance.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
@ -1726,10 +1852,12 @@ fn check_bridged_inflation_curve_for_big_commissions() {
let mut amount_full: u128 = 1337; let mut amount_full: u128 = 1337;
let total_staked_ideal: u128 = 69_000_000; 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; let total_issuance: u128 = 100_000_000;
loop { loop {
amount_full = match amount_full.checked_mul(1_000_000) { amount_full = match amount_full.checked_mul(1_000) {
Some(value) => value, Some(value) => value,
None => break, None => break,
}; };
@ -1750,6 +1878,22 @@ fn check_bridged_inflation_curve_for_big_commissions() {
), ),
(commission, 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);
} }
}); });
} }