Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Implementation of `ProcessMessage` for an `ExecuteXcm` implementation.
use frame_support::{
ensure,
traits::{ProcessMessage, ProcessMessageError},
};
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::*;
/// A message processor that delegates execution to an [XcmExecutor].
pub struct ProcessXcmMessage<MessageOrigin, XcmExecutor, Call>(
PhantomData<(MessageOrigin, XcmExecutor, Call)>,
);
impl<
MessageOrigin: Into<MultiLocation> + FullCodec + MaxEncodedLen + Clone + Eq + PartialEq + TypeInfo + Debug,
XcmExecutor: ExecuteXcm<Call>,
Call,
> ProcessMessage for ProcessXcmMessage<MessageOrigin, XcmExecutor, Call>
{
type Origin = MessageOrigin;
/// Process the given message, using no more than the remaining `weight` to do so.
fn process_message(
message: &[u8],
origin: Self::Origin,
meter: &mut WeightMeter,
) -> 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)
.map_err(|_| ProcessMessageError::Unsupported)?;
let pre = XcmExecutor::prepare(message).map_err(|_| ProcessMessageError::Unsupported)?;
let required = pre.weight_of();
ensure!(meter.can_accrue(required), ProcessMessageError::Overweight(required));
let (consumed, result) =
match XcmExecutor::execute(origin.into(), pre, hash, 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.
Outcome::Error(_) => (required, Err(ProcessMessageError::Unsupported)),
};
meter.defensive_saturating_accrue(consumed);
result
}
}
#[cfg(test)]
mod tests {
use super::*;
use frame_support::{
assert_err, assert_ok,
traits::{ProcessMessageError, ProcessMessageError::*},
};
use parity_scale_codec::Encode;
use polkadot_test_runtime::*;
use xcm::{v2, v3, VersionedXcm};
const ORIGIN: Junction = Junction::OnlyChild;
/// The processor to use for tests.
type Processor =
ProcessXcmMessage<Junction, xcm_executor::XcmExecutor<xcm_config::XcmConfig>, RuntimeCall>;
#[test]
fn process_message_trivial_works() {
// ClearOrigin works.
assert!(process(v2_xcm(true)).unwrap());
assert!(process(v3_xcm(true)).unwrap());
}
#[test]
fn process_message_trivial_fails() {
// Trap makes it fail.
assert!(!process(v3_xcm(false)).unwrap());
assert!(!process(v3_xcm(false)).unwrap());
}
#[test]
fn process_message_corrupted_fails() {
let msgs: &[&[u8]] = &[&[], &[55, 66], &[123, 222, 233]];
for msg in msgs {
assert_err!(process_raw(msg), Corrupt);
}
}
#[test]
fn process_message_overweight_fails() {
for msg in [v3_xcm(true), v3_xcm(false), v3_xcm(false), v2_xcm(false)] {
let msg = &msg.encode()[..];
// Errors if we stay below a weight limit of 1000.
for i in 0..10 {
let meter = &mut WeightMeter::from_limit((i * 10).into());
assert_err!(
Processor::process_message(msg, ORIGIN, meter),
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));
assert_eq!(meter.consumed, 1000.into());
}
}
fn v2_xcm(success: bool) -> VersionedXcm<RuntimeCall> {
let instr = if success {
v3::Instruction::<RuntimeCall>::ClearOrigin
} else {
v3::Instruction::<RuntimeCall>::Trap(1)
};
VersionedXcm::V3(v3::Xcm::<RuntimeCall>(vec![instr]))
}
fn v3_xcm(success: bool) -> VersionedXcm<RuntimeCall> {
let instr = if success {
v2::Instruction::<RuntimeCall>::ClearOrigin
} else {
v2::Instruction::<RuntimeCall>::Trap(1)
};
VersionedXcm::V2(v2::Xcm::<RuntimeCall>(vec![instr]))
}
fn process(msg: VersionedXcm<RuntimeCall>) -> Result<bool, ProcessMessageError> {
process_raw(msg.encode().as_slice())
}
fn process_raw(raw: &[u8]) -> Result<bool, ProcessMessageError> {
Processor::process_message(raw, ORIGIN, &mut WeightMeter::max_limit())
}
}