Skip to content

Commit 0e10497

Browse files
committed
feat(forks): Add transaction_data_floor_cost_calculator
1 parent d756068 commit 0e10497

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/ethereum_test_forks/base_fork.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ def __call__(self, *, data: BytesConvertible, floor: bool = False) -> int:
5151
pass
5252

5353

54+
class TransactionDataFloorCostCalculator(Protocol):
55+
"""
56+
A protocol to calculate the transaction floor cost due to its calldata for a given fork.
57+
"""
58+
59+
def __call__(self, *, data: BytesConvertible) -> int:
60+
"""
61+
Returns the transaction gas cost of calldata given its contents.
62+
"""
63+
pass
64+
65+
5466
class TransactionIntrinsicCostCalculator(Protocol):
5567
"""
5668
A protocol to calculate the intrinsic gas cost of a transaction for a given fork.
@@ -247,6 +259,16 @@ def calldata_gas_calculator(
247259
"""
248260
pass
249261

262+
@classmethod
263+
@abstractmethod
264+
def transaction_data_floor_cost_calculator(
265+
cls, block_number: int = 0, timestamp: int = 0
266+
) -> TransactionDataFloorCostCalculator:
267+
"""
268+
Returns a callable that calculates the transaction floor cost due to its calldata.
269+
"""
270+
pass
271+
250272
@classmethod
251273
@abstractmethod
252274
def transaction_intrinsic_cost_calculator(

src/ethereum_test_forks/forks/forks.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
BaseFork,
1919
CalldataGasCalculator,
2020
MemoryExpansionGasCalculator,
21+
TransactionDataFloorCostCalculator,
2122
TransactionIntrinsicCostCalculator,
2223
)
2324
from ..gas_costs import GasCosts
@@ -197,6 +198,19 @@ def fn(*, data: BytesConvertible, floor: bool = False) -> int:
197198

198199
return fn
199200

201+
@classmethod
202+
def transaction_data_floor_cost_calculator(
203+
cls, block_number: int = 0, timestamp: int = 0
204+
) -> TransactionDataFloorCostCalculator:
205+
"""
206+
At frontier, the transaction data floor cost is a constant zero.
207+
"""
208+
209+
def fn(*, data: BytesConvertible) -> int:
210+
return 0
211+
212+
return fn
213+
200214
@classmethod
201215
def transaction_intrinsic_cost_calculator(
202216
cls, block_number: int = 0, timestamp: int = 0
@@ -1185,6 +1199,21 @@ def fn(*, data: BytesConvertible, floor: bool = False) -> int:
11851199

11861200
return fn
11871201

1202+
@classmethod
1203+
def transaction_data_floor_cost_calculator(
1204+
cls, block_number: int = 0, timestamp: int = 0
1205+
) -> TransactionDataFloorCostCalculator:
1206+
"""
1207+
Starting in Prague, due to EIP-7623, the transaction data floor cost is introduced.
1208+
"""
1209+
calldata_gas_calculator = cls.calldata_gas_calculator(block_number, timestamp)
1210+
gas_costs = cls.gas_costs(block_number, timestamp)
1211+
1212+
def fn(*, data: BytesConvertible) -> int:
1213+
return calldata_gas_calculator(data=data, floor=True) + gas_costs.G_TRANSACTION
1214+
1215+
return fn
1216+
11881217
@classmethod
11891218
def transaction_intrinsic_cost_calculator(
11901219
cls, block_number: int = 0, timestamp: int = 0
@@ -1196,6 +1225,9 @@ def transaction_intrinsic_cost_calculator(
11961225
block_number, timestamp
11971226
)
11981227
gas_costs = cls.gas_costs(block_number, timestamp)
1228+
transaction_data_floor_cost_calculator = cls.transaction_data_floor_cost_calculator(
1229+
block_number, timestamp
1230+
)
11991231

12001232
def fn(
12011233
*,
@@ -1219,8 +1251,8 @@ def fn(
12191251
if return_cost_deducted_prior_execution:
12201252
return intrinsic_cost
12211253

1222-
floor_data_cost = cls.calldata_gas_calculator()(data=calldata, floor=True)
1223-
return max(intrinsic_cost, floor_data_cost + gas_costs.G_TRANSACTION)
1254+
transaction_floor_data_cost = transaction_data_floor_cost_calculator(data=calldata)
1255+
return max(intrinsic_cost, transaction_floor_data_cost)
12241256

12251257
return fn
12261258

0 commit comments

Comments
 (0)