Skip to content

Commit

Permalink
Run black
Browse files Browse the repository at this point in the history
Fix file issue (ethereum.manticore)
  • Loading branch information
montyly committed Jun 27, 2019
1 parent 8c19b7d commit e453900
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
2 changes: 1 addition & 1 deletion manticore/ethereum/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def ethereum_main(args, logger):
tx_send_ether=not args.txnoether,
tx_account=args.txaccount,
tx_preconstrain=args.txpreconstrain,
crytic_compile_args=vars(args)
crytic_compile_args=vars(args),
)

if not args.no_testcases:
Expand Down
65 changes: 35 additions & 30 deletions manticore/ethereum/manticore.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from ..utils.helpers import PickleSerializer

logger = logging.getLogger(__name__)
logging.getLogger('CryticCompile').setLevel(logging.ERROR)
logging.getLogger("CryticCompile").setLevel(logging.ERROR)

cfg = config.get_group("evm")
cfg.add("defaultgas", 3000000, "Default gas value for ethereum transactions.")
Expand Down Expand Up @@ -197,11 +197,7 @@ def constrain(self, constraint):

@staticmethod
def compile(
source_code,
contract_name=None,
libraries=None,
runtime=False,
crytic_compile_args=dict()
source_code, contract_name=None, libraries=None, runtime=False, crytic_compile_args=dict()
):
""" Get initialization bytecode from a Solidity source code """
name, source_code, init_bytecode, runtime_bytecode, srcmap, srcmap_runtime, hashes, abi, warnings = ManticoreEVM._compile(
Expand Down Expand Up @@ -344,6 +340,7 @@ def _compile_through_crytic_compile(filename, contract_name, libraries, crytic_c
:return:
"""
try:

if crytic_compile_args:
crytic_compile = CryticCompile(filename, **crytic_compile_args)
else:
Expand All @@ -352,7 +349,8 @@ def _compile_through_crytic_compile(filename, contract_name, libraries, crytic_c
if not contract_name:
if len(crytic_compile.contracts_names_without_libraries) > 1:
raise EthereumError(
f"Solidity file must contain exactly one contract or you must use a `--contract` parameter to specify one. Contracts found: {', '.join(crytic_compile.contracts_names)}")
f"Solidity file must contain exactly one contract or you must use a `--contract` parameter to specify one. Contracts found: {', '.join(crytic_compile.contracts_names)}"
)
contract_name = list(crytic_compile.contracts_names_without_libraries)[0]

if contract_name not in crytic_compile.contracts_names:
Expand Down Expand Up @@ -381,10 +379,11 @@ def _compile_through_crytic_compile(filename, contract_name, libraries, crytic_c

except InvalidCompilation as e:
raise EthereumError(
f"Errors : {e}\n. Solidity failed to generate bytecode for your contract. Check if all the abstract functions are implemented. ")
f"Errors : {e}\n. Solidity failed to generate bytecode for your contract. Check if all the abstract functions are implemented. "
)

@staticmethod
def _compile(source_code, contract_name, libraries=None, crytic_compile_args=dict()):
def _compile(source_code, contract_name, libraries=None, crytic_compile_args=None):
""" Compile a Solidity contract, used internally
:param source_code: solidity source
Expand All @@ -397,24 +396,26 @@ def _compile(source_code, contract_name, libraries=None, crytic_compile_args=dic
:return: name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi, warnings
"""

crytic_compile_args = dict() if crytic_compile_args is None else crytic_compile_args

if isinstance(source_code, io.IOBase):
source_code = source_code.name

if isinstance(source_code, str) and not is_supported(source_code):
with tempfile.NamedTemporaryFile("w+", suffix=".sol") as temp:
temp.write(source_code)
temp.flush()
compilation_result = ManticoreEVM._compile_through_crytic_compile(temp.name,
contract_name,
libraries,
crytic_compile_args)
compilation_result = ManticoreEVM._compile_through_crytic_compile(
temp.name, contract_name, libraries, crytic_compile_args
)
else:
compilation_result = ManticoreEVM._compile_through_crytic_compile(source_code,
contract_name,
libraries,
crytic_compile_args)
compilation_result = ManticoreEVM._compile_through_crytic_compile(
source_code, contract_name, libraries, crytic_compile_args
)

name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi = compilation_result
name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi = (
compilation_result
)
warnings = ""

return name, source_code, bytecode, runtime, srcmap, srcmap_runtime, hashes, abi, warnings
Expand Down Expand Up @@ -626,7 +627,9 @@ def json_create_contract(
signature = SolidityMetadata.function_signature_for_name_and_inputs(
item["name"], item["inputs"]
)
hashes[signature] = int("0x" + sha3.keccak_256(signature.encode()).hexdigest()[:8], 16)
hashes[signature] = int(
"0x" + sha3.keccak_256(signature.encode()).hexdigest()[:8], 16
)
if "signature" in item:
if item["signature"] != f"0x{hashes[signature]}":
raise Exception(
Expand Down Expand Up @@ -694,7 +697,7 @@ def solidity_create_contract(
address=None,
args=(),
gas=None,
crytic_compile_args=dict()
crytic_compile_args=None,
):
""" Creates a solidity contract and library dependencies
Expand All @@ -715,6 +718,9 @@ def solidity_create_contract(
:type gas: int
:rtype: EVMAccount
"""

crytic_compile_args = dict() if crytic_compile_args is None else crytic_compile_args

if libraries is None:
deps = {}
else:
Expand All @@ -728,7 +734,7 @@ def solidity_create_contract(
source_code,
contract_name_i,
libraries=deps,
crytic_compile_args=crytic_compile_args
crytic_compile_args=crytic_compile_args,
)
md = SolidityMetadata(*compile_results)
if contract_name_i == contract_name:
Expand Down Expand Up @@ -1155,21 +1161,20 @@ def multi_tx_analysis(
tx_account="attacker",
tx_preconstrain=False,
args=None,
crytic_compile_args=dict()
crytic_compile_args=dict(),
):
owner_account = self.create_account(balance=1000, name="owner")
attacker_account = self.create_account(balance=1000, name="attacker")
# Pretty print
logger.info("Starting symbolic create contract")

with open(solidity_filename) as f:
contract_account = self.solidity_create_contract(
f,
contract_name=contract_name,
owner=owner_account,
args=args,
crytic_compile_args=crytic_compile_args,
)
contract_account = self.solidity_create_contract(
solidity_filename,
contract_name=contract_name,
owner=owner_account,
args=args,
crytic_compile_args=crytic_compile_args,
)

if tx_account == "attacker":
tx_account = [attacker_account]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def rtd_dependent_deps():
"pyevmasm==0.2.0",
"rlp",
"ply",
"crytic-compile>=0.1.1"
"crytic-compile>=0.1.1",
]
+ rtd_dependent_deps(),
extras_require=extra_require,
Expand Down
5 changes: 4 additions & 1 deletion tests/ethereum/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ def _test(self, name, should_find, use_ctor_sym_arg=False):

with self.mevm.kill_timeout(240):
mevm.multi_tx_analysis(
filepath, contract_name="DetectThis", args=ctor_arg, crytic_compile_args={'solc_working_dir': dir}
filepath,
contract_name="DetectThis",
args=ctor_arg,
crytic_compile_args={"solc_working_dir": dir},
)

expected_findings = set(((finding, at_init) for finding, at_init in should_find))
Expand Down

0 comments on commit e453900

Please sign in to comment.