-
Notifications
You must be signed in to change notification settings - Fork 487
/
lib.rs
1304 lines (1139 loc) · 39 KB
/
lib.rs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of Frontier.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// 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.
//! # EVM Pallet
//!
//! The EVM pallet allows unmodified EVM code to be executed in a Substrate-based blockchain.
//! - [`evm::Config`]
//!
//! ## EVM Engine
//!
//! The EVM pallet uses [`SputnikVM`](https://github.com/rust-blockchain/evm) as the underlying EVM engine.
//! The engine is overhauled so that it's [`modular`](https://github.com/corepaper/evm).
//!
//! ## Execution Lifecycle
//!
//! There are a separate set of accounts managed by the EVM pallet. Substrate based accounts can call the EVM Pallet
//! to deposit or withdraw balance from the Substrate base-currency into a different balance managed and used by
//! the EVM pallet. Once a user has populated their balance, they can create and call smart contracts using this pallet.
//!
//! There's one-to-one mapping from Substrate accounts and EVM external accounts that is defined by a conversion function.
//!
//! ## EVM Pallet vs Ethereum Network
//!
//! The EVM pallet should be able to produce nearly identical results compared to the Ethereum mainnet,
//! including gas cost and balance changes.
//!
//! Observable differences include:
//!
//! - The available length of block hashes may not be 256 depending on the configuration of the System pallet
//! in the Substrate runtime.
//! - Difficulty and coinbase, which do not make sense in this pallet and is currently hard coded to zero.
//!
//! We currently do not aim to make unobservable behaviors, such as state root, to be the same. We also don't aim to follow
//! the exact same transaction / receipt format. However, given one Ethereum transaction and one Substrate account's
//! private key, one should be able to convert any Ethereum transaction into a transaction compatible with this pallet.
//!
//! The gas configurations are configurable. Right now, a pre-defined London hard fork configuration option is provided.
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(unused_crate_dependencies)]
#![allow(clippy::too_many_arguments)]
extern crate alloc;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
#[cfg(test)]
mod mock;
pub mod runner;
#[cfg(test)]
mod tests;
pub mod weights;
use alloc::{collections::btree_map::BTreeMap, vec::Vec};
use core::cmp::min;
pub use evm::{
Config as EvmConfig, Context, ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed,
};
use hash_db::Hasher;
use impl_trait_for_tuples::impl_for_tuples;
use scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
// Substrate
use frame_support::{
dispatch::{DispatchResultWithPostInfo, Pays, PostDispatchInfo},
storage::{child::KillStorageResult, KeyPrefixIterator},
traits::{
fungible::{Balanced, Credit, Debt},
tokens::{
currency::Currency,
fungible::Inspect,
imbalance::{Imbalance, OnUnbalanced, SignedImbalance},
ExistenceRequirement, Fortitude, Precision, Preservation, WithdrawReasons,
},
FindAuthor, Get, Time,
},
weights::Weight,
};
use frame_system::RawOrigin;
use sp_core::{H160, H256, U256};
use sp_runtime::{
traits::{BadOrigin, NumberFor, Saturating, UniqueSaturatedInto, Zero},
AccountId32, DispatchErrorWithPostInfo,
};
// Frontier
use fp_account::AccountId20;
use fp_evm::GenesisAccount;
pub use fp_evm::{
Account, AccountProvider, CallInfo, CreateInfo, ExecutionInfoV2 as ExecutionInfo,
FeeCalculator, IsPrecompileResult, LinearCostPrecompile, Log, Precompile, PrecompileFailure,
PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet,
TransactionValidationError, Vicinity,
};
pub use self::{
pallet::*,
runner::{Runner, RunnerError},
weights::WeightInfo,
};
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);
#[pallet::config(with_default)]
pub trait Config: frame_system::Config {
/// Account info provider.
#[pallet::no_default]
type AccountProvider: AccountProvider;
/// Calculator for current gas price.
type FeeCalculator: FeeCalculator;
/// Maps Ethereum gas to Substrate weight.
type GasWeightMapping: GasWeightMapping;
/// Weight corresponding to a gas unit.
type WeightPerGas: Get<Weight>;
/// Block number to block hash.
#[pallet::no_default]
type BlockHashMapping: BlockHashMapping;
/// Allow the origin to call on behalf of given address.
#[pallet::no_default_bounds]
type CallOrigin: EnsureAddressOrigin<Self::RuntimeOrigin>;
/// Allow the origin to withdraw on behalf of given address.
#[pallet::no_default_bounds]
type WithdrawOrigin: EnsureAddressOrigin<Self::RuntimeOrigin, Success = AccountIdOf<Self>>;
/// Mapping from address to account id.
#[pallet::no_default_bounds]
type AddressMapping: AddressMapping<AccountIdOf<Self>>;
/// Currency type for withdraw and balance storage.
#[pallet::no_default]
type Currency: Currency<AccountIdOf<Self>> + Inspect<AccountIdOf<Self>>;
/// The overarching event type.
#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Precompiles associated with this EVM engine.
type PrecompilesType: PrecompileSet;
type PrecompilesValue: Get<Self::PrecompilesType>;
/// Chain ID of EVM.
type ChainId: Get<u64>;
/// The block gas limit. Can be a simple constant, or an adjustment algorithm in another pallet.
type BlockGasLimit: Get<U256>;
/// EVM execution runner.
#[pallet::no_default]
type Runner: Runner<Self>;
/// To handle fee deduction for EVM transactions. An example is this pallet being used by `pallet_ethereum`
/// where the chain implementing `pallet_ethereum` should be able to configure what happens to the fees
/// Similar to `OnChargeTransaction` of `pallet_transaction_payment`
#[pallet::no_default_bounds]
type OnChargeTransaction: OnChargeEVMTransaction<Self>;
/// Called on create calls, used to record owner
#[pallet::no_default_bounds]
type OnCreate: OnCreate<Self>;
/// Find author for the current block.
type FindAuthor: FindAuthor<H160>;
/// Gas limit Pov size ratio.
type GasLimitPovSizeRatio: Get<u64>;
/// Define the quick clear limit of storage clearing when a contract suicides. Set to 0 to disable it.
type SuicideQuickClearLimit: Get<u32>;
/// Get the timestamp for the current block.
#[pallet::no_default]
type Timestamp: Time;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// EVM config used in the module.
fn config() -> &'static EvmConfig {
&SHANGHAI_CONFIG
}
}
pub mod config_preludes {
use super::*;
use core::str::FromStr;
use frame_support::{derive_impl, parameter_types, ConsensusEngineId};
use sp_runtime::traits::BlakeTwo256;
pub struct TestDefaultConfig;
#[derive_impl(
frame_system::config_preludes::SolochainDefaultConfig,
no_aggregated_types
)]
impl frame_system::DefaultConfig for TestDefaultConfig {}
const BLOCK_GAS_LIMIT: u64 = 150_000_000;
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
parameter_types! {
pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT);
pub const ChainId: u64 = 42;
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE);
pub WeightPerGas: Weight = Weight::from_parts(20_000, 0);
pub SuicideQuickClearLimit: u32 = 0;
}
#[register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type CallOrigin = EnsureAddressRoot<Self::AccountId>;
type WithdrawOrigin = EnsureAddressNever<Self::AccountId>;
type AddressMapping = HashedAddressMapping<BlakeTwo256>;
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
#[inject_runtime_type]
type RuntimeEvent = ();
type PrecompilesType = ();
type PrecompilesValue = ();
type ChainId = ChainId;
type BlockGasLimit = BlockGasLimit;
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type WeightInfo = ();
}
impl FixedGasWeightMappingAssociatedTypes for TestDefaultConfig {
type WeightPerGas = <Self as DefaultConfig>::WeightPerGas;
type BlockWeights = <Self as frame_system::DefaultConfig>::BlockWeights;
type GasLimitPovSizeRatio = <Self as DefaultConfig>::GasLimitPovSizeRatio;
}
pub struct FixedGasPrice;
impl FeeCalculator for FixedGasPrice {
fn min_gas_price() -> (U256, Weight) {
(1.into(), Weight::zero())
}
}
pub struct FindAuthorTruncated;
impl FindAuthor<H160> for FindAuthorTruncated {
fn find_author<'a, I>(_digests: I) -> Option<H160>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
Some(H160::from_str("1234500000000000000000000000000000000000").unwrap())
}
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Withdraw balance from EVM into currency/balances pallet.
#[pallet::call_index(0)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::withdraw())]
pub fn withdraw(
origin: OriginFor<T>,
address: H160,
value: BalanceOf<T>,
) -> DispatchResult {
let destination = T::WithdrawOrigin::ensure_address_origin(&address, origin)?;
let address_account_id = T::AddressMapping::into_account_id(address);
T::Currency::transfer(
&address_account_id,
&destination,
value,
ExistenceRequirement::AllowDeath,
)?;
Ok(())
}
/// Issue an EVM call operation. This is similar to a message call transaction in Ethereum.
#[pallet::call_index(1)]
#[pallet::weight({
let without_base_extrinsic_weight = true;
T::GasWeightMapping::gas_to_weight(*gas_limit, without_base_extrinsic_weight)
})]
pub fn call(
origin: OriginFor<T>,
source: H160,
target: H160,
input: Vec<u8>,
value: U256,
gas_limit: u64,
max_fee_per_gas: U256,
max_priority_fee_per_gas: Option<U256>,
nonce: Option<U256>,
access_list: Vec<(H160, Vec<H256>)>,
) -> DispatchResultWithPostInfo {
T::CallOrigin::ensure_address_origin(&source, origin)?;
let is_transactional = true;
let validate = true;
let info = match T::Runner::call(
source,
target,
input,
value,
gas_limit,
Some(max_fee_per_gas),
max_priority_fee_per_gas,
nonce,
access_list,
is_transactional,
validate,
None,
None,
T::config(),
) {
Ok(info) => info,
Err(e) => {
return Err(DispatchErrorWithPostInfo {
post_info: PostDispatchInfo {
actual_weight: Some(e.weight),
pays_fee: Pays::Yes,
},
error: e.error.into(),
})
}
};
match info.exit_reason {
ExitReason::Succeed(_) => {
Pallet::<T>::deposit_event(Event::<T>::Executed { address: target });
}
_ => {
Pallet::<T>::deposit_event(Event::<T>::ExecutedFailed { address: target });
}
};
Ok(PostDispatchInfo {
actual_weight: {
let mut gas_to_weight = T::GasWeightMapping::gas_to_weight(
info.used_gas.standard.unique_saturated_into(),
true,
);
if let Some(weight_info) = info.weight_info {
if let Some(proof_size_usage) = weight_info.proof_size_usage {
*gas_to_weight.proof_size_mut() = proof_size_usage;
}
}
Some(gas_to_weight)
},
pays_fee: Pays::No,
})
}
/// Issue an EVM create operation. This is similar to a contract creation transaction in
/// Ethereum.
#[pallet::call_index(2)]
#[pallet::weight({
let without_base_extrinsic_weight = true;
T::GasWeightMapping::gas_to_weight(*gas_limit, without_base_extrinsic_weight)
})]
pub fn create(
origin: OriginFor<T>,
source: H160,
init: Vec<u8>,
value: U256,
gas_limit: u64,
max_fee_per_gas: U256,
max_priority_fee_per_gas: Option<U256>,
nonce: Option<U256>,
access_list: Vec<(H160, Vec<H256>)>,
) -> DispatchResultWithPostInfo {
T::CallOrigin::ensure_address_origin(&source, origin)?;
let is_transactional = true;
let validate = true;
let info = match T::Runner::create(
source,
init,
value,
gas_limit,
Some(max_fee_per_gas),
max_priority_fee_per_gas,
nonce,
access_list,
is_transactional,
validate,
None,
None,
T::config(),
) {
Ok(info) => info,
Err(e) => {
return Err(DispatchErrorWithPostInfo {
post_info: PostDispatchInfo {
actual_weight: Some(e.weight),
pays_fee: Pays::Yes,
},
error: e.error.into(),
})
}
};
match info {
CreateInfo {
exit_reason: ExitReason::Succeed(_),
value: create_address,
..
} => {
Pallet::<T>::deposit_event(Event::<T>::Created {
address: create_address,
});
}
CreateInfo {
exit_reason: _,
value: create_address,
..
} => {
Pallet::<T>::deposit_event(Event::<T>::CreatedFailed {
address: create_address,
});
}
}
Ok(PostDispatchInfo {
actual_weight: {
let mut gas_to_weight = T::GasWeightMapping::gas_to_weight(
info.used_gas.standard.unique_saturated_into(),
true,
);
if let Some(weight_info) = info.weight_info {
if let Some(proof_size_usage) = weight_info.proof_size_usage {
*gas_to_weight.proof_size_mut() = proof_size_usage;
}
}
Some(gas_to_weight)
},
pays_fee: Pays::No,
})
}
/// Issue an EVM create2 operation.
#[pallet::call_index(3)]
#[pallet::weight({
let without_base_extrinsic_weight = true;
T::GasWeightMapping::gas_to_weight(*gas_limit, without_base_extrinsic_weight)
})]
pub fn create2(
origin: OriginFor<T>,
source: H160,
init: Vec<u8>,
salt: H256,
value: U256,
gas_limit: u64,
max_fee_per_gas: U256,
max_priority_fee_per_gas: Option<U256>,
nonce: Option<U256>,
access_list: Vec<(H160, Vec<H256>)>,
) -> DispatchResultWithPostInfo {
T::CallOrigin::ensure_address_origin(&source, origin)?;
let is_transactional = true;
let validate = true;
let info = match T::Runner::create2(
source,
init,
salt,
value,
gas_limit,
Some(max_fee_per_gas),
max_priority_fee_per_gas,
nonce,
access_list,
is_transactional,
validate,
None,
None,
T::config(),
) {
Ok(info) => info,
Err(e) => {
return Err(DispatchErrorWithPostInfo {
post_info: PostDispatchInfo {
actual_weight: Some(e.weight),
pays_fee: Pays::Yes,
},
error: e.error.into(),
})
}
};
match info {
CreateInfo {
exit_reason: ExitReason::Succeed(_),
value: create_address,
..
} => {
Pallet::<T>::deposit_event(Event::<T>::Created {
address: create_address,
});
}
CreateInfo {
exit_reason: _,
value: create_address,
..
} => {
Pallet::<T>::deposit_event(Event::<T>::CreatedFailed {
address: create_address,
});
}
}
Ok(PostDispatchInfo {
actual_weight: {
let mut gas_to_weight = T::GasWeightMapping::gas_to_weight(
info.used_gas.standard.unique_saturated_into(),
true,
);
if let Some(weight_info) = info.weight_info {
if let Some(proof_size_usage) = weight_info.proof_size_usage {
*gas_to_weight.proof_size_mut() = proof_size_usage;
}
}
Some(gas_to_weight)
},
pays_fee: Pays::No,
})
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Ethereum events from contracts.
Log { log: Log },
/// A contract has been created at given address.
Created { address: H160 },
/// A contract was attempted to be created, but the execution failed.
CreatedFailed { address: H160 },
/// A contract has been executed successfully with states applied.
Executed { address: H160 },
/// A contract has been executed with errors. States are reverted with only gas fees applied.
ExecutedFailed { address: H160 },
}
#[pallet::error]
pub enum Error<T> {
/// Not enough balance to perform action
BalanceLow,
/// Calculating total fee overflowed
FeeOverflow,
/// Calculating total payment overflowed
PaymentOverflow,
/// Withdraw fee failed
WithdrawFailed,
/// Gas price is too low.
GasPriceTooLow,
/// Nonce is invalid
InvalidNonce,
/// Gas limit is too low.
GasLimitTooLow,
/// Gas limit is too high.
GasLimitTooHigh,
/// The chain id is invalid.
InvalidChainId,
/// the signature is invalid.
InvalidSignature,
/// EVM reentrancy
Reentrancy,
/// EIP-3607,
TransactionMustComeFromEOA,
/// Undefined error.
Undefined,
}
impl<T> From<TransactionValidationError> for Error<T> {
fn from(validation_error: TransactionValidationError) -> Self {
match validation_error {
TransactionValidationError::GasLimitTooLow => Error::<T>::GasLimitTooLow,
TransactionValidationError::GasLimitTooHigh => Error::<T>::GasLimitTooHigh,
TransactionValidationError::BalanceTooLow => Error::<T>::BalanceLow,
TransactionValidationError::TxNonceTooLow => Error::<T>::InvalidNonce,
TransactionValidationError::TxNonceTooHigh => Error::<T>::InvalidNonce,
TransactionValidationError::GasPriceTooLow => Error::<T>::GasPriceTooLow,
TransactionValidationError::PriorityFeeTooHigh => Error::<T>::GasPriceTooLow,
TransactionValidationError::InvalidFeeInput => Error::<T>::GasPriceTooLow,
TransactionValidationError::InvalidChainId => Error::<T>::InvalidChainId,
TransactionValidationError::InvalidSignature => Error::<T>::InvalidSignature,
TransactionValidationError::UnknownError => Error::<T>::Undefined,
}
}
}
#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T> {
pub accounts: BTreeMap<H160, GenesisAccount>,
#[serde(skip)]
pub _marker: PhantomData<T>,
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T>
where
U256: UniqueSaturatedInto<BalanceOf<T>>,
{
fn build(&self) {
const MAX_ACCOUNT_NONCE: usize = 100;
for (address, account) in &self.accounts {
let account_id = T::AddressMapping::into_account_id(*address);
// ASSUME: in one single EVM transaction, the nonce will not increase more than
// `u128::max_value()`.
for _ in 0..min(
MAX_ACCOUNT_NONCE,
UniqueSaturatedInto::<usize>::unique_saturated_into(account.nonce),
) {
T::AccountProvider::inc_account_nonce(&account_id);
}
let _ = T::Currency::deposit_creating(
&account_id,
account.balance.unique_saturated_into(),
);
Pallet::<T>::create_account(*address, account.code.clone());
for (index, value) in &account.storage {
<AccountStorages<T>>::insert(address, index, value);
}
}
}
}
#[pallet::storage]
pub type AccountCodes<T: Config> = StorageMap<_, Blake2_128Concat, H160, Vec<u8>, ValueQuery>;
#[pallet::storage]
pub type AccountCodesMetadata<T: Config> =
StorageMap<_, Blake2_128Concat, H160, CodeMetadata, OptionQuery>;
#[pallet::storage]
pub type AccountStorages<T: Config> =
StorageDoubleMap<_, Blake2_128Concat, H160, Blake2_128Concat, H256, H256, ValueQuery>;
#[pallet::storage]
pub type Suicided<T: Config> = StorageMap<_, Blake2_128Concat, H160, (), OptionQuery>;
}
/// Utility alias for easy access to the [`AccountProvider::AccountId`] type from a given config.
pub type AccountIdOf<T> = <<T as Config>::AccountProvider as AccountProvider>::AccountId;
/// Type alias for currency balance.
pub type BalanceOf<T> = <<T as Config>::Currency as Currency<AccountIdOf<T>>>::Balance;
/// Type alias for negative imbalance during fees
type NegativeImbalanceOf<C, T> = <C as Currency<AccountIdOf<T>>>::NegativeImbalance;
#[derive(
Debug,
Clone,
Copy,
Eq,
PartialEq,
Encode,
Decode,
TypeInfo,
MaxEncodedLen
)]
pub struct CodeMetadata {
pub size: u64,
pub hash: H256,
}
impl CodeMetadata {
fn from_code(code: &[u8]) -> Self {
let size = code.len() as u64;
let hash = H256::from(sp_io::hashing::keccak_256(code));
Self { size, hash }
}
}
pub trait EnsureAddressOrigin<OuterOrigin> {
/// Success return type.
type Success;
/// Perform the origin check.
fn ensure_address_origin(
address: &H160,
origin: OuterOrigin,
) -> Result<Self::Success, BadOrigin> {
Self::try_address_origin(address, origin).map_err(|_| BadOrigin)
}
/// Try with origin.
fn try_address_origin(
address: &H160,
origin: OuterOrigin,
) -> Result<Self::Success, OuterOrigin>;
}
/// Ensure that the EVM address is the same as the Substrate address. This only works if the account
/// ID is `H160`.
pub struct EnsureAddressSame;
impl<OuterOrigin> EnsureAddressOrigin<OuterOrigin> for EnsureAddressSame
where
OuterOrigin: Into<Result<RawOrigin<H160>, OuterOrigin>> + From<RawOrigin<H160>>,
{
type Success = H160;
fn try_address_origin(address: &H160, origin: OuterOrigin) -> Result<H160, OuterOrigin> {
origin.into().and_then(|o| match o {
RawOrigin::Signed(who) if &who == address => Ok(who),
r => Err(OuterOrigin::from(r)),
})
}
}
/// Ensure that the origin is root.
pub struct EnsureAddressRoot<AccountId>(core::marker::PhantomData<AccountId>);
impl<OuterOrigin, AccountId> EnsureAddressOrigin<OuterOrigin> for EnsureAddressRoot<AccountId>
where
OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>> + From<RawOrigin<AccountId>>,
{
type Success = ();
fn try_address_origin(_address: &H160, origin: OuterOrigin) -> Result<(), OuterOrigin> {
origin.into().and_then(|o| match o {
RawOrigin::Root => Ok(()),
r => Err(OuterOrigin::from(r)),
})
}
}
/// Ensure that the origin never happens.
pub struct EnsureAddressNever<AccountId>(core::marker::PhantomData<AccountId>);
impl<OuterOrigin, AccountId> EnsureAddressOrigin<OuterOrigin> for EnsureAddressNever<AccountId> {
type Success = AccountId;
fn try_address_origin(_address: &H160, origin: OuterOrigin) -> Result<AccountId, OuterOrigin> {
Err(origin)
}
}
/// Ensure that the address is truncated hash of the origin. Only works if the account id is
/// `AccountId32`.
pub struct EnsureAddressTruncated;
impl<OuterOrigin> EnsureAddressOrigin<OuterOrigin> for EnsureAddressTruncated
where
OuterOrigin: Into<Result<RawOrigin<AccountId32>, OuterOrigin>> + From<RawOrigin<AccountId32>>,
{
type Success = AccountId32;
fn try_address_origin(address: &H160, origin: OuterOrigin) -> Result<AccountId32, OuterOrigin> {
origin.into().and_then(|o| match o {
RawOrigin::Signed(who) if AsRef::<[u8; 32]>::as_ref(&who)[0..20] == address[0..20] => {
Ok(who)
}
r => Err(OuterOrigin::from(r)),
})
}
}
/// Ensure that the address is AccountId20.
pub struct EnsureAccountId20;
impl<OuterOrigin> EnsureAddressOrigin<OuterOrigin> for EnsureAccountId20
where
OuterOrigin: Into<Result<RawOrigin<AccountId20>, OuterOrigin>> + From<RawOrigin<AccountId20>>,
{
type Success = AccountId20;
fn try_address_origin(address: &H160, origin: OuterOrigin) -> Result<AccountId20, OuterOrigin> {
let acc: AccountId20 = AccountId20::from(*address);
origin.into().and_then(|o| match o {
RawOrigin::Signed(who) if who == acc => Ok(who),
r => Err(OuterOrigin::from(r)),
})
}
}
/// Trait to be implemented for evm address mapping.
pub trait AddressMapping<A> {
fn into_account_id(address: H160) -> A;
}
/// Identity address mapping.
pub struct IdentityAddressMapping;
impl<T: From<H160>> AddressMapping<T> for IdentityAddressMapping {
fn into_account_id(address: H160) -> T {
address.into()
}
}
/// Hashed address mapping.
pub struct HashedAddressMapping<H>(core::marker::PhantomData<H>);
impl<H: Hasher<Out = H256>> AddressMapping<AccountId32> for HashedAddressMapping<H> {
fn into_account_id(address: H160) -> AccountId32 {
let mut data = [0u8; 24];
data[0..4].copy_from_slice(b"evm:");
data[4..24].copy_from_slice(&address[..]);
let hash = H::hash(&data);
AccountId32::from(Into::<[u8; 32]>::into(hash))
}
}
/// A trait for getting a block hash by number.
pub trait BlockHashMapping {
fn block_hash(number: u32) -> H256;
}
/// Returns the Substrate block hash by number.
pub struct SubstrateBlockHashMapping<T>(core::marker::PhantomData<T>);
impl<T: Config> BlockHashMapping for SubstrateBlockHashMapping<T> {
fn block_hash(number: u32) -> H256 {
let number = <NumberFor<T::Block>>::from(number);
H256::from_slice(frame_system::Pallet::<T>::block_hash(number).as_ref())
}
}
/// A mapping function that converts Ethereum gas to Substrate weight
pub trait GasWeightMapping {
fn gas_to_weight(gas: u64, without_base_weight: bool) -> Weight;
fn weight_to_gas(weight: Weight) -> u64;
}
pub trait FixedGasWeightMappingAssociatedTypes {
type WeightPerGas: Get<Weight>;
type BlockWeights: Get<frame_system::limits::BlockWeights>;
type GasLimitPovSizeRatio: Get<u64>;
}
impl<T: Config> FixedGasWeightMappingAssociatedTypes for T {
type WeightPerGas = T::WeightPerGas;
type BlockWeights = T::BlockWeights;
type GasLimitPovSizeRatio = T::GasLimitPovSizeRatio;
}
pub struct FixedGasWeightMapping<T>(core::marker::PhantomData<T>);
impl<T> GasWeightMapping for FixedGasWeightMapping<T>
where
T: FixedGasWeightMappingAssociatedTypes,
{
fn gas_to_weight(gas: u64, without_base_weight: bool) -> Weight {
let mut weight = T::WeightPerGas::get().saturating_mul(gas);
if without_base_weight {
weight = weight.saturating_sub(
T::BlockWeights::get()
.get(frame_support::dispatch::DispatchClass::Normal)
.base_extrinsic,
);
}
// Apply a gas to proof size ratio based on BlockGasLimit
let ratio = T::GasLimitPovSizeRatio::get();
if ratio > 0 {
let proof_size = gas.saturating_div(ratio);
*weight.proof_size_mut() = proof_size;
}
weight
}
fn weight_to_gas(weight: Weight) -> u64 {
weight.div(T::WeightPerGas::get().ref_time()).ref_time()
}
}
static SHANGHAI_CONFIG: EvmConfig = EvmConfig::shanghai();
impl<T: Config> Pallet<T> {
/// Check whether an account is empty.
pub fn is_account_empty(address: &H160) -> bool {
let (account, _) = Self::account_basic(address);
let code_len = <AccountCodes<T>>::decode_len(address).unwrap_or(0);
account.nonce == U256::zero() && account.balance == U256::zero() && code_len == 0
}
/// Check whether an account is a suicided contract
pub fn is_account_suicided(address: &H160) -> bool {
<Suicided<T>>::contains_key(address)
}
pub fn iter_account_storages(address: &H160) -> KeyPrefixIterator<H256> {
<AccountStorages<T>>::iter_key_prefix(address)
}
/// Remove an account if its empty.
pub fn remove_account_if_empty(address: &H160) {
if Self::is_account_empty(address) {
Self::remove_account(address);
}
}
/// Remove an account.
pub fn remove_account(address: &H160) {
if <AccountCodes<T>>::contains_key(address) {
// Remember to call `dec_sufficients` when clearing Suicided.
<Suicided<T>>::insert(address, ());
// In theory, we can always have pre-EIP161 contracts, so we
// make sure the account nonce is at least one.
let account_id = T::AddressMapping::into_account_id(*address);
T::AccountProvider::inc_account_nonce(&account_id);
}
<AccountCodes<T>>::remove(address);
<AccountCodesMetadata<T>>::remove(address);
if T::SuicideQuickClearLimit::get() > 0 {
#[allow(deprecated)]
let res = <AccountStorages<T>>::remove_prefix(address, Some(T::SuicideQuickClearLimit::get()));
match res {
KillStorageResult::AllRemoved(_) => {
<Suicided<T>>::remove(address);
let account_id = T::AddressMapping::into_account_id(*address);
T::AccountProvider::remove_account(&account_id);
}
KillStorageResult::SomeRemaining(_) => (),
}
}
}
/// Create an account.
pub fn create_account(address: H160, code: Vec<u8>) {
if <Suicided<T>>::contains_key(address) {
// This branch should never trigger, because when Suicided
// contains an address, then its nonce will be at least one,
// which causes CreateCollision error in EVM, but we add it
// here for safeguard.
return;
}
if code.is_empty() {
return;
}
if !<AccountCodes<T>>::contains_key(address) {
let account_id = T::AddressMapping::into_account_id(address);
T::AccountProvider::create_account(&account_id);
}
// Update metadata.
let meta = CodeMetadata::from_code(&code);
<AccountCodesMetadata<T>>::insert(address, meta);
<AccountCodes<T>>::insert(address, code);
}
/// Get the account metadata (hash and size) from storage if it exists,
/// or compute it from code and store it if it doesn't exist.
pub fn account_code_metadata(address: H160) -> CodeMetadata {
if let Some(meta) = <AccountCodesMetadata<T>>::get(address) {
return meta;
}
let code = <AccountCodes<T>>::get(address);
// If code is empty we return precomputed hash for empty code.
// We don't store it as this address could get code deployed in the future.
if code.is_empty() {
const EMPTY_CODE_HASH: [u8; 32] = hex_literal::hex!(
"c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
);
return CodeMetadata {