Skip to content

Commit

Permalink
add source map for deployable
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed May 8, 2024
1 parent 6d4c09c commit 8f7a4fc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
4 changes: 3 additions & 1 deletion vyper/cli/vyper_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
blueprint_bytecode - Deployment bytecode for an ERC-5202 compatible blueprint
abi - ABI in JSON format
abi_python - ABI in python format
source_map - Vyper source map
source_map - Vyper source map of deployable bytecode
source_map_runtime - Vyper source map of runtime bytecode
method_identifiers - Dictionary of method signature to method identifier
userdoc - Natspec user documentation
devdoc - Natspec developer documentation
Expand All @@ -48,6 +49,7 @@
"abi",
"layout",
"source_map",
"source_map_runtime",
"method_identifiers",
"userdoc",
"devdoc",
Expand Down
24 changes: 15 additions & 9 deletions vyper/cli/vyper_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
"evm.methodIdentifiers": "method_identifiers",
"evm.bytecode.object": "bytecode",
"evm.bytecode.opcodes": "opcodes",
"evm.bytecode.sourceMap": "source_map",
"evm.bytecode.sourceMapFull": "source_map_full",
"evm.deployedBytecode.object": "bytecode_runtime",
"evm.deployedBytecode.opcodes": "opcodes_runtime",
"evm.deployedBytecode.sourceMap": "source_map",
"evm.deployedBytecode.sourceMapFull": "source_map_full",
"evm.deployedBytecode.sourceMap": "source_map_runtime",
"evm.deployedBytecode.sourceMapFull": "source_map_full_runtime",
"interface": "interface",
"ir": "ir_dict",
"ir_runtime": "ir_runtime_dict",
Expand Down Expand Up @@ -341,24 +343,28 @@ def format_to_output_dict(compiler_data: dict) -> dict:
output_contracts["evm"] = {"methodIdentifiers": data["method_identifiers"]}

evm_keys = ("bytecode", "opcodes")
if any(i in data for i in evm_keys):
pc_maps_keys = ("source_map", "source_map_full")
if any(i in data for i in evm_keys) or any(i in data for i in pc_maps_keys):
evm = output_contracts.setdefault("evm", {}).setdefault("bytecode", {})
if "bytecode" in data:
evm["object"] = data["bytecode"]
if "opcodes" in data:
evm["opcodes"] = data["opcodes"]
if "source_map" in data:
evm["sourceMap"] = data["source_map"]["pc_pos_map_compressed"]
if "source_map_full" in data:
evm["sourceMapFull"] = data["source_map_full"]

pc_maps_keys = ("source_map", "source_map_full")
if any(i + "_runtime" in data for i in evm_keys) or any(i in data for i in pc_maps_keys):
if any(i + "_runtime" in data for i in evm_keys):
evm = output_contracts.setdefault("evm", {}).setdefault("deployedBytecode", {})
if "bytecode_runtime" in data:
evm["object"] = data["bytecode_runtime"]
if "opcodes_runtime" in data:
evm["opcodes"] = data["opcodes_runtime"]
if "source_map" in data:
evm["sourceMap"] = data["source_map"]["pc_pos_map_compressed"]
if "source_map_full" in data:
evm["sourceMapFull"] = data["source_map_full"]
if "source_map_runtime" in data:
evm["sourceMap"] = data["source_map_runtime"]["pc_pos_map_compressed"]
if "source_map_full_runtime" in data:
evm["sourceMapFull"] = data["source_map_full_runtime"]

return output_dict

Expand Down
2 changes: 2 additions & 0 deletions vyper/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"asm": output.build_asm_output,
"source_map": output.build_source_map_output,
"source_map_full": output.build_source_map_output,
"source_map_runtime": output.build_source_map_runtime_output,
"source_map_full_runtime": output.build_source_map_runtime_output,
# requires bytecode
"bytecode": output.build_bytecode_output,
"bytecode_runtime": output.build_bytecode_runtime_output,
Expand Down
19 changes: 15 additions & 4 deletions vyper/compiler/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,13 @@ def _build_node_identifier(ast_node):
return (ast_node.module_node.source_id, ast_node.node_id)


def build_source_map_output(compiler_data: CompilerData) -> dict:
def _build_source_map_output(compiler_data, bytecode, pc_maps):
"""
Generate source map output in various formats. Note that integrations
are encouraged to use pc_ast_map since the information it provides is
a superset of the other formats, and the other types are included
for legacy reasons.
"""
bytecode, pc_maps = compile_ir.assembly_to_evm(
compiler_data.assembly_runtime, insert_compiler_metadata=False
)
# sort the pc maps alphabetically
# CMC 2024-03-09 is this really necessary?
out = {}
Expand All @@ -280,6 +277,20 @@ def build_source_map_output(compiler_data: CompilerData) -> dict:
return out


def build_source_map_output(compiler_data: CompilerData) -> dict:
bytecode, pc_maps = compile_ir.assembly_to_evm(
compiler_data.assembly, insert_compiler_metadata=False
)
return _build_source_map_output(compiler_data, bytecode, pc_maps)


def build_source_map_runtime_output(compiler_data: CompilerData) -> dict:
bytecode, pc_maps = compile_ir.assembly_to_evm(
compiler_data.assembly_runtime, insert_compiler_metadata=False
)
return _build_source_map_output(compiler_data, bytecode, pc_maps)


# generate a solidity-style source map. this functionality is deprecated
# in favor of pc_ast_map, and may not be maintained to the same level
# as pc_ast_map.
Expand Down

0 comments on commit 8f7a4fc

Please sign in to comment.