bridge_hub_test_utils/
lib.rs1pub mod test_cases;
20pub mod test_data;
21
22extern crate alloc;
23
24pub use bp_test_utils::test_header;
25pub use parachains_runtimes_test_utils::*;
26use sp_runtime::Perbill;
27pub use test_cases::helpers::for_pallet_xcm_bridge_hub::{
28 ensure_opened_bridge, open_bridge_with_extrinsic, open_bridge_with_storage,
29};
30
31pub fn check_sane_fees_values(
36 const_name: &str,
37 actual: u128,
38 calculate_estimated_fee: fn() -> u128,
39 overestimate_in_percent: Perbill,
40 margin_overestimate_diff_in_percent_for_lowering: Option<i16>,
41 label: &str,
42) {
43 let estimated = calculate_estimated_fee();
44 let estimated_plus_overestimate = estimated + (overestimate_in_percent * estimated);
45 let diff_to_estimated = diff_as_percent(actual, estimated);
46 let diff_to_estimated_plus_overestimate = diff_as_percent(actual, estimated_plus_overestimate);
47
48 sp_tracing::try_init_simple();
49 tracing::error!(
50 target: "bridges::estimate",
51 %label, constant=%const_name, %actual, %estimated,
52 "{diff_to_estimated:.2?})\n[+] estimated(+33%): {estimated_plus_overestimate} ({diff_to_estimated_plus_overestimate:.2?}"
53 );
54
55 assert!(
57 estimated <= actual,
58 "estimated: {estimated}, actual: {actual}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
59 );
60 assert!(
61 estimated_plus_overestimate <= actual,
62 "estimated_plus_overestimate: {estimated_plus_overestimate}, actual: {actual}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
63 );
64
65 if let Some(margin_overestimate_diff_in_percent_for_lowering) =
66 margin_overestimate_diff_in_percent_for_lowering
67 {
68 assert!(
69 diff_to_estimated_plus_overestimate > margin_overestimate_diff_in_percent_for_lowering as f64,
70 "diff_to_estimated_plus_overestimate: {diff_to_estimated_plus_overestimate:.2}, overestimate_diff_in_percent_for_lowering: {margin_overestimate_diff_in_percent_for_lowering}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
71 );
72 }
73}
74
75pub fn diff_as_percent(left: u128, right: u128) -> f64 {
76 let left = left as f64;
77 let right = right as f64;
78 ((left - right).abs() / left) * 100f64 * (if left >= right { -1 } else { 1 }) as f64
79}
80
81#[test]
82fn diff_as_percent_works() {
83 assert_eq!(-20_f64, diff_as_percent(100, 80));
84 assert_eq!(25_f64, diff_as_percent(80, 100));
85 assert_eq!(33_f64, diff_as_percent(13351000000, 17756830000));
86}