Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RefundRelayerForMessagesFromParachain improvements #1879

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Address code review comments
  • Loading branch information
serban300 committed Feb 14, 2023
commit cf74df9ebb243cf5ecabdbf493fc742dc466369c
2 changes: 1 addition & 1 deletion bin/runtime-common/src/messages_call_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct ReceiveMessagesProofHelper<T: Config<I>, I: 'static> {
impl<T: Config<I>, I: 'static> ReceiveMessagesProofHelper<T, I> {
/// Check if the `ReceiveMessagesProof` call delivered at least some of the messages that
/// it contained.
pub fn was_partially_successful(info: ReceiveMessagesProofInfo) -> bool {
pub fn was_partially_successful(info: &ReceiveMessagesProofInfo) -> bool {
let inbound_lane_data = pallet_bridge_messages::InboundLanes::<T, I>::get(info.lane_id);
inbound_lane_data.last_delivered_nonce() > info.best_stored_nonce
}
Expand Down
166 changes: 82 additions & 84 deletions bin/runtime-common/src/refund_relayer_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use sp_runtime::{
transaction_validity::{TransactionValidity, TransactionValidityError, ValidTransaction},
DispatchResult, FixedPointOperand,
};
use sp_std::{fmt::Debug, marker::PhantomData, vec};
use sp_std::{marker::PhantomData, vec, vec::Vec};

// TODO (https://github.com/paritytech/parity-bridges-common/issues/1667):
// support multiple bridges in this extension
Expand Down Expand Up @@ -77,14 +77,6 @@ where
}
}

/// Structure containing some data for each call in a supported (`utility::batch_all`) transaction.
#[derive(Clone, Copy, PartialEq, RuntimeDebugNoBound)]
pub struct TxData<Grandpa: Debug, Parachains: Debug, Messages: Debug> {
submit_finality_proof: Option<Grandpa>,
submit_parachain_heads: Option<Parachains>,
receive_messages_proof: Messages,
}

/// Signed extension that refunds relayer for new messages coming from the parachain.
///
/// Also refunds relayer for successful finality delivery if it comes in batch (`utility.batchAll`)
Expand Down Expand Up @@ -114,11 +106,8 @@ where
R: UtilityConfig<RuntimeCall = CallOf<R>>,
CallOf<R>: IsSubType<CallableCallFor<UtilityPallet<R>, R>>,
{
fn expand_call<'a>(
&self,
call: &'a CallOf<R>,
) -> Option<TxData<&'a CallOf<R>, &'a CallOf<R>, &'a CallOf<R>>> {
let mut calls = match call.is_sub_type() {
fn expand_call<'a>(&self, call: &'a CallOf<R>) -> Option<Vec<&'a CallOf<R>>> {
let calls = match call.is_sub_type() {
Some(UtilityCall::<R>::batch_all { ref calls }) => {
if calls.len() > 3 {
return None
Expand All @@ -128,15 +117,9 @@ where
},
Some(_) => return None,
None => vec![call],
}
.into_iter()
.rev();

let receive_messages_proof = calls.next()?;
let submit_parachain_heads = calls.next();
let submit_finality_proof = calls.next();
};

Some(TxData { submit_finality_proof, submit_parachain_heads, receive_messages_proof })
Some(calls)
}
}

Expand All @@ -146,8 +129,30 @@ where
pub struct PreDispatchData<AccountId> {
/// Transaction submitter (relayer) account.
relayer: AccountId,
/// The info for each call in the transaction.
tx_info: TxData<RelayBlockNumber, SubmitParachainHeadsInfo, ReceiveMessagesProofInfo>,
/// Type of the call.
pub call_type: CallType,
}

/// Type of the call that the extension recognizes.
#[derive(Clone, Copy, PartialEq, RuntimeDebugNoBound)]
pub enum CallType {
/// Relay chain finality + parachain finality + message delivery calls.
AllFinalityAndDelivery(RelayBlockNumber, SubmitParachainHeadsInfo, ReceiveMessagesProofInfo),
/// Parachain finality + message delivery calls.
ParachainFinalityAndDelivery(SubmitParachainHeadsInfo, ReceiveMessagesProofInfo),
/// Standalone message delivery call.
Delivery(ReceiveMessagesProofInfo),
}

impl CallType {
/// Returns the pre-dispatch messages pallet state.
fn receive_messages_proof_info(&self) -> ReceiveMessagesProofInfo {
match *self {
Self::AllFinalityAndDelivery(_, _, info) => info,
Self::ParachainFinalityAndDelivery(_, info) => info,
Self::Delivery(info) => info,
}
}
}

// without this typedef rustfmt fails with internal err
Expand Down Expand Up @@ -200,13 +205,11 @@ where
None => return Ok(ValidTransaction::default()),
};

if let Some(submit_finality_proof) = calls.submit_finality_proof {
submit_finality_proof.check_obsolete_submit_finality_proof()?;
}
if let Some(submit_parachain_heads) = calls.submit_parachain_heads {
submit_parachain_heads.check_obsolete_submit_parachain_heads()?;
for call in calls {
call.check_obsolete_submit_finality_proof()?;
call.check_obsolete_submit_parachain_heads()?;
call.check_obsolete_receive_messages_proof()?;
}
calls.receive_messages_proof.check_obsolete_receive_messages_proof()?;

Ok(ValidTransaction::default())
}
Expand All @@ -222,38 +225,35 @@ where
self.validate(who, call, info, len).map(drop)?;

// Try to check if the tx matches one of types we support.
let parse_call = || {
let calls = self.expand_call(call)?;

let mut tx_info = TxData {
submit_finality_proof: None,
submit_parachain_heads: None,
receive_messages_proof: calls
.receive_messages_proof
.receive_messages_proof_info_for(LID::get())?,
};
if let Some(submit_parachain_heads) = calls.submit_parachain_heads {
tx_info.submit_parachain_heads =
Some(submit_parachain_heads.submit_parachain_heads_info_for(PID::get())?);
let parse_call_type = || {
let mut calls = self.expand_call(call)?.into_iter();
match calls.len() {
3 => Some(CallType::AllFinalityAndDelivery(
calls.next()?.submit_finality_proof_info()?,
calls.next()?.submit_parachain_heads_info_for(PID::get())?,
calls.next()?.receive_messages_proof_info_for(LID::get())?,
)),
2 => Some(CallType::ParachainFinalityAndDelivery(
calls.next()?.submit_parachain_heads_info_for(PID::get())?,
calls.next()?.receive_messages_proof_info_for(LID::get())?,
)),
1 => Some(CallType::Delivery(
calls.next()?.receive_messages_proof_info_for(LID::get())?,
)),
_ => None,
}
if let Some(submit_finality_proof) = calls.submit_finality_proof {
tx_info.submit_finality_proof =
Some(submit_finality_proof.submit_finality_proof_info()?);
}

Some(tx_info)
};

Ok(parse_call().map(|tx_info| {
Ok(parse_call_type().map(|call_type| {
log::trace!(
target: "runtime::bridge",
"RefundRelayerForMessagesFromParachain from parachain {} via {:?} \
parsed bridge transaction in pre-dispatch: {:?}",
PID::get(),
LID::get(),
tx_info,
call_type,
);
PreDispatchData { relayer: who.clone(), tx_info }
PreDispatchData { relayer: who.clone(), call_type }
}))
}

Expand All @@ -270,13 +270,13 @@ where
}

// We don't refund anything for transactions that we don't support.
let (relayer, calls_info) = match pre {
Some(Some(pre)) => (pre.relayer, pre.tx_info),
let (relayer, call_type) = match pre {
Some(Some(pre)) => (pre.relayer, pre.call_type),
_ => return Ok(()),
};

// check if relay chain state has been updated
if let Some(relay_block_number) = calls_info.submit_finality_proof {
if let CallType::AllFinalityAndDelivery(relay_block_number, _, _) = call_type {
if !SubmitFinalityProofHelper::<R, GI>::was_successful(relay_block_number) {
// we only refund relayer if all calls have updated chain state
return Ok(())
Expand All @@ -291,18 +291,21 @@ where
}

// check if parachain state has been updated
if let Some(parachain_proof_info) = calls_info.submit_parachain_heads {
if !SubmitParachainHeadsHelper::<R, PI>::was_successful(&parachain_proof_info) {
// we only refund relayer if all calls have updated chain state
return Ok(())
}
match call_type {
CallType::AllFinalityAndDelivery(_, parachain_heads_info, _) |
CallType::ParachainFinalityAndDelivery(parachain_heads_info, _) => {
if !SubmitParachainHeadsHelper::<R, PI>::was_successful(&parachain_heads_info) {
// we only refund relayer if all calls have updated chain state
return Ok(())
}
},
_ => (),
}

// Check if the `ReceiveMessagesProof` call delivered at least some of the messages that
// it contained. If this happens, we consider the transaction "helpful" and refund it.
if !ReceiveMessagesProofHelper::<R, MI>::was_partially_successful(
calls_info.receive_messages_proof,
) {
let messages_proof_info = call_type.receive_messages_proof_info();
if !ReceiveMessagesProofHelper::<R, MI>::was_partially_successful(&messages_proof_info) {
return Ok(())
}

Expand Down Expand Up @@ -467,53 +470,48 @@ mod tests {
fn all_finality_pre_dispatch_data() -> PreDispatchData<ThisChainAccountId> {
PreDispatchData {
relayer: relayer_account_at_this_chain(),
tx_info: TxData {
submit_finality_proof: Some(200),
submit_parachain_heads: Some(SubmitParachainHeadsInfo {
call_type: CallType::AllFinalityAndDelivery(
200,
SubmitParachainHeadsInfo {
at_relay_block_number: 200,
para_id: ParaId(TestParachain::get()),
para_head_hash: [1u8; 32].into(),
}),
receive_messages_proof: ReceiveMessagesProofInfo {
},
ReceiveMessagesProofInfo {
lane_id: TEST_LANE_ID,
best_proof_nonce: 200,
best_stored_nonce: 100,
},
},
),
}
}

fn parachain_finality_pre_dispatch_data() -> PreDispatchData<ThisChainAccountId> {
PreDispatchData {
relayer: relayer_account_at_this_chain(),
tx_info: TxData {
submit_finality_proof: None,
submit_parachain_heads: Some(SubmitParachainHeadsInfo {
call_type: CallType::ParachainFinalityAndDelivery(
SubmitParachainHeadsInfo {
at_relay_block_number: 200,
para_id: ParaId(TestParachain::get()),
para_head_hash: [1u8; 32].into(),
}),
receive_messages_proof: ReceiveMessagesProofInfo {
},
ReceiveMessagesProofInfo {
lane_id: TEST_LANE_ID,
best_proof_nonce: 200,
best_stored_nonce: 100,
},
},
),
}
}

fn delivery_pre_dispatch_data() -> PreDispatchData<ThisChainAccountId> {
PreDispatchData {
relayer: relayer_account_at_this_chain(),
tx_info: TxData {
submit_finality_proof: None,
submit_parachain_heads: None,
receive_messages_proof: ReceiveMessagesProofInfo {
lane_id: TEST_LANE_ID,
best_proof_nonce: 200,
best_stored_nonce: 100,
},
},
call_type: CallType::Delivery(ReceiveMessagesProofInfo {
lane_id: TEST_LANE_ID,
best_proof_nonce: 200,
best_stored_nonce: 100,
}),
}
}

Expand Down