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

remove minification #384

Merged
merged 4 commits into from
Apr 4, 2020
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Brownie is a Python-based development and testing framework for smart contracts

## Features

* Full support for [Solidity](https://github.com/ethereum/solidity) (`>=0.4.22`) and [Vyper](https://github.com/vyperlang/vyper) (`0.1.0-b16`)
* Full support for [Solidity](https://github.com/ethereum/solidity) (`>=0.4.22`) and [Vyper](https://github.com/vyperlang/vyper) (`0.1.0-b17`)
* Contract testing via [`pytest`](https://github.com/pytest-dev/pytest), including trace-based coverage evaluation
* Property-based and stateful testing via [`hypothesis`](https://github.com/HypothesisWorks/hypothesis/tree/master/hypothesis-python)
* Powerful debugging tools, including python-style tracebacks and custom error strings
Expand Down
4 changes: 2 additions & 2 deletions brownie/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def _load_project_compiler_config(project_path: Optional[Path]) -> Dict:
if not project_path:
return CONFIG["compiler"]
compiler_data = _load_config(project_path.joinpath("brownie-config"))["compiler"]
for key in [i for i in ("evm_version", "minify_source") if i not in compiler_data]:
compiler_data[key] = compiler_data["solc"].pop(key)
if "evm_version" not in compiler_data:
compiler_data["evm_version"] = compiler_data["solc"].pop("evm_version")
return compiler_data


Expand Down
1 change: 0 additions & 1 deletion brownie/data/brownie-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pytest:
revert_traceback: true
compiler:
evm_version: null
minify_source: false
solc:
version: null
optimize: true
Expand Down
38 changes: 1 addition & 37 deletions brownie/project/build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3

from typing import Dict, ItemsView, List, Optional, Sequence, Tuple, Union
from typing import Dict, ItemsView, List, Optional, Tuple, Union

from .sources import Sources, highlight_source

Expand Down Expand Up @@ -42,8 +42,6 @@ def _add(self, build_json: Dict) -> None:
contract_name = build_json["contractName"]
if "0" in build_json["pcMap"]:
build_json["pcMap"] = dict((int(k), v) for k, v in build_json["pcMap"].items())
if build_json["compiler"]["minify_source"]:
build_json = self.expand_build_offsets(build_json)
self._build[contract_name] = build_json
self._generate_revert_map(build_json["pcMap"], build_json["language"])

Expand Down Expand Up @@ -110,40 +108,6 @@ def get_dependents(self, contract_name: str) -> List:
def _stem(self, contract_name: str) -> str:
return contract_name.replace(".json", "")

def expand_build_offsets(self, build_json: Dict) -> Dict:
"""Expands minified source offsets in a build json dict."""

offset_map: Dict = {}
name = build_json["contractName"]

# minification only happens to the target contract that was compiled,
# so we ignore any import sources
source_path = build_json["sourcePath"]

for value in (
v
for v in build_json["pcMap"].values()
if "offset" in v and "path" in v and v["path"] == source_path
):
value["offset"] = self._get_offset(offset_map, name, value["offset"])

for key in ("branches", "statements"):
if source_path not in build_json["coverageMap"][key]:
continue
coverage_map = build_json["coverageMap"][key][source_path]
for fn, value in coverage_map.items():
coverage_map[fn] = dict(
(k, self._get_offset(offset_map, name, v[:2]) + tuple(v[2:]))
for k, v in value.items()
)
return build_json

def _get_offset(self, offset_map: Dict, name: str, offset: Sequence[int]) -> Tuple:
offset = tuple(offset)
if offset not in offset_map:
offset_map[offset] = self._sources.expand_offset(name, offset)
return offset_map[offset]


def _get_dev_revert(pc: int) -> Optional[str]:
# Given the program counter from a stack trace that caused a transaction
Expand Down
28 changes: 9 additions & 19 deletions brownie/project/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
from copy import deepcopy
from hashlib import sha1
from pathlib import Path
from typing import Dict, Optional, Union

Expand Down Expand Up @@ -44,7 +45,6 @@ def compile_and_format(
optimize: bool = True,
runs: int = 200,
evm_version: int = None,
minify: bool = False,
silent: bool = True,
allow_paths: Optional[str] = None,
interface_sources: Optional[Dict[str, str]] = None,
Expand All @@ -57,7 +57,6 @@ def compile_and_format(
optimize: enable solc optimizer
runs: optimizer runs
evm_version: evm version to compile for
minify: minify source files
silent: verbose reporting
allow_paths: compiler allowed filesystem import path

Expand Down Expand Up @@ -91,7 +90,7 @@ def compile_and_format(
compiler_targets[solc_version] = list(solc_sources)

for version, path_list in compiler_targets.items():
compiler_data: Dict = {"minify_source": minify}
compiler_data: Dict = {}
if version == "vyper":
language = "Vyper"
compiler_data["version"] = str(vyper.get_version())
Expand All @@ -109,7 +108,7 @@ def compile_and_format(
to_compile = {k: v for k, v in contract_sources.items() if k in path_list}

input_json = generate_input_json(
to_compile, optimize, runs, evm_version, minify, language, interfaces
to_compile, optimize, runs, evm_version, language, interfaces
)
output_json = compile_from_input_json(input_json, silent, allow_paths)

Expand All @@ -126,7 +125,6 @@ def generate_input_json(
optimize: bool = True,
runs: int = 200,
evm_version: Union[int, str, None] = None,
minify: bool = False,
language: str = "Solidity",
interface_sources: Optional[Dict[str, str]] = None,
) -> Dict:
Expand All @@ -138,7 +136,6 @@ def generate_input_json(
optimize: enable solc optimizer
runs: optimizer runs
evm_version: evm version to compile for
minify: should source code be minified?
language: source language (Solidity or Vyper)

Returns: dict
Expand All @@ -158,13 +155,13 @@ def generate_input_json(
input_json["settings"]["evmVersion"] = evm_version
if language == "Solidity":
input_json["settings"]["optimizer"] = {"enabled": optimize, "runs": runs if optimize else 0}
input_json["sources"] = _sources_dict(contract_sources, minify, language)
input_json["sources"] = _sources_dict(contract_sources, language)

if interface_sources:
if language == "Solidity":
input_json["sources"].update(_sources_dict(interface_sources, False, language))
input_json["sources"].update(_sources_dict(interface_sources, language))
else:
input_json["interfaces"] = _sources_dict(interface_sources, False, language)
input_json["interfaces"] = _sources_dict(interface_sources, language)

return input_json

Expand Down Expand Up @@ -213,7 +210,6 @@ def generate_build_json(
if compiler_data is None:
compiler_data = {}
compiler_data["evm_version"] = input_json["settings"]["evmVersion"]
minified = compiler_data.get("minify_source", False)
build_json = {}
path_list = list(input_json["sources"])

Expand All @@ -235,12 +231,6 @@ def generate_build_json(

abi = output_json["contracts"][path_str][contract_name]["abi"]
output_evm = output_json["contracts"][path_str][contract_name]["evm"]
hash_ = sources.get_hash(
input_json["sources"][path_str]["content"],
contract_name,
minified,
input_json["language"],
)

if input_json["language"] == "Solidity":
contract_node = next(
Expand Down Expand Up @@ -275,7 +265,7 @@ def generate_build_json(
"deployedSourceMap": output_evm["deployedBytecode"]["sourceMap"],
"language": input_json["language"],
"opcodes": output_evm["deployedBytecode"]["opcodes"],
"sha1": hash_,
"sha1": sha1(input_json["sources"][path_str]["content"].encode()).hexdigest(),
"source": input_json["sources"][path_str]["content"],
"sourceMap": output_evm["bytecode"].get("sourceMap", ""),
"sourcePath": path_str,
Expand All @@ -294,13 +284,13 @@ def generate_build_json(
return build_json


def _sources_dict(original: Dict, minify: bool, language: str) -> Dict:
def _sources_dict(original: Dict, language: str) -> Dict:
result: Dict = {}
for key, value in original.items():
if Path(key).suffix == ".json":
if isinstance(value, str):
value = json.loads(value)
result[key] = {"abi": value}
else:
result[key] = {"content": sources.minify(value, language)[0] if minify else value}
result[key] = {"content": value}
return result
10 changes: 4 additions & 6 deletions brownie/project/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import sys
import zipfile
from hashlib import sha1
from io import BytesIO
from pathlib import Path
from typing import Any, Dict, Iterator, KeysView, List, Optional, Set, Tuple, Union
Expand All @@ -26,7 +27,7 @@
from brownie.project import compiler
from brownie.project.build import BUILD_KEYS, Build
from brownie.project.ethpm import get_deployment_addresses, get_manifest
from brownie.project.sources import Sources, get_hash, get_pragma_spec
from brownie.project.sources import Sources, get_pragma_spec
from brownie.utils import color

FOLDERS = [
Expand Down Expand Up @@ -72,7 +73,6 @@ def _compile(self, contract_sources: Dict, compiler_config: Dict, silent: bool)
optimize=compiler_config["solc"].get("optimize", None),
runs=compiler_config["solc"].get("runs", None),
evm_version=compiler_config["evm_version"],
minify=compiler_config["minify_source"],
silent=silent,
allow_paths=allow_paths,
interface_sources=self._sources.get_interface_sources(),
Expand Down Expand Up @@ -219,8 +219,7 @@ def _compare_build_json(self, contract_name: str) -> bool:
except KeyError:
return True
# compare source hashes
hash_ = get_hash(source, contract_name, config["minify_source"], build_json["language"])
if build_json["sha1"] != hash_:
if build_json["sha1"] != sha1(source.encode()).hexdigest():
return True
# compare compiler settings
if _compare_settings(config, build_json["compiler"]):
Expand Down Expand Up @@ -438,7 +437,6 @@ def from_ethpm(uri: str) -> "TempProject":
manifest = get_manifest(uri)
compiler_config = {
"evm_version": None,
"minify_source": False,
"solc": {"version": None, "optimize": True, "runs": 200},
}
project = TempProject(manifest["package_name"], manifest["sources"], compiler_config)
Expand Down Expand Up @@ -470,7 +468,7 @@ def compile_source(
"""Compiles the given source code string and returns a TempProject container with
the ContractContainer instances."""

compiler_config: Dict = {"evm_version": evm_version, "minify_source": False}
compiler_config: Dict = {"evm_version": evm_version}

if solc_version is not None or source.lstrip().startswith("pragma"):
compiler_config["solc"] = {"version": solc_version, "optimize": optimize, "runs": runs}
Expand Down
Loading