forked from polkadot-evm/frontier
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50c50fc
commit 893ce1e
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// This file is part of Frontier. | ||
// | ||
// Copyright (c) 2020-2022 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Test mock for unit tests and benchmarking | ||
use crate::{StorageCleanerPrecompile, StorageCleanerPrecompileCall}; | ||
use frame_support::{parameter_types, weights::Weight}; | ||
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot, IdentityAddressMapping}; | ||
use precompile_utils::{testing::*, precompile_set::*}; | ||
use sp_core::{ConstU32, H256, U256}; | ||
use sp_runtime::{ | ||
traits::{BlakeTwo256, IdentityLookup}, | ||
BuildStorage, | ||
}; | ||
|
||
pub type AccountId = MockAccount; | ||
pub type Balance = u128; | ||
|
||
frame_support::construct_runtime! { | ||
pub enum Runtime { | ||
System: frame_system::{Pallet, Call, Storage, Config<T>, Event<T>}, | ||
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>}, | ||
Timestamp: pallet_timestamp::{Pallet, Call, Storage}, | ||
EVM: pallet_evm::{Pallet, Call, Storage, Config<T>, Event<T>}, | ||
} | ||
} | ||
|
||
parameter_types! { | ||
pub const BlockHashCount: u64 = 250; | ||
pub BlockWeights: frame_system::limits::BlockWeights = | ||
frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, 0)); | ||
} | ||
|
||
impl frame_system::Config for Runtime { | ||
type RuntimeEvent = RuntimeEvent; | ||
type BaseCallFilter = frame_support::traits::Everything; | ||
type BlockWeights = (); | ||
type BlockLength = (); | ||
type RuntimeOrigin = RuntimeOrigin; | ||
type RuntimeCall = RuntimeCall; | ||
type Nonce = u64; | ||
type Hash = H256; | ||
type Hashing = BlakeTwo256; | ||
type AccountId = AccountId; | ||
type Lookup = IdentityLookup<Self::AccountId>; | ||
type Block = frame_system::mocking::MockBlock<Self>; | ||
type BlockHashCount = BlockHashCount; | ||
type DbWeight = (); | ||
type Version = (); | ||
type PalletInfo = PalletInfo; | ||
type AccountData = pallet_balances::AccountData<Balance>; | ||
type OnNewAccount = (); | ||
type OnKilledAccount = (); | ||
type SystemWeightInfo = (); | ||
type SS58Prefix = (); | ||
type OnSetCode = (); | ||
type MaxConsumers = ConstU32<16>; | ||
} | ||
|
||
parameter_types! { | ||
pub const ExistentialDeposit: u64 = 0; | ||
} | ||
|
||
impl pallet_balances::Config for Runtime { | ||
type RuntimeEvent = RuntimeEvent; | ||
type WeightInfo = (); | ||
type Balance = Balance; | ||
type DustRemoval = (); | ||
type ExistentialDeposit = ExistentialDeposit; | ||
type AccountStore = System; | ||
type ReserveIdentifier = (); | ||
type RuntimeHoldReason = (); | ||
type FreezeIdentifier = (); | ||
type MaxLocks = (); | ||
type MaxReserves = (); | ||
type MaxHolds = (); | ||
type MaxFreezes = (); | ||
} | ||
|
||
parameter_types! { | ||
pub const MinimumPeriod: u64 = 1000; | ||
} | ||
impl pallet_timestamp::Config for Runtime { | ||
type Moment = u64; | ||
type OnTimestampSet = (); | ||
type MinimumPeriod = MinimumPeriod; | ||
type WeightInfo = (); | ||
} | ||
|
||
pub type Precompiles<R> = | ||
PrecompileSetBuilder<R, (PrecompileAt<AddressU64<1>, StorageCleanerPrecompile<R>>,)>; | ||
|
||
pub type PCall = StorageCleanerPrecompileCall<Runtime>; | ||
|
||
const BLOCK_GAS_LIMIT: u64 = 15_000_000; | ||
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024; | ||
|
||
parameter_types! { | ||
pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT); | ||
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE); | ||
pub WeightPerGas: Weight = Weight::from_parts(20_000, 0); | ||
pub PrecompilesValue: Precompiles<Runtime> = Precompiles::new(); | ||
} | ||
|
||
impl pallet_evm::Config for Runtime { | ||
type FeeCalculator = (); | ||
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>; | ||
type WeightPerGas = WeightPerGas; | ||
type CallOrigin = EnsureAddressRoot<Self::AccountId>; | ||
type WithdrawOrigin = EnsureAddressNever<Self::AccountId>; | ||
type AddressMapping = IdentityAddressMapping; | ||
type Currency = Balances; | ||
type RuntimeEvent = RuntimeEvent; | ||
type Runner = pallet_evm::runner::stack::Runner<Self>; | ||
type PrecompilesType = Precompiles<Runtime>; | ||
type PrecompilesValue = PrecompilesValue; | ||
type ChainId = (); | ||
type OnChargeTransaction = (); | ||
type BlockGasLimit = BlockGasLimit; | ||
type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>; | ||
type FindAuthor = (); | ||
type OnCreate = (); | ||
type GasLimitPovSizeRatio = GasLimitPovSizeRatio; | ||
type Timestamp = Timestamp; | ||
type WeightInfo = (); | ||
} | ||
|
||
/// Build test externalities, prepopulated with data for testing the precompile. | ||
pub(crate) struct ExtBuilder { | ||
balances: Vec<(AccountId, Balance)>, | ||
} | ||
|
||
impl Default for ExtBuilder { | ||
fn default() -> Self { | ||
Self { | ||
balances: vec![], | ||
} | ||
} | ||
} | ||
|
||
impl ExtBuilder { | ||
pub fn with_balances(mut self, balances: Vec<(AccountId, Balance)>) -> Self { | ||
self.balances = balances; | ||
self | ||
} | ||
|
||
pub fn build(self) -> sp_io::TestExternalities { | ||
let mut t = frame_system::GenesisConfig::<Runtime>::default() | ||
.build_storage() | ||
.unwrap(); | ||
|
||
pallet_balances::GenesisConfig::<Runtime> { | ||
balances: self.balances, | ||
} | ||
.assimilate_storage(&mut t) | ||
.unwrap(); | ||
|
||
let mut ext = sp_io::TestExternalities::new(t); | ||
ext.execute_with(|| { | ||
System::set_block_number(1); | ||
}); | ||
ext | ||
} | ||
} |