Skip to content

Commit

Permalink
update runtimes with actual xdm verification
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Feb 6, 2024
1 parent db6623a commit c209306
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
8 changes: 6 additions & 2 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,12 @@ fn extract_segment_headers(ext: &UncheckedExtrinsic) -> Option<Vec<SegmentHeader
fn is_xdm_valid(encoded_ext: Vec<u8>) -> Option<bool> {
if let Ok(ext) = UncheckedExtrinsic::decode(&mut encoded_ext.as_slice()) {
match &ext.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg }) => {
Some(Messenger::validate_relay_message(msg).is_ok())
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(Messenger::validate_relay_message_response(msg).is_ok())
}
_ => None,
}
} else {
Expand Down
16 changes: 9 additions & 7 deletions domains/pallets/messenger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ pub(crate) type StateRootOf<T> = <<T as frame_system::Config>::Hashing as Hash>:
pub(crate) type BalanceOf<T> =
<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;

pub(crate) struct ValidatedRelayMessage<Balance> {
/// A validated relay message.
#[derive(Debug)]
pub struct ValidatedRelayMessage<Balance> {
msg: Message<Balance>,
next_nonce: Nonce,
should_init_channel: bool,
Expand Down Expand Up @@ -306,7 +308,7 @@ mod pallet {
msg,
next_nonce,
should_init_channel,
} = Self::do_validate_relay_message(xdm)?;
} = Self::validate_relay_message(xdm)?;
if msg.nonce != next_nonce {
log::error!(
"Unexpected message nonce, channel next nonce {:?}, msg nonce {:?}",
Expand All @@ -323,7 +325,7 @@ mod pallet {
Self::pre_dispatch_relay_message(msg, should_init_channel)
}
Call::relay_message_response { msg: xdm } => {
let (msg, next_nonce) = Self::do_validate_relay_message_response(xdm)?;
let (msg, next_nonce) = Self::validate_relay_message_response(xdm)?;
if msg.nonce != next_nonce {
log::error!(
"Unexpected message response nonce, channel next nonce {:?}, msg nonce {:?}",
Expand Down Expand Up @@ -351,7 +353,7 @@ mod pallet {
msg,
next_nonce,
should_init_channel: _,
} = Self::do_validate_relay_message(xdm)?;
} = Self::validate_relay_message(xdm)?;

let mut valid_tx_builder = ValidTransaction::with_tag_prefix("MessengerInbox");
// Only add the requires tag if the msg nonce is in future
Expand All @@ -370,7 +372,7 @@ mod pallet {
.build()
}
Call::relay_message_response { msg: xdm } => {
let (msg, next_nonce) = Self::do_validate_relay_message_response(xdm)?;
let (msg, next_nonce) = Self::validate_relay_message_response(xdm)?;

let mut valid_tx_builder =
ValidTransaction::with_tag_prefix("MessengerOutboxResponse");
Expand Down Expand Up @@ -695,7 +697,7 @@ mod pallet {
Ok(channel_id)
}

pub(crate) fn do_validate_relay_message(
pub fn validate_relay_message(
xdm: &CrossDomainMessage<T::Hash, T::MmrHash>,
) -> Result<ValidatedRelayMessage<BalanceOf<T>>, TransactionValidityError> {
let mut should_init_channel = false;
Expand Down Expand Up @@ -792,7 +794,7 @@ mod pallet {
Ok(())
}

pub(crate) fn do_validate_relay_message_response(
pub fn validate_relay_message_response(
xdm: &CrossDomainMessage<T::Hash, T::MmrHash>,
) -> Result<(Message<BalanceOf<T>>, Nonce), TransactionValidityError> {
// channel should be open and message should be present in outbox
Expand Down
8 changes: 6 additions & 2 deletions domains/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,12 @@ impl fp_rpc::ConvertTransaction<opaque::UncheckedExtrinsic> for TransactionConve
fn is_xdm_valid(encoded_ext: Vec<u8>) -> Option<bool> {
if let Ok(ext) = UncheckedExtrinsic::decode(&mut encoded_ext.as_slice()) {
match &ext.0.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg }) => {
Some(Messenger::validate_relay_message(msg).is_ok())
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(Messenger::validate_relay_message_response(msg).is_ok())
}
_ => None,
}
} else {
Expand Down
8 changes: 6 additions & 2 deletions domains/test/runtime/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,12 @@ impl fp_rpc::ConvertTransaction<opaque::UncheckedExtrinsic> for TransactionConve
fn is_xdm_valid(encoded_ext: Vec<u8>) -> Option<bool> {
if let Ok(ext) = UncheckedExtrinsic::decode(&mut encoded_ext.as_slice()) {
match &ext.0.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg }) => {
Some(Messenger::validate_relay_message(msg).is_ok())
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(Messenger::validate_relay_message_response(msg).is_ok())
}
_ => None,
}
} else {
Expand Down
8 changes: 6 additions & 2 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,12 @@ fn extract_segment_headers(ext: &UncheckedExtrinsic) -> Option<Vec<SegmentHeader
fn is_xdm_valid(encoded_ext: Vec<u8>) -> Option<bool> {
if let Ok(ext) = UncheckedExtrinsic::decode(&mut encoded_ext.as_slice()) {
match &ext.function {
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { .. }) => None,
RuntimeCall::Messenger(pallet_messenger::Call::relay_message { msg }) => {
Some(Messenger::validate_relay_message(msg).is_ok())
}
RuntimeCall::Messenger(pallet_messenger::Call::relay_message_response { msg }) => {
Some(Messenger::validate_relay_message_response(msg).is_ok())
}
_ => None,
}
} else {
Expand Down

0 comments on commit c209306

Please sign in to comment.