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_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 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 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_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 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 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
from webu import Webu w3 = Webu()
from webu import ( Webu, WebsocketProvider, ) w3 = Webu(WebsocketProvider())
def webu(parity_process, ipc_path): wait_for_socket(ipc_path) _webu = Webu(Webu.IPCProvider(ipc_path)) return _webu
def w3(): return Webu(providers=[DummyProvider()], middlewares=[])
def w3(): return Webu(HappyUCTesterProvider())
def webu(ghuc_process, ghuc_ipc_path): wait_for_socket(ghuc_ipc_path) _webu = Webu(Webu.IPCProvider(ghuc_ipc_path)) return _webu
def w3_base(): return Webu(providers=[BaseProvider()], middlewares=[])
def label_to_hash(label): label = normalize_name(label) if '.' in label: raise ValueError("Cannot generate hash for label %r with a '.'" % label) return Webu().sha3(text=label)
def sha3_text(val): if isinstance(val, str): val = val.encode('utf-8') return Webu().sha3(val)
def webu(ghuc_process, endpoint_uri): wait_for_http(endpoint_uri) _webu = Webu(Webu.HTTPProvider(endpoint_uri)) return _webu
from webu import ( IPCProvider, Webu, ) w3 = Webu(IPCProvider())
def test_isConnected_connected(): """ Webu.isConnected() returns True when connected to a node. """ webu = Webu(ConnectedProvider()) assert webu.isConnected() is True
def ensure_hex(data): if not isinstance(data, str): return Webu().toHex(data) return data
from webu import ( HTTPProvider, Webu, ) w3 = Webu(HTTPProvider())
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 ens_setup(): w3 = Webu(HappyUCTesterProvider(HappyUCTester())) # ** Set up ENS contracts ** # remove account that creates ENS, so test transactions don't have write access accounts = w3.eth.accounts ens_key = accounts.pop() # create ENS contract eth_labelhash = w3.sha3(text='eth') eth_namehash = bytes32(0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae) resolver_namehash = bytes32(0xfdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5) reverse_tld_namehash = bytes32(0xa097f6721ce401e757d1223a763fef49b8b5f90bb18567ddb86fd205dff71d34) # noqa: E501 reverser_namehash = bytes32(0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2) ens_contract = deploy(w3, ENSFactory, ens_key) # create public resolver public_resolver = deploy(w3, PublicResolverFactory, ens_key, args=[ens_contract.address]) # set 'resolver.eth' to resolve to public resolver ens_contract.functions.setSubnodeOwner( b'\0' * 32, eth_labelhash, ens_key ).transact({'from': ens_key}) ens_contract.functions.setSubnodeOwner( eth_namehash, w3.sha3(text='resolver'), ens_key ).transact({'from': ens_key}) ens_contract.functions.setResolver( resolver_namehash, public_resolver.address ).transact({'from': ens_key}) public_resolver.functions.setAddr( resolver_namehash, public_resolver.address ).transact({'from': ens_key}) # create .eth auction registrar eth_registrar = deploy( w3, ETHRegistrarFactory, ens_key, args=[ens_contract.address, eth_namehash, 1], ) # set '.eth' to resolve to the registrar ens_contract.functions.setResolver( eth_namehash, public_resolver.address ).transact({'from': ens_key}) public_resolver.functions.setAddr( eth_namehash, eth_registrar.address ).transact({'from': ens_key}) # create reverse resolver reverse_resolver = deploy(w3, DefaultReverseResolver, ens_key, args=[ens_contract.address]) # create reverse registrar reverse_registrar = deploy( w3, ReverseRegistrar, ens_key, args=[ens_contract.address, reverse_resolver.address] ) # set 'addr.reverse' to resolve to reverse registrar ens_contract.functions.setSubnodeOwner( b'\0' * 32, w3.sha3(text='reverse'), ens_key ).transact({'from': ens_key}) ens_contract.functions.setSubnodeOwner( reverse_tld_namehash, w3.sha3(text='addr'), ens_key ).transact({'from': ens_key}) ens_contract.functions.setResolver( reverser_namehash, public_resolver.address ).transact({'from': ens_key}) public_resolver.functions.setAddr( reverser_namehash, reverse_registrar.address ).transact({'from': ens_key}) # set owner of tester.eth to an account controlled by tests ens_contract.functions.setSubnodeOwner( eth_namehash, w3.sha3(text='tester'), w3.eth.accounts[2] # note that this does not have to be the default, only in the list ).transact({'from': ens_key}) # make the registrar the owner of the 'eth' name ens_contract.functions.setSubnodeOwner( b'\0' * 32, eth_labelhash, eth_registrar.address ).transact({'from': ens_key}) # make the reverse registrar the owner of the 'addr.reverse' name ens_contract.functions.setSubnodeOwner( reverse_tld_namehash, w3.sha3(text='addr'), reverse_registrar.address ).transact({'from': ens_key}) return ENS.fromWebu(w3, ens_contract.address)