def test_txpool_content(web3_empty): web3 = web3_empty web3.miner.stop() with Timeout(60) as timeout: while web3.miner.hashrate or web3.eth.mining: timeout.sleep(random.random()) txn_1_hash = web3.eth.sendTransaction({ 'from': web3.eth.coinbase, 'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'value': 12345, }) txn_1 = web3.eth.getTransaction(txn_1_hash) txn_2_hash = web3.eth.sendTransaction({ 'from': web3.eth.coinbase, 'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'value': 54321, }) txn_2 = web3.eth.getTransaction(txn_2_hash) content = web3.txpool.content assert web3.eth.coinbase in content['pending'] pending_txns = content['pending'][web3.eth.coinbase] assert txn_1['nonce'] in pending_txns assert txn_2['nonce'] in pending_txns assert pending_txns[txn_1['nonce']][0]['hash'] == txn_1_hash assert pending_txns[txn_1['nonce']][0]['value'] == 12345 assert pending_txns[txn_2['nonce']][0]['hash'] == txn_2_hash assert pending_txns[txn_2['nonce']][0]['value'] == 54321
def make_request(self, method, params): self.logger.debug("Making request IPC. Path: %s, Method: %s", self.ipc_path, method) request = self.encode_rpc_request(method, params) 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.timeout) as timeout: while True: try: raw_response += sock.recv(4096) except socket.timeout: timeout.sleep(0) continue if raw_response == b"": timeout.sleep(0) elif has_valid_json_rpc_ending(raw_response): try: response = self.decode_rpc_response(raw_response) except JSONDecodeError: timeout.sleep(0) continue else: return response else: timeout.sleep(0) continue
def test_instatiate_existing_filter(web3, sleep_interval, wait_for_block, filter_id): with pytest.raises(TypeError): web3.eth.filter('latest', filter_id) with pytest.raises(TypeError): web3.eth.filter('latest', filter_id=filter_id) with pytest.raises(TypeError): web3.eth.filter(filter_params='latest', filter_id=filter_id) block_filter = web3.eth.filter(filter_id=filter_id) current_block = web3.eth.blockNumber wait_for_block(web3, current_block + 3) found_block_hashes = [] with Timeout(5) as timeout: while len(found_block_hashes) < 3: found_block_hashes.extend(block_filter.get_new_entries()) timeout.sleep(sleep_interval()) assert len(found_block_hashes) == 3 expected_block_hashes = [ web3.eth.getBlock(n + 1).hash for n in range(current_block, current_block + 3) ] assert found_block_hashes == expected_block_hashes
def test_async_filter_against_log_events(web3_empty, emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids ): web3 = web3_empty seen_logs = [] txn_filter = web3.eth.filter({}) txn_filter.watch(seen_logs.append) txn_hashes = [] txn_hashes.append(emitter.functions.logNoArgs(emitter_event_ids.LogNoArguments).transact()) for txn_hash in txn_hashes: wait_for_transaction(web3, txn_hash) with Timeout(5) as timeout: while not seen_logs: timeout.sleep(random.random()) txn_filter.stop_watching(30) assert set(txn_hashes) == set(log['transactionHash'] for log in seen_logs)
def test_async_filter_against_pending_transactions(web3_empty, wait_for_transaction, skip_if_testrpc ): web3 = web3_empty skip_if_testrpc(web3) seen_txns = [] txn_filter = web3.eth.filter("pending") txn_filter.watch(seen_txns.append) txn_1_hash = web3.eth.sendTransaction({ 'from': web3.eth.coinbase, 'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'value': 12345, }) txn_2_hash = web3.eth.sendTransaction({ 'from': web3.eth.coinbase, 'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'value': 54321, }) wait_for_transaction(web3, txn_1_hash) wait_for_transaction(web3, txn_2_hash) with Timeout(5) as timeout: while not seen_txns: timeout.sleep(random.random()) txn_filter.stop_watching(30) assert txn_1_hash in seen_txns assert txn_2_hash in seen_txns
def test_miner_setExtra(web3_empty, wait_for_block): web3 = web3_empty initial_extra = decode_hex( web3.eth.getBlock(web3.eth.blockNumber)['extraData']) new_extra_data = b'-this-is-32-bytes-of-extra-data-' # sanity assert initial_extra != new_extra_data web3.miner.setExtra(new_extra_data) with Timeout(60) as timeout: while True: extra_data = decode_hex( web3.eth.getBlock(web3.eth.blockNumber)['extraData']) if extra_data == new_extra_data: break timeout.sleep(random.random()) after_extra = decode_hex( web3.eth.getBlock(web3.eth.blockNumber)['extraData']) assert after_extra == new_extra_data
def test_with_custom_exception_instance(): exc = ValueError("an instance of an excepiton") timeout = Timeout(0.01, exc) timeout.start() time.sleep(0.02) with pytest.raises(ValueError) as err: timeout.check() assert err.value is exc
def _wait_for_block(web3, block_number=1, timeout=None): if not timeout: timeout = (block_number - web3.eth.blockNumber) * 3 poll_delay_counter = PollDelayCounter() with Timeout(timeout) as timeout: while True: if web3.eth.blockNumber >= block_number: break web3.manager.request_blocking("evm_mine", []) timeout.sleep(poll_delay_counter())
def _wait_for_transaction(web3, txn_hash, timeout=120): poll_delay_counter = PollDelayCounter() with Timeout(timeout) as timeout: while True: txn_receipt = web3.eth.getTransactionReceipt(txn_hash) if txn_receipt is not None: break time.sleep(poll_delay_counter()) timeout.check() return txn_receipt
def wait_for_transaction_receipt(web3, txn_hash, timeout=120, poll_latency=0.1): with Timeout(timeout) as _timeout: while True: txn_receipt = web3.eth.getTransactionReceipt(txn_hash) if txn_receipt is not None: break _timeout.sleep(poll_latency) return txn_receipt
def test_miner_stop(web3_empty): web3 = web3_empty assert web3.eth.mining assert web3.miner.hashrate web3.miner.stop() with Timeout(60) as timeout: while web3.eth.mining or web3.eth.hashrate: timeout.sleep(random.random()) timeout.check() assert not web3.eth.mining assert not web3.miner.hashrate
def test_miner_setGasPrice(web3_empty, wait_for_block): web3 = web3_empty initial_gas_price = web3.eth.gasPrice # sanity check assert web3.eth.gasPrice > 1000 web3.miner.setGasPrice(initial_gas_price // 2) with Timeout(60) as timeout: while web3.eth.gasPrice == initial_gas_price: timeout.sleep(random.random()) after_gas_price = web3.eth.gasPrice assert after_gas_price < initial_gas_price
def test_miner_start(web3_empty, wait_for_miner_start): web3 = web3_empty # sanity assert web3.eth.mining assert web3.miner.hashrate web3.miner.stop() with Timeout(60) as timeout: while web3.eth.mining or web3.eth.hashrate: timeout.sleep(random.random()) assert not web3.eth.mining assert not web3.miner.hashrate web3.miner.start(1) wait_for_miner_start(web3) assert web3.eth.mining assert web3.miner.hashrate
def test_txpool_inspect(web3_empty): web3 = web3_empty web3.miner.stop() with Timeout(60) as timeout: while web3.miner.hashrate or web3.eth.mining: timeout.sleep(random.random()) txn_1_hash = web3.eth.sendTransaction({ 'from': web3.eth.coinbase, 'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'value': 12345, }) txn_1 = web3.eth.getTransaction(txn_1_hash) txn_2_hash = web3.eth.sendTransaction({ 'from': web3.eth.coinbase, 'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', 'value': 54321, }) txn_2 = web3.eth.getTransaction(txn_2_hash) inspect_content = web3.txpool.inspect assert web3.eth.coinbase in inspect_content['pending'] pending_txns = inspect_content['pending'][web3.eth.coinbase] assert txn_1['nonce'] in pending_txns assert txn_2['nonce'] in pending_txns txn_1_summary = pending_txns[txn_1['nonce']][0] txn_2_summary = pending_txns[txn_2['nonce']][0] assert '0xd3CdA913deB6f67967B99D67aCDFa1712C293601' in txn_1_summary assert '12345 wei' in txn_1_summary assert '0xd3CdA913deB6f67967B99D67aCDFa1712C293601' in txn_2_summary assert '54321 wei' in txn_2_summary
def test_sync_filter_against_latest_blocks(web3, sleep_interval, wait_for_block): if EthereumTesterProvider not in map(type, web3.providers): web3.providers = EthereumTesterProvider() txn_filter = web3.eth.filter("latest") current_block = web3.eth.blockNumber wait_for_block(web3, current_block + 3) found_block_hashes = [] with Timeout(5) as timeout: while len(found_block_hashes) < 3: found_block_hashes.extend(txn_filter.get_new_entries()) timeout.sleep(sleep_interval()) assert len(found_block_hashes) == 3 expected_block_hashes = [ web3.eth.getBlock(n + 1).hash for n in range(current_block, current_block + 3) ] assert found_block_hashes == expected_block_hashes
def _wait_for_miner_start(web3, timeout=60): poll_delay_counter = PollDelayCounter() with Timeout(timeout) as timeout: while not web3.eth.mining or not web3.eth.hashrate: time.sleep(poll_delay_counter()) timeout.check()
def test_with_custom_exception_type(): timeout = Timeout(0.01, ValueError) timeout.start() time.sleep(0.02) with pytest.raises(ValueError): timeout.check()
def test_contextmanager_timeout(): with pytest.raises(Timeout): with Timeout(0.01) as timeout: time.sleep(0.02) timeout.check()
def test_contextmanager_completion_before_timeout(): with Timeout(0.01) as timeout: timeout.check() time.sleep(0.02)
def test_inline_timeout(): timeout = Timeout(0.01) timeout.start() time.sleep(0.02) with pytest.raises(Timeout): timeout.check()
def test_inline_completion_before_timeout(): timeout = Timeout(0.01) timeout.start() timeout.check() timeout.cancel() time.sleep(0.02)