|
| 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 | +} |
0 commit comments