-
Notifications
You must be signed in to change notification settings - Fork 390
feat(tests): unify gas cost variable #1776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
LouisTsai-Csie
wants to merge
4
commits into
ethereum:forks/osaka
Choose a base branch
from
LouisTsai-Csie:refactor/gas-cost
base: forks/osaka
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b26e5f2
refactor: unify gas cost variable name
LouisTsai-Csie 68b1733
refactor: update opcode gas constant source
LouisTsai-Csie d7d2fe1
refactor: update prague opcode gas constant source
LouisTsai-Csie 0f2fdd7
feat: implement fork dependent cost calculator
LouisTsai-Csie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,46 +115,45 @@ def gas_costs( | |
| """ | ||
| del block_number, timestamp | ||
| return GasCosts( | ||
| G_JUMPDEST=1, | ||
| G_BASE=2, | ||
| G_VERY_LOW=3, | ||
| G_LOW=5, | ||
| G_MID=8, | ||
| G_HIGH=10, | ||
| G_WARM_ACCOUNT_ACCESS=100, | ||
| G_COLD_ACCOUNT_ACCESS=2_600, | ||
| G_ACCESS_LIST_ADDRESS=2_400, | ||
| G_ACCESS_LIST_STORAGE=1_900, | ||
| G_WARM_SLOAD=100, | ||
| G_COLD_SLOAD=2_100, | ||
| G_STORAGE_SET=20_000, | ||
| G_STORAGE_RESET=2_900, | ||
| R_STORAGE_CLEAR=4_800, | ||
| G_SELF_DESTRUCT=5_000, | ||
| G_CREATE=32_000, | ||
| G_CODE_DEPOSIT_BYTE=200, | ||
| G_INITCODE_WORD=2, | ||
| G_CALL_VALUE=9_000, | ||
| G_CALL_STIPEND=2_300, | ||
| G_NEW_ACCOUNT=25_000, | ||
| G_EXP=10, | ||
| G_EXP_BYTE=50, | ||
| G_MEMORY=3, | ||
| GAS_JUMPDEST=1, | ||
| GAS_BASE=2, | ||
| GAS_VERY_LOW=3, | ||
| GAS_LOW=5, | ||
| GAS_MID=8, | ||
| GAS_HIGH=10, | ||
| GAS_WARM_ACCESS=100, | ||
| GAS_COLD_ACCOUNT_ACCESS=2_600, | ||
| TX_ACCESS_LIST_ADDRESS_COST=2_400, | ||
| TX_ACCESS_LIST_STORAGE_KEY_COST=1_900, | ||
| GAS_COLD_SLOAD=2_100, | ||
| GAS_STORAGE_SET=20_000, | ||
| GAS_STORAGE_UPDATE=2_900, | ||
| GAS_STORAGE_CLEAR_REFUND=4_800, | ||
| GAS_SELF_DESTRUCT=5_000, | ||
| GAS_CREATE=32_000, | ||
| GAS_CODE_DEPOSIT=200, | ||
| GAS_INIT_CODE_WORD_COST=2, | ||
| GAS_CALL_VALUE=9_000, | ||
| GAS_CALL_STIPEND=2_300, | ||
| GAS_NEW_ACCOUNT=25_000, | ||
| GAS_EXPONENTIATION=10, | ||
| GAS_EXPONENTIATION_PER_BYTE=50, | ||
| GAS_MEMORY=3, | ||
| G_TX_DATA_ZERO=4, | ||
| G_TX_DATA_NON_ZERO=68, | ||
| G_TX_DATA_STANDARD_TOKEN_COST=0, | ||
| G_TX_DATA_FLOOR_TOKEN_COST=0, | ||
| G_TRANSACTION=21_000, | ||
| G_TRANSACTION_CREATE=32_000, | ||
| G_LOG=375, | ||
| G_LOG_DATA=8, | ||
| G_LOG_TOPIC=375, | ||
| G_KECCAK_256=30, | ||
| G_KECCAK_256_WORD=6, | ||
| G_COPY=3, | ||
| G_BLOCKHASH=20, | ||
| G_AUTHORIZATION=0, | ||
| R_AUTHORIZATION_EXISTING_AUTHORITY=0, | ||
| STANDARD_CALLDATA_TOKEN_COST=0, | ||
| FLOOR_CALLDATA_COST=0, | ||
| TX_BASE_COST=21_000, | ||
| TX_CREATE_COST=32_000, | ||
| GAS_LOG=375, | ||
| GAS_LOG_DATA=8, | ||
| GAS_LOG_TOPIC=375, | ||
| GAS_KECCAK256=30, | ||
| GAS_KECCAK256_WORD=6, | ||
| GAS_COPY=3, | ||
| GAS_BLOCK_HASH=20, | ||
| PER_EMPTY_ACCOUNT_COST=0, | ||
| PER_AUTH_BASE_COST=0, | ||
| ) | ||
|
|
||
| @classmethod | ||
|
|
@@ -176,7 +175,7 @@ def fn(*, new_bytes: int, previous_bytes: int = 0) -> int: | |
| previous_words = ceiling_division(previous_bytes, 32) | ||
|
|
||
| def c(w: int) -> int: | ||
| return (gas_costs.G_MEMORY * w) + ((w * w) // 512) | ||
| return (gas_costs.GAS_MEMORY * w) + ((w * w) // 512) | ||
|
|
||
| return c(new_words) - c(previous_words) | ||
|
|
||
|
|
@@ -295,11 +294,12 @@ def fn( | |
| f"Authorizations are not supported in {cls.name()}" | ||
| ) | ||
|
|
||
| intrinsic_cost: int = gas_costs.G_TRANSACTION | ||
| intrinsic_cost: int = gas_costs.TX_BASE_COST | ||
|
|
||
| if contract_creation: | ||
| intrinsic_cost += gas_costs.G_INITCODE_WORD * ceiling_division( | ||
| len(Bytes(calldata)), 32 | ||
| intrinsic_cost += ( | ||
| gas_costs.GAS_INIT_CODE_WORD_COST | ||
| * ceiling_division(len(Bytes(calldata)), 32) | ||
| ) | ||
|
|
||
| return intrinsic_cost + calldata_gas_calculator(data=calldata) | ||
|
|
@@ -923,7 +923,7 @@ def fn( | |
| authorization_list_or_count=authorization_list_or_count, | ||
| ) | ||
| if contract_creation: | ||
| intrinsic_cost += gas_costs.G_TRANSACTION_CREATE | ||
| intrinsic_cost += gas_costs.TX_CREATE_COST | ||
| return intrinsic_cost | ||
|
|
||
| return fn | ||
|
|
@@ -1155,9 +1155,11 @@ def fn( | |
| ) | ||
| if access_list is not None: | ||
| for access in access_list: | ||
| intrinsic_cost += gas_costs.G_ACCESS_LIST_ADDRESS | ||
| intrinsic_cost += gas_costs.TX_ACCESS_LIST_ADDRESS_COST | ||
| for _ in access.storage_keys: | ||
| intrinsic_cost += gas_costs.G_ACCESS_LIST_STORAGE | ||
| intrinsic_cost += ( | ||
| gas_costs.TX_ACCESS_LIST_STORAGE_KEY_COST | ||
| ) | ||
| return intrinsic_cost | ||
|
|
||
| return fn | ||
|
|
@@ -1849,15 +1851,15 @@ def gas_costs( | |
| On Prague, the standard token cost and the floor token costs are | ||
| introduced due to EIP-7623. | ||
| """ | ||
| return replace( | ||
| super(Prague, cls).gas_costs( | ||
| block_number=block_number, timestamp=timestamp | ||
| ), | ||
| G_TX_DATA_STANDARD_TOKEN_COST=4, # https://eips.ethereum.org/EIPS/eip-7623 | ||
| G_TX_DATA_FLOOR_TOKEN_COST=10, | ||
| G_AUTHORIZATION=25_000, | ||
| R_AUTHORIZATION_EXISTING_AUTHORITY=12_500, | ||
| ) | ||
| from ethereum.forks.prague.vm import gas as g | ||
| from dataclasses import fields | ||
|
|
||
| kwargs = {} | ||
| for field in fields(GasCosts): | ||
| if hasattr(g, field.name): | ||
| kwargs[field.name] = int(getattr(g, field.name)) | ||
|
|
||
| return GasCosts(**kwargs) | ||
|
|
||
| @classmethod | ||
| def system_contracts( | ||
|
|
@@ -1919,8 +1921,8 @@ def fn(*, data: BytesConvertible, floor: bool = False) -> int: | |
| else: | ||
| tokens += 4 | ||
| if floor: | ||
| return tokens * gas_costs.G_TX_DATA_FLOOR_TOKEN_COST | ||
| return tokens * gas_costs.G_TX_DATA_STANDARD_TOKEN_COST | ||
| return tokens * gas_costs.FLOOR_CALLDATA_COST | ||
| return tokens * gas_costs.STANDARD_CALLDATA_TOKEN_COST | ||
|
|
||
| return fn | ||
|
|
||
|
|
@@ -1942,7 +1944,7 @@ def transaction_data_floor_cost_calculator( | |
| def fn(*, data: BytesConvertible) -> int: | ||
| return ( | ||
| calldata_gas_calculator(data=data, floor=True) | ||
| + gas_costs.G_TRANSACTION | ||
| + gas_costs.TX_BASE_COST | ||
| ) | ||
|
|
||
| return fn | ||
|
|
@@ -1987,7 +1989,8 @@ def fn( | |
| authorization_list_or_count | ||
| ) | ||
| intrinsic_cost += ( | ||
| authorization_list_or_count * gas_costs.G_AUTHORIZATION | ||
| authorization_list_or_count | ||
| * gas_costs.PER_EMPTY_ACCOUNT_COST | ||
| ) | ||
|
|
||
| if return_cost_deducted_prior_execution: | ||
|
|
@@ -2141,6 +2144,32 @@ def engine_forkchoice_updated_version( | |
| del block_number, timestamp | ||
| return 3 | ||
|
|
||
| @classmethod | ||
| def op_cost( | ||
| cls, opcode: Opcodes, *, block_number: int = 0, timestamp: int = 0 | ||
| ) -> int: | ||
| """ | ||
| Return the base gas cost for a given opcode at Prague fork. | ||
| """ | ||
| from ethereum.forks.prague.vm import gas as g | ||
|
|
||
| opcode_name = opcode.name | ||
|
|
||
| if opcode_name.startswith("PUSH") and opcode_name != "PUSH0": | ||
| gas_const_name = "G_PUSHx" | ||
| elif opcode_name.startswith("DUP") and len(opcode_name) > 3: | ||
| gas_const_name = "G_DUPx" | ||
| elif opcode_name.startswith("SWAP") and len(opcode_name) > 4: | ||
| gas_const_name = "G_SWAPx" | ||
| elif opcode_name.startswith("LOG") and len(opcode_name) > 3: | ||
| gas_const_name = "G_LOGx" | ||
| else: | ||
| gas_const_name = f"G_{opcode_name}" | ||
|
|
||
| if hasattr(g, gas_const_name): | ||
| return int(getattr(g, gas_const_name)) | ||
| return 0 | ||
|
|
||
|
|
||
| class Osaka(Prague, solc_name="cancun"): | ||
| """Osaka fork.""" | ||
|
|
@@ -2325,6 +2354,59 @@ def blob_base_cost( | |
| del block_number, timestamp | ||
| return 2**13 # EIP-7918 new parameter | ||
|
|
||
| @classmethod | ||
| def gas_costs( | ||
| cls, *, block_number: int = 0, timestamp: int = 0 | ||
| ) -> GasCosts: | ||
| """Return the gas costs for the fork.""" | ||
| from ethereum.forks.osaka.vm import gas as g | ||
| from dataclasses import fields | ||
|
|
||
| kwargs = {} | ||
| for field in fields(GasCosts): | ||
| if hasattr(g, field.name): | ||
| kwargs[field.name] = int(getattr(g, field.name)) | ||
|
|
||
| return GasCosts(**kwargs) | ||
|
|
||
| @classmethod | ||
| def op_cost( | ||
| cls, opcode: Opcodes, *, block_number: int = 0, timestamp: int = 0 | ||
| ) -> int: | ||
|
Comment on lines
+2372
to
+2375
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to define this |
||
| """ | ||
| Return the base gas cost for a given opcode at Osaka fork. | ||
|
|
||
| Args: | ||
| opcode: The opcode enum value | ||
| block_number: Block number context | ||
| timestamp: Timestamp context | ||
|
|
||
| Returns: | ||
| Base gas cost for the opcode | ||
|
|
||
| Example: | ||
| >>> Osaka.op_cost(Opcodes.ADD) | ||
| 3 | ||
| """ | ||
| from ethereum.forks.osaka.vm import gas as g | ||
|
|
||
| opcode_name = opcode.name | ||
|
|
||
| if opcode_name.startswith("PUSH") and opcode_name != "PUSH0": | ||
| gas_const_name = "G_PUSHx" | ||
| elif opcode_name.startswith("DUP") and len(opcode_name) > 3: | ||
| gas_const_name = "G_DUPx" | ||
| elif opcode_name.startswith("SWAP") and len(opcode_name) > 4: | ||
| gas_const_name = "G_SWAPx" | ||
| elif opcode_name.startswith("LOG") and len(opcode_name) > 3: | ||
| gas_const_name = "G_LOGx" | ||
| else: | ||
| gas_const_name = f"G_{opcode_name}" | ||
|
|
||
| if hasattr(g, gas_const_name): | ||
| return int(getattr(g, gas_const_name)) | ||
| return 0 | ||
|
|
||
|
|
||
| class BPO1(Osaka, bpo_fork=True): | ||
| """Mainnet BPO1 fork - Blob Parameter Only fork 1.""" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we return an error here if the field is not found rather than skipping it silently? Not sure if this is intended.