Skip to content

Commit 5d0aac4

Browse files
committed
Raise new w3.eth.waitForTransactionReceipt timeout
1 parent cc2b67c commit 5d0aac4

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

tests/core/eth-module/test_transactions.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import pytest
22

33
from web3.exceptions import (
4+
TimeExhausted,
45
ValidationError,
56
)
67
from web3.middleware.simulate_unmined_transaction import (
78
unmined_receipt_simulator_middleware,
89
)
910

11+
RECEIPT_TIMEOUT = 0.2
12+
1013

1114
@pytest.mark.parametrize(
1215
'make_chain_id, expect_success',
@@ -28,16 +31,22 @@ def test_send_transaction_with_valid_chain_id(web3, make_chain_id, expect_succes
2831
'chainId': make_chain_id(web3),
2932
}
3033
if expect_success:
31-
# just be happy that we didn't crash
32-
web3.eth.sendTransaction(transaction)
34+
txn_hash = web3.eth.sendTransaction(transaction)
35+
receipt = web3.eth.waitForTransactionReceipt(txn_hash, timeout=RECEIPT_TIMEOUT)
36+
assert receipt.get('blockNumber') is not None
3337
else:
3438
with pytest.raises(ValidationError) as exc_info:
3539
web3.eth.sendTransaction(transaction)
3640

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

3943

40-
def test_unmined_transaction_wait_for_receipt(web3, extra_accounts):
44+
def test_wait_for_missing_receipt(web3):
45+
with pytest.raises(TimeExhausted):
46+
web3.eth.waitForTransactionReceipt(b'\0' * 32, timeout=RECEIPT_TIMEOUT)
47+
48+
49+
def test_unmined_transaction_wait_for_receipt(web3):
4150
web3.middleware_stack.add(unmined_receipt_simulator_middleware)
4251
txn_hash = web3.eth.sendTransaction({
4352
'from': web3.eth.coinbase,

tests/generate_go_ethereum_fixture.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,11 @@ def verify_chain_state(web3, chain_data):
297297

298298

299299
def mine_transaction_hash(web3, txn_hash):
300-
start_time = time.time()
301300
web3.miner.start(1)
302-
while time.time() < start_time + 60:
303-
receipt = web3.eth.waitForTransactionReceipt(txn_hash)
304-
if receipt is not None:
305-
web3.miner.stop()
306-
return receipt
307-
else:
308-
time.sleep(0.1)
309-
else:
310-
raise ValueError("Math contract deploy transaction not mined during wait period")
301+
try:
302+
return web3.eth.waitForTransactionReceipt(txn_hash, timeout=60)
303+
finally:
304+
web3.miner.stop()
311305

312306

313307
def mine_block(web3):

web3/eth.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
LogFilter,
2828
TransactionFilter,
2929
)
30+
from web3._utils.threads import (
31+
Timeout,
32+
)
3033
from web3._utils.toolz import (
3134
assoc,
3235
merge,
@@ -42,6 +45,9 @@
4245
from web3.contract import (
4346
Contract,
4447
)
48+
from web3.exceptions import (
49+
TimeExhausted,
50+
)
4551
from web3.iban import (
4652
Iban,
4753
)
@@ -220,7 +226,15 @@ def getTransactionByBlock(self, block_identifier, transaction_index):
220226
)
221227

222228
def waitForTransactionReceipt(self, transaction_hash, timeout=120):
223-
return wait_for_transaction_receipt(self.web3, transaction_hash, timeout)
229+
try:
230+
return wait_for_transaction_receipt(self.web3, transaction_hash, timeout)
231+
except Timeout:
232+
raise TimeExhausted(
233+
"Transaction {} is not in the chain, after {} seconds".format(
234+
transaction_hash,
235+
timeout,
236+
)
237+
)
224238

225239
def getTransactionReceipt(self, transaction_hash):
226240
return self.web3.manager.request_blocking(

web3/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ class InsufficientData(Exception):
107107
complete a calculation
108108
"""
109109
pass
110+
111+
112+
class TimeExhausted(Exception):
113+
"""
114+
Raised when a method has not retrieved the desired result within a specified timeout.
115+
"""
116+
pass

0 commit comments

Comments
 (0)