Skip to content

Commit

Permalink
Fix default bridge byte fee to use TransactionByteFee of the bridged …
Browse files Browse the repository at this point in the history
…chain (#174)

This PR does two things:
1) (tiny refactoring) adds `TRANSACTION_BYTE_FEE` constant to
`system-parachains-constants` crate. We are using the same value (`1 *
MILLICENTS`) across all chains, so it makes sense to move it there;
2) changes default value of governance-controlled
`XcmBridgeHubRouterByteFee` constant (which is used to cover the cost of
message delivery within the remote consensus) to use
`TRANSACTION_BYTE_FEE` of the bridged chain (also does 1:5/5:1
conversion). Since governance has not set value of this constant yet, we
are changing the default value here, in the code.

~The failing test is fixed by the #137, so it makes sense to merge this
PR only after #137~ Updated

---------

Co-authored-by: Branislav Kontur <[email protected]>
Co-authored-by: Kian Paimani <[email protected]>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent f1fb358 commit 55e97c5
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Removed the `SafeCallFilter` from the Relay Chain XCM config ([polkadot-fellows/runtimes#172](https://github.com/polkadot-fellows/runtimes/pull/172)).
- Removed the `ImOnline` pallet ([polkadot-fellows/runtimes#178](https://github.com/polkadot-fellows/runtimes/pull/178))

### Fixed

- Fixed the cost of a single byte, sent over bridge to use the `TransactionByteFee` constant of the bridged chain [polkadot-fellows/runtimes#174](https://github.com/polkadot-fellows/runtimes/pull/174).

### Based on Polkadot-SDK

- Upgrade dependencies to the [[email protected]](https://github.com/paritytech/polkadot-sdk/releases/tag/polkadot-v1.5.0) release ([polkadot-fellows/runtimes#137](https://github.com/polkadot-fellows/runtimes/pull/137))
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions relay/kusama/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub mod fee {
/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);

/// Cost of every transaction byte at Kusama relay chain.
pub const TRANSACTION_BYTE_FEE: Balance = 10 * super::currency::MILLICENTS;

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
Expand Down
2 changes: 1 addition & 1 deletion relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl pallet_beefy_mmr::Config for Runtime {
}

parameter_types! {
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
pub const TransactionByteFee: Balance = kusama_runtime_constants::fee::TRANSACTION_BYTE_FEE;
/// This value increases the priority of `Operational` transactions by adding
/// a "virtual tip" that's equal to the `OperationalFeeMultiplier * final_fee`.
pub const OperationalFeeMultiplier: u8 = 5;
Expand Down
3 changes: 3 additions & 0 deletions relay/polkadot/constants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub mod fee {
/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);

/// Cost of every transaction byte at Kusama relay chain.
pub const TRANSACTION_BYTE_FEE: Balance = 10 * super::currency::MILLICENTS;

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
Expand Down
2 changes: 1 addition & 1 deletion relay/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl pallet_beefy_mmr::Config for Runtime {
}

parameter_types! {
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
pub const TransactionByteFee: Balance = polkadot_runtime_constants::fee::TRANSACTION_BYTE_FEE;
/// This value increases the priority of `Operational` transactions by adding
/// a "virtual tip" that's equal to the `OperationalFeeMultiplier * final_fee`.
pub const OperationalFeeMultiplier: u8 = 5;
Expand Down
9 changes: 8 additions & 1 deletion system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = MILLICENTS;
pub const TransactionByteFee: Balance = system_parachains_constants::kusama::fee::TRANSACTION_BYTE_FEE;
}

impl pallet_transaction_payment::Config for Runtime {
Expand Down Expand Up @@ -1709,4 +1709,11 @@ mod tests {
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);
}

#[test]
fn test_transasction_byte_fee_is_one_tenth_of_relay() {
let relay_tbf = kusama_runtime_constants::fee::TRANSACTION_BYTE_FEE;
let parachain_tbf = TransactionByteFee::get();
assert_eq!(relay_tbf / 10, parachain_tbf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use super::{
AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo,
ParachainSystem, PolkadotXcm, PoolAssets, PriceForParentDelivery, Runtime, RuntimeCall,
RuntimeEvent, RuntimeOrigin, ToPolkadotXcmRouter, TransactionByteFee,
TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
RuntimeEvent, RuntimeOrigin, ToPolkadotXcmRouter, TrustBackedAssetsInstance, WeightToFee,
XcmpQueue,
};
use crate::{ForeignAssets, ForeignAssetsInstance};
use assets_common::matching::{FromSiblingParachain, IsForeignConcreteAsset};
Expand Down Expand Up @@ -660,7 +660,7 @@ pub mod bridging {
);
/// Price of every byte of the Kusama -> Polkadot message. Can be adjusted via
/// governance `set_storage` call.
pub storage XcmBridgeHubRouterByteFee: Balance = TransactionByteFee::get();
pub storage XcmBridgeHubRouterByteFee: Balance = bp_bridge_hub_kusama::estimate_kusama_to_polkadot_byte_fee();

pub SiblingBridgeHubParaId: u32 = bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID;
pub SiblingBridgeHub: MultiLocation = MultiLocation::new(1, X1(Parachain(SiblingBridgeHubParaId::get())));
Expand Down
9 changes: 8 additions & 1 deletion system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = MILLICENTS;
pub const TransactionByteFee: Balance = system_parachains_constants::polkadot::fee::TRANSACTION_BYTE_FEE;
}

impl pallet_transaction_payment::Config for Runtime {
Expand Down Expand Up @@ -1537,4 +1537,11 @@ mod tests {
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);
}

#[test]
fn test_transasction_byte_fee_is_one_tenth_of_relay() {
let relay_tbf = polkadot_runtime_constants::fee::TRANSACTION_BYTE_FEE;
let parachain_tbf = TransactionByteFee::get();
assert_eq!(relay_tbf / 10, parachain_tbf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use super::{
AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ForeignAssets,
ParachainInfo, ParachainSystem, PolkadotXcm, PriceForParentDelivery, Runtime, RuntimeCall,
RuntimeEvent, RuntimeOrigin, ToKusamaXcmRouter, TransactionByteFee, TrustBackedAssetsInstance,
WeightToFee, XcmpQueue,
RuntimeEvent, RuntimeOrigin, ToKusamaXcmRouter, TrustBackedAssetsInstance, WeightToFee,
XcmpQueue,
};
use crate::ForeignAssetsInstance;
use assets_common::matching::{FromSiblingParachain, IsForeignConcreteAsset};
Expand Down Expand Up @@ -604,7 +604,7 @@ pub mod bridging {
);
/// Price of every byte of the Polkadot -> Kusama message. Can be adjusted via
/// governance `set_storage` call.
pub storage XcmBridgeHubRouterByteFee: Balance = TransactionByteFee::get();
pub storage XcmBridgeHubRouterByteFee: Balance = bp_bridge_hub_polkadot::estimate_polkadot_to_kusama_byte_fee();

pub SiblingBridgeHubParaId: u32 = bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID;
pub SiblingBridgeHub: MultiLocation = MultiLocation::new(1, X1(Parachain(SiblingBridgeHubParaId::get())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ license.workspace = true
[dependencies]

# Local
kusama-runtime-constants = { path = "../../../../relay/kusama/constants", default-features = false}
polkadot-runtime-constants = { path = "../../../../relay/polkadot/constants", default-features = false}
kusama-runtime-constants = { path = "../../../../relay/kusama/constants", default-features = false }
polkadot-runtime-constants = { path = "../../../../relay/polkadot/constants", default-features = false }
system-parachains-constants = { path = "../../../constants", default-features = false }

# Bridge Dependencies
bp-bridge-hub-cumulus = { default-features = false , version = "0.7.0" }
Expand All @@ -36,4 +37,5 @@ std = [
"sp-api/std",
"sp-runtime/std",
"sp-std/std",
"system-parachains-constants/std",
]
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ pub fn estimate_kusama_to_polkadot_message_fee(
.saturating_add(BridgeHubKusamaBaseConfirmationFeeInKsms::get())
}

/// Compute the per-byte fee that needs to be paid in KSMs by the sender when sending
/// message from Kusama Bridge Hub to Polkadot Bridge Hub.
pub fn estimate_kusama_to_polkadot_byte_fee() -> Balance {
// the sender pays for the same byte twice:
// 1) the first part comes from the HRMP, when message travels from Kusama Asset Hub to Kusama
// Bridge Hub;
// 2) the second part is the payment for bytes of the message delivery transaction, which is
// "mined" at Polkadot Bridge Hub. Hence, we need to use byte fees from that chain and
// convert it to KSMs here.
convert_from_udot_to_uksm(system_parachains_constants::polkadot::fee::TRANSACTION_BYTE_FEE)
}

/// Convert from uDOTs to uKSMs.
fn convert_from_udot_to_uksm(price_in_udot: Balance) -> Balance {
// assuming exchange rate is 5 DOTs for 1 KSM
Expand Down
14 changes: 13 additions & 1 deletion system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = MILLICENTS;
pub const TransactionByteFee: Balance = system_parachains_constants::kusama::fee::TRANSACTION_BYTE_FEE;
}

impl pallet_transaction_payment::Config for Runtime {
Expand Down Expand Up @@ -1116,3 +1116,15 @@ cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_transasction_byte_fee_is_one_tenth_of_relay() {
let relay_tbf = kusama_runtime_constants::fee::TRANSACTION_BYTE_FEE;
let parachain_tbf = TransactionByteFee::get();
assert_eq!(relay_tbf / 10, parachain_tbf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ license.workspace = true
[dependencies]

# Local
kusama-runtime-constants = { path = "../../../../relay/kusama/constants", default-features = false}
polkadot-runtime-constants = { path = "../../../../relay/polkadot/constants", default-features = false}
kusama-runtime-constants = { path = "../../../../relay/kusama/constants", default-features = false }
polkadot-runtime-constants = { path = "../../../../relay/polkadot/constants", default-features = false }
system-parachains-constants = { path = "../../../constants", default-features = false }

# Bridge Dependencies
bp-bridge-hub-cumulus = { default-features = false , version = "0.7.0" }
Expand All @@ -36,4 +37,5 @@ std = [
"sp-api/std",
"sp-runtime/std",
"sp-std/std",
"system-parachains-constants/std",
]
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ pub fn estimate_polkadot_to_kusama_message_fee(
.saturating_add(BridgeHubPolkadotBaseConfirmationFeeInDots::get())
}

/// Compute the per-byte fee that needs to be paid in DOTs by the sender when sending
/// message from Polkadot Bridge Hub to Kusama Bridge Hub.
pub fn estimate_polkadot_to_kusama_byte_fee() -> Balance {
// the sender pays for the same byte twice:
// 1) the first part comes from the HRMP, when message travels from Polkadot Asset Hub to
// Polkadot Bridge Hub;
// 2) the second part is the payment for bytes of the message delivery transaction, which is
// "mined" at Kusama Bridge Hub. Hence, we need to use byte fees from that chain and convert
// it to DOTs here.
convert_from_uksm_to_udot(system_parachains_constants::kusama::fee::TRANSACTION_BYTE_FEE)
}

/// Convert from uKSMs to uDOTs.
fn convert_from_uksm_to_udot(price_in_uksm: Balance) -> Balance {
// assuming exchange rate is 5 DOTs for 1 KSM
Expand Down
14 changes: 13 additions & 1 deletion system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = MILLICENTS;
pub const TransactionByteFee: Balance = system_parachains_constants::polkadot::fee::TRANSACTION_BYTE_FEE;
}

impl pallet_transaction_payment::Config for Runtime {
Expand Down Expand Up @@ -1116,3 +1116,15 @@ cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_transasction_byte_fee_is_one_tenth_of_relay() {
let relay_tbf = polkadot_runtime_constants::fee::TRANSACTION_BYTE_FEE;
let parachain_tbf = TransactionByteFee::get();
assert_eq!(relay_tbf / 10, parachain_tbf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = MILLICENTS;
pub const TransactionByteFee: Balance = system_parachains_constants::polkadot::fee::TRANSACTION_BYTE_FEE;
}

impl pallet_transaction_payment::Config for Runtime {
Expand Down Expand Up @@ -1061,3 +1061,10 @@ fn test_ed_is_one_tenth_of_relay() {
let collectives_ed = ExistentialDeposit::get();
assert_eq!(relay_ed / 10, collectives_ed);
}

#[test]
fn test_transasction_byte_fee_is_one_tenth_of_relay() {
let relay_tbf = polkadot_runtime_constants::fee::TRANSACTION_BYTE_FEE;
let parachain_tbf = TransactionByteFee::get();
assert_eq!(relay_tbf / 10, parachain_tbf);
}
5 changes: 5 additions & 0 deletions system-parachains/constants/src/kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ pub mod fee {
/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);

/// Cost of every transaction byte at Kusama system parachains.
///
/// It is the Relay Chain (Kusama) `TransactionByteFee` / 10.
pub const TRANSACTION_BYTE_FEE: Balance = super::currency::MILLICENTS;

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
Expand Down
5 changes: 5 additions & 0 deletions system-parachains/constants/src/polkadot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ pub mod fee {
/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);

/// Cost of every transaction byte at Polkadot system parachains.
///
/// It is the Relay Chain (Polkadot) `TransactionByteFee` / 10.
pub const TRANSACTION_BYTE_FEE: Balance = super::currency::MILLICENTS;

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
Expand Down
18 changes: 17 additions & 1 deletion system-parachains/encointer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10, same as statemine
pub const TransactionByteFee: Balance = MILLICENTS;
pub const TransactionByteFee: Balance = system_parachains_constants::kusama::fee::TRANSACTION_BYTE_FEE;
pub const OperationalFeeMultiplier: u8 = 5;
}

Expand Down Expand Up @@ -1116,6 +1116,10 @@ fn test_constants_compatiblity() {
::system_parachains_constants::kusama::currency::system_para_deposit(5, 3),
system_parachains_constants::kusama::currency::system_para_deposit(5, 3)
);
assert_eq!(
::system_parachains_constants::kusama::fee::TRANSACTION_BYTE_FEE,
system_parachains_constants::kusama::fee::TRANSACTION_BYTE_FEE
);
assert_eq!(
::system_parachains_constants::kusama::fee::calculate_weight_to_fee(
&::system_parachains_constants::MAXIMUM_BLOCK_WEIGHT
Expand All @@ -1126,6 +1130,13 @@ fn test_constants_compatiblity() {
);
}

#[test]
fn test_transasction_byte_fee_is_one_tenth_of_relay() {
let relay_tbf = ::kusama_runtime_constants::fee::TRANSACTION_BYTE_FEE;
let parachain_tbf = TransactionByteFee::get();
assert_eq!(relay_tbf / 10, parachain_tbf);
}

// The Encointer pallets do not have compatible versions with `polkadot-sdk`, making it difficult
// for us to reuse the `system-parachains-constants` module. Therefore, we have copies of it here
// with `test_constants_compatiblity`.
Expand Down Expand Up @@ -1209,6 +1220,11 @@ mod system_parachains_constants {
use smallvec::smallvec;
pub use sp_runtime::Perbill;

/// Cost of every transaction byte at Kusama system parachains.
///
/// It is the Relay Chain (Kusama) `TransactionByteFee` / 10.
pub const TRANSACTION_BYTE_FEE: Balance = super::currency::MILLICENTS;

/// Handles converting a weight scalar to a fee value, based on the scale and
/// granularity of the node's balance type.
///
Expand Down

0 comments on commit 55e97c5

Please sign in to comment.