Skip to content

Commit

Permalink
Fix delivery transaction estimation used by rational relayer (#1109)
Browse files Browse the repository at this point in the history
* fix delivery transaction estimation in greedy relayer

* fixed typo

* improve logging

* improve logging

* fmt

* fix compilation

* fmt

* Update relays/lib-substrate-relay/src/messages_target.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* review

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
  • Loading branch information
2 people authored and serban300 committed Apr 8, 2024
1 parent 1d4bfd4 commit 7e6a0f1
Show file tree
Hide file tree
Showing 35 changed files with 391 additions and 71 deletions.
2 changes: 1 addition & 1 deletion bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ parameter_types! {
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter<Balances, ()>;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = IdentityFee<Balance>;
type WeightToFee = bp_millau::WeightToFee;
type FeeMultiplierUpdate = pallet_transaction_payment::TargetedFeeAdjustment<
Runtime,
TargetBlockFullness,
Expand Down
2 changes: 1 addition & 1 deletion bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ parameter_types! {
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter<Balances, ()>;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = IdentityFee<Balance>;
type WeightToFee = bp_rialto::WeightToFee;
type FeeMultiplierUpdate = pallet_transaction_payment::TargetedFeeAdjustment<
Runtime,
TargetBlockFullness,
Expand Down
5 changes: 5 additions & 0 deletions bridges/primitives/chain-kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
smallvec = "1.6"

# Bridge Dependencies

bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }

# Substrate Based Dependencies

frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

Expand All @@ -23,6 +27,7 @@ std = [
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
"frame-support/std",
"sp-api/std",
"sp-std/std",
]
19 changes: 19 additions & 0 deletions bridges/primitives/chain-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,32 @@
#![allow(clippy::unnecessary_mut_passed)]

use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use frame_support::weights::{WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial};
use sp_std::prelude::*;

pub use bp_polkadot_core::*;

/// Kusama Chain
pub type Kusama = PolkadotLike;

// NOTE: This needs to be kept up to date with the Kusama runtime found in the Polkadot repo.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
const CENTS: Balance = 1_000_000_000_000 / 30_000;
// in Kusama, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

// We use this to get the account on Kusama (target) which is derived from Polkadot's (source)
// account.
pub fn derive_account_from_polkadot_id(id: bp_runtime::SourceAccount<AccountId>) -> AccountId {
Expand Down
5 changes: 4 additions & 1 deletion bridges/primitives/chain-millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod millau_hash;
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::{
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight},
Parameter, RuntimeDebug,
};
use frame_system::limits;
Expand Down Expand Up @@ -149,6 +149,9 @@ pub type AccountSigner = MultiSigner;
/// Balance of an account.
pub type Balance = u64;

/// Weight-to-Fee type used by Millau.
pub type WeightToFee = IdentityFee<Balance>;

/// Millau chain.
#[derive(RuntimeDebug)]
pub struct Millau;
Expand Down
4 changes: 4 additions & 0 deletions bridges/primitives/chain-polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ edition = "2018"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
smallvec = "1.6"

# Bridge Dependencies

bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }

# Substrate Based Dependencies

frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

Expand All @@ -24,6 +27,7 @@ std = [
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
"frame-support/std",
"sp-api/std",
"sp-std/std",
]
19 changes: 19 additions & 0 deletions bridges/primitives/chain-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,32 @@
#![allow(clippy::unnecessary_mut_passed)]

use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use frame_support::weights::{WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial};
use sp_std::prelude::*;

pub use bp_polkadot_core::*;

/// Polkadot Chain
pub type Polkadot = PolkadotLike;

// NOTE: This needs to be kept up to date with the Polkadot runtime found in the Polkadot repo.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
const CENTS: Balance = 10_000_000_000 / 100;
// in Polkadot, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

// We use this to get the account on Polkadot (target) which is derived from Kusama's (source)
// account.
pub fn derive_account_from_kusama_id(id: bp_runtime::SourceAccount<AccountId>) -> AccountId {
Expand Down
5 changes: 4 additions & 1 deletion bridges/primitives/chain-rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::{
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, IdentityFee, Weight},
Parameter, RuntimeDebug,
};
use frame_system::limits;
Expand Down Expand Up @@ -148,6 +148,9 @@ pub type Balance = u128;
/// An instant or duration in time.
pub type Moment = u64;

/// Weight-to-Fee type used by Rialto.
pub type WeightToFee = IdentityFee<Balance>;

/// Rialto chain.
#[derive(RuntimeDebug)]
pub struct Rialto;
Expand Down
9 changes: 8 additions & 1 deletion bridges/primitives/chain-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![allow(clippy::unnecessary_mut_passed)]

use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use frame_support::weights::{WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial};
use frame_support::weights::{Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial};
use sp_std::prelude::*;
use sp_version::RuntimeVersion;

Expand Down Expand Up @@ -97,6 +97,13 @@ pub const FROM_ROCOCO_LATEST_CONFIRMED_NONCE_METHOD: &str = "FromRococoInboundLa
/// Name of the `FromRococoInboundLaneApi::unrewarded_relayers_state` runtime method.
pub const FROM_ROCOCO_UNREWARDED_RELAYERS_STATE: &str = "FromRococoInboundLaneApi_unrewarded_relayers_state";

/// Weight of pay-dispatch-fee operation for inbound messages at Rococo chain.
///
/// This value corresponds to the result of `pallet_bridge_messages::WeightInfoExt::pay_inbound_dispatch_fee_overhead()`
/// call for your chain. Don't put too much reserve there, because it is used to **decrease**
/// `DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT` cost. So putting large reserve would make delivery transactions cheaper.
pub const PAY_INBOUND_DISPATCH_FEE_WEIGHT: Weight = 600_000_000;

sp_api::decl_runtime_apis! {
/// API for querying information about the finalized Rococo headers.
///
Expand Down
5 changes: 5 additions & 0 deletions bridges/primitives/chain-westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[dependencies]
parity-scale-codec = { version = "2.2.0", default-features = false, features = ["derive"] }
smallvec = "1.6"

# Bridge Dependencies

bp-header-chain = { path = "../header-chain", default-features = false }
bp-messages = { path = "../messages", default-features = false }
bp-polkadot-core = { path = "../polkadot-core", default-features = false }
bp-runtime = { path = "../runtime", default-features = false }

# Substrate Based Dependencies

frame-support = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master" , default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand All @@ -28,6 +32,7 @@ std = [
"bp-messages/std",
"bp-polkadot-core/std",
"bp-runtime/std",
"frame-support/std",
"parity-scale-codec/std",
"sp-api/std",
"sp-runtime/std",
Expand Down
19 changes: 19 additions & 0 deletions bridges/primitives/chain-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use bp_messages::{LaneId, MessageDetails, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::weights::{WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial};
use sp_std::prelude::*;
use sp_version::RuntimeVersion;

Expand All @@ -30,6 +31,24 @@ pub use bp_polkadot_core::*;
/// Westend Chain
pub type Westend = PolkadotLike;

// NOTE: This needs to be kept up to date with the Westend runtime found in the Polkadot repo.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
const CENTS: Balance = 1_000_000_000_000 / 1_000;
// in Westend, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
let p = CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec::smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational(p % q, q),
coeff_integer: p / q,
}]
}
}

pub type UncheckedExtrinsic = bp_polkadot_core::UncheckedExtrinsic<Call>;

// NOTE: This needs to be kept up to date with the Westend runtime found in the Polkadot repo.
Expand Down
2 changes: 1 addition & 1 deletion bridges/primitives/chain-wococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_std::prelude::*;

pub use bp_polkadot_core::*;
// Rococo runtime = Wococo runtime
pub use bp_rococo::{WeightToFee, SESSION_LENGTH, VERSION};
pub use bp_rococo::{WeightToFee, PAY_INBOUND_DISPATCH_FEE_WEIGHT, SESSION_LENGTH, VERSION};

/// Wococo Chain
pub type Wococo = PolkadotLike;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use sp_core::{Bytes, Pair};

use bp_messages::MessageNonce;
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
use frame_support::weights::Weight;
use messages_relay::message_lane::MessageLane;
use relay_millau_client::{HeaderId as MillauHeaderId, Millau, SigningParams as MillauSigningParams};
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SigningParams as RialtoSigningParams};
Expand Down Expand Up @@ -64,6 +65,8 @@ impl SubstrateMessageLane for MillauMessagesToRialto {
const MESSAGE_PALLET_NAME_AT_SOURCE: &'static str = bp_millau::WITH_RIALTO_MESSAGES_PALLET_NAME;
const MESSAGE_PALLET_NAME_AT_TARGET: &'static str = bp_rialto::WITH_MILLAU_MESSAGES_PALLET_NAME;

const PAY_INBOUND_DISPATCH_FEE_WEIGHT_AT_TARGET_CHAIN: Weight = bp_rialto::PAY_INBOUND_DISPATCH_FEE_WEIGHT;

type SourceChain = Millau;
type TargetChain = Rialto;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use sp_core::{Bytes, Pair};

use bp_messages::MessageNonce;
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
use frame_support::weights::Weight;
use messages_relay::message_lane::MessageLane;
use relay_millau_client::{HeaderId as MillauHeaderId, Millau, SigningParams as MillauSigningParams};
use relay_rialto_client::{HeaderId as RialtoHeaderId, Rialto, SigningParams as RialtoSigningParams};
Expand Down Expand Up @@ -64,6 +65,8 @@ impl SubstrateMessageLane for RialtoMessagesToMillau {
const MESSAGE_PALLET_NAME_AT_SOURCE: &'static str = bp_rialto::WITH_MILLAU_MESSAGES_PALLET_NAME;
const MESSAGE_PALLET_NAME_AT_TARGET: &'static str = bp_millau::WITH_RIALTO_MESSAGES_PALLET_NAME;

const PAY_INBOUND_DISPATCH_FEE_WEIGHT_AT_TARGET_CHAIN: Weight = bp_millau::PAY_INBOUND_DISPATCH_FEE_WEIGHT;

type SourceChain = Rialto;
type TargetChain = Millau;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use sp_core::{Bytes, Pair};

use bp_messages::MessageNonce;
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
use frame_support::weights::Weight;
use messages_relay::message_lane::MessageLane;
use relay_rococo_client::{HeaderId as RococoHeaderId, Rococo, SigningParams as RococoSigningParams};
use relay_substrate_client::{Chain, Client, TransactionSignScheme};
Expand Down Expand Up @@ -63,6 +64,8 @@ impl SubstrateMessageLane for RococoMessagesToWococo {
const MESSAGE_PALLET_NAME_AT_SOURCE: &'static str = bp_rococo::WITH_WOCOCO_MESSAGES_PALLET_NAME;
const MESSAGE_PALLET_NAME_AT_TARGET: &'static str = bp_wococo::WITH_ROCOCO_MESSAGES_PALLET_NAME;

const PAY_INBOUND_DISPATCH_FEE_WEIGHT_AT_TARGET_CHAIN: Weight = bp_wococo::PAY_INBOUND_DISPATCH_FEE_WEIGHT;

type SourceChain = Rococo;
type TargetChain = Wococo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use sp_core::{Bytes, Pair};

use bp_messages::MessageNonce;
use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof;
use frame_support::weights::Weight;
use messages_relay::message_lane::MessageLane;
use relay_rococo_client::{HeaderId as RococoHeaderId, Rococo, SigningParams as RococoSigningParams};
use relay_substrate_client::{Chain, Client, TransactionSignScheme};
Expand Down Expand Up @@ -62,6 +63,8 @@ impl SubstrateMessageLane for WococoMessagesToRococo {
const MESSAGE_PALLET_NAME_AT_SOURCE: &'static str = bp_wococo::WITH_ROCOCO_MESSAGES_PALLET_NAME;
const MESSAGE_PALLET_NAME_AT_TARGET: &'static str = bp_rococo::WITH_WOCOCO_MESSAGES_PALLET_NAME;

const PAY_INBOUND_DISPATCH_FEE_WEIGHT_AT_TARGET_CHAIN: Weight = bp_rococo::PAY_INBOUND_DISPATCH_FEE_WEIGHT;

type SourceChain = Wococo;
type TargetChain = Rococo;

Expand Down
3 changes: 2 additions & 1 deletion bridges/relays/bin-substrate/src/cli/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ impl SendMessage {

log::info!(
target: "bridge",
"Sending message to {}. Size: {}. Dispatch weight: {}. Fee: {}",
"Sending message to {}. Lane: {:?}. Size: {}. Dispatch weight: {}. Fee: {}",
Target::NAME,
lane,
signed_source_call.len(),
dispatch_weight,
fee,
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Chain for Kusama {
type SignedBlock = bp_kusama::SignedBlock;
type Call = ();
type Balance = bp_kusama::Balance;
type WeightToFee = bp_kusama::WeightToFee;
}

/// Kusama header type used in headers sync.
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Chain for Millau {
type SignedBlock = millau_runtime::SignedBlock;
type Call = millau_runtime::Call;
type Balance = millau_runtime::Balance;
type WeightToFee = bp_millau::WeightToFee;
}

impl ChainWithBalances for Millau {
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Chain for Polkadot {
type SignedBlock = bp_polkadot::SignedBlock;
type Call = ();
type Balance = bp_polkadot::Balance;
type WeightToFee = bp_polkadot::WeightToFee;
}

/// Polkadot header type used in headers sync.
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Chain for Rialto {
type SignedBlock = rialto_runtime::SignedBlock;
type Call = rialto_runtime::Call;
type Balance = rialto_runtime::Balance;
type WeightToFee = bp_rialto::WeightToFee;
}

impl ChainWithBalances for Rialto {
Expand Down
1 change: 1 addition & 0 deletions bridges/relays/client-rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Chain for Rococo {
type SignedBlock = bp_rococo::SignedBlock;
type Call = crate::runtime::Call;
type Balance = bp_rococo::Balance;
type WeightToFee = bp_rococo::WeightToFee;
}

impl ChainWithBalances for Rococo {
Expand Down
Loading

0 comments on commit 7e6a0f1

Please sign in to comment.