@@ -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
873874impl_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+ }
0 commit comments