Implement FixedPoint trait. (#5877)

* Implement Fixed trait.

* Fix tests

* Fix tests

* Fix tests 2

* Address review comment regarding from_i129.

* Remove precision by using log10() as suggested in review.

* Add small comments.

* Use checked versions + panic for ops::*.

* Remove repeated test.

* Uncomment test.

* Remove casts.

* Add more comments.

* Add tests.

* Panic on saturating_div_int

* More tests.

* More docs.

* Saturating renames.

* Fix to_bound doc.

* Move some impl to trait.

* Add range

* Add macro pre.

* More round() tests.

* Delete confusion.

* More impl to trait

* Add doc for fixedpoint op.

* Remove trailing spaces.

* Suggested docs changes.

* More tests and comments for roundings.

* Some quickcheck tests.

* Add missing panic, more test/comments.

* Nits.

* Rename.

* Remove primitives-types import.

* Apply review suggestions

* Fix long lines and add some fuzz.

* fix long line

* Update fuzzer

* Bump impl

* fix warnings

Co-authored-by: Gavin Wood <gavin@parity.io>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Marcio Diaz
2020-05-21 19:32:44 +02:00
committed by GitHub
parent ab9ff537cd
commit 72386f609a
17 changed files with 1661 additions and 1568 deletions
+9 -10
View File
@@ -44,7 +44,7 @@ use frame_support::{
dispatch::DispatchResult,
};
use sp_runtime::{
Fixed128,
Fixed128, FixedPointNumber,
transaction_validity::{
TransactionPriority, ValidTransaction, InvalidTransaction, TransactionValidityError,
TransactionValidity,
@@ -83,7 +83,7 @@ pub trait Trait: frame_system::Trait {
decl_storage! {
trait Store for Module<T: Trait> as TransactionPayment {
pub NextFeeMultiplier get(fn next_fee_multiplier): Multiplier = Multiplier::from_parts(0);
pub NextFeeMultiplier get(fn next_fee_multiplier): Multiplier = Multiplier::from_inner(0);
}
}
@@ -172,7 +172,7 @@ impl<T: Trait> Module<T> {
// the adjustable part of the fee
let adjustable_fee = len_fee.saturating_add(unadjusted_weight_fee);
let targeted_fee_adjustment = NextFeeMultiplier::get();
let adjusted_fee = targeted_fee_adjustment.saturated_multiply_accumulate(adjustable_fee.saturated_into());
let adjusted_fee = targeted_fee_adjustment.saturating_mul_acc_int(adjustable_fee.saturated_into());
let base_fee = Self::weight_to_fee(T::ExtrinsicBaseWeight::get());
base_fee.saturating_add(adjusted_fee.saturated_into()).saturating_add(tip)
@@ -190,7 +190,7 @@ impl<T: Trait> Module<T> {
{
let fee = UniqueSaturatedInto::<u128>::unique_saturated_into(Self::weight_to_fee(weight));
UniqueSaturatedFrom::unique_saturated_from(
NextFeeMultiplier::get().saturated_multiply_accumulate(fee)
NextFeeMultiplier::get().saturating_mul_acc_int(fee)
)
}
@@ -329,7 +329,6 @@ impl<T: Trait + Send + Sync> SignedExtension for ChargeTransactionPayment<T> whe
#[cfg(test)]
mod tests {
use super::*;
use core::num::NonZeroI128;
use codec::Encode;
use frame_support::{
impl_outer_dispatch, impl_outer_origin, parameter_types,
@@ -575,7 +574,7 @@ mod tests {
.execute_with(||
{
let len = 10;
NextFeeMultiplier::put(Fixed128::from_rational(1, NonZeroI128::new(2).unwrap()));
NextFeeMultiplier::put(Fixed128::saturating_from_rational(1, 2));
let pre = ChargeTransactionPayment::<Runtime>::from(5 /* tipped */)
.pre_dispatch(&2, CALL, &info_from_weight(100), len)
@@ -663,7 +662,7 @@ mod tests {
.execute_with(||
{
// all fees should be x1.5
NextFeeMultiplier::put(Fixed128::from_rational(1, NonZeroI128::new(2).unwrap()));
NextFeeMultiplier::put(Fixed128::saturating_from_rational(1, 2));
let len = 10;
assert!(
@@ -691,7 +690,7 @@ mod tests {
.execute_with(||
{
// all fees should be x1.5
NextFeeMultiplier::put(Fixed128::from_rational(1, NonZeroI128::new(2).unwrap()));
NextFeeMultiplier::put(Fixed128::saturating_from_rational(1, 2));
assert_eq!(
TransactionPayment::query_info(xt, len),
@@ -720,7 +719,7 @@ mod tests {
.execute_with(||
{
// Next fee multiplier is zero
assert_eq!(NextFeeMultiplier::get(), Fixed128::from_natural(0));
assert_eq!(NextFeeMultiplier::get(), Fixed128::saturating_from_integer(0));
// Tip only, no fees works
let dispatch_info = DispatchInfo {
@@ -760,7 +759,7 @@ mod tests {
.execute_with(||
{
// Add a next fee multiplier
NextFeeMultiplier::put(Fixed128::from_rational(1, NonZeroI128::new(2).unwrap())); // = 1/2 = .5
NextFeeMultiplier::put(Fixed128::saturating_from_rational(1, 2)); // = 1/2 = .5
// Base fee is unaffected by multiplier
let dispatch_info = DispatchInfo {
weight: 0,