Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bko-bump-to-1.6' into bko-bump-t…
Browse files Browse the repository at this point in the history
…o-1.7
  • Loading branch information
bkontur committed Feb 16, 2024
2 parents 8365351 + e3a80b5 commit cdd6bf7
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 83 deletions.
4 changes: 1 addition & 3 deletions relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,7 @@ impl pallet_staking::Config for Runtime {
type NominationsQuota = pallet_staking::FixedNominationsQuota<{ MaxNominations::get() }>;
type MaxUnlockingChunks = frame_support::traits::ConstU32<32>;
type HistoryDepth = frame_support::traits::ConstU32<84>;
// TODO:(PR#159) check/set correct constant and remove this comment!
type MaxControllersInDeprecationBatch = ConstU32<751>;
type MaxControllersInDeprecationBatch = ConstU32<5169>;
type BenchmarkingConfig = runtime_common::StakingBenchmarkingConfig;
type EventListeners = NominationPools;
type WeightInfo = weights::pallet_staking::WeightInfo<Runtime>;
Expand Down Expand Up @@ -935,7 +934,6 @@ impl pallet_identity::Config for Runtime {
type Slashed = Treasury;
type ForceOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;
type RegistrarOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;
// TODO:(PR#159) check adds bellow and remove this comment!
type OffchainSignature = Signature;
type SigningPublicKey = <Signature as Verify>::Signer;
type UsernameAuthorityOrigin = EnsureRoot<Self::AccountId>;
Expand Down
4 changes: 1 addition & 3 deletions relay/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,7 @@ impl pallet_staking::Config for Runtime {
type NominationsQuota = pallet_staking::FixedNominationsQuota<{ MaxNominations::get() }>;
type MaxUnlockingChunks = frame_support::traits::ConstU32<32>;
type HistoryDepth = frame_support::traits::ConstU32<84>;
// TODO:(PR#159) check/set correct constant and remove this comment!
type MaxControllersInDeprecationBatch = ConstU32<751>;
type MaxControllersInDeprecationBatch = ConstU32<5314>;
type BenchmarkingConfig = runtime_common::StakingBenchmarkingConfig;
type EventListeners = NominationPools;
type WeightInfo = weights::pallet_staking::WeightInfo<Runtime>;
Expand Down Expand Up @@ -816,7 +815,6 @@ impl pallet_identity::Config for Runtime {
type Slashed = Treasury;
type ForceOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;
type RegistrarOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;
// TODO:(PR#159) check adds bellow and remove this comment!
type OffchainSignature = Signature;
type SigningPublicKey = <Signature as Verify>::Signer;
type UsernameAuthorityOrigin = EnsureRoot<Self::AccountId>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ frame_support::parameter_types! {
/// The XCM fee that is paid for executing XCM program (with `ExportMessage` instruction) at the Kusama
/// BridgeHub.
/// (initially was calculated by test `BridgeHubKusama::can_calculate_weight_for_paid_export_message_with_reserve_transfer` + `33%`)
pub const BridgeHubKusamaBaseXcmFeeInKsms: u128 = 16_196_533_317;
pub const BridgeHubKusamaBaseXcmFeeInKsms: u128 = 16_156_041_984;

/// Transaction fee that is paid at the Kusama BridgeHub for delivering single inbound message.
/// (initially was calculated by test `BridgeHubKusama::can_calculate_fee_for_complex_message_delivery_transaction` + `33%`)
pub const BridgeHubKusamaBaseDeliveryFeeInKsms: u128 = 56_516_280_489;
pub const BridgeHubKusamaBaseDeliveryFeeInKsms: u128 = 56_374_989_788;

/// Transaction fee that is paid at the Kusama BridgeHub for delivering single outbound message confirmation.
/// (initially was calculated by test `BridgeHubKusama::can_calculate_fee_for_complex_message_confirmation_transaction` + `33%`)
pub const BridgeHubKusamaBaseConfirmationFeeInKsms: u128 = 53_943_614_276;
pub const BridgeHubKusamaBaseConfirmationFeeInKsms: u128 = 53_808_755_240;
}

/// Compute the total estimated fee that needs to be paid in KSMs by the sender when sending
Expand Down
146 changes: 111 additions & 35 deletions system-parachains/bridge-hubs/bridge-hub-kusama/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use bridge_hub_kusama_runtime::{
xcm_config::{KsmRelayLocation, RelayNetwork, XcmConfig},
AllPalletsWithoutSystem, BridgeRejectObsoleteHeadersAndMessages, Executive, ExistentialDeposit,
ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, SessionKeys,
SignedExtra, UncheckedExtrinsic,
SignedExtra, TransactionPayment, UncheckedExtrinsic,
};
use bridge_hub_test_utils::test_cases::from_parachain;
use codec::{Decode, Encode};
use frame_support::{parameter_types, traits::ConstU8};
use frame_support::{dispatch::GetDispatchInfo, parameter_types, traits::ConstU8};
use parachains_common::{AccountId, AuraId, Balance};
use sp_keyring::AccountKeyring::Alice;
use sp_runtime::{
Expand Down Expand Up @@ -97,6 +97,13 @@ fn construct_and_apply_extrinsic(
r.unwrap()
}

fn construct_and_estimate_extrinsic_fee(batch: pallet_utility::Call<Runtime>) -> Balance {
let batch_call = RuntimeCall::Utility(batch);
let batch_info = batch_call.get_dispatch_info();
let xt = construct_extrinsic(Alice, batch_call);
TransactionPayment::compute_fee(xt.encoded_size() as _, &batch_info, 0)
}

fn collator_session_keys() -> bridge_hub_test_utils::CollatorSessionKeys<Runtime> {
bridge_hub_test_utils::CollatorSessionKeys::new(
AccountId::from(Alice),
Expand Down Expand Up @@ -261,42 +268,111 @@ pub fn complex_relay_extrinsic_works() {

#[test]
pub fn can_calculate_weight_for_paid_export_message_with_reserve_transfer() {
let estimated = bridge_hub_test_utils::test_cases::can_calculate_weight_for_paid_export_message_with_reserve_transfer::<
Runtime,
XcmConfig,
WeightToFee,
>();
check_sane_fees_values(
"bp_bridge_hub_kusama::BridgeHubKusamaBaseXcmFeeInKsms",
bp_bridge_hub_kusama::BridgeHubKusamaBaseXcmFeeInKsms::get(),
|| {
bridge_hub_test_utils::test_cases::can_calculate_weight_for_paid_export_message_with_reserve_transfer::<
Runtime,
XcmConfig,
WeightToFee,
>()
},
Perbill::from_percent(33),
Some(-33),
&format!(
"Estimate fee for `ExportMessage` for runtime: {:?}",
<Runtime as frame_system::Config>::Version::get()
),
)
}

#[test]
pub fn can_calculate_fee_for_complex_message_delivery_transaction() {
check_sane_fees_values(
"bp_bridge_hub_kusama::BridgeHubKusamaBaseDeliveryFeeInKsms",
bp_bridge_hub_kusama::BridgeHubKusamaBaseDeliveryFeeInKsms::get(),
|| {
from_parachain::can_calculate_fee_for_complex_message_delivery_transaction::<
RuntimeTestsAdapter,
>(collator_session_keys(), construct_and_estimate_extrinsic_fee)
},
Perbill::from_percent(33),
Some(-33),
&format!(
"Estimate fee for `single message delivery` for runtime: {:?}",
<Runtime as frame_system::Config>::Version::get()
),
)
}

#[test]
pub fn can_calculate_fee_for_complex_message_confirmation_transaction() {
check_sane_fees_values(
"bp_bridge_hub_kusama::BridgeHubKusamaBaseConfirmationFeeInKsms",
bp_bridge_hub_kusama::BridgeHubKusamaBaseConfirmationFeeInKsms::get(),
|| {
from_parachain::can_calculate_fee_for_complex_message_confirmation_transaction::<
RuntimeTestsAdapter,
>(collator_session_keys(), construct_and_estimate_extrinsic_fee)
},
Perbill::from_percent(33),
Some(-33),
&format!(
"Estimate fee for `single message confirmation` for runtime: {:?}",
<Runtime as frame_system::Config>::Version::get()
),
)
}

use sp_runtime::Perbill;

// TODO:(PR#159): remove when `polkadot-sdk@1.8.0` bump (https://github.com/polkadot-fellows/runtimes/issues/186)
/// A helper function for comparing the actual value of a fee constant with its estimated value. The
/// estimated value can be overestimated (`overestimate_in_percent`), and if the difference to the
/// actual value is below `margin_overestimate_diff_in_percent_for_lowering`, we should lower the
/// actual value.
pub fn check_sane_fees_values(
const_name: &str,
actual: u128,
calculate_estimated_fee: fn() -> u128,
overestimate_in_percent: Perbill,
margin_overestimate_diff_in_percent_for_lowering: Option<i16>,
label: &str,
) {
let estimated = calculate_estimated_fee();
let estimated_plus_overestimate = estimated + (overestimate_in_percent * estimated);
let diff_to_estimated = diff_as_percent(actual, estimated);
let diff_to_estimated_plus_overestimate = diff_as_percent(actual, estimated_plus_overestimate);

log::error!(
target: "bridges::estimate",
"{label}:\nconstant: {const_name}\n[+] actual: {actual}\n[+] estimated: {estimated} ({diff_to_estimated:.2?})\n[+] estimated(+33%): {estimated_plus_overestimate} ({diff_to_estimated_plus_overestimate:.2?})",
);

// check if estimated value is sane
let max_expected = bp_bridge_hub_kusama::BridgeHubKusamaBaseXcmFeeInKsms::get();
assert!(
estimated <= max_expected,
"calculated: {:?}, max_expected: {:?}, please adjust `bp_bridge_hub_kusama::BridgeHubKusamaBaseXcmFeeInKsms` value",
estimated,
max_expected
estimated <= actual,
"estimated: {estimated}, actual: {actual}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
);
assert!(
estimated_plus_overestimate <= actual,
"estimated_plus_overestimate: {estimated_plus_overestimate}, actual: {actual}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
);

if let Some(margin_overestimate_diff_in_percent_for_lowering) =
margin_overestimate_diff_in_percent_for_lowering
{
assert!(
diff_to_estimated_plus_overestimate > margin_overestimate_diff_in_percent_for_lowering as f64,
"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}",
);
}
}

// TODO: replace me with direct usages of `bridge_hub_test_utils` after deps are bumped to (at
// least) 1.4
//
// Following two tests have to be implemented properly after upgrade to 1.6.
// See https://github.com/paritytech/polkadot-sdk/pull/2139/ and https://github.com/paritytech/parity-bridges-common/pull/2728
// for impl details
//
// Until that, anyone can run it manually by doing following:
//
// 1) cargo vendor ../vendored-dependencies
// 2) apply relevant changes from above PRs
// 3) change workspace Cargo.toml:
// [patch.crates-io]
// bp-polkadot-core = { path = "../vendored-dependencies/bp-polkadot-core" }
// bridge-hub-test-utils = { path = "../vendored-dependencies/bridge-hub-test-utils" }
// bridge-runtime-common = { path = "../vendored-dependencies/bridge-runtime-common" }
// 4) add actual tests code and do `cargo test -p bridge-hub-polkadot-runtime`

#[test]
pub fn can_calculate_fee_for_complex_message_delivery_transaction() {}

#[test]
pub fn can_calculate_fee_for_complex_message_confirmation_transaction() {}
// TODO:(PR#159): remove when `polkadot-sdk@1.8.0` bump (https://github.com/polkadot-fellows/runtimes/issues/186)
pub fn diff_as_percent(left: u128, right: u128) -> f64 {
let left = left as f64;
let right = right as f64;
((left - right).abs() / left) * 100f64 * (if left >= right { -1 } else { 1 }) as f64
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ frame_support::parameter_types! {
/// The XCM fee that is paid for executing XCM program (with `ExportMessage` instruction) at the Polkadot
/// BridgeHub.
/// (initially was calculated by test `BridgeHubPolkadot::can_calculate_weight_for_paid_export_message_with_reserve_transfer` + `33%`)
pub const BridgeHubPolkadotBaseXcmFeeInDots: Balance = 4_858_960_000;
pub const BridgeHubPolkadotBaseXcmFeeInDots: Balance = 4_846_812_600;

/// Transaction fee that is paid at the Polkadot BridgeHub for delivering single inbound message.
/// (initially was calculated by test `BridgeHubPolkadot::can_calculate_fee_for_complex_message_delivery_transaction` + `33%`)
pub const BridgeHubPolkadotBaseDeliveryFeeInDots: Balance = 16_954_899_613;
pub const BridgeHubPolkadotBaseDeliveryFeeInDots: Balance = 16_912_512_364;

/// Transaction fee that is paid at the Polkadot BridgeHub for delivering single outbound message confirmation.
/// (initially was calculated by test `BridgeHubPolkadot::can_calculate_fee_for_complex_message_confirmation_transaction` + `33%`)
pub const BridgeHubPolkadotBaseConfirmationFeeInDots: Balance = 16_183_099_613;
pub const BridgeHubPolkadotBaseConfirmationFeeInDots: Balance = 16_142_641_864;
}

/// Compute the total estimated fee that needs to be paid in DOTs by the sender when sending
Expand Down
Loading

0 comments on commit cdd6bf7

Please sign in to comment.