Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise external timeout #1070

Merged
merged 2 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/web3.eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ The following methods are available on the ``web3.eth`` namespace.
returns its transaction receipt.

Optionally, specify a ``timeout`` in seconds. If timeout elapses before the transaction
is added to a block, then :meth:`~Eth.waitForTransactionReceipt` raises a `Timeout` exception.
is added to a block, then :meth:`~Eth.waitForTransactionReceipt` raises a
:class:`web3.exceptions.TimeExhausted` exception.

.. code-block:: python

Expand Down
15 changes: 12 additions & 3 deletions tests/core/eth-module/test_transactions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import pytest

from web3.exceptions import (
TimeExhausted,
ValidationError,
)
from web3.middleware.simulate_unmined_transaction import (
unmined_receipt_simulator_middleware,
)

RECEIPT_TIMEOUT = 0.2


@pytest.mark.parametrize(
'make_chain_id, expect_success',
Expand All @@ -28,16 +31,22 @@ def test_send_transaction_with_valid_chain_id(web3, make_chain_id, expect_succes
'chainId': make_chain_id(web3),
}
if expect_success:
# just be happy that we didn't crash
web3.eth.sendTransaction(transaction)
txn_hash = web3.eth.sendTransaction(transaction)
receipt = web3.eth.waitForTransactionReceipt(txn_hash, timeout=RECEIPT_TIMEOUT)
assert receipt.get('blockNumber') is not None
else:
with pytest.raises(ValidationError) as exc_info:
web3.eth.sendTransaction(transaction)

assert 'chain ID' in str(exc_info.value)


def test_unmined_transaction_wait_for_receipt(web3, extra_accounts):
def test_wait_for_missing_receipt(web3):
with pytest.raises(TimeExhausted):
web3.eth.waitForTransactionReceipt(b'\0' * 32, timeout=RECEIPT_TIMEOUT)


def test_unmined_transaction_wait_for_receipt(web3):
web3.middleware_stack.add(unmined_receipt_simulator_middleware)
txn_hash = web3.eth.sendTransaction({
'from': web3.eth.coinbase,
Expand Down
14 changes: 4 additions & 10 deletions tests/generate_go_ethereum_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,11 @@ def verify_chain_state(web3, chain_data):


def mine_transaction_hash(web3, txn_hash):
start_time = time.time()
web3.miner.start(1)
while time.time() < start_time + 60:
receipt = web3.eth.waitForTransactionReceipt(txn_hash)
if receipt is not None:
web3.miner.stop()
return receipt
else:
time.sleep(0.1)
else:
raise ValueError("Math contract deploy transaction not mined during wait period")
try:
return web3.eth.waitForTransactionReceipt(txn_hash, timeout=60)
finally:
web3.miner.stop()


def mine_block(web3):
Expand Down
16 changes: 15 additions & 1 deletion web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
LogFilter,
TransactionFilter,
)
from web3._utils.threads import (
Timeout,
)
from web3._utils.toolz import (
assoc,
merge,
Expand All @@ -42,6 +45,9 @@
from web3.contract import (
Contract,
)
from web3.exceptions import (
TimeExhausted,
)
from web3.iban import (
Iban,
)
Expand Down Expand Up @@ -220,7 +226,15 @@ def getTransactionByBlock(self, block_identifier, transaction_index):
)

def waitForTransactionReceipt(self, transaction_hash, timeout=120):
return wait_for_transaction_receipt(self.web3, transaction_hash, timeout)
try:
return wait_for_transaction_receipt(self.web3, transaction_hash, timeout)
except Timeout:
raise TimeExhausted(
"Transaction {} is not in the chain, after {} seconds".format(
transaction_hash,
timeout,
)
)

def getTransactionReceipt(self, transaction_hash):
return self.web3.manager.request_blocking(
Expand Down
7 changes: 7 additions & 0 deletions web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ class InsufficientData(Exception):
complete a calculation
"""
pass


class TimeExhausted(Exception):
"""
Raised when a method has not retrieved the desired result within a specified timeout.
"""
pass