Skip to content

Commit 48450d2

Browse files
committed
Merge pull request #59 from pipermerriam/piper/fix-how-empty-responses-are-handled
change populus handling of empty responses to function calls
2 parents 17d6404 + af387a8 commit 48450d2

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

populus/contracts/common.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
)
77

88

9+
class EmptyDataError(Exception):
10+
"""
11+
Raised when a call to a function unexpectedly returns empty data `0x` when
12+
a response was expected.
13+
"""
14+
pass
15+
16+
917
class ContractBound(object):
1018
_contract = None
1119

@@ -60,7 +68,13 @@ def output_types(self):
6068

6169
def cast_return_data(self, outputs):
6270
if len(self.output_types) != 1:
63-
return decode_multi(self.output_types, outputs)
71+
try:
72+
return decode_multi(self.output_types, outputs)
73+
except AssertionError:
74+
raise EmptyDataError("call to {0} unexpectedly returned no data".format(self))
6475
output_type = self.output_types[0]
6576

66-
return decode_single(output_type, outputs)
77+
try:
78+
return decode_single(output_type, outputs)
79+
except AssertionError:
80+
raise EmptyDataError("call to {0} unexpectedly returned no data".format(self))

populus/contracts/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
def decode_single(typ, data):
99
base, sub, _ = abi.process_type(typ)
1010

11+
# ensure that we aren't trying to decode an empty response.
12+
assert len(data) > 2
13+
1114
if base == 'address':
12-
if data == "0x":
13-
return "0x" + "0" * 40
1415
return '0x' + strip_0x_prefix(data[-40:])
1516
elif base == 'string' or base == 'bytes' or base == 'hash':
1617
if sub:

tests/contracts/test_decode_multi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def test_decode_two_uint32(input, expected):
1515
assert output == expected
1616

1717

18+
def test_empty_data_raises():
19+
with pytest.raises(AssertionError):
20+
decode_multi(['uint32', 'uint32'], '0x')
21+
22+
1823
def test_decode_various():
1924
data = ('0x00000000000000000000000082a978b3f5962a5b0957d9ee9eef472ee55b42f10000000000000'
2025
'0000000000000000000000000000000000000000000000000017374757069642070696e6b20616e69'

tests/contracts/test_decode_single_type.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ def test_decode_bytes():
7676
@pytest.mark.parametrize(
7777
'input,expected',
7878
(
79-
(
80-
'0x',
81-
'0x0000000000000000000000000000000000000000',
82-
),
8379
(
8480
'0x0000000000000000000000000000000000000000000000000000000000000000',
8581
'0x0000000000000000000000000000000000000000',
@@ -105,3 +101,9 @@ def test_decode_bytes():
105101
def test_decode_address(input, expected):
106102
output = decode_single('address', input)
107103
assert output == expected
104+
105+
106+
@pytest.mark.parametrize('_type', ('address', 'bytes32', 'uint256', 'int256', 'bool'))
107+
def test_raises_on_empty_data(_type):
108+
with pytest.raises(AssertionError):
109+
decode_single(_type, '0x')

0 commit comments

Comments
 (0)