Parachains should charge for proof size weight (#2326)

* Generalize trait requirement

Use the trait directly instead of something that will have a blanket
implementation for it.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Charge for proof size weight

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix doc comments

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>

* Fix docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update lock file

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix imports

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix imports

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
This commit is contained in:
Oliver Tale-Yazdi
2023-03-17 17:25:13 +01:00
committed by GitHub
parent 1782170e08
commit 91b3512427
16 changed files with 509 additions and 47 deletions
@@ -1125,3 +1125,59 @@ fn ensure_key_ss58() {
assert_eq!(acc, RootMigController::sorted_members()[0]);
//panic!("{:x?}", acc);
}
#[cfg(test)]
mod tests {
use super::{constants::fee, *};
use crate::{CENTS, MILLICENTS};
use sp_runtime::traits::Zero;
use sp_weights::WeightToFee;
/// We can fit at least 1000 transfers in a block.
#[test]
fn sane_block_weight() {
use pallet_balances::WeightInfo;
let block = RuntimeBlockWeights::get().max_block;
let base = RuntimeBlockWeights::get().get(DispatchClass::Normal).base_extrinsic;
let transfer = base + weights::pallet_balances::WeightInfo::<Runtime>::transfer();
let fit = block.checked_div_per_component(&transfer).unwrap_or_default();
assert!(fit >= 1000, "{} should be at least 1000", fit);
}
/// The fee for one transfer is at most 1 CENT.
#[test]
fn sane_transfer_fee() {
use pallet_balances::WeightInfo;
let base = RuntimeBlockWeights::get().get(DispatchClass::Normal).base_extrinsic;
let transfer = base + weights::pallet_balances::WeightInfo::<Runtime>::transfer();
let fee: Balance = fee::WeightToFee::weight_to_fee(&transfer);
assert!(fee <= CENTS, "{} MILLICENTS should be at most 1000", fee / MILLICENTS);
}
/// Weight is being charged for both dimensions.
#[test]
fn weight_charged_for_both_components() {
let fee: Balance = fee::WeightToFee::weight_to_fee(&Weight::from_parts(10_000, 0));
assert!(!fee.is_zero(), "Charges for ref time");
let fee: Balance = fee::WeightToFee::weight_to_fee(&Weight::from_parts(0, 10_000));
assert_eq!(fee, CENTS, "10kb maps to CENT");
}
/// Filling up a block by proof size is at most 30 times more expensive than ref time.
///
/// This is just a sanity check.
#[test]
fn full_block_fee_ratio() {
let block = RuntimeBlockWeights::get().max_block;
let time_fee: Balance = fee::WeightToFee::weight_to_fee(&block.without_proof_size());
let proof_fee: Balance = fee::WeightToFee::weight_to_fee(&block.without_ref_time());
let proof_o_time = proof_fee.checked_div(time_fee).unwrap_or_default();
assert!(proof_o_time <= 30, "{} should be at most 30", proof_o_time);
let time_o_proof = time_fee.checked_div(proof_fee).unwrap_or_default();
assert!(time_o_proof <= 30, "{} should be at most 30", time_o_proof);
}
}