-
Notifications
You must be signed in to change notification settings - Fork 1.8k
added waitForTransactionReceipt to web3.eth which returns the transac… #669
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
Conversation
Test and docs yet to be added. please add |
how can I go about adding a test since it needs to have a pending transaction before it returns receipt? i did test it manually on Ropsten Network by creating a transaction manually and then running the function to test if it actually waits and returns the receipt after transaction is mined. thanks |
There is some new functionality in eth-tester to enable this. One approach would be something like: eth_tester_provider = list(web3.providers).pop()
assert hasattr(eth_tester_provider, 'ethereum_tester')
eth_tester = eth_tester_provider.ethereum_tester
def disable_in_1s():
time.sleep(1)
eth_tester.enable_auto_mine_transactions()
eth_tester.disable_auto_mine_transactions()
txn_hash = w3.eth.sendTransaction(...)
with pytest.raises(TimeoutException):
w3.eth.waitForTransactionReceipt(txn_hash, timeout=0.5)
Thread(disable_in_1s, daemon=True).start()
txn_receipt = eth.waitForTransactionReceipt(txn_hash, timeout=2)
assert txn_receipt is not None and txn_receipt['block_hash'] But yeah, it's a little hairy with the thread blocking. There's probably a better approach, but that would get it done. |
@carver what do you think about testing via something like this using middleware to simulate a transaction not yet being mined.
|
Yes, I like that much better 👍 It's simpler, and takes less time to run. For @scottydelta: It would be nice to confirm that the middleware is working with a manual check of the empty result first, then the wait (but with a short timeout so that the test completes quickly even if it fails): assert w3.eth.getTransactionReceipt(txn_hash) == None
receipt = w3.eth.waitForTransactionReceipt(txn_hash, timeout=1)
assert receipt['transaction_hash'] == txn_hash
assert receipt['block_hash'] |
You can run Based on the circle lint run, the only issue left is the ordering of the imports:
|
@carver I have fixed it on my end, just trying to make some time to add a test and complete this pull request. It will be done soon, thanks. |
web3/eth.py
Outdated
@@ -187,6 +188,10 @@ def getTransactionFromBlock(self, block_identifier, transaction_index): | |||
[block_identifier, transaction_index], | |||
) | |||
|
|||
def waitForTransactionReceipt(self, transaction_hash): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect this to have a timeout, like the underlying method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had that in mind, missed it somehow. I will add that, thanks.
@carver while I understand your initial suggested method of testing, I am finding @pipermerriam middleware method to stimulate un-mined transaction hard to understand. I haven't worked with tests alot. is the actual test executed like this?
|
Not exactly. You would add the w3.middleware_stack.add(unmined_receipt_simulator_middleware) Then you can call |
@pipermerriam thanks for the guidance. Wouldn't this test |
@scottydelta the middlewares don't know the difference between So, by constructing this middleware which responds to So, the idea is that you are testing Make sense? |
@pipermerriam It makes sense but the method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments on how to change the tests.
'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601', | ||
'value': 123457 | ||
}) | ||
assert web3.eth.getTransactionReceipt(txn_hash) is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave this assertion in place. This verifies that the transaction is showing as unmined.
}) | ||
assert web3.eth.getTransactionReceipt(txn_hash) is None | ||
assert web3.eth.getTransactionReceipt(txn_hash) is None | ||
txn_receipt = web3.eth.getTransactionReceipt(txn_hash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be changed to use the web3.eth.waitForTransactionReceipt
API, and the line above this should be removed.
def middleware(method, params): | ||
if method == 'eth_getTransactionReceipt': | ||
txn_hash = params[0] | ||
if next(receipt_counters[txn_hash]) < 2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would go ahead and bump this number up to something like 5 to ensure that the waitForTransactionReceipt
code hits this a few times prior to getting the actual receipt.
@scottydelta it looks like this is almost at the finish line. Are you able to work on it more? |
- stimulate -> simulate - poll latency reduced to 100ms - plenty of invocations for test to try out - doc fixups
…tion after its mined
What was wrong?
Fixes #574
How was it fixed?
Added
waitForTransactionReceipt
toweb3.eth
which waits for transaction to mine and returns the transaction once it is mined.Cute Animal Picture