Skip to content

Commit 884b9ff

Browse files
committed
refactor: move transaction gas constatns with other gas constants for repricing mechanism.
1 parent 9e812cc commit 884b9ff

81 files changed

Lines changed: 728 additions & 1126 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/testing/src/execution_testing/forks/forks/forks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ def gas_costs(cls) -> GasCosts:
120120
# Contract Creation
121121
CODE_DEPOSIT_PER_BYTE=200,
122122
CODE_INIT_PER_WORD=2,
123+
# Authorization
124+
AUTH_PER_EMPTY_ACCOUNT=0,
123125
# Utility
124126
MEMORY_PER_WORD=3,
125127
# Transactions
@@ -171,7 +173,7 @@ def gas_costs(cls) -> GasCosts:
171173
OPCODE_PUSH=VERY_LOW,
172174
OPCODE_DUP=VERY_LOW,
173175
OPCODE_SWAP=VERY_LOW,
174-
# Dynamic Opcode Component
176+
# Dynamic Opcode Components
175177
OPCODE_CALLDATACOPY_BASE=VERY_LOW,
176178
OPCODE_CODECOPY_BASE=VERY_LOW,
177179
OPCODE_MLOAD_BASE=VERY_LOW,
@@ -191,7 +193,6 @@ def gas_costs(cls) -> GasCosts:
191193
# replace() in the fork that activates them.
192194
TX_DATA_TOKEN_STANDARD=0,
193195
TX_DATA_TOKEN_FLOOR=0,
194-
AUTH_PER_EMPTY_ACCOUNT=0,
195196
PRECOMPILE_ECADD=0,
196197
PRECOMPILE_ECMUL=0,
197198
PRECOMPILE_ECPAIRING_BASE=0,

src/ethereum/forks/amsterdam/fork.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@
105105

106106
BASE_FEE_MAX_CHANGE_DENOMINATOR = Uint(8)
107107
ELASTICITY_MULTIPLIER = Uint(2)
108-
GAS_LIMIT_ADJUSTMENT_FACTOR = Uint(1024)
109-
GAS_LIMIT_MINIMUM = Uint(5000)
110108
EMPTY_OMMER_HASH = keccak256(rlp.encode([]))
111109
SYSTEM_ADDRESS = hex_to_address("0xfffffffffffffffffffffffffffffffffffffffe")
112110
BEACON_ROOTS_ADDRESS = hex_to_address(
@@ -1121,14 +1119,14 @@ def check_gas_limit(gas_limit: Uint, parent_gas_limit: Uint) -> bool:
11211119
11221120
The bounds of the gas limit, ``max_adjustment_delta``, is set as the
11231121
quotient of the parent block's gas limit and the
1124-
``GAS_LIMIT_ADJUSTMENT_FACTOR``. Therefore, if the gas limit that is
1125-
passed through as a parameter is greater than or equal to the *sum* of
1126-
the parent's gas and the adjustment delta then the limit for gas is too
1127-
high and fails this function's check. Similarly, if the limit is less
1128-
than or equal to the *difference* of the parent's gas and the adjustment
1129-
delta *or* the predefined ``GAS_LIMIT_MINIMUM`` then this function's
1130-
check fails because the gas limit doesn't allow for a sufficient or
1131-
reasonable amount of gas to be used on a block.
1122+
``LIMIT_ADJUSTMENT_FACTOR``. Therefore, if the gas limit that is passed
1123+
through as a parameter is greater than or equal to the *sum* of the
1124+
parent's gas and the adjustment delta then the limit for gas is too high
1125+
and fails this function's check. Similarly, if the limit is less than or
1126+
equal to the *difference* of the parent's gas and the adjustment delta *or*
1127+
the predefined ``LIMIT_MINIMUM`` then this function's check fails because
1128+
the gas limit doesn't allow for a sufficient or reasonable amount of gas to
1129+
be used on a block.
11321130
11331131
Parameters
11341132
----------
@@ -1144,12 +1142,12 @@ def check_gas_limit(gas_limit: Uint, parent_gas_limit: Uint) -> bool:
11441142
True if gas limit constraints are satisfied, False otherwise.
11451143
11461144
"""
1147-
max_adjustment_delta = parent_gas_limit // GAS_LIMIT_ADJUSTMENT_FACTOR
1145+
max_adjustment_delta = parent_gas_limit // GasCosts.LIMIT_ADJUSTMENT_FACTOR
11481146
if gas_limit >= parent_gas_limit + max_adjustment_delta:
11491147
return False
11501148
if gas_limit <= parent_gas_limit - max_adjustment_delta:
11511149
return False
1152-
if gas_limit < GAS_LIMIT_MINIMUM:
1150+
if gas_limit < GasCosts.LIMIT_MINIMUM:
11531151
return False
11541152

11551153
return True

src/ethereum/forks/amsterdam/transactions.py

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,6 @@
2828
)
2929
from .fork_types import Authorization, VersionedHash
3030

31-
GAS_TX_BASE = Uint(21000)
32-
"""
33-
Base cost of a transaction in gas units. This is the minimum amount of gas
34-
required to execute a transaction.
35-
"""
36-
37-
GAS_TX_DATA_TOKEN_FLOOR = Uint(10)
38-
"""
39-
Minimum gas cost per byte of calldata as per [EIP-7623]. Used to calculate
40-
the minimum gas cost for transactions that include calldata.
41-
42-
[EIP-7623]: https://eips.ethereum.org/EIPS/eip-7623
43-
"""
44-
45-
GAS_TX_DATA_TOKEN_STANDARD = Uint(4)
46-
"""
47-
Gas cost per byte of calldata as per [EIP-7623]. Used to calculate the
48-
gas cost for transactions that include calldata.
49-
50-
[EIP-7623]: https://eips.ethereum.org/EIPS/eip-7623
51-
"""
52-
53-
GAS_TX_CREATE = Uint(32000)
54-
"""
55-
Additional gas cost for creating a new contract.
56-
"""
57-
58-
GAS_TX_ACCESS_LIST_ADDRESS = Uint(2400)
59-
"""
60-
Gas cost for including an address in the access list of a transaction.
61-
"""
62-
63-
GAS_TX_ACCESS_LIST_STORAGE_KEY = Uint(1900)
64-
"""
65-
Gas cost for including a storage key in the access list of a transaction.
66-
"""
67-
6831
TX_MAX_GAS_LIMIT = Uint(16_777_216)
6932

7033

@@ -608,7 +571,7 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
608571
for all operations to be implemented.
609572
610573
The intrinsic cost includes:
611-
1. Base cost (`GAS_TX_BASE`)
574+
1. Base cost (`TX_BASE`)
612575
2. Cost for data (zero and non-zero bytes)
613576
3. Cost for contract creation (if applicable)
614577
4. Cost for access list entries (if applicable)
@@ -619,33 +582,34 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
619582
gas cost of the transaction and the minimum gas cost used by the
620583
transaction based on the calldata size.
621584
"""
622-
from .vm.eoa_delegation import GAS_AUTH_PER_EMPTY_ACCOUNT
623-
from .vm.gas import init_code_cost
585+
from .vm.gas import GasCosts, init_code_cost
624586

625587
tokens_in_calldata = count_tokens_in_data(tx.data)
626588

627-
data_cost = tokens_in_calldata * GAS_TX_DATA_TOKEN_STANDARD
589+
data_cost = tokens_in_calldata * GasCosts.TX_DATA_TOKEN_STANDARD
628590

629591
if tx.to == Bytes0(b""):
630-
create_cost = GAS_TX_CREATE + init_code_cost(ulen(tx.data))
592+
create_cost = GasCosts.TX_CREATE + init_code_cost(ulen(tx.data))
631593
else:
632594
create_cost = Uint(0)
633595

634596
access_list_cost = Uint(0)
635597
tokens_in_access_list = Uint(0)
636598
if has_access_list(tx):
637599
for access in tx.access_list:
638-
access_list_cost += GAS_TX_ACCESS_LIST_ADDRESS
600+
access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
639601
access_list_cost += (
640-
ulen(access.slots) * GAS_TX_ACCESS_LIST_STORAGE_KEY
602+
ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
641603
)
642604

643605
# Data token floor cost for access list bytes.
644606
access_list_cost += tokens_in_access_list * GAS_TX_DATA_TOKEN_FLOOR
645607

646608
auth_cost = Uint(0)
647609
if isinstance(tx, SetCodeTransaction):
648-
auth_cost += Uint(GAS_AUTH_PER_EMPTY_ACCOUNT * len(tx.authorizations))
610+
auth_cost += Uint(
611+
GasCosts.AUTH_PER_EMPTY_ACCOUNT * len(tx.authorizations)
612+
)
649613

650614
# Floor tokens from calldata.
651615
floor_tokens_in_calldata = tokens_in_calldata
@@ -660,7 +624,7 @@ def calculate_intrinsic_cost(tx: Transaction) -> Tuple[Uint, Uint]:
660624

661625
return (
662626
Uint(
663-
GAS_TX_BASE
627+
GasCosts.TX_BASE
664628
+ data_cost
665629
+ create_cost
666630
+ access_list_cost

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
EOA_DELEGATION_MARKER = b"\xef\x01\x00"
2929
EOA_DELEGATION_MARKER_LENGTH = len(EOA_DELEGATION_MARKER)
3030
EOA_DELEGATED_CODE_LENGTH = 23
31-
GAS_AUTH_PER_EMPTY_ACCOUNT = 25000
3231
REFUND_AUTH_PER_EXISTING_ACCOUNT = 12500
3332
NULL_ADDRESS = hex_to_address("0x0000000000000000000000000000000000000000")
3433

@@ -199,7 +198,8 @@ def set_delegation(message: Message) -> U256:
199198

200199
if account_exists(tx_state, authority):
201200
refund_counter += U256(
202-
GAS_AUTH_PER_EMPTY_ACCOUNT - REFUND_AUTH_PER_EXISTING_ACCOUNT
201+
GasCosts.AUTH_PER_EMPTY_ACCOUNT
202+
- REFUND_AUTH_PER_EXISTING_ACCOUNT
203203
)
204204

205205
if auth.address == NULL_ADDRESS:

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class GasCosts:
5858
CODE_DEPOSIT_PER_BYTE = Uint(200)
5959
CODE_INIT_PER_WORD = Uint(2)
6060

61+
# Authorization
62+
AUTH_PER_EMPTY_ACCOUNT = 25000
63+
6164
# Utility
6265
ZERO = Uint(0)
6366
MEMORY_PER_WORD = Uint(3)
@@ -100,6 +103,18 @@ class GasCosts:
100103
# Block Access Lists
101104
BLOCK_ACCESS_LIST_ITEM = Uint(2000)
102105

106+
# Transactions
107+
TX_BASE = Uint(21000)
108+
TX_CREATE = Uint(32000)
109+
TX_DATA_TOKEN_STANDARD = Uint(4)
110+
TX_DATA_TOKEN_FLOOR = Uint(10)
111+
TX_ACCESS_LIST_ADDRESS = Uint(2400)
112+
TX_ACCESS_LIST_STORAGE_KEY = Uint(1900)
113+
114+
# Block
115+
LIMIT_ADJUSTMENT_FACTOR = Uint(1024)
116+
LIMIT_MINIMUM = Uint(5000)
117+
103118
# Static Opcodes
104119
OPCODE_ADD = VERY_LOW
105120
OPCODE_SUB = VERY_LOW

src/ethereum/forks/arrow_glacier/fork.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@
6161
)
6262
from .trie import root, trie_set
6363
from .utils.message import prepare_message
64+
from .vm.gas import GasCosts
6465
from .vm.interpreter import process_message_call
6566

6667
BLOCK_REWARD = U256(2 * 10**18)
6768
BASE_FEE_MAX_CHANGE_DENOMINATOR = Uint(8)
6869
ELASTICITY_MULTIPLIER = Uint(2)
69-
GAS_LIMIT_ADJUSTMENT_FACTOR = Uint(1024)
70-
GAS_LIMIT_MINIMUM = Uint(5000)
7170
MINIMUM_DIFFICULTY = Uint(131072)
7271
MAX_OMMER_DEPTH = Uint(6)
7372
BOMB_DELAY_BLOCKS = 10700000
@@ -849,12 +848,12 @@ def check_gas_limit(gas_limit: Uint, parent_gas_limit: Uint) -> bool:
849848
850849
The bounds of the gas limit, ``max_adjustment_delta``, is set as the
851850
quotient of the parent block's gas limit and the
852-
``GAS_LIMIT_ADJUSTMENT_FACTOR``. Therefore, if the gas limit that is
851+
``LIMIT_ADJUSTMENT_FACTOR``. Therefore, if the gas limit that is
853852
passed through as a parameter is greater than or equal to the *sum* of
854853
the parent's gas and the adjustment delta then the limit for gas is too
855854
high and fails this function's check. Similarly, if the limit is less
856855
than or equal to the *difference* of the parent's gas and the adjustment
857-
delta *or* the predefined ``GAS_LIMIT_MINIMUM`` then this function's
856+
delta *or* the predefined ``LIMIT_MINIMUM`` then this function's
858857
check fails because the gas limit doesn't allow for a sufficient or
859858
reasonable amount of gas to be used on a block.
860859
@@ -872,12 +871,12 @@ def check_gas_limit(gas_limit: Uint, parent_gas_limit: Uint) -> bool:
872871
True if gas limit constraints are satisfied, False otherwise.
873872
874873
"""
875-
max_adjustment_delta = parent_gas_limit // GAS_LIMIT_ADJUSTMENT_FACTOR
874+
max_adjustment_delta = parent_gas_limit // GasCosts.LIMIT_ADJUSTMENT_FACTOR
876875
if gas_limit >= parent_gas_limit + max_adjustment_delta:
877876
return False
878877
if gas_limit <= parent_gas_limit - max_adjustment_delta:
879878
return False
880-
if gas_limit < GAS_LIMIT_MINIMUM:
879+
if gas_limit < GasCosts.LIMIT_MINIMUM:
881880
return False
882881

883882
return True

src/ethereum/forks/arrow_glacier/transactions.py

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,6 @@
2323
from .exceptions import TransactionTypeError
2424
from .fork_types import Address
2525

26-
GAS_TX_BASE = Uint(21000)
27-
"""
28-
Base cost of a transaction in gas units. This is the minimum amount of gas
29-
required to execute a transaction.
30-
"""
31-
32-
GAS_TX_DATA_PER_NON_ZERO = Uint(16)
33-
"""
34-
Gas cost per non-zero byte in the transaction data.
35-
"""
36-
37-
GAS_TX_DATA_PER_ZERO = Uint(4)
38-
"""
39-
Gas cost per zero byte in the transaction data.
40-
"""
41-
42-
GAS_TX_CREATE = Uint(32000)
43-
"""
44-
Additional gas cost for creating a new contract.
45-
"""
46-
47-
GAS_TX_ACCESS_LIST_ADDRESS = Uint(2400)
48-
"""
49-
Gas cost for including an address in the access list of a transaction.
50-
"""
51-
52-
GAS_TX_ACCESS_LIST_STORAGE_KEY = Uint(1900)
53-
"""
54-
Gas cost for including a storage key in the access list of a transaction.
55-
"""
56-
5726

5827
@slotted_freezable
5928
@dataclass
@@ -370,35 +339,37 @@ def calculate_intrinsic_cost(tx: Transaction) -> Uint:
370339
for all operations to be implemented.
371340
372341
The intrinsic cost includes:
373-
1. Base cost (`GAS_TX_BASE`)
342+
1. Base cost (`TX_BASE`)
374343
2. Cost for data (zero and non-zero bytes)
375344
3. Cost for contract creation (if applicable)
376345
4. Cost for access list entries (if applicable)
377346
378347
This function takes a transaction as a parameter and returns the intrinsic
379348
gas cost of the transaction.
380349
"""
350+
from .vm.gas import GasCosts
351+
381352
num_zeros = Uint(tx.data.count(0))
382353
num_non_zeros = ulen(tx.data) - num_zeros
383354
data_cost = (
384-
num_zeros * GAS_TX_DATA_PER_ZERO
385-
+ num_non_zeros * GAS_TX_DATA_PER_NON_ZERO
355+
num_zeros * GasCosts.TX_DATA_PER_ZERO
356+
+ num_non_zeros * GasCosts.TX_DATA_PER_NON_ZERO
386357
)
387358

388359
if tx.to == Bytes0(b""):
389-
create_cost = GAS_TX_CREATE
360+
create_cost = GasCosts.TX_CREATE
390361
else:
391362
create_cost = Uint(0)
392363

393364
access_list_cost = Uint(0)
394365
if isinstance(tx, (AccessListTransaction, FeeMarketTransaction)):
395366
for access in tx.access_list:
396-
access_list_cost += GAS_TX_ACCESS_LIST_ADDRESS
367+
access_list_cost += GasCosts.TX_ACCESS_LIST_ADDRESS
397368
access_list_cost += (
398-
ulen(access.slots) * GAS_TX_ACCESS_LIST_STORAGE_KEY
369+
ulen(access.slots) * GasCosts.TX_ACCESS_LIST_STORAGE_KEY
399370
)
400371

401-
return GAS_TX_BASE + data_cost + create_cost + access_list_cost
372+
return GasCosts.TX_BASE + data_cost + create_cost + access_list_cost
402373

403374

404375
def recover_sender(chain_id: U64, tx: Transaction) -> Address:

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ class GasCosts:
7676
PRECOMPILE_ECPAIRING_BASE = Uint(45000)
7777
PRECOMPILE_ECPAIRING_PER_POINT = Uint(34000)
7878

79+
# Transactions
80+
TX_BASE = Uint(21000)
81+
TX_CREATE = Uint(32000)
82+
TX_DATA_PER_ZERO = Uint(4)
83+
TX_DATA_PER_NON_ZERO = Uint(16)
84+
TX_ACCESS_LIST_ADDRESS = Uint(2400)
85+
TX_ACCESS_LIST_STORAGE_KEY = Uint(1900)
86+
87+
# Block
88+
LIMIT_ADJUSTMENT_FACTOR = Uint(1024)
89+
LIMIT_MINIMUM = Uint(5000)
90+
7991
# Static Opcodes
8092
OPCODE_ADD = VERY_LOW
8193
OPCODE_SUB = VERY_LOW

0 commit comments

Comments
 (0)