Newer
Older
#[test]
fn compute_fee_does_not_overflow() {
ExtBuilder::default()
.base_weight(100)
.byte_fee(10)
.balance_factor(0)
.build()
.execute_with(||
{
// Overflow is handled
let dispatch_info = DispatchInfo {
Module::<Runtime>::compute_fee(
<u64>::max_value()
),
<u64>::max_value()
);
});
}
#[test]
fn refund_does_not_recreate_account() {
ExtBuilder::default()
.balance_factor(10)
.base_weight(5)
.build()
.execute_with(||
{
// So events are emitted
System::set_block_number(10);
let len = 10;
let pre = ChargeTransactionPayment::<Runtime>::from(5 /* tipped */)
.pre_dispatch(&2, CALL, &info_from_weight(100), len)
.unwrap();
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 5);
// kill the account between pre and post dispatch
assert!(Balances::transfer(Some(2).into(), 3, Balances::free_balance(2)).is_ok());
assert_eq!(Balances::free_balance(2), 0);
assert!(
ChargeTransactionPayment::<Runtime>
::post_dispatch(pre, &info_from_weight(100), &post_info_from_weight(50), len, &Ok(()))
.is_ok()
);
assert_eq!(Balances::free_balance(2), 0);
// Transfer Event
assert!(System::events().iter().any(|event| {
event.event == Event::pallet_balances(pallet_balances::RawEvent::Transfer(2, 3, 80))
}));
// Killed Event
assert!(System::events().iter().any(|event| {
event.event == Event::system(system::RawEvent::KilledAccount(2))
}));
});
}
#[test]
fn actual_weight_higher_than_max_refunds_nothing() {
ExtBuilder::default()
.balance_factor(10)
.base_weight(5)
.build()
.execute_with(||
{
let len = 10;
let pre = ChargeTransactionPayment::<Runtime>::from(5 /* tipped */)
.pre_dispatch(&2, CALL, &info_from_weight(100), len)
.unwrap();
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 5);
assert!(
ChargeTransactionPayment::<Runtime>
::post_dispatch(pre, &info_from_weight(100), &post_info_from_weight(101), len, &Ok(()))
.is_ok()
);
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 5);
});
}
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
#[test]
fn zero_transfer_on_free_transaction() {
ExtBuilder::default()
.balance_factor(10)
.base_weight(5)
.build()
.execute_with(||
{
// So events are emitted
System::set_block_number(10);
let len = 10;
let dispatch_info = DispatchInfo {
weight: 100,
pays_fee: Pays::No,
class: DispatchClass::Normal,
};
let user = 69;
let pre = ChargeTransactionPayment::<Runtime>::from(0)
.pre_dispatch(&user, CALL, &dispatch_info, len)
.unwrap();
assert_eq!(Balances::total_balance(&user), 0);
assert!(
ChargeTransactionPayment::<Runtime>
::post_dispatch(pre, &dispatch_info, &default_post_info(), len, &Ok(()))
.is_ok()
);
assert_eq!(Balances::total_balance(&user), 0);
// No events for such a scenario
assert_eq!(System::events().len(), 0);
});
}
#[test]
fn refund_consistent_with_actual_weight() {
ExtBuilder::default()
.balance_factor(10)
.base_weight(7)
.build()
.execute_with(||
{
let info = info_from_weight(100);
let post_info = post_info_from_weight(33);
let prev_balance = Balances::free_balance(2);
let len = 10;
let tip = 5;
NextFeeMultiplier::put(Multiplier::saturating_from_rational(5, 4));
let pre = ChargeTransactionPayment::<Runtime>::from(tip)
.pre_dispatch(&2, CALL, &info, len)
.unwrap();
ChargeTransactionPayment::<Runtime>
::post_dispatch(pre, &info, &post_info, len, &Ok(()))
.unwrap();
let refund_based_fee = prev_balance - Balances::free_balance(2);
let actual_fee = Module::<Runtime>
::compute_actual_fee(len as u32, &info, &post_info, tip);
// 33 weight, 10 length, 7 base, 5 tip
assert_eq!(actual_fee, 7 + 10 + (33 * 5 / 4) + 5);
assert_eq!(refund_based_fee, actual_fee);
});
}