Skip to content

Commit

Permalink
address some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Jan 14, 2025
1 parent 12e6874 commit d644de9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 93 deletions.
10 changes: 7 additions & 3 deletions src/ethereum_test_forks/forks/forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def fn(
assert authorization_list_or_count is None, (
f"Authorizations are not supported in {cls.name()}"
)

intrinsic_cost: int = gas_costs.G_TRANSACTION

if contract_creation:
Expand Down Expand Up @@ -619,9 +620,12 @@ def valid_opcodes(
cls,
) -> List[Opcodes]:
"""Return list of Opcodes that are valid to work on this fork."""
return [Opcodes.REVERT, Opcodes.RETURNDATASIZE, Opcodes.STATICCALL] + super(
Byzantium, cls
).valid_opcodes()
return [
Opcodes.REVERT,
Opcodes.RETURNDATASIZE,
Opcodes.RETURNDATACOPY,
Opcodes.STATICCALL,
] + super(Byzantium, cls).valid_opcodes()


class Constantinople(Byzantium):
Expand Down
16 changes: 8 additions & 8 deletions tests/frontier/scenarios/programs/invalid_opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def make_all_invalid_opcode_calls() -> Bytecode:

code = Bytecode(
sum(
[
Op.MSTORE(64, opcode)
+ Op.MSTORE(32, Op.CALL(100000, invalid_opcode_caller, 0, 64, 32, 0, 0))
+ Op.MSTORE(0, Op.ADD(Op.MLOAD(0), Op.MLOAD(32)))
for opcode_range in invalid_opcode_ranges
for opcode in opcode_range
],
start=Bytecode(),
Op.MSTORE(64, opcode)
+ Op.MSTORE(
32,
Op.CALL(gas=100000, address=invalid_opcode_caller, args_offset=64, args_size=32),
)
+ Op.MSTORE(0, Op.ADD(Op.MLOAD(0), Op.MLOAD(32)))
for opcode_range in invalid_opcode_ranges
for opcode in opcode_range
)
# If any of invalid instructions works, mstore[0] will be > 1
+ Op.MSTORE(0, Op.ADD(Op.MLOAD(0), 1))
Expand Down
150 changes: 70 additions & 80 deletions tests/frontier/scenarios/scenarios/call_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class AddressBalance:
keep_gas = 100000

"""Possible calls list to make as a first call"""
first_calls: List[Opcode] = []
first_call_opcodes: List[Opcode] = []

"""Possible calls list to make as a second call"""
second_calls: List[Opcode] = []
second_call_opcodes: List[Opcode] = []

"""Balance map that we put in different accounts"""
balance: AddressBalance
Expand All @@ -44,17 +44,13 @@ class AddressBalance:

def __init__(self, scenario_input: ScenarioGeneratorInput):
"""Define possible call combinations given the fork."""
self.first_calls = [
self.first_call_opcodes = [
callcode
for callcode, evm_type in scenario_input.fork.call_opcodes()
if evm_type == EVMCodeType.LEGACY
]
self.second_calls = [
callcode
for callcode, evm_type in scenario_input.fork.call_opcodes()
if evm_type == EVMCodeType.LEGACY
]
self.second_calls.append(Op.NOOP)
self.second_call_opcodes = self.first_call_opcodes[:]
self.second_call_opcodes.append(Op.NOOP)
self.scenario_input = scenario_input
self.balance = self.AddressBalance()

Expand All @@ -73,15 +69,17 @@ def generate(self) -> List[Scenario]:
"""
scenarios_list: List[Scenario] = []

for first_call in self.first_calls:
for second_call in self.second_calls:
for first_call in self.first_call_opcodes:
for second_call in self.second_call_opcodes:
if second_call == Op.NOOP:
self._generate_one_call_scenarios(first_call, scenarios_list)
scenarios_list.append(self._generate_one_call_scenario(first_call))
else:
self._generate_two_call_scenarios(first_call, second_call, scenarios_list)
scenarios_list.append(
self._generate_two_call_scenario(first_call, second_call)
)
return scenarios_list

def _generate_one_call_scenarios(self, first_call: Opcode, scenarios_list: List[Scenario]):
def _generate_one_call_scenario(self, first_call: Opcode) -> Scenario:
"""
Generate scenario for only one call
root_contract -(CALL)-> scenario_contract -(first_call)-> operation_contract.
Expand Down Expand Up @@ -117,47 +115,43 @@ def _generate_one_call_scenarios(self, first_call: Opcode, scenarios_list: List[
balance=balance.root_contract_balance,
)

scenarios_list.append(
Scenario(
name=f"scenario_{first_call}",
code=root_contract,
env=ScenarioEnvironment(
# Define address on which behalf program is executed
code_address=(
scenario_contract
if first_call == Op.CALLCODE or first_call == Op.DELEGATECALL
else operation_contract
),
# Define code_caller for Op.CALLER
code_caller=(
root_contract if first_call == Op.DELEGATECALL else scenario_contract
),
# Define balance for Op.BALANCE
selfbalance=(
balance.scenario_contract_balance
if first_call in [Op.DELEGATECALL, Op.CALLCODE]
else (
balance.program_selfbalance
if first_call == Op.STATICCALL
else balance.first_call_value + balance.program_selfbalance
)
),
ext_balance=scenario_input.external_balance,
call_value=(
0
if first_call in [Op.STATICCALL, Op.DELEGATECALL]
else balance.first_call_value
),
call_dataload_0=int(scenario_input.external_address.hex(), 16),
call_datasize=40,
has_static=True if first_call == Op.STATICCALL else False,
return Scenario(
name=f"scenario_{first_call}",
code=root_contract,
env=ScenarioEnvironment(
# Define address on which behalf program is executed
code_address=(
scenario_contract
if first_call == Op.CALLCODE or first_call == Op.DELEGATECALL
else operation_contract
),
)
# Define code_caller for Op.CALLER
code_caller=(
root_contract if first_call == Op.DELEGATECALL else scenario_contract
),
# Define balance for Op.BALANCE
selfbalance=(
balance.scenario_contract_balance
if first_call in [Op.DELEGATECALL, Op.CALLCODE]
else (
balance.program_selfbalance
if first_call == Op.STATICCALL
else balance.first_call_value + balance.program_selfbalance
)
),
ext_balance=scenario_input.external_balance,
call_value=(
0
if first_call in [Op.STATICCALL, Op.DELEGATECALL]
else balance.first_call_value
),
call_dataload_0=int(scenario_input.external_address.hex(), 16),
call_datasize=40,
has_static=True if first_call == Op.STATICCALL else False,
),
)

def _generate_two_call_scenarios(
self, first_call: Opcode, second_call: Opcode, scenarios_list: List[Scenario]
):
def _generate_two_call_scenario(self, first_call: Opcode, second_call: Opcode) -> Scenario:
"""
Generate scenario for two types of calls combination
root_contract -(CALL)-> scenario_contract -(first_call)-> sub_contract
Expand Down Expand Up @@ -269,33 +263,29 @@ def _compute_callvalue() -> int:
+ Op.RETURN(0, 32),
)

scenarios_list.append(
Scenario(
name=f"scenario_{first_call}_{second_call}",
code=root_contract,
env=ScenarioEnvironment(
# Define address on which behalf program is executed
code_address=(
operation_contract
if second_call not in [Op.CALLCODE, Op.DELEGATECALL]
else (
sub_contract
if first_call not in [Op.CALLCODE, Op.DELEGATECALL]
else scenario_contract
)
),
# Define code_caller for Op.CALLER
code_caller=_compute_code_caller(),
selfbalance=_compute_selfbalance(),
ext_balance=scenario_input.external_balance,
call_value=_compute_callvalue(),
call_dataload_0=int(scenario_input.external_address.hex(), 16),
call_datasize=40,
has_static=(
True
if first_call == Op.STATICCALL or second_call == Op.STATICCALL
else False
),
return Scenario(
name=f"scenario_{first_call}_{second_call}",
code=root_contract,
env=ScenarioEnvironment(
# Define address on which behalf program is executed
code_address=(
operation_contract
if second_call not in [Op.CALLCODE, Op.DELEGATECALL]
else (
sub_contract
if first_call not in [Op.CALLCODE, Op.DELEGATECALL]
else scenario_contract
)
),
)
# Define code_caller for Op.CALLER
code_caller=_compute_code_caller(),
selfbalance=_compute_selfbalance(),
ext_balance=scenario_input.external_balance,
call_value=_compute_callvalue(),
call_dataload_0=int(scenario_input.external_address.hex(), 16),
call_datasize=40,
has_static=(
True if first_call == Op.STATICCALL or second_call == Op.STATICCALL else False
),
),
)
4 changes: 2 additions & 2 deletions tests/frontier/scenarios/test_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def scenarios(fork: Fork, pre: Alloc, operation: Bytecode, debug: ScenarioDebug)
# program=None select all programs
# scenario_name="" select all scenarios
"debug",
[ScenarioDebug(None, scenario_name="")],
[ScenarioDebug(test_param=None, scenario_name="")],
ids=["debug"],
)
@pytest.mark.parametrize(
Expand Down Expand Up @@ -245,7 +245,7 @@ def make_result(scenario: Scenario) -> int:
gas_limit=500_000_000,
gas_price=tx_gasprice,
to=runner_contract,
data=b"0x11223344",
data=bytes.fromhex("11223344"),
value=0,
protected=False,
)
Expand Down

0 comments on commit d644de9

Please sign in to comment.