Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Companion to #14183: FRAME: Allow message ID to be mutated in ProcessMessage #7262

Merged
merged 6 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
490 changes: 303 additions & 187 deletions Cargo.lock

Large diffs are not rendered by default.

259 changes: 259 additions & 0 deletions Cargo.toml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ impl ProcessMessage for MessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
Expand All @@ -1135,7 +1136,7 @@ impl ProcessMessage for MessageProcessor {
Junction,
xcm_executor::XcmExecutor<xcm_config::XcmConfig>,
RuntimeCall,
>::process_message(message, Junction::Parachain(para.into()), meter)
>::process_message(message, Junction::Parachain(para.into()), meter, id)
}
}

Expand Down
1 change: 1 addition & 0 deletions runtime/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ impl ProcessMessage for TestProcessMessage {
message: &[u8],
origin: AggregateMessageOrigin,
meter: &mut WeightMeter,
_id: &mut [u8; 32]
) -> Result<bool, ProcessMessageError> {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(p)) => p,
Expand Down
15 changes: 8 additions & 7 deletions runtime/parachains/src/ump_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use frame_support::{
};
use primitives::v4::{well_known_keys, Id as ParaId, UpwardMessage};
use sp_core::twox_64;
use sp_io::hashing::blake2_256;
use sp_runtime::traits::{Bounded, Hash};
use sp_std::prelude::*;

Expand Down Expand Up @@ -516,27 +517,27 @@ fn overweight_queue_works() {
queue_upward_msg(para_a, a_msg_3.clone());

MessageQueue::service_queues(Weight::from_parts(500, 500));
let hash_1 = <<Test as frame_system::Config>::Hashing as Hash>::hash(&a_msg_1[..]);
let hash_2 = <<Test as frame_system::Config>::Hashing as Hash>::hash(&a_msg_2[..]);
let hash_3 = <<Test as frame_system::Config>::Hashing as Hash>::hash(&a_msg_3[..]);
let hash_1 = blake2_256(&a_msg_1[..]);
let hash_2 = blake2_256(&a_msg_2[..]);
let hash_3 = blake2_256(&a_msg_3[..]);
assert_last_events(
[
pallet_message_queue::Event::<Test>::Processed {
hash: hash_1.clone(),
id: hash_1.clone(),
origin: Ump(UmpQueueId::Para(para_a)),
weight_used: Weight::from_parts(301, 301),
success: true,
}
.into(),
pallet_message_queue::Event::<Test>::OverweightEnqueued {
hash: hash_2.clone(),
id: hash_2.clone(),
origin: Ump(UmpQueueId::Para(para_a)),
page_index: 0,
message_index: 1,
}
.into(),
pallet_message_queue::Event::<Test>::OverweightEnqueued {
hash: hash_3.clone(),
id: hash_3.clone(),
origin: Ump(UmpQueueId::Para(para_a)),
page_index: 0,
message_index: 2,
Expand Down Expand Up @@ -564,7 +565,7 @@ fn overweight_queue_works() {
));
assert_last_event(
pallet_message_queue::Event::<Test>::Processed {
hash: hash_3,
id: hash_3,
origin: Ump(UmpQueueId::Para(para_a)),
weight_used: Weight::from_parts(501, 501),
success: true,
Expand Down
3 changes: 2 additions & 1 deletion runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ impl ProcessMessage for MessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
Expand All @@ -1145,7 +1146,7 @@ impl ProcessMessage for MessageProcessor {
Junction,
xcm_executor::XcmExecutor<xcm_config::XcmConfig>,
RuntimeCall,
>::process_message(message, Junction::Parachain(para.into()), meter)
>::process_message(message, Junction::Parachain(para.into()), meter, id)
}
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ impl ProcessMessage for MessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
Expand All @@ -1072,7 +1073,7 @@ impl ProcessMessage for MessageProcessor {
Junction,
xcm_executor::XcmExecutor<xcm_config::XcmConfig>,
RuntimeCall,
>::process_message(message, Junction::Parachain(para.into()), meter)
>::process_message(message, Junction::Parachain(para.into()), meter, id)
}
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ impl ProcessMessage for MessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
Expand All @@ -945,7 +946,7 @@ impl ProcessMessage for MessageProcessor {
Junction,
xcm_executor::XcmExecutor<xcm_config::XcmConfig>,
RuntimeCall,
>::process_message(message, Junction::Parachain(para.into()), meter)
>::process_message(message, Junction::Parachain(para.into()), meter, id)
}
}

Expand Down
13 changes: 7 additions & 6 deletions xcm/xcm-builder/src/process_xcm_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use frame_support::{
};
use parity_scale_codec::{Decode, FullCodec, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_io::hashing::blake2_256;
use sp_std::{fmt::Debug, marker::PhantomData};
use sp_weights::{Weight, WeightMeter};
use xcm::prelude::*;
Expand All @@ -44,8 +43,8 @@ impl<
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut XcmHash,
) -> Result<bool, ProcessMessageError> {
let hash = blake2_256(message);
let versioned_message = VersionedXcm::<Call>::decode(&mut &message[..])
.map_err(|_| ProcessMessageError::Corrupt)?;
let message = Xcm::<Call>::try_from(versioned_message)
Expand All @@ -55,7 +54,7 @@ impl<
ensure!(meter.can_accrue(required), ProcessMessageError::Overweight(required));

let (consumed, result) =
match XcmExecutor::execute(origin.into(), pre, hash, Weight::zero()) {
match XcmExecutor::execute(origin.into(), pre, *id, Weight::zero()) {
Outcome::Complete(w) => (w, Ok(true)),
Outcome::Incomplete(w, _) => (w, Ok(false)),
// In the error-case we assume the worst case and consume all possible weight.
Expand Down Expand Up @@ -112,16 +111,18 @@ mod tests {
// Errors if we stay below a weight limit of 1000.
for i in 0..10 {
let meter = &mut WeightMeter::from_limit((i * 10).into());
let mut id = [0; 32];
assert_err!(
Processor::process_message(msg, ORIGIN, meter),
Processor::process_message(msg, ORIGIN, meter, &mut id),
Overweight(1000.into())
);
assert_eq!(meter.consumed, 0.into());
}

// Works with a limit of 1000.
let meter = &mut WeightMeter::from_limit(1000.into());
assert_ok!(Processor::process_message(msg, ORIGIN, meter));
let mut id = [0; 32];
assert_ok!(Processor::process_message(msg, ORIGIN, meter, &mut id));
assert_eq!(meter.consumed, 1000.into());
}
}
Expand Down Expand Up @@ -149,6 +150,6 @@ mod tests {
}

fn process_raw(raw: &[u8]) -> Result<bool, ProcessMessageError> {
Processor::process_message(raw, ORIGIN, &mut WeightMeter::max_limit())
Processor::process_message(raw, ORIGIN, &mut WeightMeter::max_limit(), &mut [0; 32])
}
}
3 changes: 2 additions & 1 deletion xcm/xcm-simulator/example/src/relay_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl ProcessMessage for MessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
Expand All @@ -265,7 +266,7 @@ impl ProcessMessage for MessageProcessor {
Junction,
xcm_executor::XcmExecutor<XcmConfig>,
RuntimeCall,
>::process_message(message, Junction::Parachain(para.into()), meter)
>::process_message(message, Junction::Parachain(para.into()), meter, id)
}
}

Expand Down
3 changes: 2 additions & 1 deletion xcm/xcm-simulator/fuzzer/src/relay_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl ProcessMessage for MessageProcessor {
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, ProcessMessageError> {
let para = match origin {
AggregateMessageOrigin::Ump(UmpQueueId::Para(para)) => para,
Expand All @@ -229,7 +230,7 @@ impl ProcessMessage for MessageProcessor {
Junction,
xcm_executor::XcmExecutor<XcmConfig>,
RuntimeCall,
>::process_message(message, Junction::Parachain(para.into()), meter)
>::process_message(message, Junction::Parachain(para.into()), meter, id)
}
}

Expand Down
3 changes: 3 additions & 0 deletions xcm/xcm-simulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ macro_rules! decl_test_relay_chain {
msg: &[u8],
para: Self::Origin,
meter: &mut $crate::WeightMeter,
id: &mut [u8; 32],
) -> Result<bool, $crate::ProcessMessageError> {
use $crate::{Weight, AggregateMessageOrigin, UmpQueueId, ServiceQueues, EnqueueMessage};
use $mq as message_queue;
Expand Down Expand Up @@ -320,9 +321,11 @@ macro_rules! decl_test_network {
match destination.interior() {
$crate::Junctions::Here if destination.parent_count() == 1 => {
let encoded = $crate::encode_xcm(message, $crate::MessageKind::Ump);
let mut _id = [0; 32];
let r = <$relay_chain>::process_message(
encoded.as_slice(), para_id,
&mut $crate::WeightMeter::max_limit(),
&mut _id,
);
match r {
Err($crate::ProcessMessageError::Overweight(required)) =>
Expand Down