-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathmock.rs
More file actions
211 lines (190 loc) · 7.03 KB
/
Copy pathmock.rs
File metadata and controls
211 lines (190 loc) · 7.03 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2019-2025 PureStake Inc.
// This file is part of Moonbeam.
// Moonbeam 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.
// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.
//! Test utilities
use crate as erc20_xcm_bridge;
use frame_support::traits::Everything;
use frame_support::{construct_runtime, pallet_prelude::*, parameter_types};
use frame_system::EnsureRoot;
use pallet_evm::{
AddressMapping, EnsureAddressTruncated, FrameSystemAccountProvider, SubstrateBlockHashMapping,
};
use sp_core::{H160, H256, U256};
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
use sp_runtime::AccountId32;
pub type Balance = u128;
type Block = frame_system::mocking::MockBlock<Test>;
construct_runtime!(
pub enum Test
{
System: frame_system,
Balances: pallet_balances,
Timestamp: pallet_timestamp,
EVM: pallet_evm,
Erc20XcmBridge: erc20_xcm_bridge::{Pallet, Call, Storage, Event<T>},
}
);
parameter_types! {
pub const BlockHashCount: u32 = 250;
}
impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeTask = RuntimeTask;
type Nonce = u64;
type Block = Block;
type RuntimeCall = RuntimeCall;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId32;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type BlockWeights = ();
type BlockLength = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type SingleBlockMigrations = ();
type MultiBlockMigrator = ();
type PreInherents = ();
type PostInherents = ();
type PostTransactions = ();
type ExtensionsWeightInfo = ();
}
parameter_types! {
pub const ExistentialDeposit: u128 = 0;
}
impl pallet_balances::Config for Test {
type MaxReserves = ();
type ReserveIdentifier = [u8; 4];
type MaxLocks = ();
type Balance = Balance;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type RuntimeHoldReason = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeFreezeReason = ();
type DoneSlashHandler = ();
}
parameter_types! {
pub const MinimumPeriod: u64 = 6000 / 2;
}
impl pallet_timestamp::Config for Test {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
/// Block storage limit in bytes. Set to 40 KB.
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
parameter_types! {
pub BlockGasLimit: U256 = U256::from(u64::MAX);
// Non-zero proof_size component so `pallet_evm`'s runner has a budget large enough
// to record `ACCOUNT_CODES_METADATA_PROOF_SIZE` (~76 bytes) at the start of a call.
// Without this the runner returns `GasLimitTooLow` for any contract call.
pub const WeightPerGas: Weight = Weight::from_parts(1, 1);
pub GasLimitPovSizeRatio: u64 = {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(MAX_POV_SIZE)
};
pub GasLimitStorageGrowthRatio: u64 = {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
block_gas_limit.saturating_div(BLOCK_STORAGE_LIMIT)
};
}
pub struct HashedAddressMapping;
impl AddressMapping<AccountId32> for HashedAddressMapping {
fn into_account_id(address: H160) -> AccountId32 {
let mut data = [0u8; 32];
data[0..20].copy_from_slice(&address[..]);
AccountId32::from(Into::<[u8; 32]>::into(data))
}
}
impl pallet_evm::Config for Test {
type FeeCalculator = ();
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressTruncated;
type WithdrawOrigin = EnsureAddressTruncated;
type AddressMapping = HashedAddressMapping;
type Currency = Balances;
type PrecompilesType = ();
type PrecompilesValue = ();
type Runner = pallet_evm::runner::stack::Runner<Self>;
type ChainId = ();
type BlockGasLimit = BlockGasLimit;
type TransactionGasLimit = ();
type OnChargeTransaction = ();
type FindAuthor = ();
type BlockHashMapping = SubstrateBlockHashMapping<Self>;
type OnCreate = ();
// `()` resolves to `0` for these `Get<u64>` ratios — that disables the PoV / storage
// growth checks in the EVM runner, which would otherwise reject our small-gas
// `erc20_transfer` test calls because the mock's `BlockGasLimit = u64::MAX`
// makes the computed ratios astronomical.
type GasLimitPovSizeRatio = ();
type GasLimitStorageGrowthRatio = ();
type Timestamp = Timestamp;
type WeightInfo = pallet_evm::weights::SubstrateWeight<Test>;
type AccountProvider = FrameSystemAccountProvider<Test>;
type CreateOriginFilter = ();
type CreateInnerOriginFilter = ();
}
parameter_types! {
pub Erc20XcmBridgeTransferGasLimit: u64 = 200_000;
pub Erc20MultilocationPrefix: xcm::latest::Location = xcm::latest::Location {
parents: 0,
interior: [xcm::latest::Junction::PalletInstance(42u8)].into(),
};
pub TeleportCheckingAccount: H160 = H160(*b"erc20-teleport-check");
/// Mock counterparty: stand-in for AssetHub at para 1001. Tests use this to verify
/// `IsTeleportableErc20` admits this exact location and rejects everything else.
pub TeleportTrustedLocation: xcm::latest::Location = xcm::latest::Location::new(
1,
[xcm::latest::Junction::Parachain(1001)],
);
}
/// Minimal `Location → H160` converter for the unit-test ext: matches the leaf
/// `AccountKey20 { key, .. }` and returns `H160(key)`. Pattern matches any number of
/// preceding junctions so both `(0, [AccountKey20])` and richer locations work.
pub struct H160FromAccountKey20Junction;
impl xcm_executor::traits::ConvertLocation<H160> for H160FromAccountKey20Junction {
fn convert_location(loc: &xcm::latest::Location) -> Option<H160> {
use xcm::latest::Junction;
match loc.interior().last() {
Some(Junction::AccountKey20 { key, .. }) => Some(H160::from(*key)),
_ => None,
}
}
}
impl crate::Config for Test {
type AccountIdConverter = H160FromAccountKey20Junction;
type Erc20MultilocationPrefix = Erc20MultilocationPrefix;
type Erc20TransferGasLimit = Erc20XcmBridgeTransferGasLimit;
type EvmRunner = pallet_evm::runner::stack::Runner<Self>;
type TeleportAdminOrigin = EnsureRoot<AccountId32>;
type TeleportCheckingAccount = TeleportCheckingAccount;
type TeleportTrustedLocation = TeleportTrustedLocation;
}