-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevm.py
executable file
·68 lines (56 loc) · 2.03 KB
/
evm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# EVM From Scratch
# Python template
#
# To work on EVM From Scratch in Python:
#
# - Install Python3: https://www.python.org/downloads/
# - Go to the `python` directory: `cd python`
# - Edit `evm.py` (this file!), see TODO below
# - Run `python3 evm.py` to run the tests
import json
import os
def evm(code):
pc = 0
success = True
stack = []
while pc < len(code):
op = code[pc]
pc += 1
# TODO: implement the EVM here!
return (success, stack)
def test():
script_dirname = os.path.dirname(os.path.abspath(__file__))
json_file = os.path.join(script_dirname, "..", "evm.json")
with open(json_file) as f:
data = json.load(f)
total = len(data)
for i, test in enumerate(data):
# Note: as the test cases get more complex, you'll need to modify this
# to pass down more arguments to the evm function
code = bytes.fromhex(test['code']['bin'])
(success, stack) = evm(code)
expected_stack = [int(x, 16) for x in test['expect']['stack']]
if stack != expected_stack or success != test['expect']['success']:
print(f"❌ Test #{i + 1}/{total} {test['name']}")
if stack != expected_stack:
print("Stack doesn't match")
print(" expected:", expected_stack)
print(" actual:", stack)
else:
print("Success doesn't match")
print(" expected:", test['expect']['success'])
print(" actual:", success)
print("")
print("Test code:")
print(test['code']['asm'])
print("")
print("Hint:", test['hint'])
print("")
print(f"Progress: {i}/{len(data)}")
print("")
break
else:
print(f"✓ Test #{i + 1}/{total} {test['name']}")
if __name__ == '__main__':
test()