Skip to content

Commit e427897

Browse files
committed
refactor: BPO5 conversion to encapsulate Gas Constants in class
1 parent 080ed6f commit e427897

25 files changed

Lines changed: 307 additions & 308 deletions

src/ethereum/forks/bpo5/fork.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@
8181
from .vm import Message
8282
from .vm.eoa_delegation import is_valid_delegation
8383
from .vm.gas import (
84-
BLOB_SCHEDULE_MAX,
85-
GAS_PER_BLOB,
84+
GasCosts,
8685
calculate_blob_gas_price,
8786
calculate_data_fee,
8887
calculate_excess_blob_gas,
@@ -100,7 +99,7 @@
10099
"0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02"
101100
)
102101
SYSTEM_TRANSACTION_GAS = Uint(30000000)
103-
MAX_BLOB_GAS_PER_BLOCK = BLOB_SCHEDULE_MAX * GAS_PER_BLOB
102+
MAX_BLOB_GAS_PER_BLOCK = GasCosts.BLOB_SCHEDULE_MAX * GasCosts.GAS_PER_BLOB
104103
VERSIONED_HASH_VERSION_KZG = b"\x01"
105104

106105
WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS = hex_to_address(

src/ethereum/forks/bpo5/vm/eoa_delegation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
set_code,
2323
)
2424
from ..utils.hexadecimal import hex_to_address
25-
from ..vm.gas import GAS_COLD_ACCOUNT_ACCESS, GAS_WARM_ACCESS
25+
from ..vm.gas import GasCosts
2626
from . import Evm, Message
2727

2828
SET_CODE_TX_MAGIC = b"\x05"
@@ -147,10 +147,10 @@ def access_delegation(
147147

148148
address = Address(code[EOA_DELEGATION_MARKER_LENGTH:])
149149
if address in evm.accessed_addresses:
150-
access_gas_cost = GAS_WARM_ACCESS
150+
access_gas_cost = GasCosts.GAS_WARM_ACCESS
151151
else:
152152
evm.accessed_addresses.add(address)
153-
access_gas_cost = GAS_COLD_ACCOUNT_ACCESS
153+
access_gas_cost = GasCosts.GAS_COLD_ACCOUNT_ACCESS
154154
code = get_code(state, get_account(state, address).code_hash)
155155

156156
return True, address, code, access_gas_cost

src/ethereum/forks/bpo5/vm/gas.py

Lines changed: 123 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -24,105 +24,113 @@
2424
from . import Evm
2525
from .exceptions import OutOfGasError
2626

27-
GAS_JUMPDEST = Uint(1)
28-
GAS_BASE = Uint(2)
29-
GAS_VERY_LOW = Uint(3)
30-
GAS_STORAGE_SET = Uint(20000)
31-
GAS_COLD_STORAGE_WRITE = Uint(5000)
32-
REFUND_STORAGE_CLEAR = 4800
33-
GAS_LOW = Uint(5)
34-
GAS_MID = Uint(8)
35-
GAS_HIGH = Uint(10)
36-
GAS_EXPONENTIATION = Uint(10)
37-
GAS_EXPONENTIATION_PER_BYTE = Uint(50)
38-
GAS_MEMORY = Uint(3)
39-
GAS_KECCAK256 = Uint(30)
40-
GAS_KECCAK256_PER_WORD = Uint(6)
41-
GAS_COPY = Uint(3)
42-
GAS_BLOCK_HASH = Uint(20)
43-
GAS_LOG = Uint(375)
44-
GAS_LOG_DATA_PER_BYTE = Uint(8)
45-
GAS_LOG_TOPIC = Uint(375)
46-
GAS_CREATE = Uint(32000)
47-
GAS_CODE_DEPOSIT_PER_BYTE = Uint(200)
48-
GAS_ZERO = Uint(0)
49-
GAS_NEW_ACCOUNT = Uint(25000)
50-
GAS_CALL_VALUE = Uint(9000)
51-
GAS_CALL_STIPEND = Uint(2300)
52-
GAS_SELF_DESTRUCT = Uint(5000)
53-
GAS_SELF_DESTRUCT_NEW_ACCOUNT = Uint(25000)
54-
GAS_PRECOMPILE_ECRECOVER = Uint(3000)
55-
GAS_PRECOMPILE_P256VERIFY = Uint(6900)
56-
GAS_PRECOMPILE_SHA256_BASE = Uint(60)
57-
GAS_PRECOMPILE_SHA256_PER_WORD = Uint(12)
58-
GAS_PRECOMPILE_RIPEMD160_BASE = Uint(600)
59-
GAS_PRECOMPILE_RIPEMD160_PER_WORD = Uint(120)
60-
GAS_PRECOMPILE_IDENTITY_BASE = Uint(15)
61-
GAS_PRECOMPILE_IDENTITY_PER_WORD = Uint(3)
62-
GAS_RETURN_DATA_COPY = Uint(3)
63-
GAS_FAST_STEP = Uint(5)
64-
GAS_PRECOMPILE_BLAKE2F_PER_ROUND = Uint(1)
65-
GAS_COLD_STORAGE_ACCESS = Uint(2100)
66-
GAS_COLD_ACCOUNT_ACCESS = Uint(2600)
67-
GAS_WARM_ACCESS = Uint(100)
68-
GAS_CODE_INIT_PER_WORD = Uint(2)
69-
GAS_BLOBHASH = Uint(3)
70-
GAS_POINT_EVALUATION = Uint(50000)
71-
72-
GAS_PER_BLOB = U64(2**17)
73-
BLOB_SCHEDULE_TARGET = U64(14)
74-
BLOB_TARGET_GAS_PER_BLOCK = GAS_PER_BLOB * BLOB_SCHEDULE_TARGET
75-
BLOB_BASE_COST = Uint(2**13)
76-
BLOB_SCHEDULE_MAX = U64(21)
77-
BLOB_MIN_GASPRICE = Uint(1)
78-
BLOB_BASE_FEE_UPDATE_FRACTION = Uint(11684671)
79-
80-
GAS_PRECOMPILE_BLS_G1ADD = Uint(375)
81-
GAS_PRECOMPILE_BLS_G1MUL = Uint(12000)
82-
GAS_PRECOMPILE_BLS_G1MAP = Uint(5500)
83-
GAS_PRECOMPILE_BLS_G2ADD = Uint(600)
84-
GAS_PRECOMPILE_BLS_G2MUL = Uint(22500)
85-
GAS_PRECOMPILE_BLS_G2MAP = Uint(23800)
86-
87-
# Opcode specific vars used for repricing
88-
GAS_OPCODE_ADD = GAS_VERY_LOW
89-
GAS_OPCODE_SUB = GAS_VERY_LOW
90-
GAS_OPCODE_MUL = GAS_LOW
91-
GAS_OPCODE_DIV = GAS_LOW
92-
GAS_OPCODE_SDIV = GAS_LOW
93-
GAS_OPCODE_MOD = GAS_LOW
94-
GAS_OPCODE_SMOD = GAS_LOW
95-
GAS_OPCODE_ADDMOD = GAS_MID
96-
GAS_OPCODE_MULMOD = GAS_MID
97-
GAS_OPCODE_SIGNEXTEND = GAS_LOW
98-
GAS_OPCODE_LT = GAS_VERY_LOW
99-
GAS_OPCODE_GT = GAS_VERY_LOW
100-
GAS_OPCODE_SLT = GAS_VERY_LOW
101-
GAS_OPCODE_SGT = GAS_VERY_LOW
102-
GAS_OPCODE_EQ = GAS_VERY_LOW
103-
GAS_OPCODE_ISZERO = GAS_VERY_LOW
104-
GAS_OPCODE_AND = GAS_VERY_LOW
105-
GAS_OPCODE_OR = GAS_VERY_LOW
106-
GAS_OPCODE_XOR = GAS_VERY_LOW
107-
GAS_OPCODE_NOT = GAS_VERY_LOW
108-
GAS_OPCODE_BYTE = GAS_VERY_LOW
109-
GAS_OPCODE_SHL = GAS_VERY_LOW
110-
GAS_OPCODE_SHR = GAS_VERY_LOW
111-
GAS_OPCODE_SAR = GAS_VERY_LOW
112-
GAS_OPCODE_CLZ = GAS_LOW
113-
GAS_OPCODE_JUMP = GAS_MID
114-
GAS_OPCODE_JUMPI = GAS_HIGH
115-
GAS_OPCODE_CALLDATALOAD = GAS_VERY_LOW
116-
GAS_OPCODE_CALLDATACOPY = GAS_VERY_LOW
117-
GAS_OPCODE_CODECOPY = GAS_VERY_LOW
118-
GAS_OPCODE_RETURNDATACOPY = GAS_VERY_LOW
119-
GAS_OPCODE_MLOAD = GAS_VERY_LOW
120-
GAS_OPCODE_MSTORE = GAS_VERY_LOW
121-
GAS_OPCODE_MSTORE8 = GAS_VERY_LOW
122-
GAS_OPCODE_MCOPY = GAS_VERY_LOW
123-
GAS_OPCODE_PUSH_N = GAS_VERY_LOW
124-
GAS_OPCODE_DUP = GAS_VERY_LOW
125-
GAS_OPCODE_SWAP = GAS_VERY_LOW
27+
28+
class GasCosts:
29+
"""
30+
Constant gas values for the BPO (Blob Parameter Only) 5 fork of the EVM.
31+
32+
These values may be patched at runtime by a future gas repricing utility
33+
"""
34+
35+
GAS_JUMPDEST = Uint(1)
36+
GAS_BASE = Uint(2)
37+
GAS_VERY_LOW = Uint(3)
38+
GAS_STORAGE_SET = Uint(20000)
39+
GAS_COLD_STORAGE_WRITE = Uint(5000)
40+
REFUND_STORAGE_CLEAR = 4800
41+
GAS_LOW = Uint(5)
42+
GAS_MID = Uint(8)
43+
GAS_HIGH = Uint(10)
44+
GAS_EXPONENTIATION = Uint(10)
45+
GAS_EXPONENTIATION_PER_BYTE = Uint(50)
46+
GAS_MEMORY = Uint(3)
47+
GAS_KECCAK256 = Uint(30)
48+
GAS_KECCAK256_PER_WORD = Uint(6)
49+
GAS_COPY = Uint(3)
50+
GAS_BLOCK_HASH = Uint(20)
51+
GAS_LOG = Uint(375)
52+
GAS_LOG_DATA_PER_BYTE = Uint(8)
53+
GAS_LOG_TOPIC = Uint(375)
54+
GAS_CREATE = Uint(32000)
55+
GAS_CODE_DEPOSIT_PER_BYTE = Uint(200)
56+
GAS_ZERO = Uint(0)
57+
GAS_NEW_ACCOUNT = Uint(25000)
58+
GAS_CALL_VALUE = Uint(9000)
59+
GAS_CALL_STIPEND = Uint(2300)
60+
GAS_SELF_DESTRUCT = Uint(5000)
61+
GAS_SELF_DESTRUCT_NEW_ACCOUNT = Uint(25000)
62+
GAS_PRECOMPILE_ECRECOVER = Uint(3000)
63+
GAS_PRECOMPILE_P256VERIFY = Uint(6900)
64+
GAS_PRECOMPILE_SHA256_BASE = Uint(60)
65+
GAS_PRECOMPILE_SHA256_PER_WORD = Uint(12)
66+
GAS_PRECOMPILE_RIPEMD160_BASE = Uint(600)
67+
GAS_PRECOMPILE_RIPEMD160_PER_WORD = Uint(120)
68+
GAS_PRECOMPILE_IDENTITY_BASE = Uint(15)
69+
GAS_PRECOMPILE_IDENTITY_PER_WORD = Uint(3)
70+
GAS_RETURN_DATA_COPY = Uint(3)
71+
GAS_FAST_STEP = Uint(5)
72+
GAS_PRECOMPILE_BLAKE2F_PER_ROUND = Uint(1)
73+
GAS_COLD_STORAGE_ACCESS = Uint(2100)
74+
GAS_COLD_ACCOUNT_ACCESS = Uint(2600)
75+
GAS_WARM_ACCESS = Uint(100)
76+
GAS_CODE_INIT_PER_WORD = Uint(2)
77+
GAS_BLOBHASH = Uint(3)
78+
GAS_POINT_EVALUATION = Uint(50000)
79+
80+
GAS_PER_BLOB = U64(2**17)
81+
BLOB_SCHEDULE_TARGET = U64(14)
82+
BLOB_TARGET_GAS_PER_BLOCK = GAS_PER_BLOB * BLOB_SCHEDULE_TARGET
83+
BLOB_BASE_COST = Uint(2**13)
84+
BLOB_SCHEDULE_MAX = U64(21)
85+
BLOB_MIN_GASPRICE = Uint(1)
86+
BLOB_BASE_FEE_UPDATE_FRACTION = Uint(11684671)
87+
88+
GAS_PRECOMPILE_BLS_G1ADD = Uint(375)
89+
GAS_PRECOMPILE_BLS_G1MUL = Uint(12000)
90+
GAS_PRECOMPILE_BLS_G1MAP = Uint(5500)
91+
GAS_PRECOMPILE_BLS_G2ADD = Uint(600)
92+
GAS_PRECOMPILE_BLS_G2MUL = Uint(22500)
93+
GAS_PRECOMPILE_BLS_G2MAP = Uint(23800)
94+
95+
# Opcode specific vars used for repricing
96+
GAS_OPCODE_ADD = GAS_VERY_LOW
97+
GAS_OPCODE_SUB = GAS_VERY_LOW
98+
GAS_OPCODE_MUL = GAS_LOW
99+
GAS_OPCODE_DIV = GAS_LOW
100+
GAS_OPCODE_SDIV = GAS_LOW
101+
GAS_OPCODE_MOD = GAS_LOW
102+
GAS_OPCODE_SMOD = GAS_LOW
103+
GAS_OPCODE_ADDMOD = GAS_MID
104+
GAS_OPCODE_MULMOD = GAS_MID
105+
GAS_OPCODE_SIGNEXTEND = GAS_LOW
106+
GAS_OPCODE_LT = GAS_VERY_LOW
107+
GAS_OPCODE_GT = GAS_VERY_LOW
108+
GAS_OPCODE_SLT = GAS_VERY_LOW
109+
GAS_OPCODE_SGT = GAS_VERY_LOW
110+
GAS_OPCODE_EQ = GAS_VERY_LOW
111+
GAS_OPCODE_ISZERO = GAS_VERY_LOW
112+
GAS_OPCODE_AND = GAS_VERY_LOW
113+
GAS_OPCODE_OR = GAS_VERY_LOW
114+
GAS_OPCODE_XOR = GAS_VERY_LOW
115+
GAS_OPCODE_NOT = GAS_VERY_LOW
116+
GAS_OPCODE_BYTE = GAS_VERY_LOW
117+
GAS_OPCODE_SHL = GAS_VERY_LOW
118+
GAS_OPCODE_SHR = GAS_VERY_LOW
119+
GAS_OPCODE_SAR = GAS_VERY_LOW
120+
GAS_OPCODE_CLZ = GAS_LOW
121+
GAS_OPCODE_JUMP = GAS_MID
122+
GAS_OPCODE_JUMPI = GAS_HIGH
123+
GAS_OPCODE_CALLDATALOAD = GAS_VERY_LOW
124+
GAS_OPCODE_CALLDATACOPY = GAS_VERY_LOW
125+
GAS_OPCODE_CODECOPY = GAS_VERY_LOW
126+
GAS_OPCODE_RETURNDATACOPY = GAS_VERY_LOW
127+
GAS_OPCODE_MLOAD = GAS_VERY_LOW
128+
GAS_OPCODE_MSTORE = GAS_VERY_LOW
129+
GAS_OPCODE_MSTORE8 = GAS_VERY_LOW
130+
GAS_OPCODE_MCOPY = GAS_VERY_LOW
131+
GAS_OPCODE_PUSH_N = GAS_VERY_LOW
132+
GAS_OPCODE_DUP = GAS_VERY_LOW
133+
GAS_OPCODE_SWAP = GAS_VERY_LOW
126134

127135

128136
@dataclass
@@ -196,7 +204,7 @@ def calculate_memory_gas_cost(size_in_bytes: Uint) -> Uint:
196204
197205
"""
198206
size_in_words = ceil32(size_in_bytes) // Uint(32)
199-
linear_cost = size_in_words * GAS_MEMORY
207+
linear_cost = size_in_words * GasCosts.GAS_MEMORY
200208
quadratic_cost = size_in_words ** Uint(2) // Uint(512)
201209
total_gas_cost = linear_cost + quadratic_cost
202210
try:
@@ -251,7 +259,7 @@ def calculate_message_call_gas(
251259
gas_left: Uint,
252260
memory_cost: Uint,
253261
extra_gas: Uint,
254-
call_stipend: Uint = GAS_CALL_STIPEND,
262+
call_stipend: Uint = GasCosts.GAS_CALL_STIPEND,
255263
) -> MessageCallGas:
256264
"""
257265
Calculates the MessageCallGas (cost and gas made available to the sub-call)
@@ -323,7 +331,9 @@ def init_code_cost(init_code_length: Uint) -> Uint:
323331
The gas to be charged for the init code.
324332
325333
"""
326-
return GAS_CODE_INIT_PER_WORD * ceil32(init_code_length) // Uint(32)
334+
return (
335+
GasCosts.GAS_CODE_INIT_PER_WORD * ceil32(init_code_length) // Uint(32)
336+
)
327337

328338

329339
def calculate_excess_blob_gas(parent_header: Header) -> U64:
@@ -354,21 +364,23 @@ def calculate_excess_blob_gas(parent_header: Header) -> U64:
354364
base_fee_per_gas = parent_header.base_fee_per_gas
355365

356366
parent_blob_gas = excess_blob_gas + blob_gas_used
357-
if parent_blob_gas < BLOB_TARGET_GAS_PER_BLOCK:
367+
if parent_blob_gas < GasCosts.BLOB_TARGET_GAS_PER_BLOCK:
358368
return U64(0)
359369

360-
target_blob_gas_price = Uint(GAS_PER_BLOB)
370+
target_blob_gas_price = Uint(GasCosts.GAS_PER_BLOB)
361371
target_blob_gas_price *= calculate_blob_gas_price(excess_blob_gas)
362372

363-
base_blob_tx_price = BLOB_BASE_COST * base_fee_per_gas
373+
base_blob_tx_price = GasCosts.BLOB_BASE_COST * base_fee_per_gas
364374
if base_blob_tx_price > target_blob_gas_price:
365-
blob_schedule_delta = BLOB_SCHEDULE_MAX - BLOB_SCHEDULE_TARGET
375+
blob_schedule_delta = (
376+
GasCosts.BLOB_SCHEDULE_MAX - GasCosts.BLOB_SCHEDULE_TARGET
377+
)
366378
return (
367379
excess_blob_gas
368-
+ blob_gas_used * blob_schedule_delta // BLOB_SCHEDULE_MAX
380+
+ blob_gas_used * blob_schedule_delta // GasCosts.BLOB_SCHEDULE_MAX
369381
)
370382

371-
return parent_blob_gas - BLOB_TARGET_GAS_PER_BLOCK
383+
return parent_blob_gas - GasCosts.BLOB_TARGET_GAS_PER_BLOCK
372384

373385

374386
def calculate_total_blob_gas(tx: Transaction) -> U64:
@@ -387,7 +399,7 @@ def calculate_total_blob_gas(tx: Transaction) -> U64:
387399
388400
"""
389401
if isinstance(tx, BlobTransaction):
390-
return GAS_PER_BLOB * U64(len(tx.blob_versioned_hashes))
402+
return GasCosts.GAS_PER_BLOB * U64(len(tx.blob_versioned_hashes))
391403
else:
392404
return U64(0)
393405

@@ -408,9 +420,9 @@ def calculate_blob_gas_price(excess_blob_gas: U64) -> Uint:
408420
409421
"""
410422
return taylor_exponential(
411-
BLOB_MIN_GASPRICE,
423+
GasCosts.BLOB_MIN_GASPRICE,
412424
Uint(excess_blob_gas),
413-
BLOB_BASE_FEE_UPDATE_FRACTION,
425+
GasCosts.BLOB_BASE_FEE_UPDATE_FRACTION,
414426
)
415427

416428

0 commit comments

Comments
 (0)