Skip to content

Commit b6d35cc

Browse files
Carsons-EelsSamWilsngurukamath
authored
Eip 7918 changes (#1273)
* Update to latest EIP-7918 * EIP-7918 t8n tool update --------- Co-authored-by: Sam Wilson <[email protected]> Co-authored-by: Guruprasad Kamath <[email protected]>
1 parent dc6e9a0 commit b6d35cc

File tree

5 files changed

+100
-20
lines changed

5 files changed

+100
-20
lines changed

src/ethereum/cancun/vm/gas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ def calculate_excess_blob_gas(parent_header: Header) -> U64:
297297
parent_blob_gas = excess_blob_gas + blob_gas_used
298298
if parent_blob_gas < TARGET_BLOB_GAS_PER_BLOCK:
299299
return U64(0)
300-
else:
301-
return parent_blob_gas - TARGET_BLOB_GAS_PER_BLOCK
300+
301+
return parent_blob_gas - TARGET_BLOB_GAS_PER_BLOCK
302302

303303

304304
def calculate_total_blob_gas(tx: Transaction) -> U64:

src/ethereum/osaka/vm/gas.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from ethereum.utils.numeric import ceil32, taylor_exponential
2121

2222
from ..blocks import Header
23-
from ..transactions import TX_BASE_COST, BlobTransaction, Transaction
23+
from ..transactions import BlobTransaction, Transaction
2424
from . import Evm
2525
from .exceptions import OutOfGasError
2626

@@ -69,6 +69,9 @@
6969
GAS_POINT_EVALUATION = Uint(50000)
7070

7171
TARGET_BLOB_GAS_PER_BLOCK = U64(786432)
72+
BLOB_BASE_COST = Uint(2**14)
73+
BLOB_SCHEDULE_MAX = U64(9)
74+
BLOB_SCHEDULE_TARGET = U64(6)
7275
GAS_PER_BLOB = U64(2**17)
7376
MIN_BLOB_GASPRICE = Uint(1)
7477
BLOB_BASE_FEE_UPDATE_FRACTION = Uint(5007716)
@@ -295,24 +298,30 @@ def calculate_excess_blob_gas(parent_header: Header) -> U64:
295298
# At the fork block, these are defined as zero.
296299
excess_blob_gas = U64(0)
297300
blob_gas_used = U64(0)
301+
base_fee_per_gas = Uint(0)
298302

299303
if isinstance(parent_header, Header):
300304
# After the fork block, read them from the parent header.
301305
excess_blob_gas = parent_header.excess_blob_gas
302306
blob_gas_used = parent_header.blob_gas_used
307+
base_fee_per_gas = parent_header.base_fee_per_gas
303308

304309
parent_blob_gas = excess_blob_gas + blob_gas_used
305310
if parent_blob_gas < TARGET_BLOB_GAS_PER_BLOCK:
306311
return U64(0)
307-
else:
308-
target_blob_gas_price = Uint(
309-
TARGET_BLOB_GAS_PER_BLOCK
310-
) * calculate_blob_gas_price(parent_header.excess_blob_gas)
311-
base_blob_tx_price = TX_BASE_COST * parent_header.base_fee_per_gas
312-
if base_blob_tx_price > target_blob_gas_price:
313-
return parent_blob_gas // U64(3)
314-
else:
315-
return parent_blob_gas - TARGET_BLOB_GAS_PER_BLOCK
312+
313+
target_blob_gas_price = Uint(GAS_PER_BLOB)
314+
target_blob_gas_price *= calculate_blob_gas_price(excess_blob_gas)
315+
316+
base_blob_tx_price = BLOB_BASE_COST * base_fee_per_gas
317+
if base_blob_tx_price > target_blob_gas_price:
318+
blob_schedule_delta = BLOB_SCHEDULE_MAX - BLOB_SCHEDULE_TARGET
319+
return (
320+
excess_blob_gas
321+
+ blob_gas_used * blob_schedule_delta // BLOB_SCHEDULE_MAX
322+
)
323+
324+
return parent_blob_gas - TARGET_BLOB_GAS_PER_BLOCK
316325

317326

318327
def calculate_total_blob_gas(tx: Transaction) -> U64:

src/ethereum/prague/vm/gas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ def calculate_excess_blob_gas(parent_header: Header) -> U64:
304304
parent_blob_gas = excess_blob_gas + blob_gas_used
305305
if parent_blob_gas < TARGET_BLOB_GAS_PER_BLOCK:
306306
return U64(0)
307-
else:
308-
return parent_blob_gas - TARGET_BLOB_GAS_PER_BLOCK
307+
308+
return parent_blob_gas - TARGET_BLOB_GAS_PER_BLOCK
309309

310310

311311
def calculate_total_blob_gas(tx: Transaction) -> U64:

src/ethereum_spec_tools/evm_tools/loaders/fork_loader.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,31 @@ def TARGET_BLOB_GAS_PER_BLOCK(self) -> Any:
307307
"""TARGET_BLOB_GAS_PER_BLOCK of the fork"""
308308
return self._module("vm.gas").TARGET_BLOB_GAS_PER_BLOCK
309309

310+
@property
311+
def GAS_PER_BLOB(self) -> Any:
312+
"""GAS_PER_BLOB of the fork"""
313+
return self._module("vm.gas").GAS_PER_BLOB
314+
315+
@property
316+
def BLOB_BASE_COST(self) -> Any:
317+
"""BLOB_BASE_COST of the fork"""
318+
return self._module("vm.gas").BLOB_BASE_COST
319+
320+
@property
321+
def BLOB_SCHEDULE_MAX(self) -> Any:
322+
"""BLOB_SCHEDULE_MAX of the fork"""
323+
return self._module("vm.gas").BLOB_SCHEDULE_MAX
324+
325+
@property
326+
def BLOB_SCHEDULE_TARGET(self) -> Any:
327+
"""BLOB_SCHEDULE_TARGET of the fork"""
328+
return self._module("vm.gas").BLOB_SCHEDULE_TARGET
329+
330+
@property
331+
def calculate_blob_gas_price(self) -> Any:
332+
"""calculate_blob_gas_price of the fork"""
333+
return self._module("vm.gas").calculate_blob_gas_price
334+
310335
@property
311336
def apply_dao(self) -> Any:
312337
"""apply_dao function of the fork"""

src/ethereum_spec_tools/evm_tools/t8n/env.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def read_excess_blob_gas(self, data: Any, t8n: "T8N") -> None:
102102
self.excess_blob_gas = parse_hex_or_int(
103103
data["currentExcessBlobGas"], U64
104104
)
105-
return
106105

107106
if "parentExcessBlobGas" in data:
108107
self.parent_excess_blob_gas = parse_hex_or_int(
@@ -114,15 +113,51 @@ def read_excess_blob_gas(self, data: Any, t8n: "T8N") -> None:
114113
data["parentBlobGasUsed"], U64
115114
)
116115

117-
excess_blob_gas = (
116+
if self.excess_blob_gas is not None:
117+
return
118+
119+
assert self.parent_excess_blob_gas is not None
120+
assert self.parent_blob_gas_used is not None
121+
122+
parent_blob_gas = (
118123
self.parent_excess_blob_gas + self.parent_blob_gas_used
119124
)
120125

121126
target_blob_gas_per_block = t8n.fork.TARGET_BLOB_GAS_PER_BLOCK
122127

123-
self.excess_blob_gas = U64(0)
124-
if excess_blob_gas >= target_blob_gas_per_block:
125-
self.excess_blob_gas = excess_blob_gas - target_blob_gas_per_block
128+
if parent_blob_gas < target_blob_gas_per_block:
129+
self.excess_blob_gas = U64(0)
130+
else:
131+
self.excess_blob_gas = parent_blob_gas - target_blob_gas_per_block
132+
133+
if t8n.fork.is_after_fork("ethereum.osaka"):
134+
# Under certain conditions specified in EIP-7918, the
135+
# the excess_blob_gas is calculated differently in osaka
136+
assert self.parent_base_fee_per_gas is not None
137+
138+
GAS_PER_BLOB = t8n.fork.GAS_PER_BLOB
139+
BLOB_BASE_COST = t8n.fork.BLOB_BASE_COST
140+
BLOB_SCHEDULE_MAX = t8n.fork.BLOB_SCHEDULE_MAX
141+
BLOB_SCHEDULE_TARGET = t8n.fork.BLOB_SCHEDULE_TARGET
142+
143+
target_blob_gas_price = Uint(GAS_PER_BLOB)
144+
target_blob_gas_price *= t8n.fork.calculate_blob_gas_price(
145+
self.parent_excess_blob_gas
146+
)
147+
148+
base_blob_tx_price = (
149+
BLOB_BASE_COST * self.parent_base_fee_per_gas
150+
)
151+
if base_blob_tx_price > target_blob_gas_price:
152+
blob_schedule_delta = (
153+
BLOB_SCHEDULE_MAX - BLOB_SCHEDULE_TARGET
154+
)
155+
self.excess_blob_gas = (
156+
self.parent_excess_blob_gas
157+
+ self.parent_blob_gas_used
158+
* blob_schedule_delta
159+
// BLOB_SCHEDULE_MAX
160+
)
126161

127162
def read_base_fee_per_gas(self, data: Any, t8n: "T8N") -> None:
128163
"""
@@ -139,16 +174,27 @@ def read_base_fee_per_gas(self, data: Any, t8n: "T8N") -> None:
139174
self.base_fee_per_gas = parse_hex_or_int(
140175
data["currentBaseFee"], Uint
141176
)
142-
else:
177+
178+
if "parentGasUsed" in data:
143179
self.parent_gas_used = parse_hex_or_int(
144180
data["parentGasUsed"], Uint
145181
)
182+
183+
if "parentGasLimit" in data:
146184
self.parent_gas_limit = parse_hex_or_int(
147185
data["parentGasLimit"], Uint
148186
)
187+
188+
if "parentBaseFee" in data:
149189
self.parent_base_fee_per_gas = parse_hex_or_int(
150190
data["parentBaseFee"], Uint
151191
)
192+
193+
if self.base_fee_per_gas is None:
194+
assert self.parent_gas_limit is not None
195+
assert self.parent_gas_used is not None
196+
assert self.parent_base_fee_per_gas is not None
197+
152198
parameters: List[object] = [
153199
self.block_gas_limit,
154200
self.parent_gas_limit,

0 commit comments

Comments
 (0)