Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,24 @@ pub trait BridgedChainWithMessages: ChainWithMessages {
fn transaction_payment(transaction: MessageTransaction<WeightOf<Self>>) -> BalanceOf<Self>;
}

pub(crate) type ThisChain<B> = <B as MessageBridge>::ThisChain;
pub(crate) type BridgedChain<B> = <B as MessageBridge>::BridgedChain;
pub(crate) type HashOf<C> = <C as ChainWithMessages>::Hash;
pub(crate) type AccountIdOf<C> = <C as ChainWithMessages>::AccountId;
pub(crate) type SignerOf<C> = <C as ChainWithMessages>::Signer;
pub(crate) type SignatureOf<C> = <C as ChainWithMessages>::Signature;
pub(crate) type WeightOf<C> = <C as ChainWithMessages>::Weight;
pub(crate) type BalanceOf<C> = <C as ChainWithMessages>::Balance;

pub(crate) type CallOf<C> = <C as ThisChainWithMessages>::Call;
/// This chain in context of message bridge.
pub type ThisChain<B> = <B as MessageBridge>::ThisChain;
/// Bridged chain in context of message bridge.
pub type BridgedChain<B> = <B as MessageBridge>::BridgedChain;
/// Hash used on the chain.
pub type HashOf<C> = <C as ChainWithMessages>::Hash;
/// Account id used on the chain.
pub type AccountIdOf<C> = <C as ChainWithMessages>::AccountId;
/// Public key of the chain account that may be used to verify signature.
pub type SignerOf<C> = <C as ChainWithMessages>::Signer;
/// Signature type used on the chain.
pub type SignatureOf<C> = <C as ChainWithMessages>::Signature;
/// Type of weight that used on the chain.
pub type WeightOf<C> = <C as ChainWithMessages>::Weight;
/// Type of balances that is used on the chain.
pub type BalanceOf<C> = <C as ChainWithMessages>::Balance;
/// Type of call that is used on this chain.
pub type CallOf<C> = <C as ThisChainWithMessages>::Call;

/// Raw storage proof type (just raw trie nodes).
type RawStorageProof = Vec<Vec<u8>>;
Expand Down Expand Up @@ -229,9 +237,7 @@ pub mod source {
impl<BridgedHeaderHash> Size for FromBridgedChainMessagesDeliveryProof<BridgedHeaderHash> {
fn size_hint(&self) -> u32 {
u32::try_from(
self.storage_proof
.iter()
.fold(0usize, |sum, node| sum.saturating_add(node.len())),
self.storage_proof.iter().fold(0usize, |sum, node| sum.saturating_add(node.len())),
)
.unwrap_or(u32::MAX)
}
Expand Down Expand Up @@ -259,12 +265,15 @@ pub mod source {
#[derive(RuntimeDebug)]
pub struct FromThisChainMessageVerifier<B>(PhantomData<B>);

pub(crate) const OUTBOUND_LANE_DISABLED: &str = "The outbound message lane is disabled.";
pub(crate) const TOO_MANY_PENDING_MESSAGES: &str = "Too many pending messages at the lane.";
pub(crate) const BAD_ORIGIN: &str =
"Unable to match the source origin to expected target origin.";
pub(crate) const TOO_LOW_FEE: &str =
"Provided fee is below minimal threshold required by the lane.";
/// The error message returned from LaneMessageVerifier when outbound lane is disabled.
pub const OUTBOUND_LANE_DISABLED: &str = "The outbound message lane is disabled.";
/// The error message returned from LaneMessageVerifier when too many pending messages at the
/// lane.
pub const TOO_MANY_PENDING_MESSAGES: &str = "Too many pending messages at the lane.";
/// The error message returned from LaneMessageVerifier when call origin is mismatch.
pub const BAD_ORIGIN: &str = "Unable to match the source origin to expected target origin.";
/// The error message returned from LaneMessageVerifier when the message fee is too low.
pub const TOO_LOW_FEE: &str = "Provided fee is below minimal threshold required by the lane.";

impl<B>
LaneMessageVerifier<
Expand Down Expand Up @@ -472,9 +481,7 @@ pub mod target {
impl<BridgedHeaderHash> Size for FromBridgedChainMessagesProof<BridgedHeaderHash> {
fn size_hint(&self) -> u32 {
u32::try_from(
self.storage_proof
.iter()
.fold(0usize, |sum, node| sum.saturating_add(node.len())),
self.storage_proof.iter().fold(0usize, |sum, node| sum.saturating_add(node.len())),
)
.unwrap_or(u32::MAX)
}
Expand Down
11 changes: 7 additions & 4 deletions relays/lib-substrate-relay/src/messages_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use async_trait::async_trait;
use bp_messages::{LaneId, MessageNonce};
use bp_runtime::{AccountIdOf, IndexOf};
use frame_support::weights::Weight;
use messages_relay::message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf};
use messages_relay::{
message_lane::{MessageLane, SourceHeaderIdOf, TargetHeaderIdOf},
relay_strategy::RelayStrategy,
};
use relay_substrate_client::{
metrics::{FloatStorageValueMetric, StorageProofOverheadMetric},
BlockNumberOf, Chain, Client, HashOf,
Expand All @@ -39,7 +42,7 @@ use sp_runtime::FixedU128;
use std::ops::RangeInclusive;

/// Substrate <-> Substrate messages relay parameters.
pub struct MessagesRelayParams<SC: Chain, SS, TC: Chain, TS> {
pub struct MessagesRelayParams<SC: Chain, SS, TC: Chain, TS, Strategy: RelayStrategy> {
/// Messages source client.
pub source_client: Client<SC>,
/// Sign parameters for messages source chain.
Expand All @@ -54,10 +57,10 @@ pub struct MessagesRelayParams<SC: Chain, SS, TC: Chain, TS> {
pub target_to_source_headers_relay: Option<OnDemandHeadersRelay<TC>>,
/// Identifier of lane that needs to be served.
pub lane_id: LaneId,
/// Relayer operating mode.
pub relayer_mode: messages_relay::message_lane_loop::RelayerMode,
/// Metrics parameters.
pub metrics_params: MetricsParams,
/// Relay strategy
pub relay_strategy: Strategy,
}

/// Message sync pipeline for Substrate <-> Substrate relays.
Expand Down
9 changes: 3 additions & 6 deletions relays/lib-substrate-relay/src/messages_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,8 @@ where
P::MESSAGE_PALLET_NAME_AT_TARGET,
&self.lane_id,
);
let proof = self
.client
.prove_storage(vec![inbound_data_key], id.1)
.await?
.iter_nodes()
.collect();
let proof =
self.client.prove_storage(vec![inbound_data_key], id.1).await?.iter_nodes().collect();
let proof = FromBridgedChainMessagesDeliveryProof {
bridged_header_hash: id.1,
storage_proof: proof,
Expand Down Expand Up @@ -434,6 +430,7 @@ fn compute_prepaid_messages_refund<P: SubstrateMessageLane>(
#[cfg(test)]
mod tests {
use super::*;
use messages_relay::relay_strategy::AltruisticStrategy;
use relay_rococo_client::{Rococo, SigningParams as RococoSigningParams};
use relay_wococo_client::{SigningParams as WococoSigningParams, Wococo};

Expand Down
2 changes: 2 additions & 0 deletions relays/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ parking_lot = "0.11.0"
bp-messages = { path = "../../primitives/messages" }
bp-runtime = { path = "../../primitives/runtime" }
relay-utils = { path = "../utils" }

sp-arithmetic = { git = "https://github.com/darwinia-network/substrate", tag = "darwinia-v0.11.6-1" }
1 change: 1 addition & 0 deletions relays/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod metrics;

pub mod message_lane;
pub mod message_lane_loop;
pub mod relay_strategy;

mod message_race_delivery;
mod message_race_loop;
Expand Down
4 changes: 3 additions & 1 deletion relays/messages/src/message_lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use num_traits::{SaturatingAdd, Zero};
use relay_utils::{BlockNumberBase, HeaderId};
use sp_arithmetic::traits::AtLeast32BitUnsigned;
use std::{fmt::Debug, ops::Sub};

/// One-way message lane.
Expand All @@ -40,7 +41,8 @@ pub trait MessageLane: 'static + Clone + Send + Sync {
/// 1) pay transaction fees;
/// 2) pay message delivery and dispatch fee;
/// 3) pay relayer rewards.
type SourceChainBalance: Clone
type SourceChainBalance: AtLeast32BitUnsigned
+ Clone
+ Copy
+ Debug
+ PartialOrd
Expand Down
27 changes: 17 additions & 10 deletions relays/messages/src/message_lane_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::{
message_race_delivery::run as run_message_delivery_race,
message_race_receiving::run as run_message_receiving_race,
metrics::MessageLaneLoopMetrics,
relay_strategy::RelayStrategy,
};

use async_trait::async_trait;
Expand All @@ -46,7 +47,7 @@ use std::{collections::BTreeMap, fmt::Debug, future::Future, ops::RangeInclusive

/// Message lane loop configuration params.
#[derive(Debug, Clone)]
pub struct Params {
pub struct Params<Strategy: RelayStrategy> {
/// Id of lane this loop is servicing.
pub lane: LaneId,
/// Interval at which we ask target node about its updates.
Expand All @@ -58,7 +59,7 @@ pub struct Params {
/// The loop will auto-restart if there has been no updates during this period.
pub stall_timeout: Duration,
/// Message delivery race parameters.
pub delivery_params: MessageDeliveryParams,
pub delivery_params: MessageDeliveryParams<Strategy>,
}

/// Relayer operating mode.
Expand All @@ -73,7 +74,7 @@ pub enum RelayerMode {

/// Message delivery race parameters.
#[derive(Debug, Clone)]
pub struct MessageDeliveryParams {
pub struct MessageDeliveryParams<Strategy: RelayStrategy> {
/// Maximal number of unconfirmed relayer entries at the inbound lane. If there's that number
/// of entries in the `InboundLaneData::relayers` set, all new messages will be rejected until
/// reward payment will be proved (by including outbound lane state to the message delivery
Expand All @@ -89,8 +90,8 @@ pub struct MessageDeliveryParams {
pub max_messages_weight_in_single_batch: Weight,
/// Maximal cumulative size of relayed messages in single delivery transaction.
pub max_messages_size_in_single_batch: u32,
/// Relayer operating mode.
pub relayer_mode: RelayerMode,
/// Relay strategy
pub relay_strategy: Strategy,
}

/// Message details.
Expand Down Expand Up @@ -257,8 +258,8 @@ pub fn metrics_prefix<P: MessageLane>(lane: &LaneId) -> String {
}

/// Run message lane service loop.
pub async fn run<P: MessageLane>(
params: Params,
pub async fn run<P: MessageLane, Strategy: RelayStrategy>(
params: Params<Strategy>,
source_client: impl SourceClient<P>,
target_client: impl TargetClient<P>,
metrics_params: MetricsParams,
Expand Down Expand Up @@ -286,8 +287,13 @@ pub async fn run<P: MessageLane>(

/// Run one-way message delivery loop until connection with target or source node is lost, or exit
/// signal is received.
async fn run_until_connection_lost<P: MessageLane, SC: SourceClient<P>, TC: TargetClient<P>>(
params: Params,
async fn run_until_connection_lost<
P: MessageLane,
Strategy: RelayStrategy,
SC: SourceClient<P>,
TC: TargetClient<P>,
>(
params: Params<Strategy>,
source_client: SC,
target_client: TC,
metrics_msg: Option<MessageLaneLoopMetrics>,
Expand Down Expand Up @@ -450,6 +456,7 @@ async fn run_until_connection_lost<P: MessageLane, SC: SourceClient<P>, TC: Targ
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::relay_strategy::AltruisticStrategy;
use futures::stream::StreamExt;
use parking_lot::Mutex;
use relay_utils::{HeaderId, MaybeConnectionError};
Expand Down Expand Up @@ -807,7 +814,7 @@ pub(crate) mod tests {
max_messages_in_single_batch: 4,
max_messages_weight_in_single_batch: 4,
max_messages_size_in_single_batch: 4,
relayer_mode: RelayerMode::Altruistic,
relay_strategy: AltruisticStrategy,
},
},
source_client,
Expand Down
Loading