Skip to content
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

feat(test): test opcode programs in different scenarios #808

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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
"""
172 changes: 172 additions & 0 deletions tests/frontier/scenarios/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
"""
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, Hash


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
BLOCKHASH_0 = 10
COINBASE = 11
TIMESTAMP = 12
NUMBER = 13
DIFFICULTY_RANDAO = 14
GASLIMIT = 15
BASEFEE = 16
BLOBHASH_0 = 17
BLOBBASEFEE = 18


@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
coinbase: Address
blockhash_0: Hash
timestamp: int
number: int
difficulty_randao: int
gaslimit: int
basefee: int
blobhash_0: Hash
blob_basefee: int


@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:
winsvega marked this conversation as resolved.
Show resolved Hide resolved
"""
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.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
if res.result == ScenarioExpectOpcode.BLOCKHASH_0:
return int(exec_env.blockhash_0.hex(), 16)
if res.result == ScenarioExpectOpcode.COINBASE:
return int(exec_env.coinbase.hex(), 16)
if res.result == ScenarioExpectOpcode.TIMESTAMP:
return exec_env.timestamp
if res.result == ScenarioExpectOpcode.NUMBER:
return exec_env.number
if res.result == ScenarioExpectOpcode.DIFFICULTY_RANDAO:
return exec_env.difficulty_randao
if res.result == ScenarioExpectOpcode.GASLIMIT:
return exec_env.gaslimit
if res.result == ScenarioExpectOpcode.SELFBALANCE:
return int(env.selfbalance)
if res.result == ScenarioExpectOpcode.BASEFEE:
return exec_env.basefee
if res.result == ScenarioExpectOpcode.BLOBHASH_0:
return int(exec_env.blobhash_0.hex(), 16)
if res.result == ScenarioExpectOpcode.BLOBBASEFEE:
return exec_env.blob_basefee

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
Loading