Skip to content

Commit 302cd40

Browse files
committed
add pallet testing
1 parent 9bfdbbc commit 302cd40

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

sugondat/chain/pallets/length-fee-adjustment/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
pub use pallet::*;
44

5+
#[cfg(test)]
6+
mod mock;
7+
#[cfg(test)]
8+
mod tests;
9+
510
/// Currently, the `pallet_transaction_payment` uses the following formula:
611
///
712
/// ```ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use crate as pallet_sugondat_length_fee_adjustment;
2+
use frame_support::{
3+
parameter_types,
4+
traits::ConstU64,
5+
weights::{Weight, WeightToFee as WeightToFeeT},
6+
};
7+
use pallet_transaction_payment::Multiplier;
8+
use sp_core::H256;
9+
use sp_runtime::{
10+
traits::{BlakeTwo256, IdentityLookup},
11+
FixedPointNumber, Perquintill, SaturatedConversion,
12+
};
13+
14+
type Block = frame_system::mocking::MockBlock<Test>;
15+
type Balance = u64;
16+
17+
frame_support::construct_runtime!(
18+
pub enum Test {
19+
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
20+
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
21+
TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>},
22+
LengthFeeAdjustment: pallet_sugondat_length_fee_adjustment::{Pallet, Storage},
23+
}
24+
);
25+
26+
parameter_types! {
27+
pub const BlockHashCount: u64 = 250;
28+
pub const SS58Prefix: u8 = 42;
29+
}
30+
31+
impl frame_system::Config for Test {
32+
type BaseCallFilter = frame_support::traits::Everything;
33+
type BlockWeights = ();
34+
type BlockLength = ();
35+
type DbWeight = ();
36+
type RuntimeOrigin = RuntimeOrigin;
37+
type RuntimeCall = RuntimeCall;
38+
type Nonce = u64;
39+
type Hash = H256;
40+
type Hashing = BlakeTwo256;
41+
type AccountId = u64;
42+
type Lookup = IdentityLookup<Self::AccountId>;
43+
type Block = Block;
44+
type RuntimeEvent = RuntimeEvent;
45+
type BlockHashCount = BlockHashCount;
46+
type Version = ();
47+
type PalletInfo = PalletInfo;
48+
type AccountData = pallet_balances::AccountData<u64>;
49+
type OnNewAccount = ();
50+
type OnKilledAccount = ();
51+
type SystemWeightInfo = ();
52+
type SS58Prefix = SS58Prefix;
53+
type OnSetCode = ();
54+
type MaxConsumers = frame_support::traits::ConstU32<16>;
55+
}
56+
57+
impl pallet_balances::Config for Test {
58+
type Balance = u64;
59+
type RuntimeEvent = RuntimeEvent;
60+
type DustRemoval = ();
61+
type ExistentialDeposit = ConstU64<1>;
62+
type AccountStore = System;
63+
type MaxLocks = ();
64+
type MaxReserves = ();
65+
type ReserveIdentifier = [u8; 8];
66+
type WeightInfo = ();
67+
type FreezeIdentifier = ();
68+
type MaxFreezes = ();
69+
type RuntimeHoldReason = ();
70+
type RuntimeFreezeReason = ();
71+
type MaxHolds = ();
72+
}
73+
74+
parameter_types! {
75+
pub TransactionByteFee: Balance = 333333u64;
76+
pub TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
77+
pub AdjustmentVariableBlockFullness: Multiplier = Multiplier::saturating_from_rational(75, 1_000_000);
78+
pub MinimumMultiplierBlockFullness: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
79+
pub MaximumMultiplierBlockFullness: Multiplier = Multiplier::saturating_from_integer(10);
80+
pub MaximumBlockLength: u32 = 5 * 1024 * 1024;
81+
pub AdjustmentVariableBlockSize: Multiplier = Multiplier::saturating_from_rational(1, 840);
82+
pub MinimumMultiplierBlockSize: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
83+
pub MaximumMultiplierBlockSize: Multiplier = Multiplier::saturating_from_integer(10);
84+
85+
pub static WeightToFee: u64 = 1;
86+
pub static OperationalFeeMultiplier: u8 = 5;
87+
}
88+
89+
impl WeightToFeeT for WeightToFee {
90+
type Balance = u64;
91+
92+
fn weight_to_fee(weight: &Weight) -> Self::Balance {
93+
Self::Balance::saturated_from(weight.ref_time())
94+
.saturating_mul(WEIGHT_TO_FEE.with(|v| *v.borrow()))
95+
}
96+
}
97+
98+
impl pallet_transaction_payment::Config for Test {
99+
type RuntimeEvent = RuntimeEvent;
100+
type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter<Balances, ()>;
101+
type OperationalFeeMultiplier = OperationalFeeMultiplier;
102+
type WeightToFee = WeightToFee;
103+
type LengthToFee = LengthFeeAdjustment;
104+
type FeeMultiplierUpdate = ();
105+
}
106+
107+
impl pallet_sugondat_length_fee_adjustment::Config for Test {
108+
type TransactionByteFee = TransactionByteFee;
109+
type MaximumBlockLength = MaximumBlockLength;
110+
type AdjustmentVariableBlockSize = AdjustmentVariableBlockSize;
111+
type MinimumMultiplierBlockSize = MinimumMultiplierBlockSize;
112+
type MaximumMultiplierBlockSize = MaximumMultiplierBlockSize;
113+
type TargetBlockFullness = TargetBlockFullness;
114+
type AdjustmentVariableBlockFullness = AdjustmentVariableBlockFullness;
115+
type MaximumMultiplierBlockFullness = MaximumMultiplierBlockFullness;
116+
type MinimumMultiplierBlockFullness = MinimumMultiplierBlockFullness;
117+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use crate::{
2+
mock::{LengthFeeAdjustment, Test},
3+
*,
4+
};
5+
use pallet_transaction_payment::Multiplier;
6+
use sp_runtime::BuildStorage;
7+
use sp_runtime::{traits::Get, FixedPointNumber};
8+
use sp_weights::{Weight, WeightToFee};
9+
10+
fn new_test_ext() -> sp_io::TestExternalities {
11+
frame_system::GenesisConfig::<Test>::default()
12+
.build_storage()
13+
.unwrap()
14+
.into()
15+
}
16+
17+
#[test]
18+
fn test_weight_to_fee() {
19+
new_test_ext().execute_with(|| {
20+
// test with multipliers smaller, equal and bigger than 1
21+
let multipliers = vec![
22+
Multiplier::from_rational(12, 19),
23+
Multiplier::saturating_from_integer(1),
24+
Multiplier::from_rational(19, 12),
25+
];
26+
let max_block_len = <Test as Config>::MaximumBlockLength::get() as u64;
27+
// test 1000 step between 0 and the maximum size of a block
28+
for len in (0..max_block_len).step_by(max_block_len as usize / 1000) {
29+
for multiplier in multipliers.iter() {
30+
NextLengthMultiplier::<Test>::put(multiplier);
31+
32+
let length_fee = len * <Test as Config>::TransactionByteFee::get();
33+
let expected = multiplier.saturating_mul_int(length_fee);
34+
35+
assert_eq!(
36+
LengthFeeAdjustment::weight_to_fee(&Weight::from_parts(len, 0)),
37+
expected
38+
);
39+
}
40+
}
41+
});
42+
}
43+
44+
#[test]
45+
fn test_default_next_length_multiplier() {
46+
new_test_ext().execute_with(|| {
47+
assert_eq!(
48+
NextLengthMultiplier::<Test>::get(),
49+
NextLengthMultiplierDefualt::get()
50+
)
51+
});
52+
}
53+
54+
#[test]
55+
fn test_default_target_block_size() {
56+
new_test_ext().execute_with(|| {
57+
assert_eq!(
58+
TargetBlockSize::<Test>::get(),
59+
TargetBlockSizeDefualt::get()
60+
)
61+
});
62+
}

0 commit comments

Comments
 (0)