Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit d239955

Browse files
committed
Add call() & raise TransactionFailed()
1 parent 13743a4 commit d239955

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

ethereum/tester2.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
config_string = ':info'
5757
# configure_logging(config_string=config_string)
5858

59+
class TransactionFailed(Exception):
60+
pass
61+
62+
5963
class ABIContract(object): # pylint: disable=too-few-public-methods
6064

6165
def __init__(self, _chain, _abi, address):
@@ -69,20 +73,23 @@ def __init__(self, _chain, _abi, address):
6973
self.translator = abi_translator
7074

7175
for function_name in self.translator.function_data:
72-
function = self.method_factory(_chain, function_name)
76+
if self.translator.function_data[function_name]['is_constant']:
77+
function = self.method_factory(_chain.call, function_name)
78+
else:
79+
function = self.method_factory(_chain.tx, function_name)
7380
method = types.MethodType(function, self)
7481
setattr(self, function_name, method)
7582

7683
@staticmethod
77-
def method_factory(test_chain, function_name):
84+
def method_factory(tx_or_call, function_name):
7885
""" Return a proxy for calling a contract method with automatic encoding of
7986
argument and decoding of results.
8087
"""
8188

8289
def kall(self, *args, **kwargs):
8390
key = kwargs.get('sender', k0)
8491

85-
result = test_chain.tx( # pylint: disable=protected-access
92+
result = tx_or_call( # pylint: disable=protected-access
8693
sender=key,
8794
to=self.address,
8895
value=kwargs.get('value', 0),
@@ -119,9 +126,19 @@ def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, g
119126
success, output = apply_transaction(self.head_state, transaction)
120127
self.block.transactions.append(transaction)
121128
if not success:
122-
return False
129+
raise TransactionFailed()
123130
return output
124131

132+
def call(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE):
133+
snapshot = self.snapshot()
134+
try:
135+
output = self.tx(sender, to, value, data, startgas, gasprice)
136+
self.revert(snapshot)
137+
return output
138+
except Exception as e:
139+
self.revert(snapshot)
140+
raise e
141+
125142
def contract(self, sourcecode, args=[], sender=k0, value=0, language='evm', startgas=STARTGAS, gasprice=GASPRICE):
126143
if language == 'evm':
127144
assert len(args) == 0

0 commit comments

Comments
 (0)