forked from ethereum/web3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solidity allows messages in `require` statements which can help a lot when debugging. This commit parses the revert reasons from failing `call`s and raises them as `SolidityError` exceptions. Closes ethereum#941.
- Loading branch information
Showing
6 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Raise `SolidityError` exceptions that contain the revert reason when a `call` fails. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pytest | ||
|
||
from web3._utils.method_formatters import ( | ||
get_error_formatters, | ||
get_revert_reason, | ||
) | ||
from web3._utils.rpc_abi import ( | ||
RPC, | ||
) | ||
from web3.exceptions import ( | ||
SolidityError, | ||
) | ||
from web3.types import ( | ||
RPCResponse, | ||
) | ||
|
||
REVERT_WITH_MSG = RPCResponse({ | ||
'jsonrpc': '2.0', | ||
'error': { | ||
'code': -32015, | ||
'message': 'VM execution error.', | ||
'data': ( | ||
'Reverted ' | ||
'0x08c379a' | ||
'00000000000000000000000000000000000000000000000000000000000000020' | ||
'0000000000000000000000000000000000000000000000000000000000000016' | ||
'6e6f7420616c6c6f77656420746f206d6f6e69746f7200000000000000000000' | ||
), | ||
}, | ||
'id': 2987, | ||
}) | ||
|
||
REVERT_WITHOUT_MSG = RPCResponse({ | ||
'jsonrpc': '2.0', | ||
'error': { | ||
'code': -32015, | ||
'message': 'VM execution error.', | ||
'data': 'Reverted 0x', | ||
}, | ||
'id': 2987, | ||
}) | ||
|
||
OTHER_ERROR = RPCResponse({ | ||
"jsonrpc": "2.0", | ||
"error": { | ||
"code": -32601, | ||
"message": "Method not found", | ||
}, | ||
"id": 1, | ||
}) | ||
|
||
|
||
def test_get_revert_reason() -> None: | ||
assert get_revert_reason(REVERT_WITH_MSG) == 'not allowed to monitor' | ||
assert get_revert_reason(REVERT_WITHOUT_MSG) == '' | ||
assert get_revert_reason(OTHER_ERROR) is None | ||
|
||
|
||
def test_get_error_formatters() -> None: | ||
formatters = get_error_formatters(RPC.eth_call) | ||
with pytest.raises(SolidityError, match='not allowed to monitor'): | ||
formatters(REVERT_WITH_MSG) | ||
with pytest.raises(SolidityError): | ||
formatters(REVERT_WITHOUT_MSG) | ||
assert formatters(OTHER_ERROR) == OTHER_ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters