Skip to content

Commit

Permalink
Estimate message fee api (paritytech#600)
Browse files Browse the repository at this point in the history
* estimate_message_delivery_and_dispatch_fee runtime API

* auto-determine message fees in relay

* remove fee argument from relay calls

* Fix import of weight contant

Co-authored-by: Hernando Castano <castano.ha@gmail.com>
  • Loading branch information
2 people authored and serban300 committed Apr 9, 2024
1 parent efae193 commit 4125a26
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 35 deletions.
15 changes: 14 additions & 1 deletion bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

pub mod rialto_messages;

use crate::rialto_messages::{ToRialtoMessagePayload, WithRialtoMessageBridge};

use bridge_runtime_common::messages::{source::estimate_message_dispatch_and_delivery_fee, MessageBridge};
use codec::Decode;
use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList};
use sp_api::impl_runtime_apis;
Expand Down Expand Up @@ -532,7 +535,17 @@ impl_runtime_apis! {
}
}

impl bp_rialto::ToRialtoOutboundLaneApi<Block> for Runtime {
impl bp_rialto::ToRialtoOutboundLaneApi<Block, Balance, ToRialtoMessagePayload> for Runtime {
fn estimate_message_delivery_and_dispatch_fee(
_lane_id: bp_message_lane::LaneId,
payload: ToRialtoMessagePayload,
) -> Option<Balance> {
estimate_message_dispatch_and_delivery_fee::<WithRialtoMessageBridge>(
&payload,
WithRialtoMessageBridge::RELAYER_FEE_PERCENT,
).ok()
}

fn messages_dispatch_weight(
lane: bp_message_lane::LaneId,
begin: bp_message_lane::MessageNonce,
Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ impl MessageBridge for WithRialtoMessageBridge {
<crate::Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight) as _
}

fn this_balance_to_bridged_balance(this_balance: bp_millau::Balance) -> bp_rialto::Balance {
fn bridged_balance_to_this_balance(bridged_balance: bp_rialto::Balance) -> bp_millau::Balance {
// 1:1 conversion that will probably change in the future
this_balance as _
bridged_balance as _
}
}

Expand Down
15 changes: 14 additions & 1 deletion bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub mod kovan;
pub mod millau_messages;
pub mod rialto_poa;

use crate::millau_messages::{ToMillauMessagePayload, WithMillauMessageBridge};

use bridge_runtime_common::messages::{source::estimate_message_dispatch_and_delivery_fee, MessageBridge};
use codec::Decode;
use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList};
use sp_api::impl_runtime_apis;
Expand Down Expand Up @@ -695,7 +698,17 @@ impl_runtime_apis! {
}
}

impl bp_millau::ToMillauOutboundLaneApi<Block> for Runtime {
impl bp_millau::ToMillauOutboundLaneApi<Block, Balance, ToMillauMessagePayload> for Runtime {
fn estimate_message_delivery_and_dispatch_fee(
_lane_id: bp_message_lane::LaneId,
payload: ToMillauMessagePayload,
) -> Option<Balance> {
estimate_message_dispatch_and_delivery_fee::<WithMillauMessageBridge>(
&payload,
WithMillauMessageBridge::RELAYER_FEE_PERCENT,
).ok()
}

fn messages_dispatch_weight(
lane: bp_message_lane::LaneId,
begin: bp_message_lane::MessageNonce,
Expand Down
4 changes: 2 additions & 2 deletions bridges/bin/rialto/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ impl MessageBridge for WithMillauMessageBridge {
<crate::Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight) as _
}

fn this_balance_to_bridged_balance(this_balance: bp_rialto::Balance) -> bp_millau::Balance {
fn bridged_balance_to_this_balance(bridged_balance: bp_millau::Balance) -> bp_rialto::Balance {
// 1:1 conversion that will probably change in the future
this_balance as _
bridged_balance as _
}
}

Expand Down
33 changes: 16 additions & 17 deletions bridges/bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ pub trait MessageBridge {
/// Convert weight of the Bridged chain to the fee (paid in Balance) of the Bridged chain.
fn bridged_weight_to_bridged_balance(weight: WeightOf<BridgedChain<Self>>) -> BalanceOf<BridgedChain<Self>>;

/// Convert This chain Balance into Bridged chain Balance.
fn this_balance_to_bridged_balance(this_balance: BalanceOf<ThisChain<Self>>) -> BalanceOf<BridgedChain<Self>>;
/// Convert Bridged chain Balance into This chain Balance.
fn bridged_balance_to_this_balance(bridged_balance: BalanceOf<BridgedChain<Self>>) -> BalanceOf<ThisChain<Self>>;
}

/// Chain that has `message-lane` and `call-dispatch` modules.
Expand Down Expand Up @@ -170,12 +170,11 @@ pub mod source {
// `CallDispatch`, so we verify the message accordingly.
pallet_bridge_call_dispatch::verify_message_origin(submitter, payload).map_err(|_| BAD_ORIGIN)?;

let minimal_fee_in_bridged_tokens =
let minimal_fee_in_this_tokens =
estimate_message_dispatch_and_delivery_fee::<B>(payload, B::RELAYER_FEE_PERCENT)?;

// compare with actual fee paid
let actual_fee_in_bridged_tokens = B::this_balance_to_bridged_balance(*delivery_and_dispatch_fee);
if actual_fee_in_bridged_tokens < minimal_fee_in_bridged_tokens {
if *delivery_and_dispatch_fee < minimal_fee_in_this_tokens {
return Err(TOO_LOW_FEE);
}

Expand Down Expand Up @@ -225,22 +224,22 @@ pub mod source {
pub fn estimate_message_dispatch_and_delivery_fee<B: MessageBridge>(
payload: &FromThisChainMessagePayload<B>,
relayer_fee_percent: u32,
) -> Result<BalanceOf<BridgedChain<B>>, &'static str> {
) -> Result<BalanceOf<ThisChain<B>>, &'static str> {
// the fee (in Bridged tokens) of all transactions that are made on the Bridged chain
let delivery_fee = B::bridged_weight_to_bridged_balance(B::weight_of_delivery_transaction());
let dispatch_fee = B::bridged_weight_to_bridged_balance(payload.weight.into());
let reward_confirmation_fee =
B::bridged_weight_to_bridged_balance(B::weight_of_reward_confirmation_transaction_on_target_chain());

// the fee (in Bridged tokens) of all transactions that are made on This chain
let delivery_confirmation_fee = B::this_balance_to_bridged_balance(B::this_weight_to_this_balance(
B::weight_of_delivery_confirmation_transaction_on_this_chain(),
));
// the fee (in This tokens) of all transactions that are made on This chain
let delivery_confirmation_fee =
B::this_weight_to_this_balance(B::weight_of_delivery_confirmation_transaction_on_this_chain());

// minimal fee (in Bridged tokens) is a sum of all required fees
// minimal fee (in This tokens) is a sum of all required fees
let minimal_fee = delivery_fee
.checked_add(&dispatch_fee)
.and_then(|fee| fee.checked_add(&reward_confirmation_fee))
.map(B::bridged_balance_to_this_balance)
.and_then(|fee| fee.checked_add(&delivery_confirmation_fee));

// before returning, add extra fee that is paid to the relayer (relayer interest)
Expand Down Expand Up @@ -563,7 +562,7 @@ mod tests {
const REWARD_CONFIRMATION_TRANSACTION_WEIGHT: Weight = 100;
const THIS_CHAIN_WEIGHT_TO_BALANCE_RATE: Weight = 2;
const BRIDGED_CHAIN_WEIGHT_TO_BALANCE_RATE: Weight = 4;
const THIS_CHAIN_TO_BRIDGED_CHAIN_BALANCE_RATE: u32 = 6;
const BRIDGED_CHAIN_TO_THIS_CHAIN_BALANCE_RATE: u32 = 6;
const BRIDGED_CHAIN_MAX_EXTRINSIC_WEIGHT: Weight = 2048;
const BRIDGED_CHAIN_MAX_EXTRINSIC_SIZE: u32 = 1024;

Expand Down Expand Up @@ -606,8 +605,8 @@ mod tests {
BridgedChainBalance(weight as u32 * BRIDGED_CHAIN_WEIGHT_TO_BALANCE_RATE as u32)
}

fn this_balance_to_bridged_balance(this_balance: ThisChainBalance) -> BridgedChainBalance {
BridgedChainBalance(this_balance.0 * THIS_CHAIN_TO_BRIDGED_CHAIN_BALANCE_RATE as u32)
fn bridged_balance_to_this_balance(bridged_balance: BridgedChainBalance) -> ThisChainBalance {
ThisChainBalance(bridged_balance.0 * BRIDGED_CHAIN_TO_THIS_CHAIN_BALANCE_RATE as u32)
}
}

Expand Down Expand Up @@ -649,7 +648,7 @@ mod tests {
unreachable!()
}

fn this_balance_to_bridged_balance(_this_balance: BridgedChainBalance) -> ThisChainBalance {
fn bridged_balance_to_this_balance(_this_balance: ThisChainBalance) -> BridgedChainBalance {
unreachable!()
}
}
Expand Down Expand Up @@ -799,7 +798,7 @@ mod tests {

#[test]
fn message_fee_is_checked_by_verifier() {
const EXPECTED_MINIMAL_FEE: u32 = 2640;
const EXPECTED_MINIMAL_FEE: u32 = 8140;

// payload of the This -> Bridged chain message
let payload = source::FromThisChainMessagePayload::<OnThisChainBridge> {
Expand All @@ -815,7 +814,7 @@ mod tests {
&payload,
OnThisChainBridge::RELAYER_FEE_PERCENT,
),
Ok(BridgedChainBalance(EXPECTED_MINIMAL_FEE)),
Ok(ThisChainBalance(EXPECTED_MINIMAL_FEE)),
);

// and now check that the verifier checks the fee
Expand Down
20 changes: 18 additions & 2 deletions bridges/primitives/millau/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use bp_message_lane::{LaneId, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::{
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
RuntimeDebug,
Parameter, RuntimeDebug,
};
use frame_system::limits;
use sp_core::Hasher as HasherT;
Expand Down Expand Up @@ -191,6 +191,9 @@ pub const IS_KNOWN_MILLAU_BLOCK_METHOD: &str = "MillauHeaderApi_is_known_block";
/// Name of the `MillauHeaderApi::incomplete_headers` runtime method.
pub const INCOMPLETE_MILLAU_HEADERS_METHOD: &str = "MillauHeaderApi_incomplete_headers";

/// Name of the `ToMillauOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
pub const TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToMillauOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToMillauOutboundLaneApi::messages_dispatch_weight` runtime method.
pub const TO_MILLAU_MESSAGES_DISPATCH_WEIGHT_METHOD: &str = "ToMillauOutboundLaneApi_messages_dispatch_weight";
/// Name of the `ToMillauOutboundLaneApi::latest_received_nonce` runtime method.
Expand Down Expand Up @@ -235,7 +238,20 @@ sp_api::decl_runtime_apis! {
///
/// This API is implemented by runtimes that are sending messages to Millau chain, not the
/// Millau runtime itself.
pub trait ToMillauOutboundLaneApi {
pub trait ToMillauOutboundLaneApi<OutboundMessageFee: Parameter, OutboundPayload: Parameter> {
/// Estimate message delivery and dispatch fee that needs to be paid by the sender on
/// this chain.
///
/// Returns `None` if message is too expensive to be sent to Millau from this chain.
///
/// Please keep in mind that this method returns lowest message fee required for message
/// to be accepted to the lane. It may be good idea to pay a bit over this price to account
/// future exchange rate changes and guarantee that relayer would deliver your message
/// to the target chain.
fn estimate_message_delivery_and_dispatch_fee(
lane_id: LaneId,
payload: OutboundPayload,
) -> Option<OutboundMessageFee>;
/// Returns total dispatch weight and encoded payload size of all messages in given inclusive range.
///
/// If some (or all) messages are missing from the storage, they'll also will
Expand Down
20 changes: 18 additions & 2 deletions bridges/primitives/rialto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use bp_message_lane::{LaneId, MessageNonce, UnrewardedRelayersState};
use bp_runtime::Chain;
use frame_support::{
weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight},
RuntimeDebug,
Parameter, RuntimeDebug,
};
use frame_system::limits;
use sp_core::Hasher as HasherT;
Expand Down Expand Up @@ -152,6 +152,9 @@ pub const IS_KNOWN_RIALTO_BLOCK_METHOD: &str = "RialtoHeaderApi_is_known_block";
/// Name of the `RialtoHeaderApi::incomplete_headers` runtime method.
pub const INCOMPLETE_RIALTO_HEADERS_METHOD: &str = "RialtoHeaderApi_incomplete_headers";

/// Name of the `ToRialtoOutboundLaneApi::estimate_message_delivery_and_dispatch_fee` runtime method.
pub const TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD: &str =
"ToRialtoOutboundLaneApi_estimate_message_delivery_and_dispatch_fee";
/// Name of the `ToRialtoOutboundLaneApi::messages_dispatch_weight` runtime method.
pub const TO_RIALTO_MESSAGES_DISPATCH_WEIGHT_METHOD: &str = "ToRialtoOutboundLaneApi_messages_dispatch_weight";
/// Name of the `ToRialtoOutboundLaneApi::latest_generated_nonce` runtime method.
Expand Down Expand Up @@ -196,7 +199,20 @@ sp_api::decl_runtime_apis! {
///
/// This API is implemented by runtimes that are sending messages to Rialto chain, not the
/// Rialto runtime itself.
pub trait ToRialtoOutboundLaneApi {
pub trait ToRialtoOutboundLaneApi<OutboundMessageFee: Parameter, OutboundPayload: Parameter> {
/// Estimate message delivery and dispatch fee that needs to be paid by the sender on
/// this chain.
///
/// Returns `None` if message is too expensive to be sent to Rialto from this chain.
///
/// Please keep in mind that this method returns lowest message fee required for message
/// to be accepted to the lane. It may be good idea to pay a bit over this price to account
/// future exchange rate changes and guarantee that relayer would deliver your message
/// to the target chain.
fn estimate_message_delivery_and_dispatch_fee(
lane_id: LaneId,
payload: OutboundPayload,
) -> Option<OutboundMessageFee>;
/// Returns total dispatch weight and encoded payload size of all messages in given inclusive range.
///
/// If some (or all) messages are missing from the storage, they'll also will
Expand Down
8 changes: 4 additions & 4 deletions bridges/relays/substrate/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ pub enum Command {
/// Hex-encoded lane id.
#[structopt(long)]
lane: HexLaneId,
/// Delivery and dispatch fee.
/// Delivery and dispatch fee. If not passed, determined automatically.
#[structopt(long)]
fee: bp_millau::Balance,
fee: Option<bp_millau::Balance>,
/// Message type.
#[structopt(subcommand)]
message: ToRialtoMessage,
Expand Down Expand Up @@ -138,9 +138,9 @@ pub enum Command {
/// Hex-encoded lane id.
#[structopt(long)]
lane: HexLaneId,
/// Delivery and dispatch fee.
/// Delivery and dispatch fee. If not passed, determined automatically.
#[structopt(long)]
fee: bp_rialto::Balance,
fee: Option<bp_rialto::Balance>,
/// Message type.
#[structopt(subcommand)]
message: ToMillauMessage,
Expand Down
60 changes: 56 additions & 4 deletions bridges/relays/substrate/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

#![warn(missing_docs)]

use codec::Encode;
use codec::{Decode, Encode};
use frame_support::weights::GetDispatchInfo;
use pallet_bridge_call_dispatch::{CallOrigin, MessagePayload};
use relay_kusama_client::Kusama;
use relay_millau_client::{Millau, SigningParams as MillauSigningParams};
use relay_rialto_client::{Rialto, SigningParams as RialtoSigningParams};
use relay_substrate_client::{ConnectionParams, TransactionSignScheme};
use relay_substrate_client::{Chain, ConnectionParams, TransactionSignScheme};
use relay_utils::initialize::initialize_relay;
use sp_core::{Bytes, Pair};
use sp_runtime::traits::IdentifyAccount;
Expand Down Expand Up @@ -315,8 +315,27 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
}
};

let lane = lane.into();
let fee = match fee {
Some(fee) => fee,
None => match estimate_message_delivery_and_dispatch_fee(
&millau_client,
bp_rialto::TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD,
lane,
payload.clone(),
)
.await
{
Ok(Some(fee)) => fee,
Ok(None) => return Err("Failed to estimate message fee. Message is too heavy?".into()),
Err(error) => return Err(format!("Failed to estimate message fee: {:?}", error)),
},
};

log::error!(target: "bridge", "Sending message to Rialto. Fee: {}", fee);

let millau_call = millau_runtime::Call::BridgeRialtoMessageLane(
millau_runtime::MessageLaneCall::send_message(lane.into(), payload, fee),
millau_runtime::MessageLaneCall::send_message(lane, payload, fee),
);

let signed_millau_call = Millau::sign_transaction(
Expand Down Expand Up @@ -444,8 +463,27 @@ async fn run_command(command: cli::Command) -> Result<(), String> {
}
};

let lane = lane.into();
let fee = match fee {
Some(fee) => fee,
None => match estimate_message_delivery_and_dispatch_fee(
&rialto_client,
bp_millau::TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD,
lane,
payload.clone(),
)
.await
{
Ok(Some(fee)) => fee,
Ok(None) => return Err("Failed to estimate message fee. Message is too heavy?".into()),
Err(error) => return Err(format!("Failed to estimate message fee: {:?}", error)),
},
};

log::info!(target: "bridge", "Sending message to Millau. Fee: {}", fee);

let rialto_call = rialto_runtime::Call::BridgeMillauMessageLane(
rialto_runtime::MessageLaneCall::send_message(lane.into(), payload, fee),
rialto_runtime::MessageLaneCall::send_message(lane, payload, fee),
);

let signed_rialto_call = Rialto::sign_transaction(
Expand All @@ -465,3 +503,17 @@ async fn run_command(command: cli::Command) -> Result<(), String> {

Ok(())
}

async fn estimate_message_delivery_and_dispatch_fee<Fee: Decode, C: Chain, P: Encode>(
client: &relay_substrate_client::Client<C>,
estimate_fee_method: &str,
lane: bp_message_lane::LaneId,
payload: P,
) -> Result<Option<Fee>, relay_substrate_client::Error> {
let encoded_response = client
.state_call(estimate_fee_method.into(), (lane, payload).encode().into(), None)
.await?;
let decoded_response: Option<Fee> =
Decode::decode(&mut &encoded_response.0[..]).map_err(relay_substrate_client::Error::ResponseParseFailed)?;
Ok(decoded_response)
}

0 comments on commit 4125a26

Please sign in to comment.