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

Commit

Permalink
Add call() & raise TransactionFailed()
Browse files Browse the repository at this point in the history
  • Loading branch information
karlfloersch committed May 8, 2017
1 parent d4c8667 commit 1ca06f3
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions ethereum/tools/tester2.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
config_string = ':info'
# configure_logging(config_string=config_string)

class TransactionFailed(Exception):
pass


class ABIContract(object): # pylint: disable=too-few-public-methods

def __init__(self, _chain, _abi, address):
Expand All @@ -70,20 +74,23 @@ def __init__(self, _chain, _abi, address):
self.translator = abi_translator

for function_name in self.translator.function_data:
function = self.method_factory(_chain, function_name)
if self.translator.function_data[function_name]['is_constant']:
function = self.method_factory(_chain.call, function_name)
else:
function = self.method_factory(_chain.tx, function_name)
method = types.MethodType(function, self)
setattr(self, function_name, method)

@staticmethod
def method_factory(test_chain, function_name):
def method_factory(tx_or_call, function_name):
""" Return a proxy for calling a contract method with automatic encoding of
argument and decoding of results.
"""

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

result = test_chain.tx( # pylint: disable=protected-access
result = tx_or_call( # pylint: disable=protected-access
sender=key,
to=self.address,
value=kwargs.get('value', 0),
Expand Down Expand Up @@ -121,9 +128,19 @@ def tx(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, g
success, output = apply_transaction(self.head_state, transaction)
self.block.transactions.append(transaction)
if not success:
return False
raise TransactionFailed()
return output

def call(self, sender=k0, to=b'\x00' * 20, value=0, data=b'', startgas=STARTGAS, gasprice=GASPRICE):
snapshot = self.snapshot()
try:
output = self.tx(sender, to, value, data, startgas, gasprice)
self.revert(snapshot)
return output
except Exception as e:
self.revert(snapshot)
raise e

def contract(self, sourcecode, args=[], sender=k0, value=0, language='evm', startgas=STARTGAS, gasprice=GASPRICE):
if language == 'evm':
assert len(args) == 0
Expand Down

0 comments on commit 1ca06f3

Please sign in to comment.