git.delta.rocks / unique-network / refs/commits / f8304b7ec440

difftreelog

test(xcm) make barrier unit tests cleaner

Daniel Shiposha2022-09-05parent: #5a619a9.patch.diff
in: master

15 files changed

modifiedruntime/common/mod.rsdiffbeforeafterboth
25pub mod weights;25pub mod weights;
2626
27#[cfg(test)]27#[cfg(test)]
28mod tests;28pub mod tests;
2929
30use sp_core::H160;30use sp_core::H160;
31use frame_support::traits::{Currency, OnUnbalanced, Imbalance};31use frame_support::traits::{Currency, OnUnbalanced, Imbalance};
modifiedruntime/common/tests/mod.rsdiffbeforeafterboth
14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
16
17use sp_runtime::BuildStorage;
18use sp_core::{Public, Pair};
19use sp_std::vec;
20use up_common::types::AuraId;
21use crate::{GenesisConfig, ParachainInfoConfig, AuraConfig};
1622
17mod xcm;23pub mod xcm;
24
25fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
26 TPublic::Pair::from_string(&format!("//{}", seed), None)
27 .expect("static values are valid; qed")
28 .public()
29}
30
31fn new_test_ext(para_id: u32) -> sp_io::TestExternalities {
32 let cfg = GenesisConfig {
33 aura: AuraConfig {
34 authorities: vec![
35 get_from_seed::<AuraId>("Alice"),
36 get_from_seed::<AuraId>("Bob"),
37 ],
38 },
39 parachain_info: ParachainInfoConfig {
40 parachain_id: para_id.into(),
41 },
42 ..GenesisConfig::default()
43 };
44
45 cfg.build_storage()
46 .unwrap()
47 .into()
48}
1849
modifiedruntime/common/tests/xcm.rsdiffbeforeafterboth
17use xcm_executor::traits::ShouldExecute;17use xcm_executor::traits::ShouldExecute;
18use xcm::latest::prelude::*;18use xcm::latest::prelude::*;
19use logtest::Logger;19use logtest::Logger;
20use crate::{20use crate::Call;
21 Call,21use super::new_test_ext;
22 xcm_config::Barrier,
23};
2422
25fn catch_xcm_barrier_log(logger: &mut Logger, expected_msg: &str) {23fn catch_xcm_barrier_log(
24 logger: &mut Logger,
25 expected_msg: &str
26) -> Result<(), String> {
26 for record in logger {27 for record in logger {
27 if record.target() == "xcm::barrier"28 if record.target() == "xcm::barrier"
28 && record.args() == expected_msg {29 && record.args() == expected_msg {
29 return;30 return Ok(());
30 }31 }
31 }32 }
3233
33 panic!("the expected XCM barrier log `{}` is not found", expected_msg);34 Err(format!("the expected XCM barrier log `{}` is not found", expected_msg))
34}35}
3536
36#[test]37/// WARNING: Uses log capturing
38/// See https://docs.rs/logtest/latest/logtest/index.html#constraints
37fn xcm_barrier_denies_transact() {39pub fn barrier_denies_transact<B: ShouldExecute>(logger: &mut Logger) {
38 // We have a `AllowTopLevelPaidExecutionFrom` barrier,
39 // so an XCM program should start from one of the following commands:
40 // * `WithdrawAsset`
41 // * `ReceiveTeleportedAsset`
42 // * `ReserveAssetDeposited`
43 // * `ClaimAsset`
44 // We use the `WithdrawAsset` in this test
45
46 let location = MultiLocation {40 let location = MultiLocation {
47 parents: 0,41 parents: 0,
48 interior: Junctions::Here,42 interior: Junctions::Here,
49 };43 };
50
51 // let id = AssetId::Concrete(location.clone());
52 // let fun = Fungibility::Fungible(42);
53 // let multiasset = MultiAsset {
54 // id,
55 // fun,
56 // };
57 // let withdraw_inst = WithdrawAsset(multiasset.into());
5844
59 // We will never decode this "call",45 // We will never decode this "call",
60 // so it is irrelevant what we are passing to the `transact` cmd.46 // so it is irrelevant what we are passing to the `transact` cmd.
6854
69 let mut xcm_program = Xcm::<Call>(vec![transact_inst]);55 let mut xcm_program = Xcm::<Call>(vec![transact_inst]);
70
71 let mut logger = Logger::start();
7256
73 let max_weight = 100_000;57 let max_weight = 100_000;
74 let mut weight_credit = 0;58 let mut weight_credit = 100_000_000;
7559
76 let result = Barrier::should_execute(60 let result = B::should_execute(
77 &location,61 &location,
78 &mut xcm_program,62 &mut xcm_program,
79 max_weight,63 max_weight,
8266
83 assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");67 assert!(result.is_err(), "the barrier should disallow the XCM transact cmd");
8468
85 catch_xcm_barrier_log(&mut logger, "transact XCM rejected");69 catch_xcm_barrier_log(logger, "transact XCM rejected").unwrap();
86}70}
71
72fn xcm_execute<B: ShouldExecute>(
73 self_para_id: u32,
74 location: &MultiLocation,
75 xcm: &mut Xcm<Call>
76) -> Result<(), ()> {
77 new_test_ext(self_para_id).execute_with(|| {
78 let max_weight = 100_000;
79 let mut weight_credit = 100_000_000;
80
81 B::should_execute(
82 &location,
83 xcm,
84 max_weight,
85 &mut weight_credit
86 )
87 })
88}
89
90fn make_multiassets(location: &MultiLocation) -> MultiAssets {
91 let id = AssetId::Concrete(location.clone());
92 let fun = Fungibility::Fungible(42);
93 let multiasset = MultiAsset {
94 id,
95 fun,
96 };
97
98 multiasset.into()
99}
100
101fn make_transfer_reserve_asset(location: &MultiLocation) -> Xcm<Call> {
102 let assets = make_multiassets(location);
103 let inst = TransferReserveAsset {
104 assets,
105 dest: location.clone(),
106 xcm: Xcm(vec![]),
107 };
108
109 Xcm::<Call>(vec![inst])
110}
111
112fn make_deposit_reserve_asset(location: &MultiLocation) -> Xcm<Call> {
113 let assets = make_multiassets(location);
114 let inst = DepositReserveAsset {
115 assets: assets.into(),
116 max_assets: 42,
117 dest: location.clone(),
118 xcm: Xcm(vec![]),
119 };
120
121 Xcm::<Call>(vec![inst])
122}
123
124fn expect_transfer_location_denied<B: ShouldExecute>(
125 logger: &mut Logger,
126 self_para_id: u32,
127 location: &MultiLocation,
128 xcm: &mut Xcm<Call>
129) -> Result<(), String> {
130 let result = xcm_execute::<B>(self_para_id, location, xcm);
131
132 if result.is_ok() {
133 return Err("the barrier should deny the unknown location".into());
134 }
135
136 catch_xcm_barrier_log(logger, "Unexpected deposit or transfer location")
137}
138
139/// WARNING: Uses log capturing
140/// See https://docs.rs/logtest/latest/logtest/index.html#constraints
141pub fn barrier_denies_transfer_from_unknown_location<B>(
142 logger: &mut Logger,
143 self_para_id: u32,
144) -> Result<(), String>
145where
146 B: ShouldExecute
147{
148 const UNKNOWN_PARACHAIN_ID: u32 = 4057;
149
150 let unknown_location = MultiLocation {
151 parents: 1,
152 interior: X1(Parachain(UNKNOWN_PARACHAIN_ID)),
153 };
154
155 let mut transfer_reserve_asset = make_transfer_reserve_asset(&unknown_location);
156 let mut deposit_reserve_asset = make_deposit_reserve_asset(&unknown_location);
157
158 expect_transfer_location_denied::<B>(
159 logger,
160 self_para_id,
161 &unknown_location,
162 &mut transfer_reserve_asset
163 )?;
164
165 expect_transfer_location_denied::<B>(
166 logger,
167 self_para_id,
168 &unknown_location,
169 &mut deposit_reserve_asset
170 )?;
171
172 Ok(())
173}
87174
modifiedruntime/opal/src/lib.rsdiffbeforeafterboth
3737
38pub mod xcm_config;38pub mod xcm_config;
39
40#[cfg(test)]
41mod tests;
3942
40pub use runtime_common::*;43pub use runtime_common::*;
4144
addedruntime/opal/src/tests/logcapture.rsdiffbeforeafterboth

no changes

addedruntime/opal/src/tests/mod.rsdiffbeforeafterboth

no changes

addedruntime/opal/src/tests/xcm.rsdiffbeforeafterboth

no changes

modifiedruntime/quartz/src/lib.rsdiffbeforeafterboth
3737
38pub mod xcm_config;38pub mod xcm_config;
39
40#[cfg(test)]
41mod tests;
3942
40pub use runtime_common::*;43pub use runtime_common::*;
4144
addedruntime/quartz/src/tests/logcapture.rsdiffbeforeafterboth

no changes

addedruntime/quartz/src/tests/mod.rsdiffbeforeafterboth

no changes

addedruntime/quartz/src/tests/xcm.rsdiffbeforeafterboth

no changes

modifiedruntime/unique/src/lib.rsdiffbeforeafterboth
3737
38pub mod xcm_config;38pub mod xcm_config;
39
40#[cfg(test)]
41mod tests;
3942
40pub use runtime_common::*;43pub use runtime_common::*;
4144
addedruntime/unique/src/tests/logcapture.rsdiffbeforeafterboth

no changes

addedruntime/unique/src/tests/mod.rsdiffbeforeafterboth

no changes

addedruntime/unique/src/tests/xcm.rsdiffbeforeafterboth

no changes