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

fix(cli): RJUMPV in evm_bytes_to_python #683

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
11 changes: 9 additions & 2 deletions src/cli/evm_bytes_to_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ def process_evm_bytes(evm_bytes_hex_string: Any) -> str: # noqa: D103
raise ValueError(f"Unknown opcode: {opcode_byte}")

if opcode.data_portion_length > 0:
data_portion = evm_bytes[: opcode.data_portion_length]
data_portion = hex(int.from_bytes(evm_bytes[: opcode.data_portion_length], "big"))
evm_bytes = evm_bytes[opcode.data_portion_length :]
opcodes_strings.append(f'Op.{opcode._name_}("0x{data_portion.hex()}")')
opcodes_strings.append(f"Op.{opcode._name_}[{data_portion}]")
elif opcode == Op.RJUMPV:
max_index = evm_bytes.pop(0)
operands: List[str] = []
for _ in range(max_index + 1):
operands.append(hex(int.from_bytes(evm_bytes[:2], "big")))
evm_bytes = evm_bytes[2:]
opcodes_strings.append(f"Op.{opcode._name_}[{','.join(operands)}]")
else:
opcodes_strings.append(f"Op.{opcode._name_}")

Expand Down
16 changes: 11 additions & 5 deletions src/cli/tests/test_evm_bytes_to_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

basic_vector = [
"0x60008080808061AAAA612d5ff1600055",
'Op.PUSH1("0x00") + Op.DUP1 + Op.DUP1 + Op.DUP1 + Op.DUP1 + Op.PUSH2("0xaaaa") + Op.PUSH2("0x2d5f") + Op.CALL + Op.PUSH1("0x00") + Op.SSTORE', # noqa: E501
"Op.PUSH1[0x0] + Op.DUP1 + Op.DUP1 + Op.DUP1 + Op.DUP1 + Op.PUSH2[0xaaaa] + Op.PUSH2[0x2d5f] + Op.CALL + Op.PUSH1[0x0] + Op.SSTORE", # noqa: E501
marioevz marked this conversation as resolved.
Show resolved Hide resolved
]
complex_vector = [
"0x7fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf5f527fc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf6020527fe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff60405260786040356020355f35608a565b5f515f55602051600155604051600255005b5e56", # noqa: E501
'Op.PUSH32("0xa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf") + Op.PUSH0 + Op.MSTORE + Op.PUSH32("0xc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf") + Op.PUSH1("0x20") + Op.MSTORE + Op.PUSH32("0xe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff") + Op.PUSH1("0x40") + Op.MSTORE + Op.PUSH1("0x78") + Op.PUSH1("0x40") + Op.CALLDATALOAD + Op.PUSH1("0x20") + Op.CALLDATALOAD + Op.PUSH0 + Op.CALLDATALOAD + Op.PUSH1("0x8a") + Op.JUMP + Op.JUMPDEST + Op.PUSH0 + Op.MLOAD + Op.PUSH0 + Op.SSTORE + Op.PUSH1("0x20") + Op.MLOAD + Op.PUSH1("0x01") + Op.SSTORE + Op.PUSH1("0x40") + Op.MLOAD + Op.PUSH1("0x02") + Op.SSTORE + Op.STOP + Op.JUMPDEST + Op.MCOPY + Op.JUMP', # noqa: E501
"Op.PUSH32[0xa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf] + Op.PUSH0 + Op.MSTORE + Op.PUSH32[0xc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf] + Op.PUSH1[0x20] + Op.MSTORE + Op.PUSH32[0xe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff] + Op.PUSH1[0x40] + Op.MSTORE + Op.PUSH1[0x78] + Op.PUSH1[0x40] + Op.CALLDATALOAD + Op.PUSH1[0x20] + Op.CALLDATALOAD + Op.PUSH0 + Op.CALLDATALOAD + Op.PUSH1[0x8a] + Op.JUMP + Op.JUMPDEST + Op.PUSH0 + Op.MLOAD + Op.PUSH0 + Op.SSTORE + Op.PUSH1[0x20] + Op.MLOAD + Op.PUSH1[0x1] + Op.SSTORE + Op.PUSH1[0x40] + Op.MLOAD + Op.PUSH1[0x2] + Op.SSTORE + Op.STOP + Op.JUMPDEST + Op.MCOPY + Op.JUMP", # noqa: E501
]


Expand Down Expand Up @@ -42,13 +42,18 @@ def test_evm_bytes_to_python(evm_bytes, python_opcodes):
)
def test_individual_opcodes(opcode):
"""Test each opcode individually"""
data_portion = b""
if opcode.data_portion_length > 0:
expected_output = f'Op.{opcode._name_}("0x")'
expected_output = f"Op.{opcode._name_}[0x0]"
data_portion = b"\x00" * opcode.data_portion_length
elif opcode == Op.RJUMPV:
expected_output = f"Op.{opcode._name_}[0x0]"
data_portion = b"\0\0\0"
else:
expected_output = f"Op.{opcode._name_}"

bytecode = opcode.int().to_bytes(1, byteorder="big").hex()
assert process_evm_bytes("0x" + bytecode) == expected_output
bytecode = opcode.int().to_bytes(1, byteorder="big") + data_portion
assert process_evm_bytes("0x" + bytecode.hex()) == expected_output


def test_invalid_opcode():
Expand All @@ -61,3 +66,4 @@ def test_unknown_opcode():
"""Opcode not defined in Op"""
with pytest.raises(ValueError):
process_evm_bytes("0x0F")
process_evm_bytes("0x0F")