Skip to content

Commit

Permalink
Message transactions mortality (#1191)
Browse files Browse the repository at this point in the history
* transactions mortality in message and complex relays

* logging + enable in test deployments

* spellcheck

* fmt
  • Loading branch information
svyatonik authored and bkchr committed Apr 10, 2024
1 parent e23266c commit 1ef41a5
Show file tree
Hide file tree
Showing 20 changed files with 253 additions and 64 deletions.
19 changes: 10 additions & 9 deletions bridges/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub const ACCOUNT_DERIVATION_PREFIX: &[u8] = b"pallet-bridge/account-derivation/
/// A unique prefix for entropy when generating a cross-chain account ID for the Root account.
pub const ROOT_ACCOUNT_DERIVATION_PREFIX: &[u8] = b"pallet-bridge/account-derivation/root";

/// Generic header Id.
#[derive(RuntimeDebug, Default, Clone, Copy, Eq, Hash, PartialEq)]
pub struct HeaderId<Hash, Number>(pub Number, pub Hash);

/// Unique identifier of the chain.
///
/// In addition to its main function (identifying the chain), this type may also be used to
Expand Down Expand Up @@ -159,20 +163,17 @@ pub enum TransactionEra<BlockNumber, BlockHash> {
/// Transaction is immortal.
Immortal,
/// Transaction is valid for a given number of blocks, starting from given block.
Mortal(BlockNumber, BlockHash, u32),
Mortal(HeaderId<BlockHash, BlockNumber>, u32),
}

impl<BlockNumber: Copy + Into<u64>, BlockHash: Copy> TransactionEra<BlockNumber, BlockHash> {
/// Prepare transaction era, based on mortality period and current best block number.
pub fn new(
best_block_number: BlockNumber,
best_block_hash: BlockHash,
best_block_id: HeaderId<BlockHash, BlockNumber>,
mortality_period: Option<u32>,
) -> Self {
mortality_period
.map(|mortality_period| {
TransactionEra::Mortal(best_block_number, best_block_hash, mortality_period)
})
.map(|mortality_period| TransactionEra::Mortal(best_block_id, mortality_period))
.unwrap_or(TransactionEra::Immortal)
}

Expand All @@ -185,16 +186,16 @@ impl<BlockNumber: Copy + Into<u64>, BlockHash: Copy> TransactionEra<BlockNumber,
pub fn frame_era(&self) -> sp_runtime::generic::Era {
match *self {
TransactionEra::Immortal => sp_runtime::generic::Era::immortal(),
TransactionEra::Mortal(header_number, _, period) =>
sp_runtime::generic::Era::mortal(period as _, header_number.into()),
TransactionEra::Mortal(header_id, period) =>
sp_runtime::generic::Era::mortal(period as _, header_id.0.into()),
}
}

/// Returns header hash that needs to be included in the signature payload.
pub fn signed_payload(&self, genesis_hash: BlockHash) -> BlockHash {
match *self {
TransactionEra::Immortal => genesis_hash,
TransactionEra::Mortal(_, header_hash, _) => header_hash,
TransactionEra::Mortal(header_id, _) => header_id.1,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Kusama-to-Polkadot messages sync entrypoint.

use std::{ops::RangeInclusive, time::Duration};
use std::ops::RangeInclusive;

use codec::Encode;
use sp_core::{Bytes, Pair};
Expand All @@ -41,6 +41,7 @@ use substrate_relay_helper::{
},
messages_source::SubstrateMessagesSource,
messages_target::SubstrateMessagesTarget,
STALL_TIMEOUT,
};

/// Kusama-to-Polkadot message lane.
Expand Down Expand Up @@ -91,6 +92,7 @@ impl SubstrateMessageLane for KusamaMessagesToPolkadot {

fn make_messages_receiving_proof_transaction(
&self,
best_block_id: KusamaHeaderId,
transaction_nonce: bp_runtime::IndexOf<Kusama>,
_generated_at_block: PolkadotHeaderId,
proof: <Self::MessageLane as MessageLane>::MessagesReceivingProof,
Expand All @@ -106,7 +108,10 @@ impl SubstrateMessageLane for KusamaMessagesToPolkadot {
let transaction = Kusama::sign_transaction(
genesis_hash,
&self.message_lane.source_sign,
relay_substrate_client::TransactionEra::immortal(),
relay_substrate_client::TransactionEra::new(
best_block_id,
self.message_lane.source_transactions_mortality,
),
UnsignedTransaction::new(call, transaction_nonce),
);
log::trace!(
Expand All @@ -125,6 +130,7 @@ impl SubstrateMessageLane for KusamaMessagesToPolkadot {

fn make_messages_delivery_transaction(
&self,
best_block_id: PolkadotHeaderId,
transaction_nonce: bp_runtime::IndexOf<Polkadot>,
_generated_at_header: KusamaHeaderId,
_nonces: RangeInclusive<MessageNonce>,
Expand All @@ -146,7 +152,10 @@ impl SubstrateMessageLane for KusamaMessagesToPolkadot {
let transaction = Polkadot::sign_transaction(
genesis_hash,
&self.message_lane.target_sign,
relay_substrate_client::TransactionEra::immortal(),
relay_substrate_client::TransactionEra::new(
best_block_id,
self.message_lane.target_transactions_mortality,
),
UnsignedTransaction::new(call, transaction_nonce),
);
log::trace!(
Expand All @@ -170,7 +179,13 @@ type PolkadotTargetClient = SubstrateMessagesTarget<KusamaMessagesToPolkadot>;
pub async fn run(
params: MessagesRelayParams<Kusama, KusamaSigningParams, Polkadot, PolkadotSigningParams>,
) -> anyhow::Result<()> {
let stall_timeout = Duration::from_secs(5 * 60);
let stall_timeout = relay_substrate_client::bidirectional_transaction_stall_timeout(
params.source_transactions_mortality,
params.target_transactions_mortality,
Kusama::AVERAGE_BLOCK_INTERVAL,
Polkadot::AVERAGE_BLOCK_INTERVAL,
STALL_TIMEOUT,
);
let relayer_id_at_kusama = (*params.source_sign.public().as_array_ref()).into();

let lane_id = params.lane_id;
Expand All @@ -179,8 +194,10 @@ pub async fn run(
message_lane: SubstrateMessageLaneToSubstrate {
source_client: source_client.clone(),
source_sign: params.source_sign,
source_transactions_mortality: params.source_transactions_mortality,
target_client: params.target_client.clone(),
target_sign: params.target_sign,
target_transactions_mortality: params.target_transactions_mortality,
relayer_id_at_source: relayer_id_at_kusama,
},
};
Expand All @@ -206,12 +223,17 @@ pub async fn run(
Max messages in single transaction: {}\n\t\
Max messages size in single transaction: {}\n\t\
Max messages weight in single transaction: {}\n\t\
Relayer mode: {:?}",
Relayer mode: {:?}\n\t\
Tx mortality: {:?}/{:?}\n\t\
Stall timeout: {:?}",
lane.message_lane.relayer_id_at_source,
max_messages_in_single_batch,
max_messages_size_in_single_batch,
max_messages_weight_in_single_batch,
params.relayer_mode,
params.source_transactions_mortality,
params.target_transactions_mortality,
stall_timeout,
);

let (metrics_params, metrics_values) = add_standalone_metrics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Millau-to-Rialto messages sync entrypoint.

use std::{ops::RangeInclusive, time::Duration};
use std::ops::RangeInclusive;

use codec::Encode;
use frame_support::dispatch::GetDispatchInfo;
Expand All @@ -41,6 +41,7 @@ use substrate_relay_helper::{
},
messages_source::SubstrateMessagesSource,
messages_target::SubstrateMessagesTarget,
STALL_TIMEOUT,
};

/// Millau-to-Rialto message lane.
Expand Down Expand Up @@ -89,6 +90,7 @@ impl SubstrateMessageLane for MillauMessagesToRialto {

fn make_messages_receiving_proof_transaction(
&self,
best_block_id: MillauHeaderId,
transaction_nonce: IndexOf<Millau>,
_generated_at_block: RialtoHeaderId,
proof: <Self::MessageLane as MessageLane>::MessagesReceivingProof,
Expand All @@ -102,7 +104,10 @@ impl SubstrateMessageLane for MillauMessagesToRialto {
let transaction = Millau::sign_transaction(
genesis_hash,
&self.message_lane.source_sign,
relay_substrate_client::TransactionEra::immortal(),
relay_substrate_client::TransactionEra::new(
best_block_id,
self.message_lane.source_transactions_mortality,
),
UnsignedTransaction::new(call, transaction_nonce),
);
log::trace!(
Expand All @@ -122,6 +127,7 @@ impl SubstrateMessageLane for MillauMessagesToRialto {

fn make_messages_delivery_transaction(
&self,
best_block_id: RialtoHeaderId,
transaction_nonce: IndexOf<Rialto>,
_generated_at_header: MillauHeaderId,
_nonces: RangeInclusive<MessageNonce>,
Expand All @@ -142,7 +148,10 @@ impl SubstrateMessageLane for MillauMessagesToRialto {
let transaction = Rialto::sign_transaction(
genesis_hash,
&self.message_lane.target_sign,
relay_substrate_client::TransactionEra::immortal(),
relay_substrate_client::TransactionEra::new(
best_block_id,
self.message_lane.target_transactions_mortality,
),
UnsignedTransaction::new(call, transaction_nonce),
);
log::trace!(
Expand All @@ -167,7 +176,13 @@ type RialtoTargetClient = SubstrateMessagesTarget<MillauMessagesToRialto>;
pub async fn run(
params: MessagesRelayParams<Millau, MillauSigningParams, Rialto, RialtoSigningParams>,
) -> anyhow::Result<()> {
let stall_timeout = Duration::from_secs(5 * 60);
let stall_timeout = relay_substrate_client::bidirectional_transaction_stall_timeout(
params.source_transactions_mortality,
params.target_transactions_mortality,
Millau::AVERAGE_BLOCK_INTERVAL,
Rialto::AVERAGE_BLOCK_INTERVAL,
STALL_TIMEOUT,
);
let relayer_id_at_millau = (*params.source_sign.public().as_array_ref()).into();

let lane_id = params.lane_id;
Expand All @@ -176,8 +191,10 @@ pub async fn run(
message_lane: SubstrateMessageLaneToSubstrate {
source_client: source_client.clone(),
source_sign: params.source_sign,
source_transactions_mortality: params.source_transactions_mortality,
target_client: params.target_client.clone(),
target_sign: params.target_sign,
target_transactions_mortality: params.target_transactions_mortality,
relayer_id_at_source: relayer_id_at_millau,
},
};
Expand All @@ -200,12 +217,17 @@ pub async fn run(
Max messages in single transaction: {}\n\t\
Max messages size in single transaction: {}\n\t\
Max messages weight in single transaction: {}\n\t\
Relayer mode: {:?}",
Relayer mode: {:?}\n\t\
Tx mortality: {:?}/{:?}\n\t\
Stall timeout: {:?}",
lane.message_lane.relayer_id_at_source,
max_messages_in_single_batch,
max_messages_size_in_single_batch,
max_messages_weight_in_single_batch,
params.relayer_mode,
params.source_transactions_mortality,
params.target_transactions_mortality,
stall_timeout,
);

let (metrics_params, metrics_values) = add_standalone_metrics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Polkadot-to-Kusama messages sync entrypoint.

use std::{ops::RangeInclusive, time::Duration};
use std::ops::RangeInclusive;

use codec::Encode;
use sp_core::{Bytes, Pair};
Expand All @@ -41,6 +41,7 @@ use substrate_relay_helper::{
},
messages_source::SubstrateMessagesSource,
messages_target::SubstrateMessagesTarget,
STALL_TIMEOUT,
};

/// Polkadot-to-Kusama message lane.
Expand Down Expand Up @@ -90,6 +91,7 @@ impl SubstrateMessageLane for PolkadotMessagesToKusama {

fn make_messages_receiving_proof_transaction(
&self,
best_block_id: PolkadotHeaderId,
transaction_nonce: bp_runtime::IndexOf<Polkadot>,
_generated_at_block: KusamaHeaderId,
proof: <Self::MessageLane as MessageLane>::MessagesReceivingProof,
Expand All @@ -105,7 +107,10 @@ impl SubstrateMessageLane for PolkadotMessagesToKusama {
let transaction = Polkadot::sign_transaction(
genesis_hash,
&self.message_lane.source_sign,
relay_substrate_client::TransactionEra::immortal(),
relay_substrate_client::TransactionEra::new(
best_block_id,
self.message_lane.source_transactions_mortality,
),
UnsignedTransaction::new(call, transaction_nonce),
);
log::trace!(
Expand All @@ -124,6 +129,7 @@ impl SubstrateMessageLane for PolkadotMessagesToKusama {

fn make_messages_delivery_transaction(
&self,
best_block_id: KusamaHeaderId,
transaction_nonce: bp_runtime::IndexOf<Kusama>,
_generated_at_header: PolkadotHeaderId,
_nonces: RangeInclusive<MessageNonce>,
Expand All @@ -145,7 +151,10 @@ impl SubstrateMessageLane for PolkadotMessagesToKusama {
let transaction = Kusama::sign_transaction(
genesis_hash,
&self.message_lane.target_sign,
relay_substrate_client::TransactionEra::immortal(),
relay_substrate_client::TransactionEra::new(
best_block_id,
self.message_lane.target_transactions_mortality,
),
UnsignedTransaction::new(call, transaction_nonce),
);
log::trace!(
Expand All @@ -169,7 +178,13 @@ type KusamaTargetClient = SubstrateMessagesTarget<PolkadotMessagesToKusama>;
pub async fn run(
params: MessagesRelayParams<Polkadot, PolkadotSigningParams, Kusama, KusamaSigningParams>,
) -> anyhow::Result<()> {
let stall_timeout = Duration::from_secs(5 * 60);
let stall_timeout = relay_substrate_client::bidirectional_transaction_stall_timeout(
params.source_transactions_mortality,
params.target_transactions_mortality,
Polkadot::AVERAGE_BLOCK_INTERVAL,
Kusama::AVERAGE_BLOCK_INTERVAL,
STALL_TIMEOUT,
);
let relayer_id_at_polkadot = (*params.source_sign.public().as_array_ref()).into();

let lane_id = params.lane_id;
Expand All @@ -178,8 +193,10 @@ pub async fn run(
message_lane: SubstrateMessageLaneToSubstrate {
source_client: source_client.clone(),
source_sign: params.source_sign,
source_transactions_mortality: params.source_transactions_mortality,
target_client: params.target_client.clone(),
target_sign: params.target_sign,
target_transactions_mortality: params.target_transactions_mortality,
relayer_id_at_source: relayer_id_at_polkadot,
},
};
Expand All @@ -205,12 +222,17 @@ pub async fn run(
Max messages in single transaction: {}\n\t\
Max messages size in single transaction: {}\n\t\
Max messages weight in single transaction: {}\n\t\
Relayer mode: {:?}",
Relayer mode: {:?}\n\t\
Tx mortality: {:?}/{:?}\n\t\
Stall timeout: {:?}",
lane.message_lane.relayer_id_at_source,
max_messages_in_single_batch,
max_messages_size_in_single_batch,
max_messages_weight_in_single_batch,
params.relayer_mode,
params.source_transactions_mortality,
params.target_transactions_mortality,
stall_timeout,
);

let (metrics_params, metrics_values) = add_standalone_metrics(
Expand Down
Loading

0 comments on commit 1ef41a5

Please sign in to comment.