Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Nov 22, 2019
1 parent f61a512 commit afbcc1b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
12 changes: 2 additions & 10 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sr_primitives::{
impl_opaque_keys, MultiSignature
};
use sr_primitives::traits::{
NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify, IdentifyAccount, Convert
NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, ConvertWeight, Verify, IdentifyAccount
};
use sr_primitives::weights::{Weight, DispatchInfo};
use sr_api::impl_runtime_apis;
Expand Down Expand Up @@ -209,14 +209,6 @@ impl balances::Trait for Runtime {
type CreationFee = CreationFee;
}

pub struct WeightToFee;

impl Convert<DispatchInfo, Balance> for WeightToFee {
fn convert(info: DispatchInfo) -> Balance {
info.weight.into()
}
}

parameter_types! {
pub const TransactionBaseFee: Balance = 0;
pub const TransactionByteFee: Balance = 1;
Expand All @@ -227,7 +219,7 @@ impl transaction_payment::Trait for Runtime {
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = WeightToFee;
type WeightToFee = ConvertWeight;
type FeeMultiplierUpdate = ();
}

Expand Down
12 changes: 9 additions & 3 deletions bin/node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod tests {
use sr_primitives::{
Fixed64,
traits::{Header as HeaderT, Hash as HashT, Convert}, ApplyResult,
transaction_validity::InvalidTransaction, weights::GetDispatchInfo,
transaction_validity::InvalidTransaction, weights::{GetDispatchInfo, DispatchInfo},
};
use contracts::ContractAddressFor;
use substrate_executor::{NativeExecutor, WasmExecutionMethod};
Expand Down Expand Up @@ -90,7 +90,10 @@ mod tests {
(extrinsic.encode().len() as Balance);

let weight = default_transfer_call().get_dispatch_info().weight;
let weight_fee = <Runtime as transaction_payment::Trait>::WeightToFee::convert(weight);
let weight_fee = <Runtime as transaction_payment::Trait>::WeightToFee::convert(DispatchInfo {
weight,
class: Default::default(),
});

fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee) + TransferFee::get()
}
Expand Down Expand Up @@ -1077,7 +1080,10 @@ mod tests {
balance_alice -= length_fee;

let weight = default_transfer_call().get_dispatch_info().weight;
let weight_fee = LinearWeightToFee::<WeightFeeCoefficient>::convert(weight);
let weight_fee = LinearWeightToFee::<WeightFeeCoefficient>::convert(DispatchInfo {
weight,
class: Default::default(),
});

// we know that weight to fee multiplier is effect-less in block 1.
assert_eq!(weight_fee as Balance, MILLICENTS);
Expand Down
5 changes: 4 additions & 1 deletion bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ mod tests {
if fm == next { panic!("The fee should ever increase"); }
fm = next;
iterations += 1;
let fee = <Runtime as transaction_payment::Trait>::WeightToFee::convert(tx_weight);
let fee = <Runtime as transaction_payment::Trait>::WeightToFee::convert(DispatchInfo {
weight: tx_weight,
class: Default::default(),
});
let adjusted_fee = fm.saturated_multiply_accumulate(fee);
println!(
"iteration {}, new fm = {:?}. Fee at this point is: {} units / {} millicents, \
Expand Down
4 changes: 2 additions & 2 deletions palette/balances/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#![cfg(test)]

use sr_primitives::{Perbill, traits::{ConvertInto, IdentityLookup}, testing::Header,
use sr_primitives::{Perbill, traits::{ConvertWeight, IdentityLookup}, testing::Header,
weights::{DispatchInfo, Weight}};
use primitives::H256;
use runtime_io;
Expand Down Expand Up @@ -87,7 +87,7 @@ impl transaction_payment::Trait for Runtime {
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
type WeightToFee = ConvertWeight;
type FeeMultiplierUpdate = ();
}
impl Trait for Runtime {
Expand Down
4 changes: 2 additions & 2 deletions palette/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ mod tests {
use primitives::H256;
use sr_primitives::{
generic::Era, Perbill, DispatchError, weights::Weight, testing::{Digest, Header, Block},
traits::{Bounded, Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto},
traits::{Bounded, Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertWeight},
transaction_validity::{InvalidTransaction, UnknownTransaction}, ApplyError,
};
use support::{
Expand Down Expand Up @@ -434,7 +434,7 @@ mod tests {
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
type WeightToFee = ConvertWeight;
type FeeMultiplierUpdate = ();
}
impl custom::Trait for Runtime {}
Expand Down
8 changes: 4 additions & 4 deletions palette/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ mod tests {
Perbill,
testing::{Header, TestXt},
traits::{BlakeTwo256, IdentityLookup, Extrinsic},
weights::{DispatchClass, DispatchInfo, GetDispatchInfo},
weights::{Weight, DispatchClass, DispatchInfo, GetDispatchInfo},
};
use balances::Call as BalancesCall;
use rstd::cell::RefCell;
Expand Down Expand Up @@ -318,9 +318,9 @@ mod tests {
}

pub struct WeightToFee(u64);
impl Convert<Weight, u64> for WeightToFee {
fn convert(t: Weight) -> u64 {
WEIGHT_TO_FEE.with(|v| *v.borrow() * (t as u64))
impl Convert<DispatchInfo, u64> for WeightToFee {
fn convert(t: DispatchInfo) -> u64 {
WEIGHT_TO_FEE.with(|v| *v.borrow() * (t.weight as u64))
}
}

Expand Down
10 changes: 9 additions & 1 deletion primitives/sr-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::transaction_validity::{
ValidTransaction, TransactionValidity, TransactionValidityError, UnknownTransaction,
};
use crate::generic::{Digest, DigestItem};
use crate::weights::DispatchInfo;
use crate::weights::{DispatchInfo, Weight};
pub use arithmetic::traits::{
SimpleArithmetic, UniqueSaturatedInto, UniqueSaturatedFrom, Saturating, SaturatedConversion,
Zero, One, Bounded, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv,
Expand Down Expand Up @@ -235,6 +235,14 @@ impl<A, B: From<A>> Convert<A, B> for ConvertInto {
fn convert(a: A) -> B { a.into() }
}

pub struct ConvertWeight;

impl<Balance: From<Weight>> Convert<DispatchInfo, Balance> for ConvertWeight {
fn convert(info: DispatchInfo) -> Balance {
info.weight.into()
}
}

/// Convenience type to work around the highly unergonomic syntax needed
/// to invoke the functions of overloaded generic traits, in this case
/// `TryFrom` and `TryInto`.
Expand Down

0 comments on commit afbcc1b

Please sign in to comment.