-
-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor - move hevm utils to its own file, add workflow job
- Loading branch information
1 parent
fce5119
commit 813514a
Showing
5 changed files
with
124 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import subprocess | ||
|
||
from tests.venom_utils import parse_from_basic_block | ||
from vyper.ir.compile_ir import assembly_to_evm | ||
from vyper.venom import StoreExpansionPass, VenomCompiler | ||
from vyper.venom.analysis import IRAnalysesCache | ||
from vyper.venom.basicblock import IRInstruction, IRLiteral | ||
|
||
HAS_HEVM: bool = False | ||
|
||
|
||
def _prep_hevm(venom_source_code): | ||
ctx = parse_from_basic_block(venom_source_code) | ||
|
||
num_calldataloads = 0 | ||
num_return_values = 0 | ||
for fn in ctx.functions.values(): | ||
for bb in fn.get_basic_blocks(): | ||
for inst in bb.instructions: | ||
# transform `param` instructions into "symbolic" values for | ||
# hevm via calldataload | ||
if inst.opcode == "param": | ||
# hevm limit: 256 bytes of symbolic calldata | ||
assert num_calldataloads < 8 | ||
|
||
inst.opcode = "calldataload" | ||
inst.operands = [IRLiteral(num_calldataloads * 32)] | ||
num_calldataloads += 1 | ||
|
||
term = bb.instructions[-1] | ||
# test convention, terminate by `return`ing the variables | ||
# you want to check | ||
assert term.opcode == "return" | ||
num_return_values = 0 | ||
for op in term.operands: | ||
ptr = IRLiteral(num_return_values * 32) | ||
new_inst = IRInstruction("mstore", [op, ptr]) | ||
bb.insert_instruction(new_inst, index=-1) | ||
num_return_values += 1 | ||
|
||
# return 0, 32 * num_variables | ||
term.operands = [IRLiteral(num_return_values * 32), IRLiteral(0)] | ||
|
||
ac = IRAnalysesCache(fn) | ||
# requirement for venom_to_assembly | ||
StoreExpansionPass(ac, fn).run_pass() | ||
|
||
compiler = VenomCompiler([ctx]) | ||
return assembly_to_evm(compiler.generate_evm(no_optimize=True))[0].hex() | ||
|
||
|
||
def hevm_check(pre, post, verbose=False): | ||
global HAS_HEVM | ||
if not HAS_HEVM: | ||
return | ||
|
||
# perform hevm equivalence check | ||
if verbose: | ||
print("HEVM COMPARE.") | ||
print("BEFORE:", pre) | ||
print("OPTIMIZED:", post) | ||
bytecode1 = _prep_hevm(pre) | ||
bytecode2 = _prep_hevm(post) | ||
|
||
# debug: | ||
if verbose: | ||
print("RUN HEVM:") | ||
print(bytecode1) | ||
print(bytecode2) | ||
|
||
subp_args = ["hevm", "equivalence", "--code-a", bytecode1, "--code-b", bytecode2] | ||
|
||
if verbose: | ||
subprocess.check_call(subp_args) | ||
else: | ||
subprocess.check_output(subp_args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters