1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
45// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
1617//! Module contains predefined test-case scenarios for "BridgeHub" `Runtime`s.
1819pub mod test_cases;
20pub mod test_data;
2122extern crate alloc;
2324pub 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};
3031/// A helper function for comparing the actual value of a fee constant with its estimated value. The
32/// estimated value can be overestimated (`overestimate_in_percent`), and if the difference to the
33/// actual value is below `margin_overestimate_diff_in_percent_for_lowering`, we should lower the
34/// actual value.
35pub 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) {
43let estimated = calculate_estimated_fee();
44let estimated_plus_overestimate = estimated + (overestimate_in_percent * estimated);
45let diff_to_estimated = diff_as_percent(actual, estimated);
46let diff_to_estimated_plus_overestimate = diff_as_percent(actual, estimated_plus_overestimate);
4748 sp_tracing::try_init_simple();
49tracing::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);
5455// check if estimated value is sane
56assert!(
57 estimated <= actual,
58"estimated: {estimated}, actual: {actual}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
59 );
60assert!(
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 );
6465if let Some(margin_overestimate_diff_in_percent_for_lowering) =
66 margin_overestimate_diff_in_percent_for_lowering
67 {
68assert!(
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}
7475pub fn diff_as_percent(left: u128, right: u128) -> f64 {
76let left = left as f64;
77let right = right as f64;
78 ((left - right).abs() / left) * 100f64 * (if left >= right { -1 } else { 1 }) as f64
79}
8081#[test]
82fn diff_as_percent_works() {
83assert_eq!(-20_f64, diff_as_percent(100, 80));
84assert_eq!(25_f64, diff_as_percent(80, 100));
85assert_eq!(33_f64, diff_as_percent(13351000000, 17756830000));
86}