def __init__(self): self.result = { "hash32": test_block_hash.lower(), "address": test_address.lower(), "hexaddress": test_address.lower(), "checksumaddress": test_address, "hexstring": test_hexstring.lower(), "data": test_hexstring.lower(), "blocknumber": hex(56333), "integer": hex(9000), "data_list": test_data_list, "anyaddress": test_any_address.lower(), "blockid": test_block_num, } self.result_expect = { "hash32": to_bytes(None, self.result["hash32"]), "address": to_bytes(None, self.result["address"]), "hexaddress": to_normalized_address(self.result["hexaddress"]), "checksumaddress": to_checksum_address(self.result["checksumaddress"]), "anyaddress": to_bytes(None, self.result["anyaddress"]), "hexstring": to_text(None, self.result["hexstring"]), "data": to_text(None, self.result["data"]), "data_list": list(map(lambda z: to_text(z), self.result["data_list"])), "blocknumber": to_int(None, self.result["blocknumber"]), "integer": to_int(None, self.result["integer"]), "blockid": to_int(None, self.result["blockid"]), }
def parity_export_blocks_process(parity_binary, datadir, chain_config_file_path, parity_port): run_command = ( parity_binary, 'export', 'blocks', os.path.join(datadir, 'blocks_export.rlp'), '--base-path', datadir, '--no-ws', '--no-warp', '--chain', chain_config_file_path, '--jsonrpc-apis', 'all', '--jsonrpc-port', parity_port, '--fat-db', 'on', ) print(' '.join(run_command)) try: proc = common.get_process(run_command) yield proc finally: time.sleep(10) common.kill_proc_gracefully(proc) output, errors = proc.communicate() print("Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def geth_process(geth_binary, datadir, genesis_file, geth_command_arguments): init_datadir_command = ( geth_binary, '--datadir', str(datadir), 'init', str(genesis_file), ) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) proc = subprocess.Popen( geth_command_arguments, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print("Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def parity_export_blocks_process( parity_binary, datadir, chain_config_file_path, parity_port): run_command = ( parity_binary, 'export', 'blocks', os.path.join(datadir, 'blocks_export.rlp'), '--base-path', datadir, '--no-ws', '--no-ui', '--no-warp', '--chain', chain_config_file_path, '--rpcapi', 'all', '--rpcport', parity_port, # '--author', common.COINBASE[2:], ) print(' '.join(run_command)) try: proc = common.get_process(run_command) yield proc finally: common.kill_proc_gracefully(proc) output, errors = proc.communicate() print( "Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ) )
def geth_process(geth_binary, datadir, genesis_file, geth_command_arguments): init_datadir_command = ( geth_binary, '--datadir', str(datadir), 'init', str(genesis_file), ) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) proc = subprocess.Popen( geth_command_arguments, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print( "Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ) )
def get_parity_process(parity_binary, datadir, ipc_path, keys_path, chain_config_file_path, parity_port): run_command = ( parity_binary, '--base-path', datadir, '--ipc-path', ipc_path, '--no-ws', '--no-warp', '--chain', chain_config_file_path, '--keys-path', keys_path, '--jsonrpc-apis', 'all', '--jsonrpc-port', parity_port, '--fat-db', 'on', ) print(' '.join(run_command)) try: proc = common.get_process(run_command) yield proc finally: common.kill_proc_gracefully(proc) output, errors = proc.communicate() print("Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def parity_export_blocks_process(parity_binary, datadir, chain_config_file_path, parity_port): run_command = ( parity_binary, 'export', 'blocks', os.path.join(datadir, 'blocks_export.rlp'), '--base-path', datadir, '--no-ws', '--no-ui', '--no-warp', '--chain', chain_config_file_path, '--rpcapi', 'all', '--rpcport', parity_port, # '--author', common.COINBASE[2:], ) print(' '.join(run_command)) try: proc = common.get_process(run_command) yield proc finally: common.kill_proc_gracefully(proc) output, errors = proc.communicate() print("Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def test_admin_setSolc(web3, skip_if_testrpc): skip_if_testrpc(web3) try: solc_path = subprocess.check_output(['which', 'solc']).strip() except subprocess.CalledProcessError: pytest.skip('solc binary not found') solc_version = subprocess.check_output(['solc', '--version']).strip() actual = web3.admin.setSolc(solc_path) assert to_text(solc_version) in actual assert to_text(solc_path) in actual
def test_admin_setSolc(webu, skip_if_testrpc): skip_if_testrpc(webu) try: solc_path = subprocess.check_output(['which', 'solc']).strip() except subprocess.CalledProcessError: pytest.skip('solc binary not found') solc_version = subprocess.check_output(['solc', '--version']).strip() actual = webu.admin.setSolc(solc_path) assert to_text(solc_version) in actual assert to_text(solc_path) in actual
def parse_obj(cls: Type[eth_models.T], data: Dict): data["hash32"] = to_bytes(hexstr=data["hash32"]) data["address"] = to_bytes(None, data["address"]) data["hexaddress"] = to_normalized_address(data["hexaddress"]) data["checksumaddress"] = data["checksumaddress"] data["anyaddress"] = to_bytes(None, data["anyaddress"]) data["hexstring"] = to_text(None, data["hexstring"]) data["data"] = to_text(None, data["data"]) data["data_list"] = list(map(lambda z: to_text(z), data["data_list"])) data["blocknumber"] = to_int(None, data["blocknumber"]) data["integer"] = to_int(None, data["integer"]) data["blockid"] = to_int(None, data["blockid"]) return cls(**data)
def get_ghuc_process(ghuc_binary, datadir, genesis_file_path, ipc_path, port, networkid, skip_init=False): if not skip_init: init_datadir_command = ( ghuc_binary, '--datadir', datadir, 'init', genesis_file_path, ) print(' '.join(init_datadir_command)) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_ghuc_command = ( ghuc_binary, '--datadir', datadir, '--ipcpath', ipc_path, '--nodiscover', '--port', port, '--networkid', networkid, '--coinbase', COINBASE[2:], ) print(' '.join(run_ghuc_command)) try: proc = get_process(run_ghuc_command) yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print("Ghuc Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def release_package(self, package_name: str, version: str, manifest_uri: str) -> bytes: """ Returns the release id generated by releasing a package on the current registry. Requires ``web3.PM`` to have a registry set. Requires ``web3.eth.defaultAccount`` to be the registry owner. * Parameters: * ``package_name``: Must be a valid package name, matching the given manifest. * ``version``: Must be a valid package version, matching the given manifest. * ``manifest_uri``: Must be a valid content-addressed URI. Currently, only IPFS and Github content-addressed URIs are supported. """ validate_is_supported_manifest_uri(manifest_uri) raw_manifest = to_text(resolve_uri_contents(manifest_uri)) validate_raw_manifest_format(raw_manifest) manifest = json.loads(raw_manifest) validate_manifest_against_schema(manifest) if package_name != manifest["package_name"]: raise ManifestValidationError( f"Provided package name: {package_name} does not match the package name " f"found in the manifest: {manifest['package_name']}.") if version != manifest["version"]: raise ManifestValidationError( f"Provided package version: {version} does not match the package version " f"found in the manifest: {manifest['version']}.") self._validate_set_registry() return self.registry._release(package_name, version, manifest_uri)
def is_encodable(_type, value): try: base, sub, arrlist = _type except ValueError: base, sub, arrlist = process_type(_type) if arrlist: if not is_list_like(value): return False if arrlist[-1] and len(value) != arrlist[-1][0]: return False sub_type = (base, sub, arrlist[:-1]) return all(is_encodable(sub_type, sub_value) for sub_value in value) elif base == 'address' and is_ens_name(value): # ENS names can be used anywhere an address is needed # Web3.py will resolve the name to an address before encoding it return True elif base == 'bytes' and isinstance(value, str): # Hex-encoded bytes values can be used anywhere a bytes value is needed if is_hex(value) and len(value) % 2 == 0: # Require hex-encoding of full bytes (even length) bytes_val = to_bytes(hexstr=value) return eth_abi_is_encodable(_type, bytes_val) else: return False elif base == 'string' and isinstance(value, bytes): # bytes that were encoded with utf-8 can be used anywhere a string is needed try: string_val = to_text(value) except UnicodeDecodeError: return False else: return eth_abi_is_encodable(_type, string_val) else: return eth_abi_is_encodable(_type, value)
def normalize_class_name(value): """ For `type()` calls: * Python 3.4 wants `str` * Python 3.5 doesn't care. """ return to_text(text=value)
def process_and_validate_raw_manifest(raw_manifest: bytes) -> Manifest: raw_manifest_text = to_text(raw_manifest).rstrip("\n") validate_raw_manifest_format(raw_manifest_text) manifest = json.loads(raw_manifest_text) validate_manifest_against_schema(manifest) validate_manifest_deployments(manifest) return manifest
def test_dummy_ipfs_backend(): pkg = DummyIPFSBackend().fetch_uri_contents( "ipfs://QmeD2s7KaBUoGYTP1eutHBmBkMMMoycdfiyGMx2DKrWXyV" ) mnfst = to_text(pkg) manifest = json.loads(mnfst) assert manifest["package_name"] == "safe-math-lib"
def process(self, request): with self._lock, self._socket as sock: try: sock.sendall(request) except BrokenPipeError: # one extra attempt, then give up sock = self._socket.reset() sock.sendall(request) raw_response = b'' with Timeout(self.ipc_timeout) as timeout: while True: try: raw_response += sock.recv(BUFSIZE) except socket.timeout: timeout.sleep(0) continue if raw_response == b"": timeout.sleep(0) elif has_valid_json_rpc_ending(raw_response): try: json.loads(to_text(raw_response)) except JSONDecodeError: timeout.sleep(0) continue else: return raw_response else: timeout.sleep(0) continue return response
def handle_new_block(blockhash, recursive_call=False, ommertext=None): if not ommertext: if not recursive_call: ommertext = '│' else: ommertext = '┝' block = w3.eth.getBlock(blockhash) blocknum = block['number'] extradata = block['extraData'] ommers = block['uncles'] parent = block['parentHash'] vote, safechars = detect_progpow_vote(extradata) try: extratext = to_text(extradata[:safechars]) except UnicodeDecodeError as err: extratext = '' # NOTE: handling (printing) ommers before printing non-ommer block info # (so that ommers are printed first) if not recursive_call: if len(ommers) > 0: # handle ommers of this block for ommer_blockhash in ommers: handle_new_block(ommer_blockhash, recursive_call=True) # also print (possibly re-print!..) parent handle_new_block(parent, recursive_call=True, ommertext='╞') print(blocknum, len(ommers), ommertext, to_hex(blockhash), vote, extratext) return
def coerce_type(solidity_type: str, value: bytes) -> Union[int, bool, str, ChecksumAddress, hex]: """ Converts input to the indicated type. Args: solidity_type (str): String representation of type. value (bytes): The value to be converted. Returns: (Union[int, bool, str, ChecksumAddress, hex]): The type representation of the value. """ if "int" in solidity_type: converted_value = to_int(value) elif "bool" in solidity_type: converted_value = bool(to_int(value)) elif "string" in solidity_type: # length * 2 is stored in lower end bits # TODO handle bytes and strings greater than 32 bytes length = int(int.from_bytes(value[-2:], "big") / 2) converted_value = to_text(value[:length]) elif "address" in solidity_type: converted_value = to_checksum_address(value) else: converted_value = value.hex() return converted_value
def get_manifest_from_content_addressed_uri(uri: str) -> Dict[str, Any]: """ Return manifest data stored at a content addressed URI. """ parse_result = parse.urlparse(uri) scheme = parse_result.scheme if scheme == IPFS_SCHEME: ipfs_backend = get_ipfs_backend() if ipfs_backend.can_handle_uri(uri): raw_manifest_data = ipfs_backend.fetch_uri_contents(uri) manifest_data = to_text(raw_manifest_data) return json.loads(manifest_data) else: raise TypeError( "The IPFS Backend: {0} cannot handle the given URI: {1}.".format( type(ipfs_backend).__name__, uri ) ) if scheme in INTERNET_SCHEMES: raise UriNotSupportedError("Internet URIs are not yet supported.") if scheme in SWARM_SCHEMES: raise UriNotSupportedError("Swarm URIs are not yet supported.") raise UriNotSupportedError("The URI scheme:{0} is not supported.".format(scheme))
def set_registry(self, address: Union[Address, ChecksumAddress, ENS]) -> None: """ Sets the current registry used in ``web3.pm`` functions that read/write to an on-chain registry. This method accepts checksummed/canonical addresses or ENS names. Addresses must point to an on-chain instance of an ERC1319 registry implementation. To use an ENS domain as the address, make sure a valid ENS instance set as ``web3.ens``. * Parameters: * ``address``: Address of on-chain Registry. """ if is_canonical_address(address): addr_string = to_text(address) self.registry = SimpleRegistry(to_checksum_address(addr_string), self.web3) elif is_checksum_address(address): self.registry = SimpleRegistry(cast(ChecksumAddress, address), self.web3) elif is_ens_name(address): self._validate_set_ens() addr_lookup = self.web3.ens.address(str(address)) if not addr_lookup: raise NameNotFound( f"No address found after ENS lookup for name: {address!r}." ) self.registry = SimpleRegistry(addr_lookup, self.web3) else: raise PMError( "Expected a canonical/checksummed address or ENS name for the address, " f"instead received {type(address)}." )
def is_encodable(_type, value): if not isinstance(_type, str): raise ValueError("is_encodable only accepts type strings") base, sub, arrlist = process_type(_type) if arrlist: if not is_list_like(value): return False if arrlist[-1] and len(value) != arrlist[-1][0]: return False sub_type = (base, sub, arrlist[:-1]) return all(is_encodable(collapse_type(*sub_type), sub_value) for sub_value in value) elif base == 'address' and is_ens_name(value): # ENS names can be used anywhere an address is needed # Web3.py will resolve the name to an address before encoding it return True elif base == 'bytes' and isinstance(value, str): # Hex-encoded bytes values can be used anywhere a bytes value is needed if is_hex(value) and len(value) % 2 == 0: # Require hex-encoding of full bytes (even length) bytes_val = to_bytes(hexstr=value) return eth_abi_is_encodable(_type, bytes_val) else: return False elif base == 'string' and isinstance(value, bytes): # bytes that were encoded with utf-8 can be used anywhere a string is needed try: string_val = to_text(value) except UnicodeDecodeError: return False else: return eth_abi_is_encodable(_type, string_val) else: return eth_abi_is_encodable(_type, value)
def get_geth_process(geth_binary, datadir, genesis_file_path, ipc_path, port, networkid, skip_init=False): if not skip_init: init_datadir_command = ( geth_binary, '--datadir', datadir, 'init', genesis_file_path, ) print(' '.join(init_datadir_command)) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_geth_command = ( geth_binary, '--datadir', datadir, '--ipcpath', ipc_path, '--nodiscover', '--port', port, '--networkid', networkid, '--etherbase', COINBASE[2:], ) print(' '.join(run_geth_command)) try: proc = get_process(run_geth_command) yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print( "Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ) )
def geth_process(geth_binary, datadir, genesis_file, geth_ipc_path): geth_port = get_open_port() init_datadir_command = ( geth_binary, '--datadir', str(datadir), 'init', str(genesis_file), ) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_geth_command = ( geth_binary, '--datadir', str(datadir), '--ipcpath', geth_ipc_path, '--nodiscover', '--fakepow', '--port', geth_port, ) proc = subprocess.Popen( run_geth_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print("Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def geth_process(geth_binary, datadir, genesis, ipc_file, open_port, keystore): init_datadir_command = ( geth_binary, '--datadir', str(datadir), 'init', str(genesis.path), ) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_geth_command = ( geth_binary, '--datadir', str(datadir), '--ipcpath', str(ipc_file), '--nodiscover', '--port', open_port, '--networkid', str(genesis.data['config']['chainId']), '--etherbase', remove_0x_prefix(tests.go_ethereum.common.COINBASE), '--gasprice', '0', # Gas price threshold for the cpu miner '--targetgaslimit', genesis.data['gasLimit']) print(' '.join(run_geth_command)) try: proc = get_process(run_geth_command) yield proc finally: tests.go_ethereum.common.kill_proc_gracefully(proc) output, errors = proc.communicate() print("Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def test_ipfs_and_infura_gateway_backends_fetch_uri_contents( base_uri, backend, safe_math_manifest ): uri = "ipfs://Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGV" assert backend.base_uri == base_uri with requests_mock.Mocker() as m: m.get(requests_mock.ANY, text=json.dumps(safe_math_manifest)) contents = backend.fetch_uri_contents(uri) contents_dict = json.loads(to_text(contents)) assert contents_dict["package_name"] == "safe-math-lib"
def resolve_sources( package: Package, ipfs_backend: BaseIPFSBackend) -> Iterable[Tuple[str, str]]: for path, source in package.manifest["sources"].items(): if is_ipfs_uri(source): contents = to_text( ipfs_backend.fetch_uri_contents(source)).rstrip("\n") else: # for inlined sources contents = source yield path, contents
def from_uri(cls, uri: str, w3: Web3) -> "Package": """ Return a Package object instantiated by a manifest located at a content-addressed URI. URI schemes supported: - IPFS `ipfs://Qm...` - HTTP `https://raw.githubusercontent.com/repo/path.json#hash` - Registry `ercXXX://registry.eth/greeter?version=1.0.0` """ contents = resolve_uri_contents(uri) manifest = json.loads(to_text(contents)) return cls(manifest, w3)
def to_string(value): '''Convert a value to a string''' if type(value) in (bytes, HexBytes): value = HexBytes(value).hex() value = str(value) if value.startswith("0x") and eth_utils.is_hex(value): try: return eth_utils.to_text(hexstr=value) except UnicodeDecodeError as e: raise ValueError(e) return value
def to_string(value: Any) -> str: """Convert a value to a string""" if isinstance(value, bytes): value = HexBytes(value).hex() value = str(value) if value.startswith("0x") and eth_utils.is_hex(value): try: return eth_utils.to_text(hexstr=value) except UnicodeDecodeError as e: raise ValueError(e) return value
def validate_value(cls, value): if is_bytes(value): try: value = to_text(value) except UnicodeDecodeError: cls.invalidate_value( value, msg='not decodable as unicode string', ) super().validate_value(value)
def get_process(command_list, terminates=False): proc = subprocess.Popen( command_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) if terminates: wait_for_popen(proc, 30) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print("Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def geth_process(geth_command_arguments): proc = subprocess.Popen( geth_command_arguments, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) logging.warning('start geth: %r' % (geth_command_arguments, )) try: yield proc finally: logging.warning('shutting down geth') kill_popen_gracefully( proc, logging.getLogger('tests.integration.lightchain')) output, errors = proc.communicate() logging.warning("Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ))
def decode_params_bytes(self): result: dict[str, Any] = {} for k, v in self.params.items(): if isinstance(v, bytes): try: str_value = eth_utils.to_text(v) except UnicodeDecodeError: str_value = eth_utils.to_hex(v) result[k] = "".join( filter(lambda x: x in string.printable, str_value)) else: result[k] = v return result
def to_list(self, decode_bytes=False) -> list[NameTypeValue]: result = [] for param in self.function_abi["inputs"]: name = param["name"] type_ = param["type"] value = self.params[name] if decode_bytes and isinstance(value, bytes): try: value = eth_utils.to_text(value) except UnicodeDecodeError: value = eth_utils.to_hex(value) result.append(NameTypeValue(name, type_, value)) return result
def get_process(command_list, terminates=False): proc = subprocess.Popen( command_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) if terminates: wait_for_popen(proc, 30) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print( "Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( to_text(output), to_text(errors), ) )
def decode_rpc_response(self, response): return json.loads(to_text(response))
def decode_rpc_response(self, response): text_response = to_text(response) return FriendlyJsonSerde().json_decode(text_response)