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

Parse revert reason #1585

Merged
merged 23 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/941.feature.rst
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.
36 changes: 36 additions & 0 deletions tests/core/contracts/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
CONTRACT_RECEIVE_FUNCTION_CODE,
CONTRACT_RECEIVE_FUNCTION_RUNTIME,
)
from web3._utils.module_testing.revert_contract import (
_REVERT_CONTRACT_ABI,
REVERT_CONTRACT_BYTECODE,
REVERT_CONTRACT_RUNTIME_CODE,
)

CONTRACT_NESTED_TUPLE_SOURCE = """
pragma solidity >=0.4.19 <0.6.0;
Expand Down Expand Up @@ -893,6 +898,37 @@ def CallerTesterContract(web3, CALLER_TESTER_CONTRACT):
return web3.eth.contract(**CALLER_TESTER_CONTRACT)


@pytest.fixture()
def REVERT_CONTRACT_CODE():
return REVERT_CONTRACT_BYTECODE


@pytest.fixture()
def REVERT_CONTRACT_RUNTIME():
return REVERT_CONTRACT_RUNTIME_CODE


@pytest.fixture()
def REVERT_CONTRACT_ABI():
return _REVERT_CONTRACT_ABI


@pytest.fixture()
def REVERT_FUNCTION_CONTRACT(REVERT_CONTRACT_CODE,
REVERT_CONTRACT_RUNTIME,
REVERT_CONTRACT_ABI):
return {
'bytecode': REVERT_CONTRACT_CODE,
'bytecode_runtime': REVERT_CONTRACT_RUNTIME,
'abi': REVERT_CONTRACT_ABI,
}


@pytest.fixture()
def RevertContract(web3, REVERT_FUNCTION_CONTRACT):
return web3.eth.contract(**REVERT_FUNCTION_CONTRACT)


class LogFunctions:
LogAnonymous = 0
LogNoArguments = 1
Expand Down
22 changes: 22 additions & 0 deletions tests/core/contracts/test_contract_call_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
MismatchedABI,
NoABIFound,
NoABIFunctionsFound,
SolidityError,
ValidationError,
)

Expand Down Expand Up @@ -120,6 +121,19 @@ def payable_tester_contract(web3, PayableTesterContract, address_conversion_func
return deploy(web3, PayableTesterContract, address_conversion_func)


@pytest.fixture()
def revert_contract(web3, RevertContract, address_conversion_func):
return deploy(web3, RevertContract, address_conversion_func)


@pytest.fixture()
def call_transaction():
return {
'data': '0x61bc221a',
'to': '0xc305c901078781C232A2a521C2aF7980f8385ee9'
}


@pytest.fixture(params=[
'0x0406040604060406040604060406040604060406040604060406040604060406',
'0406040604060406040604060406040604060406040604060406040604060406',
Expand Down Expand Up @@ -855,3 +869,11 @@ def test_call_tuple_contract(tuple_contract, method_input, expected):
def test_call_nested_tuple_contract(nested_tuple_contract, method_input, expected):
result = nested_tuple_contract.functions.method(method_input).call()
assert result == expected


def test_call_revert_contract(revert_contract):
with pytest.raises(SolidityError, match="Function has been reverted."):
# eth-tester will do a gas estimation if we don't submit a gas value,
# which does not contain the revert reason. Avoid that by giving a gas
# value.
revert_contract.functions.revertWithMessage().call({'gas': 100000})
65 changes: 65 additions & 0 deletions tests/core/utilities/test_method_formatters.py
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:
wolovim marked this conversation as resolved.
Show resolved Hide resolved
assert get_revert_reason(REVERT_WITH_MSG) == 'execution reverted: 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
16 changes: 15 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
MATH_ABI,
MATH_BYTECODE,
)
from web3._utils.module_testing.revert_contract import (
_REVERT_CONTRACT_ABI,
REVERT_CONTRACT_BYTECODE,
)


@pytest.fixture(scope="module")
Expand All @@ -19,7 +23,17 @@ def math_contract_factory(web3):

@pytest.fixture(scope="module")
def emitter_contract_factory(web3):
contract_factory = web3.eth.contract(abi=CONTRACT_EMITTER_ABI, bytecode=CONTRACT_EMITTER_CODE)
contract_factory = web3.eth.contract(
abi=CONTRACT_EMITTER_ABI, bytecode=CONTRACT_EMITTER_CODE
)
return contract_factory


@pytest.fixture(scope="module")
def revert_contract_factory(web3):
contract_factory = web3.eth.contract(
abi=_REVERT_CONTRACT_ABI, bytecode=REVERT_CONTRACT_BYTECODE
)
return contract_factory


Expand Down
26 changes: 16 additions & 10 deletions tests/integration/generate_fixtures/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,28 @@
"config": {
"chainId": 131277322940537, # the string 'web3py' as an integer
"homesteadBlock": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
# "petersburgBlock": 0,
wolovim marked this conversation as resolved.
Show resolved Hide resolved
# "istanbulBlock": 0,
wolovim marked this conversation as resolved.
Show resolved Hide resolved
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"eip160Block": 0,
},
"nonce": "0x0000000000000042",
"alloc": {
COINBASE: {
"balance": "1000000000000000000000000000"
},
UNLOCKABLE_ACCOUNT: {
"balance": "1000000000000000000000000000"
},
RAW_TXN_ACCOUNT: {
"balance": "1000000000000000000000000000"
}
COINBASE: {"balance": "1000000000000000000000000000"},
UNLOCKABLE_ACCOUNT: {"balance": "1000000000000000000000000000"},
RAW_TXN_ACCOUNT: {"balance": "1000000000000000000000000000"},
"0000000000000000000000000000000000000001": {"balance": "1"},
"0000000000000000000000000000000000000002": {"balance": "1"},
"0000000000000000000000000000000000000003": {"balance": "1"},
"0000000000000000000000000000000000000004": {"balance": "1"},
"0000000000000000000000000000000000000005": {"balance": "1"},
"0000000000000000000000000000000000000006": {"balance": "1"},
# "0000000000000000000000000000000000000007": {"balance": "1"},
wolovim marked this conversation as resolved.
Show resolved Hide resolved
# "0000000000000000000000000000000000000008": {"balance": "1"},
# "0000000000000000000000000000000000000009": {"balance": "1"},
},
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down
Loading