Skip to content

Commit 363a979

Browse files
evgeny-stmpolaczyk
andauthored
Enabled GasLimitStorageGrowthRatio (#997)
Co-authored-by: tmpolaczyk <[email protected]>
1 parent fefe9fe commit 363a979

10 files changed

Lines changed: 447 additions & 39 deletions

File tree

Cargo.lock

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chains/container-chains/runtime-templates/frontier/src/lib.rs

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,8 @@ parameter_types! {
867867
pub WeightPerGas: Weight = Weight::from_parts(WEIGHT_PER_GAS, 0);
868868
pub SuicideQuickClearLimit: u32 = 0;
869869
pub GasLimitPovSizeRatio: u32 = 16;
870-
pub GasLimitStorageGrowthRatio: u64 = 366;
870+
/// Hardcoding the value, since it is computed on block execution. Check calculations in the tests
871+
pub GasLimitStorageGrowthRatio: u64 = 1464;
871872
}
872873

873874
impl_on_charge_evm_transaction!();
@@ -897,7 +898,7 @@ impl pallet_evm::Config for Runtime {
897898
type OnCreate = ();
898899
type FindAuthor = FindAuthorAdapter;
899900
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
900-
type GasLimitStorageGrowthRatio = ();
901+
type GasLimitStorageGrowthRatio = GasLimitStorageGrowthRatio;
901902
type Timestamp = Timestamp;
902903
type WeightInfo = ();
903904
}
@@ -1548,14 +1549,22 @@ impl_runtime_apis! {
15481549
max_fee_per_gas: Option<U256>,
15491550
max_priority_fee_per_gas: Option<U256>,
15501551
nonce: Option<U256>,
1551-
_estimate: bool,
1552+
estimate: bool,
15521553
access_list: Option<Vec<(H160, Vec<H256>)>>,
15531554
) -> Result<pallet_evm::CallInfo, sp_runtime::DispatchError> {
1555+
let config = if estimate {
1556+
let mut config = <Runtime as pallet_evm::Config>::config().clone();
1557+
config.estimate = true;
1558+
Some(config)
1559+
} else {
1560+
None
1561+
};
15541562
let is_transactional = false;
15551563
let validate = true;
15561564

15571565
// Estimated encoded transaction size must be based on the heaviest transaction
15581566
// type (EIP1559Transaction) to be compatible with all transaction types.
1567+
// TODO: remove, since we will get rid of base_cost
15591568
let mut estimated_transaction_len = data.len() +
15601569
// pallet ethereum index: 1
15611570
// transact call index: 1
@@ -1571,13 +1580,15 @@ impl_runtime_apis! {
15711580
// 65 bytes signature
15721581
258;
15731582

1574-
if access_list.is_some() {
1575-
estimated_transaction_len += access_list.encoded_size();
1583+
if let Some(ref list) = access_list {
1584+
estimated_transaction_len += list.encoded_size();
15761585
}
1586+
15771587
let gas_limit = gas_limit.min(u64::MAX.into()).low_u64();
15781588
let without_base_extrinsic_weight = true;
1579-
let (weight_limit, proof_size_base_cost) =
1580-
match <Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
1589+
1590+
let (weight_limit, proof_size_base_cost) = match
1591+
<Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
15811592
gas_limit,
15821593
without_base_extrinsic_weight
15831594
) {
@@ -1601,7 +1612,7 @@ impl_runtime_apis! {
16011612
validate,
16021613
weight_limit,
16031614
proof_size_base_cost,
1604-
<Runtime as pallet_evm::Config>::config(),
1615+
config.as_ref().unwrap_or(<Runtime as pallet_evm::Config>::config()),
16051616
).map_err(|err| err.error.into())
16061617
}
16071618

@@ -1613,25 +1624,67 @@ impl_runtime_apis! {
16131624
max_fee_per_gas: Option<U256>,
16141625
max_priority_fee_per_gas: Option<U256>,
16151626
nonce: Option<U256>,
1616-
_estimate: bool,
1627+
estimate: bool,
16171628
access_list: Option<Vec<(H160, Vec<H256>)>>,
16181629
) -> Result<pallet_evm::CreateInfo, sp_runtime::DispatchError> {
1630+
let config = if estimate {
1631+
let mut config = <Runtime as pallet_evm::Config>::config().clone();
1632+
config.estimate = true;
1633+
Some(config)
1634+
} else {
1635+
None
1636+
};
16191637
let is_transactional = false;
16201638
let validate = true;
1639+
1640+
let mut estimated_transaction_len = data.len() +
1641+
// from: 20
1642+
// value: 32
1643+
// gas_limit: 32
1644+
// nonce: 32
1645+
// 1 byte transaction action variant
1646+
// chain id 8 bytes
1647+
// 65 bytes signature
1648+
190;
1649+
1650+
if max_fee_per_gas.is_some() {
1651+
estimated_transaction_len += 32;
1652+
}
1653+
if max_priority_fee_per_gas.is_some() {
1654+
estimated_transaction_len += 32;
1655+
}
1656+
if let Some(ref list) = access_list {
1657+
estimated_transaction_len += list.encoded_size();
1658+
}
1659+
1660+
let gas_limit = gas_limit.min(u64::MAX.into()).low_u64();
1661+
let without_base_extrinsic_weight = true;
1662+
1663+
let (weight_limit, proof_size_base_cost) = match
1664+
<Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
1665+
gas_limit,
1666+
without_base_extrinsic_weight
1667+
) {
1668+
weight_limit if weight_limit.proof_size() > 0 => {
1669+
(Some(weight_limit), Some(estimated_transaction_len as u64))
1670+
}
1671+
_ => (None, None),
1672+
};
1673+
16211674
<Runtime as pallet_evm::Config>::Runner::create(
16221675
from,
16231676
data,
16241677
value,
1625-
gas_limit.min(u64::MAX.into()).low_u64(),
1678+
gas_limit,
16261679
max_fee_per_gas,
16271680
max_priority_fee_per_gas,
16281681
nonce,
16291682
access_list.unwrap_or_default(),
16301683
is_transactional,
16311684
validate,
1632-
None,
1633-
None,
1634-
<Runtime as pallet_evm::Config>::config(),
1685+
weight_limit,
1686+
proof_size_base_cost,
1687+
config.as_ref().unwrap_or(<Runtime as pallet_evm::Config>::config()),
16351688
).map_err(|err| err.error.into())
16361689
}
16371690

@@ -1844,3 +1897,19 @@ cumulus_pallet_parachain_system::register_validate_block! {
18441897
CheckInherents = CheckInherents,
18451898
BlockExecutor = pallet_author_inherent::BlockExecutor::<Runtime, Executive>,
18461899
}
1900+
1901+
#[cfg(test)]
1902+
mod tests {
1903+
use super::*;
1904+
1905+
/// Block storage limit in bytes. Set to 40 KB.
1906+
const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;
1907+
1908+
#[test]
1909+
fn check_ratio_constant() {
1910+
assert_eq!(
1911+
BlockGasLimit::get().min(u64::MAX.into()).low_u64() / BLOCK_STORAGE_LIMIT,
1912+
GasLimitStorageGrowthRatio::get()
1913+
);
1914+
}
1915+
}

chains/container-chains/runtime-templates/frontier/src/precompiles.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use {
1919
xcm_config::{AssetId, ForeignAssetsInstance, XcmConfig},
2020
AccountId, Balances, ForeignAssetsCreator, Runtime,
2121
},
22-
frame_support::parameter_types,
22+
frame_support::{parameter_types, traits::ConstU64},
2323
pallet_evm_precompile_balances_erc20::{Erc20BalancesPrecompile, Erc20Metadata},
2424
pallet_evm_precompile_batch::BatchPrecompile,
2525
pallet_evm_precompile_call_permit::CallPermitPrecompile,
@@ -71,6 +71,10 @@ pub const FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 18];
7171
/// Const to identify ERC20_BALANCES_PRECOMPILE address
7272
pub const ERC20_BALANCES_PRECOMPILE: u64 = 2048;
7373

74+
/// System account size in bytes = Pallet_Name_Hash (16) + Storage_name_hash (16) +
75+
/// Blake2_128Concat (16) + AccountId (20) + AccountInfo (4 + 12 + AccountData (4* 16)) = 148
76+
pub const SYSTEM_ACCOUNT_SIZE: u64 = 148;
77+
7478
parameter_types! {
7579
pub ForeignAssetPrefix: &'static [u8] = FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX;
7680
}
@@ -99,7 +103,7 @@ type TemplatePrecompilesAt<R> = (
99103
// Template specific precompiles:
100104
PrecompileAt<
101105
AddressU64<ERC20_BALANCES_PRECOMPILE>,
102-
Erc20BalancesPrecompile<R, NativeErc20Metadata>,
106+
Erc20BalancesPrecompile<R, NativeErc20Metadata, ConstU64<SYSTEM_ACCOUNT_SIZE>>,
103107
(CallableByContract, CallableByPrecompile),
104108
>,
105109
PrecompileAt<AddressU64<2049>, BatchPrecompile<R>, SubcallWithMaxNesting<2>>,
@@ -110,8 +114,8 @@ type TemplatePrecompilesAt<R> = (
110114
>,
111115
PrecompileAt<
112116
AddressU64<2051>,
113-
XcmUtilsPrecompile<R, XcmConfig>,
114-
CallableByContract<AllExceptXcmExecute<R, XcmConfig>>,
117+
XcmUtilsPrecompile<R, XcmConfig, ConstU64<SYSTEM_ACCOUNT_SIZE>>,
118+
CallableByContract<AllExceptXcmExecute<R, XcmConfig, ConstU64<SYSTEM_ACCOUNT_SIZE>>>,
115119
>,
116120
PrecompileAt<
117121
AddressU64<2052>,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
pragma solidity >=0.8.3;
3+
4+
contract Fibonacci {
5+
function fib2(uint256 n) public pure returns (uint256 b) {
6+
if (n == 0) {
7+
return 0;
8+
}
9+
uint256 a = 1;
10+
b = 1;
11+
for (uint256 i = 2; i < n; i++) {
12+
uint256 c = a + b;
13+
a = b;
14+
b = c;
15+
}
16+
return b;
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
pragma solidity >=0.8.2 <0.9.0;
3+
4+
contract StorageLoop {
5+
mapping(uint256 => uint256) public map;
6+
mapping(uint256 => uint256) public map2;
7+
8+
function store(uint16 n) public {
9+
for (uint16 i = 0; i < n; i++) {
10+
map[i] = i + 1;
11+
}
12+
}
13+
14+
function store2(uint256 i) public {
15+
map2[i] = i;
16+
}
17+
}

0 commit comments

Comments
 (0)