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

Commit

Permalink
support to select a contract by name from compiled source
Browse files Browse the repository at this point in the history
  • Loading branch information
heikoheiko committed Apr 29, 2015
1 parent 6807ecf commit b7a551c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
20 changes: 14 additions & 6 deletions ethereum/_solidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,28 @@ def contract_names(cls, code):
return names

@classmethod
def compile(cls, code):
def compile(cls, code, contract_name=''):
"returns binary of last contract in code"
sorted_contracts = cls.combined(code)
return sorted_contracts[-1][1]['binary'].decode('hex')
if contract_name:
idx = [x[0] for x in sorted_contracts].index(contract_name)
else:
idx = -1
return sorted_contracts[idx][1]['binary'].decode('hex')

@classmethod
def mk_full_signature(cls, code):
def mk_full_signature(cls, code, contract_name=''):
"returns signature of last contract in code"
sorted_contracts = cls.combined(code)
return sorted_contracts[-1][1]['json-abi']
if contract_name:
idx = [x[0] for x in sorted_contracts].index(contract_name)
else:
idx = -1
return sorted_contracts[idx][1]['json-abi']

@classmethod
def combined(cls, code):
p = subprocess.Popen(['solc', '--combined-json', 'json-abi,binary,sol-abi'],
p = subprocess.Popen(['solc', '--add-std=1', '--combined-json', 'json-abi,binary,sol-abi'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdoutdata, stderrdata = p.communicate(input=code)
if p.returncode:
Expand All @@ -83,7 +91,7 @@ def combined(cls, code):
data['sol-abi'] = yaml.safe_load(data['sol-abi'])

names = cls.contract_names(code)
assert len(names) == len(contracts)
assert len(names) <= len(contracts) # imported contracts are not returned
sorted_contracts = []
for name in names:
sorted_contracts.append((name, contracts[name]))
Expand Down
16 changes: 10 additions & 6 deletions ethereum/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,24 @@ def contract(self, code, sender=k0, endowment=0, language='serpent', gas=None):
assert len(self.block.get_code(o)), "Contract code empty"
return o

def abi_contract(me, code, sender=k0, endowment=0, language='serpent', gas=None):

def abi_contract(me, code, sender=k0, endowment=0, language='serpent', contract_name='',
gas=None):
class _abi_contract():

def __init__(self, _state, code, sender=k0,
endowment=0, language='serpent'):
def __init__(self, _state, code, sender=k0, endowment=0, language='serpent'):
if contract_name:
assert language == 'solidity'
cn_args = dict(contract_name=contract_name)
else:
cn_args = {}
if language not in languages:
languages[language] = __import__(language)
language = languages[language]
evm = language.compile(code)
evm = language.compile(code, **cn_args)
self.address = me.evm(evm, sender, endowment, gas)
assert len(me.block.get_code(self.address)), \
"Contract code empty"
sig = language.mk_full_signature(code)
sig = language.mk_full_signature(code, **cn_args)
self._translator = abi.ContractTranslator(sig)

def kall_factory(f):
Expand Down
2 changes: 1 addition & 1 deletion ethereum/tests/profile_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def do_test_vm(filename, testname=None, testdata=None, limit=99999999, profiler=
testutils.run_vm_test(testutils.fixture_to_bytes(testdata), testutils.VERIFY, profiler=profiler)

if __name__ == '__main__':
num = 1000
num = 5000
print 'profile_vm.py [no_cprofile]'
print 'loading tests'
fixtures = testutils.get_tests_from_file_or_dir(
Expand Down

0 comments on commit b7a551c

Please sign in to comment.