Skip to content

Commit 1f54ef6

Browse files
bkonturdharjeezy
authored andcommitted
Added tests for XCM barriers: AllowSubscriptions, WithUniqueTopic and TrailingSetTopicAsId (paritytech#3955)
Closes: paritytech#1756
1 parent d5d2408 commit 1f54ef6

5 files changed

Lines changed: 181 additions & 10 deletions

File tree

polkadot/xcm/xcm-builder/src/tests/barriers.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,62 @@ fn suspension_should_work() {
309309
);
310310
assert_eq!(r, Ok(()));
311311
}
312+
313+
#[test]
314+
fn allow_subscriptions_from_should_work() {
315+
// allow only parent
316+
AllowSubsFrom::set(vec![Location::parent()]);
317+
318+
let valid_xcm_1 = Xcm::<TestCall>(vec![SubscribeVersion {
319+
query_id: 42,
320+
max_response_weight: Weight::from_parts(5000, 5000),
321+
}]);
322+
let valid_xcm_2 = Xcm::<TestCall>(vec![UnsubscribeVersion]);
323+
let invalid_xcm_1 = Xcm::<TestCall>(vec![
324+
SetAppendix(Xcm(vec![])),
325+
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
326+
]);
327+
let invalid_xcm_2 = Xcm::<TestCall>(vec![
328+
SubscribeVersion { query_id: 42, max_response_weight: Weight::from_parts(5000, 5000) },
329+
SetTopic([0; 32]),
330+
]);
331+
332+
let test_data = vec![
333+
(
334+
valid_xcm_1.clone(),
335+
Parachain(1).into_location(),
336+
// not allowed origin
337+
Err(ProcessMessageError::Unsupported),
338+
),
339+
(valid_xcm_1, Location::parent(), Ok(())),
340+
(
341+
valid_xcm_2.clone(),
342+
Parachain(1).into_location(),
343+
// not allowed origin
344+
Err(ProcessMessageError::Unsupported),
345+
),
346+
(valid_xcm_2, Location::parent(), Ok(())),
347+
(
348+
invalid_xcm_1,
349+
Location::parent(),
350+
// invalid XCM
351+
Err(ProcessMessageError::BadFormat),
352+
),
353+
(
354+
invalid_xcm_2,
355+
Location::parent(),
356+
// invalid XCM
357+
Err(ProcessMessageError::BadFormat),
358+
),
359+
];
360+
361+
for (mut message, origin, expected_result) in test_data {
362+
let r = AllowSubscriptionsFrom::<IsInVec<AllowSubsFrom>>::should_execute(
363+
&origin,
364+
message.inner_mut(),
365+
Weight::from_parts(10, 10),
366+
&mut props(Weight::zero()),
367+
);
368+
assert_eq!(r, expected_result, "Failed for origin: {origin:?} and message: {message:?}");
369+
}
370+
}

polkadot/xcm/xcm-builder/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod locking;
3636
mod origins;
3737
mod pay;
3838
mod querying;
39+
mod routing;
3940
mod transacting;
4041
mod version_subscriptions;
4142
mod weight;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (C) Parity Technologies (UK) Ltd.
2+
// This file is part of Polkadot.
3+
4+
// Polkadot is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// Polkadot is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use super::*;
18+
use frame_support::{assert_ok, traits::Everything};
19+
use xcm_executor::traits::Properties;
20+
21+
fn props() -> Properties {
22+
Properties { weight_credit: Weight::zero(), message_id: None }
23+
}
24+
25+
#[test]
26+
fn trailing_set_topic_as_id_with_unique_topic_should_work() {
27+
type AllowSubscriptions = AllowSubscriptionsFrom<Everything>;
28+
29+
// check the validity of XCM for the `AllowSubscriptions` barrier
30+
let valid_xcm = Xcm::<()>(vec![SubscribeVersion {
31+
query_id: 42,
32+
max_response_weight: Weight::from_parts(5000, 5000),
33+
}]);
34+
assert_eq!(
35+
AllowSubscriptions::should_execute(
36+
&Location::parent(),
37+
valid_xcm.clone().inner_mut(),
38+
Weight::from_parts(10, 10),
39+
&mut props(),
40+
),
41+
Ok(())
42+
);
43+
44+
// simulate sending `valid_xcm` with the `WithUniqueTopic` router
45+
let mut sent_xcm = sp_io::TestExternalities::default().execute_with(|| {
46+
assert_ok!(send_xcm::<WithUniqueTopic<TestMessageSender>>(Location::parent(), valid_xcm,));
47+
sent_xcm()
48+
});
49+
assert_eq!(1, sent_xcm.len());
50+
51+
// `sent_xcm` should contain `SubscribeVersion` and have `SetTopic` added
52+
let mut sent_xcm = sent_xcm.remove(0).1;
53+
let _ = sent_xcm
54+
.0
55+
.matcher()
56+
.assert_remaining_insts(2)
57+
.expect("two instructions")
58+
.match_next_inst(|instr| match instr {
59+
SubscribeVersion { .. } => Ok(()),
60+
_ => Err(ProcessMessageError::BadFormat),
61+
})
62+
.expect("expected instruction `SubscribeVersion`")
63+
.match_next_inst(|instr| match instr {
64+
SetTopic(..) => Ok(()),
65+
_ => Err(ProcessMessageError::BadFormat),
66+
})
67+
.expect("expected instruction `SetTopic`");
68+
69+
// `sent_xcm` contains `SetTopic` and is now invalid for `AllowSubscriptions`
70+
assert_eq!(
71+
AllowSubscriptions::should_execute(
72+
&Location::parent(),
73+
sent_xcm.clone().inner_mut(),
74+
Weight::from_parts(10, 10),
75+
&mut props(),
76+
),
77+
Err(ProcessMessageError::BadFormat)
78+
);
79+
80+
// let's apply `TrailingSetTopicAsId` before `AllowSubscriptions`
81+
let mut props = props();
82+
assert!(props.message_id.is_none());
83+
84+
// should pass, and the `message_id` is set
85+
assert_eq!(
86+
TrailingSetTopicAsId::<AllowSubscriptions>::should_execute(
87+
&Location::parent(),
88+
sent_xcm.clone().inner_mut(),
89+
Weight::from_parts(10, 10),
90+
&mut props,
91+
),
92+
Ok(())
93+
);
94+
assert!(props.message_id.is_some());
95+
}

polkadot/xcm/xcm-builder/src/tests/version_subscriptions.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,32 @@ fn simple_version_subscriptions_should_work() {
2727
]);
2828
let mut hash = fake_message_hash(&message);
2929
let weight_limit = Weight::from_parts(20, 20);
30-
let r = XcmExecutor::<TestConfig>::prepare_and_execute(
31-
origin,
32-
message,
33-
&mut hash,
34-
weight_limit,
35-
Weight::zero(),
30+
31+
// this case fails because the origin is not allowed
32+
assert_eq!(
33+
XcmExecutor::<TestConfig>::prepare_and_execute(
34+
origin,
35+
message.clone(),
36+
&mut hash,
37+
weight_limit,
38+
Weight::zero(),
39+
),
40+
Outcome::Error { error: XcmError::Barrier }
41+
);
42+
43+
// this case fails because the additional `SetAppendix` instruction is not allowed in the
44+
// `AllowSubscriptionsFrom`
45+
assert_eq!(
46+
XcmExecutor::<TestConfig>::prepare_and_execute(
47+
Parent,
48+
message,
49+
&mut hash,
50+
weight_limit,
51+
Weight::zero(),
52+
),
53+
Outcome::Error { error: XcmError::Barrier }
3654
);
37-
assert_eq!(r, Outcome::Error { error: XcmError::Barrier });
3855

39-
let origin = Parachain(1000);
4056
let message = Xcm::<TestCall>(vec![SubscribeVersion {
4157
query_id: 42,
4258
max_response_weight: Weight::from_parts(5000, 5000),

polkadot/xcm/xcm-executor/src/traits/should_execute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ pub struct Properties {
3333
/// Trait to determine whether the execution engine should actually execute a given XCM.
3434
///
3535
/// Can be amalgamated into a tuple to have multiple trials. If any of the tuple elements returns
36-
/// `Ok()`, the execution stops. Else, `Err(_)` is returned if all elements reject the message.
36+
/// `Ok(())`, the execution stops. Else, `Err(_)` is returned if all elements reject the message.
3737
pub trait ShouldExecute {
38-
/// Returns `true` if the given `message` may be executed.
38+
/// Returns `Ok(())` if the given `message` may be executed.
3939
///
4040
/// - `origin`: The origin (sender) of the message.
4141
/// - `instructions`: The message itself.

0 commit comments

Comments
 (0)