def wait(self, seconds: int) -> None: try: with Timeout(seconds) as timeout: while len(list(self.event_waiting.keys())): timeout.sleep(2) except Exception as e: print(e) message = "NO EVENTS WERE TRIGGERED FOR: " + str( self.event_waiting) if len(self.event_unknown) > 0: message += "\n UNKOWN EVENTS: " + str(self.event_unknown) # FIXME Events triggered in an internal transaction # don't have the transactionHash we are looking for here # so we just check if the number of unknown events we find # is the same as the found events waiting_events = sum( [len(lst) for lst in self.event_waiting.values()]) if waiting_events == len(self.event_unknown): sandwitch_print(message) else: raise Exception( message + " waiting_events " + str(waiting_events), " len(self.event_unknown) " + str(len(self.event_unknown)), )
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_latest_blocks(web3, sleep_interval, wait_for_block, skip_if_testrpc): skip_if_testrpc(web3) seen_blocks = [] txn_filter = web3.eth.filter("latest") txn_filter.watch(seen_blocks.append) current_block = web3.eth.blockNumber wait_for_block(web3, current_block + 3) with Timeout(5) as timeout: while len(seen_blocks) < 2: timeout.sleep(sleep_interval()) txn_filter.stop_watching(3) expected_block_hashes = [ web3.eth.getBlock(n)['hash'] for n in range(current_block + 1, current_block + 3) ] assert len(seen_blocks) >= 2 assert set(expected_block_hashes).issubset(seen_blocks)
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 wait(self, seconds): try: with Timeout(seconds) as timeout: while len(list(self.event_waiting.keys())): timeout.sleep(2) except Exception as e: print(e) message = 'NO EVENTS WERE TRIGGERED FOR: ' + str( self.event_waiting) if len(self.event_unkown) > 0: message += '\n UNKOWN EVENTS: ' + str(self.event_unkown) # FIXME Events triggered in an internal transaction # don't have the transactionHash we are looking for here # so we just check if the number of unknown events we find # is the same as the found events waiting_events = 0 for ev in list(self.event_waiting.keys()): waiting_events += len(list(self.event_waiting[ev].keys())) if waiting_events == len(self.event_unkown): print('----------------------------------') print(message) print('----------------------------------') else: raise Exception( message + ' waiting_events ' + str(waiting_events), ' len(self.event_unkown) ' + str(len(self.event_unkown)))
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_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): 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) else: try: response = self.decode_rpc_response(raw_response) except JSONDecodeError: timeout.sleep(0) continue else: return response
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 make_request(self, text): request = text.encode('utf-8') 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 = json.loads(raw_response.decode('utf-8')) except JSONDecodeError: timeout.sleep(0) continue else: return response else: timeout.sleep(0) continue
def wait_for_transaction_receipt(web3, txn_hash, timeout=120): with Timeout(timeout) as _timeout: while True: txn_receipt = web3.eth.getTransactionReceipt(txn_hash) if txn_receipt is not None: break _timeout.sleep(random.random()) return txn_receipt
def wait_for_transaction_receipt(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 time.sleep(poll_latency) return txn_receipt
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_block(web3, block_number=1, timeout=60 * 10): poll_delay_counter = PollDelayCounter() with Timeout(timeout) as timeout: while True: if web3.eth.blockNumber >= block_number: break if is_all_testrpc_providers(web3.providers): web3.manager.request_blocking("evm_mine", []) time.sleep(poll_delay_counter()) timeout.check()
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, txid, timeout=180): receipt = None with Timeout(timeout) as time: while not receipt or not receipt['blockNumber']: try: receipt = web3.eth.getTransactionReceipt(txid) except ValueError as ex: if str(ex).find('EmptyResponse') != -1: pass # Empty response from a Parity light client else: raise ex time.sleep(5) return receipt
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 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 wait_for_transaction_receipt( web3: Web3, txid: str, timeout: int = 180 ) -> Optional[Dict[str, Any]]: receipt = None with Timeout(timeout) as time: while not receipt or not receipt["blockNumber"]: # pylint: disable=E1136 try: receipt = web3.eth.getTransactionReceipt(txid) except ValueError as ex: if str(ex).find("EmptyResponse") != -1: pass # Empty response from a Parity light client else: raise ex time.sleep(5) return receipt
def test_send_signed_transaction_local_timeout(self, method_call_mock): """ Tests the case when the transaction is signed locally (the private key is provided). """ error = Timeout() transaction = SimpleTransactionMock(error_to_throw=error) private_key = "abc" config = TestFile.get_config_mock(4000000000, 0, private_key=private_key) try: send_signed_transaction(config, transaction, attempts=1) self.fail("An error was expected") except Timeout as e: # the error is supposed to be re-raised because we are out of retries self.assertTrue(e is error) self.assertEqual(private_key, config.web3_client.eth.account.signed_private_key)
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_claim_rewards_timeout_exception(self): """ Tests whether a timeout exception is thrown when the __claim_rewards function transaction timeouts. """ # The following causes an exception in the auditing node, but it should be caught and # should not propagate with mock.patch( 'audit.threads.claim_rewards_thread.send_signed_transaction' ) as mocked_sign: try: mocked_sign.side_effect = Timeout() self.__claim_rewards_thread._ClaimRewardsThread__claim_rewards( ) self.fail("An exception should have been thrown") except Timeout: pass
def __update_min_price(self): """ Updates smart contract with the minimum price in the audit node's configuration. """ msg = "Make sure the account has enough Ether, " \ + "the Ethereum node is connected and synced, " \ + "and restart your node to try again." min_price_in_mini_qsp = self.config.min_price_in_qsp * (10**18) self.logger.info( "Updating min_price in the smart contract for address {0} to {1} QSP." .format( self.config.account, self.config.min_price_in_qsp, )) transaction = self.config.audit_contract.functions.setAuditNodePrice( min_price_in_mini_qsp) try: tx_hash = send_signed_transaction( self.config, transaction, wait_for_transaction_receipt=True) # If the tx_hash is None, the transaction did not actually complete. Exit if not tx_hash: raise Exception("The min price transaction did not complete") self.logger.debug("Successfully updated min price to {0}.".format( self.config.min_price_in_qsp)) except Timeout as e: error_msg = "Update min price timed out, " \ + "increase the tx_timeout_seconds in config.yaml and restart the node. " \ + msg + " {0}, {1}." formatted_error = error_msg.format(str(transaction), str(e)) self.logger.debug(formatted_error) raise Timeout(formatted_error) from e except DeduplicationException as e: error_msg = "A transaction already exists for updating min price," \ + " but has not yet been mined. " + msg \ + " This may take several iterations. {0}, {1}." self.logger.debug(error_msg.format(str(transaction), str(e))) raise e except TransactionNotConfirmedException as e: error_msg = "A transaction occurred, but was then uncled and never recovered. {0}, {1}" self.logger.debug(error_msg.format(str(transaction), str(e))) raise e except Exception as e: error_msg = "Error occurred setting min price. " + msg + " {0}, {1}." self.logger.exception(error_msg.format(str(transaction), str(e))) raise e
def test_sync_filter_against_log_events(web3_empty, emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids): web3 = web3_empty txn_filter = web3.eth.filter({}) txn_hashes = [] txn_hashes.append(emitter.transact().logNoArgs( emitter_event_ids.LogNoArguments)) for txn_hash in txn_hashes: wait_for_transaction(web3, txn_hash) with Timeout(5) as timeout: while not txn_filter.get_new_entries(): timeout.sleep(random.random()) seen_logs = txn_filter.get_new_entries() assert set(txn_hashes) == set(log['transactionHash'] for log in seen_logs)
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_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 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 make_request(self, method, params): request = self.encode_rpc_request(method, params) with self._lock, self._socket as sock: sock.sendall(request) raw_response = b"" with Timeout(10) as timeout: while True: try: raw_response += sock.recv(4096) except socket.timeout: timeout.sleep(0) continue if raw_response == b"": timeout.sleep(0) else: try: response = self.decode_rpc_response(raw_response) except JSONDecodeError: timeout.sleep(0) continue else: return response
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_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)