def test_isConnected_disconnected(): """ Webu.isConnected() returns False when configured with a provider that's not connected to a node. """ webu = Webu(DisconnectedProvider()) assert webu.isConnected() is False
def test_auto_provider_none(): # init without provider succeeds, even when no provider available w3 = Webu() # non-node requests succeed w3.toHex(0) == '0x0' type(w3.providers[0]) == AutoProvider
def init_webu(providers=default): from webu import Webu if providers is default: w3 = Webu(ens=None) else: w3 = Webu(providers, ens=None) return customize_webu(w3)
def generate_go_happyuc_fixture(destination_dir): with contextlib.ExitStack() as stack: datadir = stack.enter_context(tempdir()) keystore_dir = os.path.join(datadir, 'keystore') ensure_path_exists(keystore_dir) keyfile_path = os.path.join(keystore_dir, KEYFILE_FILENAME) with open(keyfile_path, 'w') as keyfile: keyfile.write(KEYFILE_DATA) genesis_file_path = os.path.join(datadir, 'genesis.json') with open(genesis_file_path, 'w') as genesis_file: genesis_file.write(json.dumps(GENESIS_DATA)) ghuc_ipc_path_dir = stack.enter_context(tempdir()) ghuc_ipc_path = os.path.join(ghuc_ipc_path_dir, 'ghuc.ipc') ghuc_port = get_open_port() ghuc_binary = get_ghuc_binary() with get_ghuc_process(ghuc_binary=ghuc_binary, datadir=datadir, genesis_file_path=genesis_file_path, ghuc_ipc_path=ghuc_ipc_path, ghuc_port=ghuc_port): wait_for_socket(ghuc_ipc_path) webu = Webu(Webu.IPCProvider(ghuc_ipc_path)) chain_data = setup_chain_state(webu) # close ghuc by exiting context # must be closed before copying data dir verify_chain_state(webu, chain_data) # verify that chain state is still valid after closing # and re-opening ghuc with get_ghuc_process(ghuc_binary=ghuc_binary, datadir=datadir, genesis_file_path=genesis_file_path, ghuc_ipc_path=ghuc_ipc_path, ghuc_port=ghuc_port): wait_for_socket(ghuc_ipc_path) webu = Webu(Webu.IPCProvider(ghuc_ipc_path)) verify_chain_state(webu, chain_data) static_data = { 'raw_txn_account': RAW_TXN_ACCOUNT, 'keyfile_pw': KEYFILE_PW, } config = merge(chain_data, static_data) pprint.pprint(config) write_config_json(config, datadir) shutil.copytree(datadir, destination_dir)
def test_soliditySha3_ens(self, webu, types, values, expected): with ens_addresses( webu, { 'one.eth': "0x49EdDD3769c0712032808D86597B84ac5c2F5614", 'two.eth': "0xA6b759bBbf4B59D24acf7E06e79f3a5D104fdCE5", }): # when called as class method, any name lookup attempt will fail with pytest.raises(InvalidAddress): Webu.soliditySha3(types, values) # when called as instance method method, ens lookups can succeed actual = webu.soliditySha3(types, values) assert actual == expected
def test_labelhash(ens, label, expected_hash): if isinstance(expected_hash, type): with pytest.raises(expected_hash): ens.labelhash(label) else: labelhash = ens.labelhash(label) assert isinstance(labelhash, bytes) hash_hex = Webu.toHex(labelhash) assert hash_hex == expected_hash
def bytes32(val): if isinstance(val, int): result = Webu.toBytes(val) else: raise TypeError('val %r could not be converted to bytes') if len(result) < 32: return result.rjust(32, b'\0') else: return result
def test_autoprovider_detection(): def no_provider(): return None def must_not_call(): assert False auto = AutoProvider([ no_provider, DisconnectedProvider, ConnectedProvider, must_not_call, ]) w3 = Webu(auto) assert w3.isConnected() assert isinstance(auto._active_provider, ConnectedProvider)
def name_to_hash(name): node = EMPTY_SHA3_BYTES if name: labels = name.split(".") for label in reversed(labels): labelhash = label_to_hash(label) assert isinstance(labelhash, bytes) assert isinstance(node, bytes) node = Webu().sha3(node + labelhash) return node
def generate_go_happyuc_fixture(destination_dir): with contextlib.ExitStack() as stack: datadir = stack.enter_context(common.tempdir()) keystore_dir = os.path.join(datadir, 'keystore') common.ensure_path_exists(keystore_dir) keyfile_path = os.path.join(keystore_dir, common.KEYFILE_FILENAME) with open(keyfile_path, 'w') as keyfile: keyfile.write(common.KEYFILE_DATA) genesis_file_path = os.path.join(datadir, 'genesis.json') with open(genesis_file_path, 'w') as genesis_file: genesis_file.write(json.dumps(common.GENESIS_DATA)) ghuc_ipc_path_dir = stack.enter_context(common.tempdir()) ghuc_ipc_path = os.path.join(ghuc_ipc_path_dir, 'ghuc.ipc') ghuc_port = common.get_open_port() ghuc_binary = common.get_ghuc_binary() ghuc_proc = stack.enter_context( common.get_ghuc_process( # noqa: F841 ghuc_binary=ghuc_binary, datadir=datadir, genesis_file_path=genesis_file_path, ipc_path=ghuc_ipc_path, port=ghuc_port, networkid=str(common.GENESIS_DATA['config']['chainId']))) common.wait_for_socket(ghuc_ipc_path) webu = Webu(Webu.IPCProvider(ghuc_ipc_path)) chain_data = setup_chain_state(webu) static_data = { 'raw_txn_account': common.RAW_TXN_ACCOUNT, 'keyfile_pw': common.KEYFILE_PW, } pprint.pprint(merge(chain_data, static_data)) shutil.copytree(datadir, destination_dir)
def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info): expected_raw_txn = transaction_info['signed'] key = transaction_info['key'] transaction = dissoc(transaction_info, 'signed', 'key', 'unsigned') # validate r, in order to validate the transaction hash # There is some ambiguity about whether `r` will always be deterministically # generated from the transaction hash and private key, mostly due to code # author's ignorance. The example test fixtures and implementations seem to agree, so far. # See ecdsa_raw_sign() in /eth_keys/backends/native/ecdsa.py signed = acct.signTransaction(transaction, key) assert signed.r == Webu.toInt(hexstr=expected_raw_txn[-130:-66]) # confirm that signed transaction can be recovered to the sender expected_sender = acct.privateKeyToAccount(key).address assert acct.recoverTransaction(signed.rawTransaction) == expected_sender
def test_time_based_gas_price_strategy(strategy_params, expected): fixture_middleware = construct_result_generator_middleware({ 'eth_getBlockByHash': _get_block_by_something, 'eth_getBlockByNumber': _get_block_by_something, }) w3 = Webu( providers=[BaseProvider()], middlewares=[fixture_middleware], ) time_based_gas_price_strategy = construct_time_based_gas_price_strategy( **strategy_params, ) w3.eth.setGasPriceStrategy(time_based_gas_price_strategy) actual = w3.eth.generateGasPrice() assert actual == expected
def test_setup_name(ens, name, normalized_name, namehash_hex): address = ens.webu.eth.accounts[3] assert not ens.name(address) owner = ens.owner('tester') ens.setup_name(name, address) assert ens.name(address) == normalized_name # check that .eth is only appended if guess_tld is True if ens.nameprep(name) != normalized_name: assert ens.address(name, guess_tld=False) is None # check that the correct namehash is set: node = Webu.toBytes(hexstr=namehash_hex) assert ens.resolver(normalized_name).addr(node) == address # check that the correct owner is set: assert ens.owner(name) == owner ens.setup_name(None, address) ens.setup_address(name, None) assert not ens.name(address) assert not ens.address(name)
def test_set_address(ens, name, full_name, namehash_hex): assert ens.address(name) is None owner = ens.owner('tester') ens.setup_address(name, TEST_ADDRESS) assert ens.address(name) == TEST_ADDRESS # check that .eth is only appended if guess_tld is True namehash = Webu.toBytes(hexstr=namehash_hex) normal_name = ens.nameprep(full_name) if ens.nameprep(name) == normal_name: assert ens.address(name, guess_tld=False) == TEST_ADDRESS else: assert ens.address(name, guess_tld=False) is None # check that the correct namehash is set: assert ens.resolver(normal_name).addr(namehash) == TEST_ADDRESS # check that the correct owner is set: assert ens.owner(name) == owner ens.setup_address(name, None) assert ens.address(name) is None
def test_sha3_primitive(primitive, digest): assert Webu.sha3(primitive) == digest
def test_sha3_primitive_invalid(primitive, exception): with pytest.raises(exception): Webu.sha3(primitive)
def test_sha3_hexstr(hexstr, digest): assert Webu.sha3(hexstr=hexstr) == digest
def test_sha3_text(message, digest): assert Webu.sha3(text=message) == digest
def test_sha3_raise_if_no_args(): with pytest.raises(TypeError): Webu.sha3()
def test_sha3_raise_if_hexstr_and_text(): with pytest.raises(TypeError): Webu.sha3(hexstr='0x', text='')
def test_sha3_raise_if_primitive_and(kwargs): # must not set more than one input with pytest.raises(TypeError): Webu.sha3('', **kwargs)
def w3_base(): return Webu(providers=[BaseProvider()], middlewares=[])
def test_to_text_identity(): assert Webu.toText(text='pass-through') == 'pass-through'
def test_to_bytes_text(val, expected): assert Webu.toBytes(text=val) == expected
from webu import ( IPCProvider, Webu, ) w3 = Webu(IPCProvider())
from webu import Webu w3 = Webu()
def test_to_text(val, expected): assert Webu.toText(val) == expected
def webu(parity_process, endpoint_uri, event_loop): event_loop.run_until_complete(wait_for_ws(endpoint_uri, event_loop)) _webu = Webu(Webu.WebsocketProvider(endpoint_uri)) return _webu
def generate_parity_fixture(destination_dir): """ The parity fixture generation strategy is to start a ghuc client with existing fixtures copied into a temp datadir. Then a parity client is started is peered with the ghuc client. """ with contextlib.ExitStack() as stack: ghuc_datadir = stack.enter_context(common.tempdir()) ghuc_port = common.get_open_port() ghuc_ipc_path_dir = stack.enter_context(common.tempdir()) ghuc_ipc_path = os.path.join(ghuc_ipc_path_dir, 'ghuc.ipc') ghuc_keystore_dir = os.path.join(ghuc_datadir, 'keystore') common.ensure_path_exists(ghuc_keystore_dir) ghuc_keyfile_path = os.path.join(ghuc_keystore_dir, common.KEYFILE_FILENAME) with open(ghuc_keyfile_path, 'w') as keyfile: keyfile.write(common.KEYFILE_DATA) genesis_file_path = os.path.join(ghuc_datadir, 'genesis.json') with open(genesis_file_path, 'w') as genesis_file: genesis_file.write(json.dumps(common.GENESIS_DATA)) stack.enter_context( common.get_ghuc_process(common.get_ghuc_binary(), ghuc_datadir, genesis_file_path, ghuc_ipc_path, ghuc_port, str(CHAIN_CONFIG['params']['networkID']))) # set up fixtures common.wait_for_socket(ghuc_ipc_path) webu_ghuc = Webu(Webu.IPCProvider(ghuc_ipc_path)) chain_data = go_happyuc.setup_chain_state(webu_ghuc) fixture_block_count = webu_ghuc.eth.blockNumber datadir = stack.enter_context(common.tempdir()) keystore_dir = os.path.join(datadir, 'keys') os.makedirs(keystore_dir, exist_ok=True) parity_keyfile_path = os.path.join(keystore_dir, common.KEYFILE_FILENAME) with open(parity_keyfile_path, 'w') as keyfile: keyfile.write(common.KEYFILE_DATA) chain_config_file_path = os.path.join(datadir, 'chain_config.json') with open(chain_config_file_path, 'w') as chain_file: chain_file.write(json.dumps(CHAIN_CONFIG)) parity_ipc_path_dir = stack.enter_context(common.tempdir()) parity_ipc_path = os.path.join(parity_ipc_path_dir, 'jsonrpc.ipc') parity_port = common.get_open_port() parity_binary = get_parity_binary() parity_proc = stack.enter_context( get_parity_process( # noqa: F841 parity_binary=parity_binary, datadir=datadir, ipc_path=parity_ipc_path, keys_path=keystore_dir, chain_config_file_path=chain_config_file_path, parity_port=parity_port, )) common.wait_for_socket(parity_ipc_path) webu = Webu(Webu.IPCProvider(parity_ipc_path)) time.sleep(10) connect_nodes(webu, webu_ghuc) wait_for_chain_sync(webu, fixture_block_count) static_data = { 'raw_txn_account': common.RAW_TXN_ACCOUNT, 'keyfile_pw': common.KEYFILE_PW, } pprint.pprint(merge(chain_data, static_data)) shutil.copytree(datadir, destination_dir) parity_proc = stack.enter_context( parity_export_blocks_process( # noqa: F841 parity_binary=parity_binary, datadir=destination_dir, chain_config_file_path=os.path.join(destination_dir, 'chain_config.json'), parity_port=parity_port, ))
def test_to_text_hexstr(val, expected): assert Webu.toText(hexstr=val) == expected