Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 105 additions & 40 deletions packages/testing/src/execution_testing/forks/forks/forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@
TransactionDataFloorCostCalculator,
TransactionIntrinsicCostCalculator,
)
from ..gas_costs import GasCosts
from ..gas_costs import (
GAS_HIGH,
GAS_LOW,
GAS_MID,
GAS_VERY_LOW,
GasCosts,
)
from .helpers import ceiling_division, fake_exponential

CURRENT_FILE = Path(realpath(__file__))
Expand Down Expand Up @@ -201,6 +207,39 @@ def gas_costs(
GAS_PRECOMPILE_BLS_PAIRING_PER_PAIR=0,
GAS_PRECOMPILE_P256VERIFY=0,
GAS_BLOCK_ACCESS_LIST_ITEM=0,
# Opcode specific gas constants for repricing
GAS_OPCODE_ADD=GAS_VERY_LOW,
GAS_OPCODE_SUB=GAS_VERY_LOW,
GAS_OPCODE_MUL=GAS_LOW,
GAS_OPCODE_DIV=GAS_LOW,
GAS_OPCODE_SDIV=GAS_LOW,
GAS_OPCODE_MOD=GAS_LOW,
GAS_OPCODE_SMOD=GAS_LOW,
GAS_OPCODE_ADDMOD=GAS_MID,
GAS_OPCODE_MULMOD=GAS_MID,
GAS_OPCODE_SIGNEXTEND=GAS_LOW,
GAS_OPCODE_LT=GAS_VERY_LOW,
GAS_OPCODE_GT=GAS_VERY_LOW,
GAS_OPCODE_SLT=GAS_VERY_LOW,
GAS_OPCODE_SGT=GAS_VERY_LOW,
GAS_OPCODE_EQ=GAS_VERY_LOW,
GAS_OPCODE_ISZERO=GAS_VERY_LOW,
GAS_OPCODE_AND=GAS_VERY_LOW,
GAS_OPCODE_OR=GAS_VERY_LOW,
GAS_OPCODE_XOR=GAS_VERY_LOW,
GAS_OPCODE_NOT=GAS_VERY_LOW,
GAS_OPCODE_BYTE=GAS_VERY_LOW,
GAS_OPCODE_JUMP=GAS_MID,
GAS_OPCODE_JUMPI=GAS_HIGH,
GAS_OPCODE_CALLDATALOAD=GAS_VERY_LOW,
GAS_OPCODE_CALLDATACOPY=GAS_VERY_LOW,
GAS_OPCODE_CODECOPY=GAS_VERY_LOW,
GAS_OPCODE_MLOAD=GAS_VERY_LOW,
GAS_OPCODE_MSTORE=GAS_VERY_LOW,
GAS_OPCODE_MSTORE8=GAS_VERY_LOW,
GAS_OPCODE_PUSH_N=GAS_VERY_LOW,
GAS_OPCODE_DUP_N=GAS_VERY_LOW,
GAS_OPCODE_SWAP_N=GAS_VERY_LOW,
)

@classmethod
Expand Down Expand Up @@ -338,33 +377,33 @@ def opcode_gas_map(
return {
# Stop and arithmetic operations
Opcodes.STOP: 0,
Opcodes.ADD: gas_costs.GAS_VERY_LOW,
Opcodes.MUL: gas_costs.GAS_LOW,
Opcodes.SUB: gas_costs.GAS_VERY_LOW,
Opcodes.DIV: gas_costs.GAS_LOW,
Opcodes.SDIV: gas_costs.GAS_LOW,
Opcodes.MOD: gas_costs.GAS_LOW,
Opcodes.SMOD: gas_costs.GAS_LOW,
Opcodes.ADDMOD: gas_costs.GAS_MID,
Opcodes.MULMOD: gas_costs.GAS_MID,
Opcodes.ADD: gas_costs.GAS_OPCODE_ADD,
Opcodes.MUL: gas_costs.GAS_OPCODE_MUL,
Opcodes.SUB: gas_costs.GAS_OPCODE_SUB,
Opcodes.DIV: gas_costs.GAS_OPCODE_DIV,
Opcodes.SDIV: gas_costs.GAS_OPCODE_SDIV,
Opcodes.MOD: gas_costs.GAS_OPCODE_MOD,
Opcodes.SMOD: gas_costs.GAS_OPCODE_SMOD,
Opcodes.ADDMOD: gas_costs.GAS_OPCODE_ADDMOD,
Opcodes.MULMOD: gas_costs.GAS_OPCODE_MULMOD,
Opcodes.EXP: lambda op: (
gas_costs.GAS_EXPONENTIATION
+ gas_costs.GAS_EXPONENTIATION_PER_BYTE
* ((op.metadata["exponent"].bit_length() + 7) // 8)
),
Opcodes.SIGNEXTEND: gas_costs.GAS_LOW,
Opcodes.SIGNEXTEND: gas_costs.GAS_OPCODE_SIGNEXTEND,
# Comparison & bitwise logic operations
Opcodes.LT: gas_costs.GAS_VERY_LOW,
Opcodes.GT: gas_costs.GAS_VERY_LOW,
Opcodes.SLT: gas_costs.GAS_VERY_LOW,
Opcodes.SGT: gas_costs.GAS_VERY_LOW,
Opcodes.EQ: gas_costs.GAS_VERY_LOW,
Opcodes.ISZERO: gas_costs.GAS_VERY_LOW,
Opcodes.AND: gas_costs.GAS_VERY_LOW,
Opcodes.OR: gas_costs.GAS_VERY_LOW,
Opcodes.XOR: gas_costs.GAS_VERY_LOW,
Opcodes.NOT: gas_costs.GAS_VERY_LOW,
Opcodes.BYTE: gas_costs.GAS_VERY_LOW,
Opcodes.LT: gas_costs.GAS_OPCODE_LT,
Opcodes.GT: gas_costs.GAS_OPCODE_GT,
Opcodes.SLT: gas_costs.GAS_OPCODE_SLT,
Opcodes.SGT: gas_costs.GAS_OPCODE_SGT,
Opcodes.EQ: gas_costs.GAS_OPCODE_EQ,
Opcodes.ISZERO: gas_costs.GAS_OPCODE_ISZERO,
Opcodes.AND: gas_costs.GAS_OPCODE_AND,
Opcodes.OR: gas_costs.GAS_OPCODE_OR,
Opcodes.XOR: gas_costs.GAS_OPCODE_XOR,
Opcodes.NOT: gas_costs.GAS_OPCODE_NOT,
Opcodes.BYTE: gas_costs.GAS_OPCODE_BYTE,
# SHA3
Opcodes.SHA3: cls._with_memory_expansion(
lambda op: (
Expand All @@ -380,15 +419,17 @@ def opcode_gas_map(
Opcodes.ORIGIN: gas_costs.GAS_BASE,
Opcodes.CALLER: gas_costs.GAS_BASE,
Opcodes.CALLVALUE: gas_costs.GAS_BASE,
Opcodes.CALLDATALOAD: gas_costs.GAS_VERY_LOW,
Opcodes.CALLDATALOAD: gas_costs.GAS_OPCODE_CALLDATALOAD,
Opcodes.CALLDATASIZE: gas_costs.GAS_BASE,
Opcodes.CALLDATACOPY: cls._with_memory_expansion(
cls._with_data_copy(gas_costs.GAS_VERY_LOW, gas_costs),
cls._with_data_copy(
gas_costs.GAS_OPCODE_CALLDATACOPY, gas_costs
),
memory_expansion_calculator,
),
Opcodes.CODESIZE: gas_costs.GAS_BASE,
Opcodes.CODECOPY: cls._with_memory_expansion(
cls._with_data_copy(gas_costs.GAS_VERY_LOW, gas_costs),
cls._with_data_copy(gas_costs.GAS_OPCODE_CODECOPY, gas_costs),
memory_expansion_calculator,
),
Opcodes.GASPRICE: gas_costs.GAS_BASE,
Expand All @@ -410,13 +451,16 @@ def opcode_gas_map(
# Stack, memory, storage and flow operations
Opcodes.POP: gas_costs.GAS_BASE,
Opcodes.MLOAD: cls._with_memory_expansion(
gas_costs.GAS_VERY_LOW, memory_expansion_calculator
gas_costs.GAS_OPCODE_MLOAD,
memory_expansion_calculator,
),
Opcodes.MSTORE: cls._with_memory_expansion(
gas_costs.GAS_VERY_LOW, memory_expansion_calculator
gas_costs.GAS_OPCODE_MSTORE,
memory_expansion_calculator,
),
Opcodes.MSTORE8: cls._with_memory_expansion(
gas_costs.GAS_VERY_LOW, memory_expansion_calculator
gas_costs.GAS_OPCODE_MSTORE8,
memory_expansion_calculator,
),
Opcodes.SLOAD: lambda op: (
gas_costs.GAS_WARM_SLOAD
Expand All @@ -426,25 +470,25 @@ def opcode_gas_map(
Opcodes.SSTORE: lambda op: cls._calculate_sstore_gas(
op, gas_costs
),
Opcodes.JUMP: gas_costs.GAS_MID,
Opcodes.JUMPI: gas_costs.GAS_HIGH,
Opcodes.JUMP: gas_costs.GAS_OPCODE_JUMP,
Opcodes.JUMPI: gas_costs.GAS_OPCODE_JUMPI,
Opcodes.PC: gas_costs.GAS_BASE,
Opcodes.MSIZE: gas_costs.GAS_BASE,
Opcodes.GAS: gas_costs.GAS_BASE,
Opcodes.JUMPDEST: gas_costs.GAS_JUMPDEST,
# Push operations (PUSH1 through PUSH32)
**{
getattr(Opcodes, f"PUSH{i}"): gas_costs.GAS_VERY_LOW
getattr(Opcodes, f"PUSH{i}"): gas_costs.GAS_OPCODE_PUSH_N
for i in range(1, 33)
},
# Dup operations (DUP1 through DUP16)
**{
getattr(Opcodes, f"DUP{i}"): gas_costs.GAS_VERY_LOW
getattr(Opcodes, f"DUP{i}"): gas_costs.GAS_OPCODE_DUP_N
for i in range(1, 17)
},
# Swap operations (SWAP1 through SWAP16)
**{
getattr(Opcodes, f"SWAP{i}"): gas_costs.GAS_VERY_LOW
getattr(Opcodes, f"SWAP{i}"): gas_costs.GAS_OPCODE_SWAP_N
for i in range(1, 17)
},
# Logging operations
Expand Down Expand Up @@ -1690,7 +1734,10 @@ def opcode_gas_map(
**base_map,
Opcodes.RETURNDATASIZE: gas_costs.GAS_BASE,
Opcodes.RETURNDATACOPY: cls._with_memory_expansion(
cls._with_data_copy(gas_costs.GAS_VERY_LOW, gas_costs),
cls._with_data_copy(
gas_costs.GAS_OPCODE_RETURNDATACOPY,
gas_costs,
),
memory_expansion_calculator,
),
Opcodes.STATICCALL: cls._with_memory_expansion(
Expand Down Expand Up @@ -1730,6 +1777,7 @@ def gas_costs(
GAS_PRECOMPILE_ECMUL=40_000,
GAS_PRECOMPILE_ECPAIRING_BASE=100_000,
GAS_PRECOMPILE_ECPAIRING_PER_POINT=80_000,
GAS_OPCODE_RETURNDATACOPY=GAS_VERY_LOW,
)


Expand Down Expand Up @@ -1768,6 +1816,20 @@ def create_opcodes(
block_number=block_number, timestamp=timestamp
)

@classmethod
def gas_costs(
cls, *, block_number: int = 0, timestamp: int = 0
) -> GasCosts:
"""At Constantinople, shift opcodes are introduced."""
return replace(
super(Constantinople, cls).gas_costs(
block_number=block_number, timestamp=timestamp
),
GAS_OPCODE_SHL=GAS_VERY_LOW,
GAS_OPCODE_SHR=GAS_VERY_LOW,
GAS_OPCODE_SAR=GAS_VERY_LOW,
)

@classmethod
def opcode_gas_map(
cls, *, block_number: int = 0, timestamp: int = 0
Expand All @@ -1784,9 +1846,9 @@ def opcode_gas_map(
)
return {
**base_map,
Opcodes.SHL: gas_costs.GAS_VERY_LOW,
Opcodes.SHR: gas_costs.GAS_VERY_LOW,
Opcodes.SAR: gas_costs.GAS_VERY_LOW,
Opcodes.SHL: gas_costs.GAS_OPCODE_SHL,
Opcodes.SHR: gas_costs.GAS_OPCODE_SHR,
Opcodes.SAR: gas_costs.GAS_OPCODE_SAR,
Opcodes.EXTCODEHASH: cls._with_account_access(0, gas_costs),
Opcodes.CREATE2: cls._with_memory_expansion(
lambda op: cls._calculate_create2_gas(op, gas_costs),
Expand Down Expand Up @@ -2652,6 +2714,8 @@ def gas_costs(
block_number=block_number, timestamp=timestamp
),
GAS_PRECOMPILE_POINT_EVALUATION=50_000,
GAS_OPCODE_BLOBHASH=GAS_VERY_LOW,
GAS_OPCODE_MCOPY=GAS_VERY_LOW,
)

@classmethod
Expand Down Expand Up @@ -2680,15 +2744,15 @@ def opcode_gas_map(
return {
**base_map,
# EIP-4844: Shard Blob Transactions
Opcodes.BLOBHASH: gas_costs.GAS_VERY_LOW,
Opcodes.BLOBHASH: gas_costs.GAS_OPCODE_BLOBHASH,
# EIP-7516: BLOBBASEFEE opcode
Opcodes.BLOBBASEFEE: gas_costs.GAS_BASE,
# EIP-1153: Transient storage opcodes
Opcodes.TLOAD: gas_costs.GAS_WARM_SLOAD,
Opcodes.TSTORE: gas_costs.GAS_WARM_SLOAD,
# EIP-5656: MCOPY - Memory copying instruction
Opcodes.MCOPY: cls._with_memory_expansion(
cls._with_data_copy(gas_costs.GAS_VERY_LOW, gas_costs),
cls._with_data_copy(gas_costs.GAS_OPCODE_MCOPY, gas_costs),
memory_expansion_calculator,
),
}
Expand Down Expand Up @@ -2781,6 +2845,7 @@ def gas_costs(
GAS_PRECOMPILE_BLS_G2MAP=23_800,
GAS_PRECOMPILE_BLS_PAIRING_BASE=37_700,
GAS_PRECOMPILE_BLS_PAIRING_PER_PAIR=32_600,
GAS_OPCODE_CLZ=GAS_LOW,
)

@classmethod
Expand Down Expand Up @@ -3150,7 +3215,7 @@ def opcode_gas_map(
)
return {
**base_map,
Opcodes.CLZ: gas_costs.GAS_LOW,
Opcodes.CLZ: gas_costs.GAS_OPCODE_CLZ,
}

@classmethod
Expand Down
49 changes: 49 additions & 0 deletions packages/testing/src/execution_testing/forks/gas_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from dataclasses import dataclass

# Common Gas Cost Tiers
GAS_VERY_LOW = 3
GAS_LOW = 5
GAS_MID = 8
GAS_HIGH = 10


@dataclass(kw_only=True, frozen=True)
class GasCosts:
Expand Down Expand Up @@ -91,3 +97,46 @@ class GasCosts:
REFUND_AUTH_PER_EXISTING_ACCOUNT: int

GAS_BLOCK_ACCESS_LIST_ITEM: int

# Opcode specific gas constants for repricing
GAS_OPCODE_ADD: int
GAS_OPCODE_SUB: int
GAS_OPCODE_MUL: int
GAS_OPCODE_DIV: int
GAS_OPCODE_SDIV: int
GAS_OPCODE_MOD: int
GAS_OPCODE_SMOD: int
GAS_OPCODE_ADDMOD: int
GAS_OPCODE_MULMOD: int
GAS_OPCODE_SIGNEXTEND: int
GAS_OPCODE_LT: int
GAS_OPCODE_GT: int
GAS_OPCODE_SLT: int
GAS_OPCODE_SGT: int
GAS_OPCODE_EQ: int
GAS_OPCODE_ISZERO: int
GAS_OPCODE_AND: int
GAS_OPCODE_OR: int
GAS_OPCODE_XOR: int
GAS_OPCODE_NOT: int
GAS_OPCODE_BYTE: int
GAS_OPCODE_JUMP: int
GAS_OPCODE_JUMPI: int
GAS_OPCODE_CALLDATALOAD: int
GAS_OPCODE_CALLDATACOPY: int
GAS_OPCODE_CODECOPY: int
GAS_OPCODE_MLOAD: int
GAS_OPCODE_MSTORE: int
GAS_OPCODE_MSTORE8: int
GAS_OPCODE_PUSH_N: int
GAS_OPCODE_DUP_N: int
GAS_OPCODE_SWAP_N: int

# Defined post-Frontier
GAS_OPCODE_SHL: int = 0
GAS_OPCODE_SHR: int = 0
GAS_OPCODE_SAR: int = 0
GAS_OPCODE_RETURNDATACOPY: int = 0
GAS_OPCODE_BLOBHASH: int = 0
GAS_OPCODE_MCOPY: int = 0
GAS_OPCODE_CLZ: int = 0
42 changes: 41 additions & 1 deletion src/ethereum/forks/amsterdam/vm/gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
GAS_COLD_ACCOUNT_ACCESS = Uint(2600)
GAS_WARM_ACCESS = Uint(100)
GAS_CODE_INIT_PER_WORD = Uint(2)
GAS_BLOBHASH_OPCODE = Uint(3)
GAS_BLOBHASH = Uint(3)
GAS_POINT_EVALUATION = Uint(50000)

GAS_PER_BLOB = U64(2**17)
Expand All @@ -87,6 +87,46 @@

GAS_BLOCK_ACCESS_LIST_ITEM = Uint(2000)

# Opcode specific vars used for repricing
GAS_OPCODE_ADD = GAS_VERY_LOW
GAS_OPCODE_SUB = GAS_VERY_LOW
GAS_OPCODE_MUL = GAS_LOW
GAS_OPCODE_DIV = GAS_LOW
GAS_OPCODE_SDIV = GAS_LOW
GAS_OPCODE_MOD = GAS_LOW
GAS_OPCODE_SMOD = GAS_LOW
GAS_OPCODE_ADDMOD = GAS_MID
GAS_OPCODE_MULMOD = GAS_MID
GAS_OPCODE_SIGNEXTEND = GAS_LOW
GAS_OPCODE_LT = GAS_VERY_LOW
GAS_OPCODE_GT = GAS_VERY_LOW
GAS_OPCODE_SLT = GAS_VERY_LOW
GAS_OPCODE_SGT = GAS_VERY_LOW
GAS_OPCODE_EQ = GAS_VERY_LOW
GAS_OPCODE_ISZERO = GAS_VERY_LOW
GAS_OPCODE_AND = GAS_VERY_LOW
GAS_OPCODE_OR = GAS_VERY_LOW
GAS_OPCODE_XOR = GAS_VERY_LOW
GAS_OPCODE_NOT = GAS_VERY_LOW
GAS_OPCODE_BYTE = GAS_VERY_LOW
GAS_OPCODE_SHL = GAS_VERY_LOW
GAS_OPCODE_SHR = GAS_VERY_LOW
GAS_OPCODE_SAR = GAS_VERY_LOW
GAS_OPCODE_CLZ = GAS_LOW
GAS_OPCODE_JUMP = GAS_MID
GAS_OPCODE_JUMPI = GAS_HIGH
GAS_OPCODE_CALLDATALOAD = GAS_VERY_LOW
GAS_OPCODE_CALLDATACOPY = GAS_VERY_LOW
GAS_OPCODE_CODECOPY = GAS_VERY_LOW
GAS_OPCODE_RETURNDATACOPY = GAS_VERY_LOW
GAS_OPCODE_MLOAD = GAS_VERY_LOW
GAS_OPCODE_MSTORE = GAS_VERY_LOW
GAS_OPCODE_MSTORE8 = GAS_VERY_LOW
GAS_OPCODE_MCOPY = GAS_VERY_LOW
GAS_OPCODE_PUSH_N = GAS_VERY_LOW
GAS_OPCODE_DUP_N = GAS_VERY_LOW
GAS_OPCODE_SWAP_N = GAS_VERY_LOW


@dataclass
class ExtendMemory:
Expand Down
Loading