Skip to content

Commit e3e427f

Browse files
committed
Limit number of iterations in genesis nonce building (#753)
1 parent 6d2101d commit e3e427f

File tree

5 files changed

+48
-25
lines changed

5 files changed

+48
-25
lines changed

frame/ethereum/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ impl<T: Config> Pallet<T> {
814814
target,
815815
input,
816816
value,
817-
gas_limit.low_u64(),
817+
gas_limit.unique_saturated_into(),
818818
max_fee_per_gas,
819819
max_priority_fee_per_gas,
820820
nonce,
@@ -841,7 +841,7 @@ impl<T: Config> Pallet<T> {
841841
from,
842842
input,
843843
value,
844-
gas_limit.low_u64(),
844+
gas_limit.unique_saturated_into(),
845845
max_fee_per_gas,
846846
max_priority_fee_per_gas,
847847
nonce,

frame/evm/src/lib.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use sp_runtime::{
7777
traits::{BadOrigin, Saturating, UniqueSaturatedInto, Zero},
7878
AccountId32, DispatchErrorWithPostInfo,
7979
};
80-
use sp_std::vec::Vec;
80+
use sp_std::{cmp::min, vec::Vec};
8181

8282
pub use evm::{
8383
Config as EvmConfig, Context, ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed,
@@ -410,21 +410,26 @@ pub mod pallet {
410410
}
411411

412412
#[pallet::genesis_build]
413-
impl<T: Config> GenesisBuild<T> for GenesisConfig {
413+
impl<T: Config> GenesisBuild<T> for GenesisConfig
414+
where
415+
U256: UniqueSaturatedInto<BalanceOf<T>>,
416+
{
414417
fn build(&self) {
418+
const MAX_ACCOUNT_NONCE: usize = 100;
419+
415420
for (address, account) in &self.accounts {
416421
let account_id = T::AddressMapping::into_account_id(*address);
417422

418423
// ASSUME: in one single EVM transaction, the nonce will not increase more than
419424
// `u128::max_value()`.
420-
for _ in 0..account.nonce.low_u128() {
425+
for _ in 0..min(
426+
MAX_ACCOUNT_NONCE,
427+
UniqueSaturatedInto::<usize>::unique_saturated_into(account.nonce),
428+
) {
421429
frame_system::Pallet::<T>::inc_account_nonce(&account_id);
422430
}
423431

424-
T::Currency::deposit_creating(
425-
&account_id,
426-
account.balance.low_u128().unique_saturated_into(),
427-
);
432+
T::Currency::deposit_creating(&account_id, account.balance.unique_saturated_into());
428433

429434
Pallet::<T>::create_account(*address, account.code.clone());
430435

@@ -707,6 +712,7 @@ where
707712
Opposite = C::PositiveImbalance,
708713
>,
709714
OU: OnUnbalanced<NegativeImbalanceOf<C, T>>,
715+
U256: UniqueSaturatedInto<<C as Currency<<T as frame_system::Config>::AccountId>>::Balance>,
710716
{
711717
// Kept type as Option to satisfy bound of Default
712718
type LiquidityInfo = Option<NegativeImbalanceOf<C, T>>;
@@ -718,7 +724,7 @@ where
718724
let account_id = T::AddressMapping::into_account_id(*who);
719725
let imbalance = C::withdraw(
720726
&account_id,
721-
fee.low_u128().unique_saturated_into(),
727+
fee.unique_saturated_into(),
722728
WithdrawReasons::FEE,
723729
ExistenceRequirement::AllowDeath,
724730
)
@@ -738,7 +744,7 @@ where
738744
// Calculate how much refund we should return
739745
let refund_amount = paid
740746
.peek()
741-
.saturating_sub(corrected_fee.low_u128().unique_saturated_into());
747+
.saturating_sub(corrected_fee.unique_saturated_into());
742748
// refund to the account that paid the fees. If this fails, the
743749
// account might have dropped below the existential balance. In
744750
// that case we don't refund anything.
@@ -769,7 +775,7 @@ where
769775
.same()
770776
.unwrap_or_else(|_| C::NegativeImbalance::zero());
771777

772-
let (base_fee, tip) = adjusted_paid.split(base_fee.low_u128().unique_saturated_into());
778+
let (base_fee, tip) = adjusted_paid.split(base_fee.unique_saturated_into());
773779
// Handle base fee. Can be either burned, rationed, etc ...
774780
OU::on_unbalanced(base_fee);
775781
return Some(tip);
@@ -793,7 +799,10 @@ impl<T> OnChargeEVMTransaction<T> for ()
793799
<T::Currency as Currency<<T as frame_system::Config>::AccountId>>::PositiveImbalance:
794800
Imbalance<<T::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance, Opposite = <T::Currency as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance>,
795801
<T::Currency as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance:
796-
Imbalance<<T::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance, Opposite = <T::Currency as Currency<<T as frame_system::Config>::AccountId>>::PositiveImbalance>, {
802+
Imbalance<<T::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance, Opposite = <T::Currency as Currency<<T as frame_system::Config>::AccountId>>::PositiveImbalance>,
803+
U256: UniqueSaturatedInto<BalanceOf<T>>,
804+
805+
{
797806
// Kept type as Option to satisfy bound of Default
798807
type LiquidityInfo = Option<NegativeImbalanceOf<T::Currency, T>>;
799808

frame/evm/src/runner/stack.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
//! EVM stack-based runner.
1919
2020
use crate::{
21-
runner::Runner as RunnerT, AccountCodes, AccountStorages, AddressMapping, BlockHashMapping,
22-
Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, Pallet, RunnerError,
21+
runner::Runner as RunnerT, AccountCodes, AccountStorages, AddressMapping, BalanceOf,
22+
BlockHashMapping, Config, Error, Event, FeeCalculator, OnChargeEVMTransaction, Pallet,
23+
RunnerError,
2324
};
2425
use evm::{
2526
backend::Backend as BackendT,
@@ -41,8 +42,11 @@ pub struct Runner<T: Config> {
4142
_marker: PhantomData<T>,
4243
}
4344

44-
impl<T: Config> Runner<T> {
45-
/// Execute an EVM operation.
45+
impl<T: Config> Runner<T>
46+
where
47+
BalanceOf<T>: TryFrom<U256> + Into<U256>,
48+
{
49+
/// Execute an already validated EVM operation.
4650
pub fn execute<'config, 'precompiles, F, R>(
4751
source: H160,
4852
value: U256,
@@ -245,7 +249,10 @@ impl<T: Config> Runner<T> {
245249
}
246250
}
247251

248-
impl<T: Config> RunnerT<T> for Runner<T> {
252+
impl<T: Config> RunnerT<T> for Runner<T>
253+
where
254+
BalanceOf<T>: TryFrom<U256> + Into<U256>,
255+
{
249256
type Error = Error<T>;
250257

251258
fn call(
@@ -542,6 +549,8 @@ impl<'vicinity, 'config, T: Config> BackendT for SubstrateStackState<'vicinity,
542549

543550
impl<'vicinity, 'config, T: Config> StackStateT<'config>
544551
for SubstrateStackState<'vicinity, 'config, T>
552+
where
553+
BalanceOf<T>: TryFrom<U256> + Into<U256>,
545554
{
546555
fn metadata(&self) -> &StackSubstateMetadata<'config> {
547556
self.substate.metadata()
@@ -630,7 +639,10 @@ impl<'vicinity, 'config, T: Config> StackStateT<'config>
630639
T::Currency::transfer(
631640
&source,
632641
&target,
633-
transfer.value.low_u128().unique_saturated_into(),
642+
transfer
643+
.value
644+
.try_into()
645+
.map_err(|_| ExitError::OutOfFund)?,
634646
ExistenceRequirement::AllowDeath,
635647
)
636648
.map_err(|_| ExitError::OutOfFund)

frame/evm/src/tests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,10 @@ fn issuance_after_tip() {
268268
result.expect("EVM can be called");
269269
let after_tip = <Test as Config>::Currency::total_issuance();
270270
// Only base fee is burned
271-
let (base_fee, _) = <Test as Config>::FeeCalculator::min_gas_price();
272-
assert_eq!(after_tip, (before_tip - (base_fee.low_u64() * 21_000)));
271+
let base_fee: u64 = <Test as Config>::FeeCalculator::min_gas_price()
272+
.0
273+
.unique_saturated_into();
274+
assert_eq!(after_tip, (before_tip - (base_fee * 21_000)));
273275
});
274276
}
275277

@@ -355,7 +357,7 @@ fn refunds_and_priority_should_work() {
355357
assert_eq!(after_call, before_call - total_cost);
356358

357359
let after_tip = EVM::account_basic(&author).0.balance;
358-
assert_eq!(after_tip, (before_tip + actual_tip.low_u128()));
360+
assert_eq!(after_tip, (before_tip + actual_tip));
359361
});
360362
}
361363

template/runtime/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use sp_runtime::{
2424
create_runtime_str, generic, impl_opaque_keys,
2525
traits::{
2626
AccountIdLookup, BlakeTwo256, Block as BlockT, DispatchInfoOf, Dispatchable,
27-
IdentifyAccount, NumberFor, PostDispatchInfoOf, Verify,
27+
IdentifyAccount, NumberFor, PostDispatchInfoOf, UniqueSaturatedInto, Verify,
2828
},
2929
transaction_validity::{TransactionSource, TransactionValidity, TransactionValidityError},
3030
ApplyExtrinsicResult, MultiSignature,
@@ -663,7 +663,7 @@ impl_runtime_apis! {
663663
to,
664664
data,
665665
value,
666-
gas_limit.low_u64(),
666+
gas_limit.unique_saturated_into(),
667667
max_fee_per_gas,
668668
max_priority_fee_per_gas,
669669
nonce,
@@ -697,7 +697,7 @@ impl_runtime_apis! {
697697
from,
698698
data,
699699
value,
700-
gas_limit.low_u64(),
700+
gas_limit.unique_saturated_into(),
701701
max_fee_per_gas,
702702
max_priority_fee_per_gas,
703703
nonce,

0 commit comments

Comments
 (0)