From e4539006b13619427d1d3bd983fea5b0873a81b0 Mon Sep 17 00:00:00 2001 From: Josselin Date: Thu, 27 Jun 2019 08:57:56 +0200 Subject: [PATCH] Run black Fix file issue (ethereum.manticore) --- manticore/ethereum/cli.py | 2 +- manticore/ethereum/manticore.py | 65 +++++++++++++++++--------------- setup.py | 2 +- tests/ethereum/test_detectors.py | 5 ++- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/manticore/ethereum/cli.py b/manticore/ethereum/cli.py index 7d7536ccf..eea4b7f45 100644 --- a/manticore/ethereum/cli.py +++ b/manticore/ethereum/cli.py @@ -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: diff --git a/manticore/ethereum/manticore.py b/manticore/ethereum/manticore.py index b9621eab9..1b12c2270 100644 --- a/manticore/ethereum/manticore.py +++ b/manticore/ethereum/manticore.py @@ -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.") @@ -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( @@ -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: @@ -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: @@ -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 @@ -397,6 +396,8 @@ 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 @@ -404,17 +405,17 @@ def _compile(source_code, contract_name, libraries=None, crytic_compile_args=dic 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 @@ -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( @@ -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 @@ -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: @@ -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: @@ -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] diff --git a/setup.py b/setup.py index b4f187c67..204658ee6 100644 --- a/setup.py +++ b/setup.py @@ -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, diff --git a/tests/ethereum/test_detectors.py b/tests/ethereum/test_detectors.py index 5707b9027..e0d11f779 100644 --- a/tests/ethereum/test_detectors.py +++ b/tests/ethereum/test_detectors.py @@ -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))