Пример #1
0
def test_instatiate_existing_filter(webu, sleep_interval, wait_for_block,
                                    filter_id):
    with pytest.raises(TypeError):
        webu.eth.filter('latest', filter_id)
    with pytest.raises(TypeError):
        webu.eth.filter('latest', filter_id=filter_id)
    with pytest.raises(TypeError):
        webu.eth.filter(filter_params='latest', filter_id=filter_id)

    block_filter = webu.eth.filter(filter_id=filter_id)

    current_block = webu.eth.blockNumber

    wait_for_block(webu, 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 = [
        webu.eth.getBlock(n + 1).hash
        for n in range(current_block, current_block + 3)
    ]
    assert found_block_hashes == expected_block_hashes
Пример #2
0
def test_txpool_content(webu_empty):
    webu = webu_empty

    webu.miner.stop()

    with Timeout(60) as timeout:
        while webu.miner.hashrate or webu.eth.mining:
            timeout.sleep(random.random())

    txn_1_hash = webu.eth.sendTransaction({
        'from': webu.eth.coinbase,
        'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
        'value': 12345,
    })
    txn_1 = webu.eth.getTransaction(txn_1_hash)
    txn_2_hash = webu.eth.sendTransaction({
        'from': webu.eth.coinbase,
        'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
        'value': 54321,
    })
    txn_2 = webu.eth.getTransaction(txn_2_hash)

    content = webu.txpool.content

    assert webu.eth.coinbase in content['pending']

    pending_txns = content['pending'][webu.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
Пример #3
0
    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
Пример #4
0
def test_async_filter_against_log_events(webu_empty,
                                         emitter,
                                         wait_for_transaction,
                                         emitter_log_topics,
                                         emitter_event_ids
                                         ):
    webu = webu_empty

    seen_logs = []
    txn_filter = webu.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(webu, 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)
Пример #5
0
def test_async_filter_against_pending_transactions(webu_empty,
                                                   wait_for_transaction,
                                                   skip_if_testrpc):
    webu = webu_empty
    skip_if_testrpc(webu)

    seen_txns = []
    txn_filter = webu.eth.filter("pending")
    txn_filter.watch(seen_txns.append)

    txn_1_hash = webu.eth.sendTransaction({
        'from': webu.eth.coinbase,
        'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
        'value': 12345,
    })
    txn_2_hash = webu.eth.sendTransaction({
        'from': webu.eth.coinbase,
        'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
        'value': 54321,
    })

    wait_for_transaction(webu, txn_1_hash)
    wait_for_transaction(webu, 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
Пример #6
0
 def _wait_for_block(webu, block_number=1, timeout=None):
     if not timeout:
         timeout = (block_number - webu.eth.blockNumber) * 3
     poll_delay_counter = PollDelayCounter()
     with Timeout(timeout) as timeout:
         while True:
             if webu.eth.blockNumber >= block_number:
                 break
             webu.manager.request_blocking("evm_mine", [])
             timeout.sleep(poll_delay_counter())
Пример #7
0
def wait_for_transaction_receipt(webu,
                                 txn_hash,
                                 timeout=120,
                                 poll_latency=0.1):
    with Timeout(timeout) as _timeout:
        while True:
            txn_receipt = webu.eth.getTransactionReceipt(txn_hash)
            if txn_receipt is not None:
                break
            _timeout.sleep(poll_latency)
    return txn_receipt
Пример #8
0
    def _wait_for_transaction(webu, txn_hash, timeout=120):
        poll_delay_counter = PollDelayCounter()
        with Timeout(timeout) as timeout:
            while True:
                txn_receipt = webu.eth.getTransactionReceipt(txn_hash)
                if txn_receipt is not None:
                    break
                time.sleep(poll_delay_counter())
                timeout.check()

        return txn_receipt
Пример #9
0
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
Пример #10
0
def test_miner_stop(webu_empty):
    webu = webu_empty

    assert webu.eth.mining
    assert webu.miner.hashrate

    webu.miner.stop()

    with Timeout(60) as timeout:
        while webu.eth.mining or webu.eth.hashrate:
            timeout.sleep(random.random())
            timeout.check()

    assert not webu.eth.mining
    assert not webu.miner.hashrate
Пример #11
0
def test_miner_setGasPrice(webu_empty, wait_for_block):
    webu = webu_empty

    initial_gas_price = webu.eth.gasPrice

    # sanity check
    assert webu.eth.gasPrice > 1000

    webu.miner.setGasPrice(initial_gas_price // 2)

    with Timeout(60) as timeout:
        while webu.eth.gasPrice == initial_gas_price:
            timeout.sleep(random.random())

    after_gas_price = webu.eth.gasPrice
    assert after_gas_price < initial_gas_price
Пример #12
0
def test_miner_setExtra(webu_empty, wait_for_block):
    webu = webu_empty

    initial_extra = decode_hex(webu.eth.getBlock(webu.eth.blockNumber)['extraData'])

    new_extra_data = b'-this-is-32-bytes-of-extra-data-'

    # sanity
    assert initial_extra != new_extra_data

    webu.miner.setExtra(new_extra_data)

    with Timeout(60) as timeout:
        while True:
            extra_data = decode_hex(webu.eth.getBlock(webu.eth.blockNumber)['extraData'])
            if extra_data == new_extra_data:
                break
            timeout.sleep(random.random())

    after_extra = decode_hex(webu.eth.getBlock(webu.eth.blockNumber)['extraData'])

    assert after_extra == new_extra_data
Пример #13
0
def test_txpool_inspect(webu_empty):
    webu = webu_empty

    webu.miner.stop()

    with Timeout(60) as timeout:
        while webu.miner.hashrate or webu.eth.mining:
            timeout.sleep(random.random())

    txn_1_hash = webu.eth.sendTransaction({
        'from': webu.eth.coinbase,
        'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
        'value': 12345,
    })
    txn_1 = webu.eth.getTransaction(txn_1_hash)
    txn_2_hash = webu.eth.sendTransaction({
        'from': webu.eth.coinbase,
        'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
        'value': 54321,
    })
    txn_2 = webu.eth.getTransaction(txn_2_hash)

    inspect_content = webu.txpool.inspect

    assert webu.eth.coinbase in inspect_content['pending']

    pending_txns = inspect_content['pending'][webu.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
Пример #14
0
def test_miner_start(webu_empty, wait_for_miner_start):
    webu = webu_empty

    # sanity
    assert webu.eth.mining
    assert webu.miner.hashrate

    webu.miner.stop()

    with Timeout(60) as timeout:
        while webu.eth.mining or webu.eth.hashrate:
            timeout.sleep(random.random())

    assert not webu.eth.mining
    assert not webu.miner.hashrate

    webu.miner.start(1)

    wait_for_miner_start(webu)

    assert webu.eth.mining
    assert webu.miner.hashrate
Пример #15
0
def test_sync_filter_against_latest_blocks(webu, sleep_interval,
                                           wait_for_block):
    if HappyUCTesterProvider not in map(type, webu.providers):
        webu.providers = HappyUCTesterProvider()

    txn_filter = webu.eth.filter("latest")

    current_block = webu.eth.blockNumber

    wait_for_block(webu, 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 = [
        webu.eth.getBlock(n + 1).hash
        for n in range(current_block, current_block + 3)
    ]
    assert found_block_hashes == expected_block_hashes
Пример #16
0
 def _wait_for_miner_start(webu, timeout=60):
     poll_delay_counter = PollDelayCounter()
     with Timeout(timeout) as timeout:
         while not webu.eth.mining or not webu.eth.hashrate:
             time.sleep(poll_delay_counter())
             timeout.check()
Пример #17
0
def test_contextmanager_timeout():
    with pytest.raises(Timeout):
        with Timeout(0.01) as timeout:
            time.sleep(0.02)
            timeout.check()
Пример #18
0
def test_inline_timeout():
    timeout = Timeout(0.01)
    timeout.start()
    time.sleep(0.02)
    with pytest.raises(Timeout):
        timeout.check()
Пример #19
0
def test_inline_completion_before_timeout():
    timeout = Timeout(0.01)
    timeout.start()
    timeout.check()
    timeout.cancel()
    time.sleep(0.02)
Пример #20
0
def test_contextmanager_completion_before_timeout():
    with Timeout(0.01) as timeout:
        timeout.check()
    time.sleep(0.02)
Пример #21
0
def test_with_custom_exception_type():
    timeout = Timeout(0.01, ValueError)
    timeout.start()
    time.sleep(0.02)
    with pytest.raises(ValueError):
        timeout.check()