Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Darwinia feemarket strategy (#8)
Browse files Browse the repository at this point in the history
* Fee market relay strategy

* Pick darwinia-v0.11.6-2
  • Loading branch information
fewensa authored Nov 22, 2021
1 parent 1224522 commit e26d591
Show file tree
Hide file tree
Showing 13 changed files with 631 additions and 301 deletions.
8 changes: 2 additions & 6 deletions bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,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 @@ -483,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

0 comments on commit e26d591

Please sign in to comment.