Newer
Older
// Copyright (C) Parity Technologies (UK) Ltd.
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
// This file is part of Parity Bridges Common.
// Parity Bridges Common 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.
// Parity Bridges Common 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 Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
#![cfg(test)]
use crate as pallet_xcm_bridge_hub_router;
use bp_xcm_bridge_hub_router::XcmChannelStatusProvider;
use frame_support::{construct_runtime, parameter_types};
use frame_system::EnsureRoot;
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, ConstU128, IdentityLookup},
BuildStorage,
};
use xcm::prelude::*;
use xcm_builder::NetworkExportTable;
pub type AccountId = u64;
type Block = frame_system::mocking::MockBlock<TestRuntime>;
/// HRMP fee.
pub const HRMP_FEE: u128 = 500;
/// Base bridge fee.
pub const BASE_FEE: u128 = 1_000_000;
/// Byte bridge fee.
pub const BYTE_FEE: u128 = 1_000;
construct_runtime! {
pub enum TestRuntime
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
XcmBridgeHubRouter: pallet_xcm_bridge_hub_router::{Pallet, Storage},
}
}
parameter_types! {
pub ThisNetworkId: NetworkId = Polkadot;
pub BridgedNetworkId: NetworkId = Kusama;
pub UniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(ThisNetworkId::get()), Parachain(1000));
pub SiblingBridgeHubLocation: MultiLocation = ParentThen(X1(Parachain(1002))).into();
pub BridgeFeeAsset: AssetId = MultiLocation::parent().into();
pub BridgeTable: Vec<(NetworkId, MultiLocation, Option<MultiAsset>)>
= vec![(BridgedNetworkId::get(), SiblingBridgeHubLocation::get(), Some((BridgeFeeAsset::get(), BASE_FEE).into()))];
}
impl frame_system::Config for TestRuntime {
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type RuntimeCall = RuntimeCall;
type Block = Block;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = frame_support::traits::ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type BaseCallFilter = frame_support::traits::Everything;
type SystemWeightInfo = ();
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
impl pallet_xcm_bridge_hub_router::Config<()> for TestRuntime {
type WeightInfo = ();
type UniversalLocation = UniversalLocation;
type BridgedNetworkId = BridgedNetworkId;
type Bridges = NetworkExportTable<BridgeTable>;
type BridgeHubOrigin = EnsureRoot<AccountId>;
type ToBridgeHubSender = TestToBridgeHubSender;
type WithBridgeHubChannel = TestWithBridgeHubChannel;
type ByteFee = ConstU128<BYTE_FEE>;
type FeeAsset = BridgeFeeAsset;
}
pub struct TestToBridgeHubSender;
impl TestToBridgeHubSender {
pub fn is_message_sent() -> bool {
frame_support::storage::unhashed::get_or_default(b"TestToBridgeHubSender.Sent")
}
}
impl SendXcm for TestToBridgeHubSender {
type Ticket = ();
fn validate(
_destination: &mut Option<MultiLocation>,
_message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
Ok(((), (BridgeFeeAsset::get(), HRMP_FEE).into()))
}
fn deliver(_ticket: Self::Ticket) -> Result<XcmHash, SendError> {
frame_support::storage::unhashed::put(b"TestToBridgeHubSender.Sent", &true);
Ok([0u8; 32])
}
}
pub struct TestWithBridgeHubChannel;
impl TestWithBridgeHubChannel {
pub fn make_congested() {
frame_support::storage::unhashed::put(b"TestWithBridgeHubChannel.Congested", &true);
}
}
impl XcmChannelStatusProvider for TestWithBridgeHubChannel {
fn is_congested() -> bool {
frame_support::storage::unhashed::get_or_default(b"TestWithBridgeHubChannel.Congested")
}
}
/// Return test externalities to use in tests.
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<TestRuntime>::default().build_storage().unwrap();
sp_io::TestExternalities::new(t)
}
/// Run pallet test.
pub fn run_test<T>(test: impl FnOnce() -> T) -> T {
new_test_ext().execute_with(test)