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

feat: raise virtual machine errors #181

Merged
merged 3 commits into from
Oct 18, 2021
Merged

Conversation

antazoey
Copy link
Member

@antazoey antazoey commented Oct 11, 2021

What I did

  • Improved error handling around failed transactions.
  • Handle when a transaction is reported as "failing"

How I did it

  • Raise TransactionError from web3.py's ValueError (web3.py raises ValueError for different Virtual-machine related errors). Also raise when a transaciton is reported as failing.

How to verify it

when specifying a poor gas limit (too high, too low, etc), you should get TransactionErrors.

Like: ERROR: {'code': -32000, 'message': 'Transaction requires at least 63744 gas but got 1000'}
when the gas is too low (contract = test_account.deploy(contract_type, gas_limit=1000))

and ERROR: {'code': -32000, 'message': 'Transaction gas limit is 100000000 and exceeds block gas limit of 30000000'}
when the gas is too high (contract = test_account.deploy(contract_type, gas_limit=100000000))

Note: TransactionError ultimately will help us write the ape.test.reverts() fixture!

Checklist

  • Passes all linting checks (pre-commit and CI jobs)
  • New test cases have been added and are passing
  • Documentation has been updated
  • PR title follows Conventional Commit standard (will be automatically included in the changelog)

src/ape/api/accounts.py Outdated Show resolved Hide resolved
src/ape/api/accounts.py Outdated Show resolved Hide resolved
@antazoey antazoey marked this pull request as draft October 12, 2021 22:28
@antazoey antazoey marked this pull request as ready for review October 13, 2021 15:03
@antazoey antazoey closed this Oct 13, 2021
@antazoey antazoey changed the title fix: estimate gas bugfixes feat: transaction error handling Oct 13, 2021
@antazoey antazoey reopened this Oct 13, 2021
src/ape/api/accounts.py Outdated Show resolved Hide resolved
src/ape/api/accounts.py Outdated Show resolved Hide resolved
src/ape/api/accounts.py Outdated Show resolved Hide resolved
src/ape/api/accounts.py Outdated Show resolved Hide resolved
src/ape/api/providers.py Outdated Show resolved Hide resolved
tests/functional/api/test_accounts.py Show resolved Hide resolved
tests/functional/api/test_accounts.py Show resolved Hide resolved
tests/functional/ethereum/test_ecosystem.py Show resolved Hide resolved
@antazoey antazoey marked this pull request as draft October 13, 2021 21:51
@antazoey antazoey marked this pull request as ready for review October 13, 2021 22:46
@antazoey antazoey changed the title feat: transaction error handling feat: raise transaction error from web3 errors Oct 13, 2021
@antazoey antazoey changed the title feat: raise transaction error from web3 errors feat: raise transaction error in HTTP provider send_call() Oct 13, 2021
src/ape/api/contracts.py Outdated Show resolved Hide resolved
src/ape_http/providers.py Outdated Show resolved Hide resolved
@antazoey antazoey marked this pull request as ready for review October 14, 2021 20:53
src/ape/api/providers.py Show resolved Hide resolved
src/ape/exceptions.py Show resolved Hide resolved
src/ape/exceptions.py Outdated Show resolved Hide resolved
src/ape_http/providers.py Outdated Show resolved Hide resolved
@antazoey antazoey changed the title feat: raise transaction error in HTTP provider send_call() feat: virtual machine error API Oct 14, 2021
src/ape/api/networks.py Outdated Show resolved Hide resolved
src/ape_ethereum/ecosystem.py Outdated Show resolved Hide resolved
src/ape_http/providers.py Outdated Show resolved Hide resolved
src/ape/utils.py Outdated
@@ -140,6 +142,41 @@ def compute_checksum(source: bytes, algorithm: str = "md5") -> str:
return hasher(source).hexdigest()


def get_tx_error_from_web3_value_error(web3_value_error: ValueError) -> Optional[TransactionError]:
Copy link
Member Author

@antazoey antazoey Oct 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, this method only returns errors instead of raising them.
If it returns None, we did not detect a transaction related error and the user of the method handle that however.

In our ape-http example, if we don't get a transaction error from this, we raise the original error.

@antazoey
Copy link
Member Author

antazoey commented Oct 15, 2021

I have noticed an issue:

If estimating gas (not specifying a gas limit), and calling a transaction that will revert, the transaction fails at that point with the revert message, so that is why brownie must raise the VirtualMachineError then as well.


fixed

src/ape_http/providers.py Outdated Show resolved Hide resolved
@antazoey antazoey changed the title feat: virtual machine error API feat: raise virtual machine errors Oct 15, 2021
src/ape/api/accounts.py Outdated Show resolved Hide resolved
src/ape/utils.py Show resolved Hide resolved
src/ape_http/providers.py Outdated Show resolved Hide resolved

# If this is the cause of a would-be revert,
# raise the VirtualMachineError so that we can confirm tx-reverts.
if isinstance(txn, VirtualMachineError):
Copy link
Member Author

@antazoey antazoey Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We either have to do this, or any test that uses ape.reverts() will have to specify a gas_limit.


Edit:

Comment outdated, now says if isinstance(tx_error, VirtualMachineError):

@antazoey antazoey force-pushed the feat/tx-error branch 3 times, most recently from 449d9f4 to 28a1420 Compare October 18, 2021 16:29
src/ape_http/providers.py Outdated Show resolved Hide resolved
@antazoey antazoey merged commit 5ac1029 into ApeWorX:main Oct 18, 2021
@antazoey antazoey deleted the feat/tx-error branch October 18, 2021 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants