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(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 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.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 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_past_events_async_filter_with_callback(web3, sleep_interval, emitter, Emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids, call_as_instance): txn_hash = emitter.transact().logNoArgs(emitter_event_ids.LogNoArguments) txn_receipt = wait_for_transaction(web3, txn_hash) seen_logs = [] if call_as_instance: filter = emitter.pastEvents('LogNoArguments', {}, seen_logs.append) else: filter = Emitter.pastEvents('LogNoArguments', {}, seen_logs.append) with Timeout(30) as timeout: while not seen_logs: timeout.sleep(sleep_interval()) filter.stop_watching(30) assert len(seen_logs) == 1 event_data = seen_logs[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_sync_filter_against_pending_transactions(web3_empty, wait_for_transaction, skip_if_testrpc ): web3 = web3_empty skip_if_testrpc(web3) txn_filter = web3.eth.filter("pending") 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 txn_filter.get_new_entries(): timeout.sleep(random.random()) seen_txns = txn_filter.get_new_entries() assert txn_1_hash in seen_txns assert txn_2_hash in seen_txns
def test_past_events_filter_using_get_entries_api( web3, sleep_interval, emitter, Emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids, call_as_instance): txn_hash = emitter.transact().logNoArgs(emitter_event_ids.LogNoArguments) txn_receipt = wait_for_transaction(web3, txn_hash) if call_as_instance: filter = emitter.pastEvents('LogNoArguments') else: filter = Emitter.pastEvents('LogNoArguments') with Timeout(30) as timeout: while not filter.get(False): timeout.sleep(sleep_interval()) log_entries = filter.get(False) 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_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_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, method, params): request = self.encode_rpc_request(method, params) self._lock.acquire() try: with get_ipc_socket(self.ipc_path) as sock: sock.sendall(request) # TODO: use a BytesIO object here response_raw = b"" with Timeout(10) as timeout: while True: try: response_raw += sock.recv(4096) except socket.timeout: timeout.sleep(0) continue if response_raw == b"": timeout.sleep(0) else: try: json.loads(force_text(response_raw)) except JSONDecodeError: timeout.sleep(0) continue else: break finally: self._lock.release() return response_raw
def test_on_filter_with_only_event_name(web3, sleep_interval, emitter, Emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids, call_as_instance): seen_logs = [] if call_as_instance: filter = emitter.on('LogNoArguments', {}, seen_logs.append) else: filter = Emitter.on('LogNoArguments', {}, seen_logs.append) txn_hash = emitter.transact().logNoArgs(emitter_event_ids.LogNoArguments) wait_for_transaction(web3, txn_hash) with Timeout(30) as timeout: while not seen_logs: timeout.sleep(sleep_interval()) filter.stop_watching(30) assert len(seen_logs) == 1 assert seen_logs[0]['transactionHash'] == txn_hash
def add_logs(contract, event_name): logs[event_name] = [] def add(log): logs[event_name].append(log) save_logs(contract, event_name, add) with Timeout(20) as timeout: timeout.sleep(2)
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 test_on_filter_with_event_name_and_single_argument( web3, sleep_interval, emitter, Emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids, call_as_instance): seen_logs = [] if call_as_instance: filter = emitter.on('LogTripleWithIndex', {'filter': { 'arg1': 2, }}, seen_logs.append) else: filter = Emitter.on('LogTripleWithIndex', {'filter': { 'arg1': 2, }}, seen_logs.append) txn_hashes = [] txn_hashes.append(emitter.transact().logTriple( emitter_event_ids.LogTripleWithIndex, 2, 1, 3)) txn_hashes.append(emitter.transact().logTriple( emitter_event_ids.LogTripleWithIndex, 1, 2, 3)) txn_hashes.append(emitter.transact().logTriple( emitter_event_ids.LogTripleWithIndex, 12345, 2, 54321)) for txn_hash in txn_hashes: wait_for_transaction(web3, txn_hash) with Timeout(30) as timeout: while len(seen_logs) < 2: timeout.sleep(sleep_interval()) filter.stop_watching(30) assert len(seen_logs) == 2 assert {l['transactionHash'] for l in seen_logs} == set(txn_hashes[1:])
def distribute(self): with Timeout() as timeout: while (not self.distribution_ended and (not self.auction_ended or not len(self.addresses_claimable))): timeout.sleep(2) log.info('Auction ended. We should have all the addresses. %s, %s' % (len(self.addresses_claimable), self.addresses_claimable)) # 82495 gas / claimTokens # Call the distributor contract with batches of bidder addresses while len(self.addresses_claimable): batch_number = min(self.batch_number, len(self.addresses_claimable)) batch = self.addresses_claimable[:batch_number] self.addresses_claimable = self.addresses_claimable[batch_number:] self.claimed = self.claimed + batch print('Distributing tokens to {0} addresses: {1}'.format( batch_number, batch)) txhash = self.distributor.transact({ 'gas': 4000000 }).distribute(batch) receipt, success = check_succesful_tx(self.web3, txhash) assert success is True assert receipt is not None self.distribution_ended_checks()
def wait(self, seconds): try: with Timeout(seconds) as timeout: while len(list(self.event_waiting.keys())): timeout.sleep(2) except: 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 another 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 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_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", []) sleep(poll_delay_counter()) timeout.check()
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 isinstance(web3.currentProvider, (TestRPCProvider, EthereumTesterProvider)): web3._requestManager.request_blocking("evm_mine", []) 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 sleep(poll_delay_counter()) timeout.check() return txn_receipt
def distribute(self): with Timeout() as timeout: while (not self.distribution_ended and (not self.auction_ended or not len(self.addresses_unclaimed))): timeout.sleep(2) unclaimed_number = len(self.addresses_unclaimed) log.info('Auction ended. We should have all the addresses: %s' % (len(self.bidder_addresses.keys()))) log.info('Unclaimed tokens - addresses: %s' % (unclaimed_number)) # 87380 gas / claimTokens # We need to calculate from gas estimation if unclaimed_number > 0 and not self.batch_number: valid_bid_address = self.addresses_unclaimed[0] claim_tx_gas = self.auction.estimateGas({ 'from': self.account }).proxyClaimTokens(valid_bid_address) # claim_tx_gas = 50000 log.info('ESTIMATED claimTokens tx GAS: %s', (claim_tx_gas)) self.batch_number = int(self.total_distribute_tx_gas // claim_tx_gas) log.info('BATCH number: %s', (self.batch_number)) # Call the distributor contract with batches of bidder addresses while len(self.addresses_unclaimed): batch_number = min(self.batch_number, len(self.addresses_unclaimed)) batch = self.addresses_unclaimed[:batch_number] self.addresses_unclaimed = self.addresses_unclaimed[batch_number:] self.addresses_claimed = self.addresses_claimed + batch log.info('Distributing tokens to %s addresses: %s' % (batch_number, ','.join(batch))) # Send the distribute transaction tx = { 'from': self.account, 'gas': int(self.total_distribute_tx_gas) } if self.gas_price: tx['gas_price'] = int(self.gas_price) txhash = self.distributor.transact(tx).distribute(batch) if self.wait: receipt, success = check_succesful_tx(self.web3, txhash) assert success is True assert receipt is not None self.distribution_ended_checks()
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 distribution_ended_checks(self): print('Waiting to make sure we get all ClaimedTokens events') with Timeout(300) as timeout: while not self.distribution_ended or len(self.claimed) != len( self.verified_claims): print('self.distribution_ended', self.distribution_ended) print('self.claimed', len(self.claimed), len(self.verified_claims), self.claimed) print('self.verified_claims', self.verified_claims) timeout.sleep(50) assert len(self.claimed) == len(self.verified_claims) self.filter_claims.stop() self.filter_distributed.stop() print('DISTRIBUTION COMPLETE')
def distribution_ended_checks(self): log.info('Waiting to make sure we get all ClaimedTokens events') with Timeout(300) as timeout: while not self.distribution_ended or len(self.addresses_claimed) != \ len(self.verified_claims): log.info('Distribution ended: %s', str(self.distribution_ended)) log.info('Claimed %s, verified claims %s' % (len(self.addresses_claimed), len(self.verified_claims))) timeout.sleep(50) assert len(self.addresses_claimed) == len(self.verified_claims) self.filter_claims.stop() self.filter_distributed.stop() log.info('DISTRIBUTION COMPLETE') if self.file: log.info('The following file has been created: %s', self.file)
def test_on_filter_using_get_interface(web3, sleep_interval, emitter, Emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids, call_as_instance): if call_as_instance: filter = emitter.on('LogNoArguments', {}) else: filter = Emitter.on('LogNoArguments', {}) txn_hash = emitter.transact().logNoArgs(emitter_event_ids.LogNoArguments) txn_receipt = wait_for_transaction(web3, txn_hash) with Timeout(30) as timeout: while not filter.get(False): timeout.sleep(sleep_interval()) log_entries = filter.get() assert len(log_entries) == 1 assert log_entries[0]['transactionHash'] == txn_hash
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_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_on_sync_filter_with_event_name_and_non_indexed_argument(web3, sleep_interval, emitter, Emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids, call_as_instance ): if call_as_instance: filter = emitter.on('LogTripleWithIndex', {'filter': { 'arg0': 1, 'arg1': 2, }}) else: filter = Emitter.on('LogTripleWithIndex', {'filter': { 'arg0': 1, 'arg1': 2, }}) txn_hashes = [] txn_hashes.append( emitter.transact().logTriple(emitter_event_ids.LogTripleWithIndex, 2, 1, 3) ) txn_hashes.append( emitter.transact().logTriple(emitter_event_ids.LogTripleWithIndex, 1, 2, 3) ) txn_hashes.append( emitter.transact().logTriple(emitter_event_ids.LogTripleWithIndex, 12345, 2, 54321) ) for txn_hash in txn_hashes: wait_for_transaction(web3, txn_hash) with Timeout(30) as timeout: while not filter.get_all_entries(): timeout.sleep(sleep_interval()) seen_logs = filter.get_new_entries() assert len(seen_logs) == 1 assert seen_logs[0]['transactionHash'] == txn_hashes[1]
def wait(transfer_filter, timeout=30): with Timeout(timeout) as timeout: while not transfer_filter.get(False): timeout.sleep(2)
def test_waitfor_last_events_timeout(): with Timeout(20) as timeout: timeout.sleep(2)