Skip to content

Commit c741b05

Browse files
committed
Clean up waitForTransactionReceipt for merging
- stimulate -> simulate - poll latency reduced to 100ms - plenty of invocations for test to try out - doc fixups
1 parent 88880f5 commit c741b05

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,3 @@ def web3():
111111
w3.eth.enable_unaudited_features()
112112

113113
return w3
114-

docs/web3.eth.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,20 @@ The following methods are available on the ``web3.eth`` namespace.
328328
})
329329
330330
331-
.. py:method:: Eth.waitForTransactionReceipt(transaction_hash)
331+
.. py:method:: Eth.waitForTransactionReceipt(transaction_hash, timeout=120)
332332
333-
* Delegates to ``wait_for_transaction_receipt`` Method from ``web3.utils.transactions``
333+
Waits for the transaction specified by ``transaction_hash`` to be included in a block, then
334+
returns its transaction receipt.
334335

335-
Returns the transaction receipt specified by ``transaction_hash``. It waits for transaction to mine before returning the receipt.
336+
Optionally, specify a ``timeout`` in seconds. If timeout elapses before the transaction
337+
is added to a block, then :meth:`~Eth.waitForTransactionReceipt` returns None.
336338

337339
.. code-block:: python
338340
339-
>>> web3.eth.waitForTransactionReceipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060') # not yet mined
340-
# waits for transaction to mine before returning transaction data
341+
>>> web3.eth.waitForTransactionReceipt('0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060')
342+
# If transaction is not yet in a block, time passes, while the thread sleeps...
343+
# ...
344+
# Then when the transaction is added to a block, its receipt is returned:
341345
AttributeDict({
342346
'blockHash': '0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd',
343347
'blockNumber': 46147,

tests/core/eth-module/test_transactions.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from web3.exceptions import (
44
ValidationError,
55
)
6-
from web3.middleware.stimulate_unmined_transaction import (
6+
from web3.middleware.simulate_unmined_transaction import (
77
unmined_receipt_simulator_middleware,
88
)
99

@@ -52,8 +52,7 @@ def test_unmined_transaction_wait_for_receipt(web3, extra_accounts):
5252
'value': 123457
5353
})
5454
assert web3.eth.getTransactionReceipt(txn_hash) is None
55-
assert web3.eth.getTransactionReceipt(txn_hash) is None
56-
txn_receipt = web3.eth.getTransactionReceipt(txn_hash)
57-
print(txn_receipt)
55+
56+
txn_receipt = web3.eth.waitForTransactionReceipt(txn_hash)
5857
assert txn_receipt['transactionHash'] == txn_hash
59-
assert txn_receipt['blockHash']
58+
assert txn_receipt['blockHash'] is not None

web3/eth.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ def getTransactionFromBlock(self, block_identifier, transaction_index):
204204
)
205205

206206
def waitForTransactionReceipt(self, transaction_hash, timeout=120):
207-
txn_receipt = wait_for_transaction_receipt(self.web3, transaction_hash, timeout)
208-
return txn_receipt
207+
return wait_for_transaction_receipt(self.web3, transaction_hash, timeout)
209208

210209
def getTransactionReceipt(self, transaction_hash):
211210
return self.web3.manager.request_blocking(

web3/middleware/stimulate_unmined_transaction.py renamed to web3/middleware/simulate_unmined_transaction.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
counter = itertools.count()
55

6+
INVOCATIONS_BEFORE_RESULT = 5
7+
68

79
def unmined_receipt_simulator_middleware(make_request, web3):
810
receipt_counters = collections.defaultdict(itertools.count)
911

1012
def middleware(method, params):
1113
if method == 'eth_getTransactionReceipt':
1214
txn_hash = params[0]
13-
if next(receipt_counters[txn_hash]) < 2:
15+
if next(receipt_counters[txn_hash]) < INVOCATIONS_BEFORE_RESULT:
1416
return {'result': None}
15-
return make_request(method, params)
17+
else:
18+
return make_request(method, params)
19+
else:
20+
return make_request(method, params)
1621
return middleware

web3/utils/transactions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import math
2-
import random
32

43
from cytoolz import (
54
assoc,
@@ -50,13 +49,13 @@ def fill_transaction_defaults(web3, transaction):
5049
return merge(defaults, transaction)
5150

5251

53-
def wait_for_transaction_receipt(web3, txn_hash, timeout=120):
52+
def wait_for_transaction_receipt(web3, txn_hash, timeout=120, poll_latency=0.1):
5453
with Timeout(timeout) as _timeout:
5554
while True:
5655
txn_receipt = web3.eth.getTransactionReceipt(txn_hash)
5756
if txn_receipt is not None:
5857
break
59-
_timeout.sleep(random.random())
58+
_timeout.sleep(poll_latency)
6059
return txn_receipt
6160

6261

0 commit comments

Comments
 (0)