Skip to content

Commit

Permalink
cover all opcodes in different scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Oct 16, 2024
1 parent 10860cc commit b28f2ef
Show file tree
Hide file tree
Showing 11 changed files with 1,432 additions and 0 deletions.
116 changes: 116 additions & 0 deletions converted-ethereum-tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,119 @@
GeneralStateTests/stBadOpcode/opc0CDiffPlaces.json
GeneralStateTests/stBadOpcode/opc0DDiffPlaces.json
GeneralStateTests/stBadOpcode/opc0EDiffPlaces.json
GeneralStateTests/stBadOpcode/opc0FDiffPlaces.json
GeneralStateTests/stBadOpcode/opc1EDiffPlaces.json
GeneralStateTests/stBadOpcode/opc1FDiffPlaces.json
GeneralStateTests/stBadOpcode/opc2ADiffPlaces.json
GeneralStateTests/stBadOpcode/opc2BDiffPlaces.json
GeneralStateTests/stBadOpcode/opc2CDiffPlaces.json
GeneralStateTests/stBadOpcode/opc2DDiffPlaces.json
GeneralStateTests/stBadOpcode/opc2EDiffPlaces.json
GeneralStateTests/stBadOpcode/opc2FDiffPlaces.json
GeneralStateTests/stBadOpcode/opc4ADiffPlaces.json
GeneralStateTests/stBadOpcode/opc4BDiffPlaces.json
GeneralStateTests/stBadOpcode/opc4CDiffPlaces.json
GeneralStateTests/stBadOpcode/opc4DDiffPlaces.json
GeneralStateTests/stBadOpcode/opc4EDiffPlaces.json
GeneralStateTests/stBadOpcode/opc4FDiffPlaces.json
GeneralStateTests/stBadOpcode/opc5CDiffPlaces.json
GeneralStateTests/stBadOpcode/opc5DDiffPlaces.json
GeneralStateTests/stBadOpcode/opc5EDiffPlaces.json
GeneralStateTests/stBadOpcode/opc5FDiffPlaces.json
GeneralStateTests/stBadOpcode/opc21DiffPlaces.json
GeneralStateTests/stBadOpcode/opc22DiffPlaces.json
GeneralStateTests/stBadOpcode/opc23DiffPlaces.json
GeneralStateTests/stBadOpcode/opc24DiffPlaces.json
GeneralStateTests/stBadOpcode/opc25DiffPlaces.json
GeneralStateTests/stBadOpcode/opc26DiffPlaces.json
GeneralStateTests/stBadOpcode/opc27DiffPlaces.json
GeneralStateTests/stBadOpcode/opc28DiffPlaces.json
GeneralStateTests/stBadOpcode/opc29DiffPlaces.json
GeneralStateTests/stBadOpcode/opc49DiffPlaces.json
GeneralStateTests/stBadOpcode/opcA5DiffPlaces.json
GeneralStateTests/stBadOpcode/opcA6DiffPlaces.json
GeneralStateTests/stBadOpcode/opcA7DiffPlaces.json
GeneralStateTests/stBadOpcode/opcA8DiffPlaces.json
GeneralStateTests/stBadOpcode/opcA9DiffPlaces.json
GeneralStateTests/stBadOpcode/opcAADiffPlaces.json
GeneralStateTests/stBadOpcode/opcABDiffPlaces.json
GeneralStateTests/stBadOpcode/opcACDiffPlaces.json
GeneralStateTests/stBadOpcode/opcADDiffPlaces.json
GeneralStateTests/stBadOpcode/opcAEDiffPlaces.json
GeneralStateTests/stBadOpcode/opcAFDiffPlaces.json
GeneralStateTests/stBadOpcode/opcB0DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB1DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB2DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB3DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB4DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB5DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB6DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB7DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB8DiffPlaces.json
GeneralStateTests/stBadOpcode/opcB9DiffPlaces.json
GeneralStateTests/stBadOpcode/opcBADiffPlaces.json
GeneralStateTests/stBadOpcode/opcBBDiffPlaces.json
GeneralStateTests/stBadOpcode/opcBCDiffPlaces.json
GeneralStateTests/stBadOpcode/opcBDDiffPlaces.json
GeneralStateTests/stBadOpcode/opcBEDiffPlaces.json
GeneralStateTests/stBadOpcode/opcBFDiffPlaces.json
GeneralStateTests/stBadOpcode/opcC0DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC1DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC2DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC3DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC4DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC5DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC6DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC7DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC8DiffPlaces.json
GeneralStateTests/stBadOpcode/opcC9DiffPlaces.json
GeneralStateTests/stBadOpcode/opcCADiffPlaces.json
GeneralStateTests/stBadOpcode/opcCBDiffPlaces.json
GeneralStateTests/stBadOpcode/opcCCDiffPlaces.json
GeneralStateTests/stBadOpcode/opcCDDiffPlaces.json
GeneralStateTests/stBadOpcode/opcCEDiffPlaces.json
GeneralStateTests/stBadOpcode/opcCFDiffPlaces.json
GeneralStateTests/stBadOpcode/opcD0DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD1DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD2DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD3DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD4DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD5DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD6DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD7DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD8DiffPlaces.json
GeneralStateTests/stBadOpcode/opcD9DiffPlaces.json
GeneralStateTests/stBadOpcode/opcDADiffPlaces.json
GeneralStateTests/stBadOpcode/opcDBDiffPlaces.json
GeneralStateTests/stBadOpcode/opcDCDiffPlaces.json
GeneralStateTests/stBadOpcode/opcDDDiffPlaces.json
GeneralStateTests/stBadOpcode/opcDEDiffPlaces.json
GeneralStateTests/stBadOpcode/opcDFDiffPlaces.json
GeneralStateTests/stBadOpcode/opcE0DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE1DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE2DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE3DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE4DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE5DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE6DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE7DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE8DiffPlaces.json
GeneralStateTests/stBadOpcode/opcE9DiffPlaces.json
GeneralStateTests/stBadOpcode/opcEADiffPlaces.json
GeneralStateTests/stBadOpcode/opcEBDiffPlaces.json
GeneralStateTests/stBadOpcode/opcECDiffPlaces.json
GeneralStateTests/stBadOpcode/opcEDDiffPlaces.json
GeneralStateTests/stBadOpcode/opcEEDiffPlaces.json
GeneralStateTests/stBadOpcode/opcEFDiffPlaces.json
GeneralStateTests/stBadOpcode/opcF6DiffPlaces.json
GeneralStateTests/stBadOpcode/opcF7DiffPlaces.json
GeneralStateTests/stBadOpcode/opcF8DiffPlaces.json
GeneralStateTests/stBadOpcode/opcF9DiffPlaces.json
GeneralStateTests/stBadOpcode/opcFBDiffPlaces.json
GeneralStateTests/stBadOpcode/opcFCDiffPlaces.json
GeneralStateTests/stBadOpcode/opcFEDiffPlaces.json


([#748](https://github.com/ethereum/execution-spec-tests/pull/748))
GeneralStateTests/stBadOpcode/badOpcodes.json
GeneralStateTests/stBugs/evmBytecode.json
Expand Down
3 changes: 3 additions & 0 deletions tests/frontier/scenarios/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Scenarios common import
"""
136 changes: 136 additions & 0 deletions tests/frontier/scenarios/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"""
Define Scenario class for test_scenarios test
"""

from dataclasses import dataclass
from enum import Enum

from ethereum_test_forks import Fork, Frontier
from ethereum_test_tools import Address, Alloc, Bytecode


class ScenarioExpectOpcode(Enum):
"""Opcodes that are replaced to real values computed by the scenario"""

TX_ORIGIN = 1
CODE_ADDRESS = 2
CODE_CALLER = 3
SELFBALANCE = 4
BALANCE = 5
CALL_VALUE = 6
CALL_DATALOAD_0 = 7
CALL_DATASIZE = 8
GASPRICE = 9


@dataclass
class ProgramResult:
"""
Describe expected result of a program
Attributes:
result (int | ScenarioExpectOpcode): The result of the program
from_fork (Fork): The result is only valid from this fork (default: Frontier)
"""

result: int | ScenarioExpectOpcode

"""The result is only valid from this fork"""
from_fork: Fork = Frontier


@dataclass
class ScenarioEnvironment:
"""
Scenario evm environment
Each scenario must define an environment on which program is executed
This is so post state verification could check results of evm opcodes
"""

code_address: Address # Op.ADDRESS, address scope for program
code_caller: Address # Op.CALLER, caller of the program
selfbalance: int # Op.SELFBALANCE, balance of the environment of the program
ext_balance: int # Op.BALANCE(external) of fixed address deployed in state
call_value: int # Op.CALLVALUE of call that is done to the program
call_dataload_0: int # Op.CALLDATALOAD(0) expected result
call_datasize: int # Op.CALLDATASIZE expected result


@dataclass
class ExecutionEnvironment:
"""
Scenario execution environment which is determined by test
"""

fork: Fork
gasprice: int
origin: Address


@dataclass
class ScenarioGeneratorInput:
"""
Parameters for the scenario generator function
Attributes:
fork (Fork): Fork for which we ask to generate scenarios
pre (Alloc): Access to the state to be able to deploy contracts into pre
operation (Bytecode): Evm bytecode program that will be tested
external_address (Address): Static external address for ext opcodes
"""

fork: Fork
pre: Alloc
operation_code: Bytecode
external_address: Address
external_balance: int


@dataclass
class Scenario:
"""
Describe test scenario that will be run in test for each program
Attributes:
name (str): Scenario name for the test vector
code (Address): Address that is an entry point for scenario code
env (ScenarioEnvironment): Evm values for ScenarioExpectAddress map
reverting (bool): If scenario reverts program execution, making result 0 (default: False)
"""

name: str
code: Address
env: ScenarioEnvironment
reverting: bool = False


def translate_result(
res: ProgramResult, env: ScenarioEnvironment, exec_env: ExecutionEnvironment
) -> int:
"""
Translate expected program result code into concrete value,
given the scenario evm environment and test execution environment
"""
if exec_env.fork < res.from_fork:
return 0
if isinstance(res.result, ScenarioExpectOpcode):
if res.result == ScenarioExpectOpcode.TX_ORIGIN:
return int(exec_env.origin.hex(), 16)
if res.result == ScenarioExpectOpcode.CODE_ADDRESS:
return int(env.code_address.hex(), 16)
if res.result == ScenarioExpectOpcode.CODE_CALLER:
return int(env.code_caller.hex(), 16)
if res.result == ScenarioExpectOpcode.SELFBALANCE:
return int(env.selfbalance)
if res.result == ScenarioExpectOpcode.BALANCE:
return int(env.ext_balance)
if res.result == ScenarioExpectOpcode.CALL_VALUE:
return int(env.call_value)
if res.result == ScenarioExpectOpcode.CALL_DATALOAD_0:
return env.call_dataload_0
if res.result == ScenarioExpectOpcode.CALL_DATASIZE:
return env.call_datasize
if res.result == ScenarioExpectOpcode.GASPRICE:
return exec_env.gasprice

return res.result
3 changes: 3 additions & 0 deletions tests/frontier/scenarios/programs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Scenarios common import
"""
Loading

0 comments on commit b28f2ef

Please sign in to comment.