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

Commit b7a551c

Browse files
committed
support to select a contract by name from compiled source
1 parent 6807ecf commit b7a551c

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

ethereum/_solidity.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,28 @@ def contract_names(cls, code):
5757
return names
5858

5959
@classmethod
60-
def compile(cls, code):
60+
def compile(cls, code, contract_name=''):
6161
"returns binary of last contract in code"
6262
sorted_contracts = cls.combined(code)
63-
return sorted_contracts[-1][1]['binary'].decode('hex')
63+
if contract_name:
64+
idx = [x[0] for x in sorted_contracts].index(contract_name)
65+
else:
66+
idx = -1
67+
return sorted_contracts[idx][1]['binary'].decode('hex')
6468

6569
@classmethod
66-
def mk_full_signature(cls, code):
70+
def mk_full_signature(cls, code, contract_name=''):
6771
"returns signature of last contract in code"
6872
sorted_contracts = cls.combined(code)
69-
return sorted_contracts[-1][1]['json-abi']
73+
if contract_name:
74+
idx = [x[0] for x in sorted_contracts].index(contract_name)
75+
else:
76+
idx = -1
77+
return sorted_contracts[idx][1]['json-abi']
7078

7179
@classmethod
7280
def combined(cls, code):
73-
p = subprocess.Popen(['solc', '--combined-json', 'json-abi,binary,sol-abi'],
81+
p = subprocess.Popen(['solc', '--add-std=1', '--combined-json', 'json-abi,binary,sol-abi'],
7482
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
7583
stdoutdata, stderrdata = p.communicate(input=code)
7684
if p.returncode:
@@ -83,7 +91,7 @@ def combined(cls, code):
8391
data['sol-abi'] = yaml.safe_load(data['sol-abi'])
8492

8593
names = cls.contract_names(code)
86-
assert len(names) == len(contracts)
94+
assert len(names) <= len(contracts) # imported contracts are not returned
8795
sorted_contracts = []
8896
for name in names:
8997
sorted_contracts.append((name, contracts[name]))

ethereum/tester.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,24 @@ def contract(self, code, sender=k0, endowment=0, language='serpent', gas=None):
9797
assert len(self.block.get_code(o)), "Contract code empty"
9898
return o
9999

100-
def abi_contract(me, code, sender=k0, endowment=0, language='serpent', gas=None):
101-
100+
def abi_contract(me, code, sender=k0, endowment=0, language='serpent', contract_name='',
101+
gas=None):
102102
class _abi_contract():
103103

104-
def __init__(self, _state, code, sender=k0,
105-
endowment=0, language='serpent'):
104+
def __init__(self, _state, code, sender=k0, endowment=0, language='serpent'):
105+
if contract_name:
106+
assert language == 'solidity'
107+
cn_args = dict(contract_name=contract_name)
108+
else:
109+
cn_args = {}
106110
if language not in languages:
107111
languages[language] = __import__(language)
108112
language = languages[language]
109-
evm = language.compile(code)
113+
evm = language.compile(code, **cn_args)
110114
self.address = me.evm(evm, sender, endowment, gas)
111115
assert len(me.block.get_code(self.address)), \
112116
"Contract code empty"
113-
sig = language.mk_full_signature(code)
117+
sig = language.mk_full_signature(code, **cn_args)
114118
self._translator = abi.ContractTranslator(sig)
115119

116120
def kall_factory(f):

ethereum/tests/profile_vm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def do_test_vm(filename, testname=None, testdata=None, limit=99999999, profiler=
1616
testutils.run_vm_test(testutils.fixture_to_bytes(testdata), testutils.VERIFY, profiler=profiler)
1717

1818
if __name__ == '__main__':
19-
num = 1000
19+
num = 5000
2020
print 'profile_vm.py [no_cprofile]'
2121
print 'loading tests'
2222
fixtures = testutils.get_tests_from_file_or_dir(

0 commit comments

Comments
 (0)