def test_set_address(ens, name, full_name, namehash_hex, TEST_ADDRESS): assert ens.address(name) is None owner = ens.owner('tester') ens.setup_address(name, TEST_ADDRESS) assert is_same_address(ens.address(name), TEST_ADDRESS) # check that .eth is only appended if guess_tld is True namehash = Web3.toBytes(hexstr=namehash_hex) normal_name = ens.nameprep(full_name) if ens.nameprep(name) == normal_name: assert is_same_address(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 is_same_address( 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_set_address_equivalence(ens, name, equivalent, TEST_ADDRESS): assert ens.address(name) is None ens.setup_address(name, TEST_ADDRESS) assert is_same_address(ens.address(name), TEST_ADDRESS) assert is_same_address(ens.address(equivalent), TEST_ADDRESS) ens.setup_address(name, None) assert ens.address(name) is None
def test_event_rich_log(web3, emitter, emitter_event_ids, wait_for_transaction, contract_fn, event_name, call_args, expected_args): emitter_fn = emitter.functions[contract_fn] event_id = getattr(emitter_event_ids, event_name) txn_hash = emitter_fn(event_id, *call_args).transact() txn_receipt = wait_for_transaction(web3, txn_hash) event_instance = emitter.events[event_name]() rich_logs = event_instance.processReceipt(txn_receipt) assert len(rich_logs) == 1 rich_log = rich_logs[0] assert rich_log['args'] == expected_args assert rich_log.args == expected_args for arg in expected_args: assert getattr(rich_log.args, arg) == expected_args[arg] assert rich_log['blockHash'] == txn_receipt['blockHash'] assert rich_log['blockNumber'] == txn_receipt['blockNumber'] assert rich_log['transactionIndex'] == txn_receipt['transactionIndex'] assert is_same_address(rich_log['address'], emitter.address) assert rich_log['event'] == event_name quiet_event = emitter.events['LogBytes'] empty_rich_log = quiet_event().processReceipt(txn_receipt) assert empty_rich_log == tuple()
def test_dynamic_length_argument_extraction(web3, emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids): string_0 = "this-is-the-first-string-which-exceeds-32-bytes-in-length" string_1 = "this-is-the-second-string-which-exceeds-32-bytes-in-length" txn_hash = emitter.functions.logDynamicArgs(string_0, string_1).transact() txn_receipt = wait_for_transaction(web3, txn_hash) assert len(txn_receipt['logs']) == 1 log_entry = txn_receipt['logs'][0] event_abi = emitter._find_matching_event_abi('LogDynamicArgs') event_topic = emitter_log_topics.LogDynamicArgs assert event_topic in log_entry['topics'] string_0_topic = web3.sha3(text=string_0) assert string_0_topic in log_entry['topics'] event_data = get_event_data(event_abi, log_entry) expected_args = { "arg0": string_0_topic, "arg1": string_1, } assert event_data['args'] == expected_args assert event_data['blockHash'] == txn_receipt['blockHash'] assert event_data['blockNumber'] == txn_receipt['blockNumber'] assert event_data['transactionIndex'] == txn_receipt['transactionIndex'] assert is_same_address(event_data['address'], emitter.address) assert event_data['event'] == 'LogDynamicArgs'
def test_event_data_extraction(web3, emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids, contract_fn, event_name, call_args, expected_args): emitter_fn = emitter.functions[contract_fn] event_id = getattr(emitter_event_ids, event_name) txn_hash = emitter_fn(event_id, *call_args).transact() txn_receipt = wait_for_transaction(web3, txn_hash) assert len(txn_receipt['logs']) == 1 log_entry = txn_receipt['logs'][0] event_abi = emitter._find_matching_event_abi(event_name) event_topic = getattr(emitter_log_topics, event_name) is_anonymous = event_abi['anonymous'] if is_anonymous: assert event_topic not in log_entry['topics'] else: assert event_topic in log_entry['topics'] event_data = get_event_data(event_abi, log_entry) assert event_data['args'] == expected_args assert event_data['blockHash'] == txn_receipt['blockHash'] assert event_data['blockNumber'] == txn_receipt['blockNumber'] assert event_data['transactionIndex'] == txn_receipt['transactionIndex'] assert is_same_address(event_data['address'], emitter.address) assert event_data['event'] == event_name
def test_get_all_entries_returned_block_data( web3, emitter, Emitter, wait_for_transaction, emitter_event_ids, call_as_instance, ): txn_hash = emitter.functions.logNoArgs(emitter_event_ids.LogNoArguments).transact() txn_receipt = wait_for_transaction(web3, txn_hash) if call_as_instance: contract = emitter else: contract = Emitter events = contract.events.LogNoArguments.createFilter(fromBlock=txn_receipt['blockNumber']) log_entries = events.get_all_entries() assert len(log_entries) == 1 event_data = log_entries[0] assert event_data['args'] == {} assert event_data['blockHash'] == txn_receipt['blockHash'] assert event_data['blockNumber'] == txn_receipt['blockNumber'] assert event_data['transactionIndex'] == txn_receipt['transactionIndex'] assert is_same_address(event_data['address'], emitter.address) assert event_data['event'] == 'LogNoArguments'
def test_eth_sendTransaction(self, web3, unlocked_account_dual_type): txn_params = { 'from': unlocked_account_dual_type, 'to': unlocked_account_dual_type, 'value': 1, 'gas': 21000, 'gasPrice': web3.eth.gasPrice, } txn_hash = web3.eth.sendTransaction(txn_params) txn = web3.eth.getTransaction(txn_hash) assert is_same_address(txn['from'], txn_params['from']) assert is_same_address(txn['to'], txn_params['to']) assert txn['value'] == 1 assert txn['gas'] == 21000 assert txn['gasPrice'] == txn_params['gasPrice']
def test_personal_sign_and_ecrecover(self, web3, unlockable_account_dual_type, unlockable_account_pw): message = 'test-web3-personal-sign' signature = web3.personal.sign(message, unlockable_account_dual_type, unlockable_account_pw) signer = web3.personal.ecRecover(message, signature) assert is_same_address(signer, unlockable_account_dual_type)
def test_eth_modifyTransaction(self, web3, unlocked_account): txn_params = { 'from': unlocked_account, 'to': unlocked_account, 'value': 1, 'gas': 21000, 'gasPrice': web3.eth.gasPrice, } txn_hash = web3.eth.sendTransaction(txn_params) modified_txn_hash = web3.eth.modifyTransaction( txn_hash, gasPrice=(txn_params['gasPrice'] * 2), value=2) modified_txn = web3.eth.getTransaction(modified_txn_hash) assert is_same_address(modified_txn['from'], txn_params['from']) assert is_same_address(modified_txn['to'], txn_params['to']) assert modified_txn['value'] == 2 assert modified_txn['gas'] == 21000 assert modified_txn['gasPrice'] == txn_params['gasPrice'] * 2
def test_eth_sendTransaction_with_nonce(self, web3, unlocked_account): txn_params = { 'from': unlocked_account, 'to': unlocked_account, 'value': 1, 'gas': 21000, # Increased gas price to ensure transaction hash different from other tests 'gasPrice': web3.eth.gasPrice * 2, 'nonce': web3.eth.getTransactionCount(unlocked_account), } txn_hash = web3.eth.sendTransaction(txn_params) txn = web3.eth.getTransaction(txn_hash) assert is_same_address(txn['from'], txn_params['from']) assert is_same_address(txn['to'], txn_params['to']) assert txn['value'] == 1 assert txn['gas'] == 21000 assert txn['gasPrice'] == txn_params['gasPrice'] assert txn['nonce'] == txn_params['nonce']
def assert_contains_log(result): assert len(result) == 1 log_entry = result[0] assert log_entry['blockNumber'] == block_with_txn_with_log[ 'number'] assert log_entry['blockHash'] == block_with_txn_with_log['hash'] assert log_entry['logIndex'] == 0 assert is_same_address(log_entry['address'], emitter_contract_address) assert log_entry['transactionIndex'] == 0 assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
def test_personal_sendTransaction(self, web3, unlockable_account_dual_type, unlockable_account_pw): assert web3.eth.getBalance(unlockable_account_dual_type) > web3.toWei( 1, 'ether') txn_params = { 'from': unlockable_account_dual_type, 'to': unlockable_account_dual_type, 'gas': 21000, 'value': 1, 'gasPrice': web3.toWei(1, 'gwei'), } txn_hash = web3.personal.sendTransaction(txn_params, unlockable_account_pw) assert txn_hash transaction = web3.eth.getTransaction(txn_hash) assert is_same_address(transaction['from'], txn_params['from']) assert is_same_address(transaction['to'], txn_params['to']) assert transaction['gas'] == txn_params['gas'] assert transaction['value'] == txn_params['value'] assert transaction['gasPrice'] == txn_params['gasPrice']
def test_eth_getTransactionReceipt_with_log_entry(self, web3, block_with_txn_with_log, emitter_contract, txn_hash_with_log): receipt = web3.eth.getTransactionReceipt(txn_hash_with_log) assert is_dict(receipt) assert receipt['blockNumber'] == block_with_txn_with_log['number'] assert receipt['blockHash'] == block_with_txn_with_log['hash'] assert receipt['transactionIndex'] == 0 assert receipt['transactionHash'] == HexBytes(txn_hash_with_log) assert len(receipt['logs']) == 1 log_entry = receipt['logs'][0] assert log_entry['blockNumber'] == block_with_txn_with_log['number'] assert log_entry['blockHash'] == block_with_txn_with_log['hash'] assert log_entry['logIndex'] == 0 assert is_same_address(log_entry['address'], emitter_contract.address) assert log_entry['transactionIndex'] == 0 assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log)
def address_in(address, addresses): return any(is_same_address(address, item) for item in addresses)
def setup_chain_state(web3): coinbase = web3.eth.coinbase assert is_same_address(coinbase, common.COINBASE) # # Math Contract # math_contract_factory = web3.eth.contract( abi=MATH_ABI, bytecode=MATH_BYTECODE, ) math_deploy_receipt = common.deploy_contract(web3, 'math', math_contract_factory) assert is_dict(math_deploy_receipt) # # Emitter Contract # emitter_contract_factory = web3.eth.contract( abi=EMITTER_ABI, bytecode=EMITTER_BYTECODE, ) emitter_deploy_receipt = common.deploy_contract(web3, 'emitter', emitter_contract_factory) emitter_contract = emitter_contract_factory( emitter_deploy_receipt['contractAddress']) txn_hash_with_log = emitter_contract.functions.logDouble( which=EMITTER_ENUM['LogDoubleWithIndex'], arg0=12345, arg1=54321, ).transact({ 'from': web3.eth.coinbase, }) print('TXN_HASH_WITH_LOG:', txn_hash_with_log) txn_receipt_with_log = common.mine_transaction_hash( web3, txn_hash_with_log) block_with_log = web3.eth.getBlock(txn_receipt_with_log['blockHash']) print('BLOCK_HASH_WITH_LOG:', block_with_log['hash']) # # Empty Block # empty_block_number = common.mine_block(web3) print('MINED_EMPTY_BLOCK') empty_block = web3.eth.getBlock(empty_block_number) assert is_dict(empty_block) assert not empty_block['transactions'] print('EMPTY_BLOCK_HASH:', empty_block['hash']) # # Block with Transaction # web3.personal.unlockAccount(coinbase, common.KEYFILE_PW) web3.miner.start(1) mined_txn_hash = web3.eth.sendTransaction({ 'from': coinbase, 'to': coinbase, 'value': 1, 'gas': 21000, 'gas_price': web3.eth.gasPrice, }) mined_txn_receipt = common.mine_transaction_hash(web3, mined_txn_hash) print('MINED_TXN_HASH:', mined_txn_hash) block_with_txn = web3.eth.getBlock(mined_txn_receipt['blockHash']) print('BLOCK_WITH_TXN_HASH:', block_with_txn['hash']) geth_fixture = { 'math_deploy_txn_hash': math_deploy_receipt['transactionHash'], 'math_address': math_deploy_receipt['contractAddress'], 'emitter_deploy_txn_hash': emitter_deploy_receipt['transactionHash'], 'emitter_address': emitter_deploy_receipt['contractAddress'], 'txn_hash_with_log': txn_hash_with_log, 'block_hash_with_log': block_with_log['hash'], 'empty_block_hash': empty_block['hash'], 'mined_txn_hash': mined_txn_hash, 'block_with_txn_hash': block_with_txn['hash'], } return geth_fixture