Skip to content

Commit 483e27d

Browse files
committed
enable test_solidity.py
1 parent 3198480 commit 483e27d

File tree

3 files changed

+51
-68
lines changed

3 files changed

+51
-68
lines changed

ethereum/todo_tests/test_solidity.py ethereum/tests/test_solidity.py

+39-65
Original file line numberDiff line numberDiff line change
@@ -5,76 +5,50 @@
55

66
from ethereum.utils import encode_hex
77

8-
from ethereum import tester
8+
from ethereum.tools import tester
99
from ethereum import utils
10-
from ethereum import _solidity
11-
from ethereum._solidity import get_solidity
10+
import ethereum.config as config
11+
from ethereum.tools import _solidity
12+
from ethereum.tools._solidity import get_solidity
1213

1314
SOLIDITY_AVAILABLE = get_solidity() is not None
1415
CONTRACTS_DIR = path.join(path.dirname(__file__), 'contracts')
1516

17+
skip_if_no_solidity = pytest.mark.skipif(
18+
not SOLIDITY_AVAILABLE,
19+
reason='solc compiler not available')
1620

1721
def bytecode_is_generated(cinfo, cname):
1822
return 'code' in cinfo[cname] and len(cinfo[cname]['code']) > 10
1923

2024

21-
@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
22-
reason='solc compiler not available')
23-
def test_library_from_file():
24-
state = tester.state()
25-
state.env.config['HOMESTEAD_FORK_BLKNUM'] = 0 # enable CALLCODE opcode
26-
27-
library = state.abi_contract(
28-
None,
29-
path=path.join(CONTRACTS_DIR, 'seven_library.sol'),
30-
language='solidity',
31-
)
32-
33-
libraries = {
34-
'SevenLibrary': encode_hex(library.address),
35-
}
36-
contract = state.abi_contract(
37-
None,
38-
path=path.join(CONTRACTS_DIR, 'seven_contract.sol'),
39-
libraries=libraries,
40-
language='solidity',
41-
)
42-
43-
# pylint: disable=no-member
44-
assert library.seven() == 7
45-
assert contract.test() == 7
46-
47-
48-
@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
49-
reason='solc compiler not available')
25+
@skip_if_no_solidity
5026
def test_library_from_code():
5127
with open(path.join(CONTRACTS_DIR, 'seven_library.sol')) as handler:
5228
library_code = handler.read()
5329

5430
with open(path.join(CONTRACTS_DIR, 'seven_contract_without_import.sol')) as handler:
5531
contract_code = handler.read()
5632

57-
state = tester.state()
58-
state.env.config['HOMESTEAD_FORK_BLKNUM'] = 0 # enable CALLCODE opcode
33+
state = tester.Chain()
34+
env = config.Env()
35+
env.config['HOMESTEAD_FORK_BLKNUM'] = 0 # enable CALLCODE opcode
5936

60-
library = state.abi_contract(
61-
library_code,
62-
path=None,
37+
library = state.contract(
38+
sourcecode=library_code,
6339
language='solidity',
6440
)
6541

6642
libraries = {
6743
'SevenLibrary': encode_hex(library.address),
6844
}
69-
contract = state.abi_contract(
70-
contract_code,
71-
path=None,
45+
contract = state.contract(
46+
sourcecode=contract_code,
7247
libraries=libraries,
7348
language='solidity',
7449
)
7550

7651
# pylint: disable=no-member
77-
assert library.seven() == 7
7852
assert contract.test() == 7
7953

8054

@@ -129,17 +103,16 @@ def test_symbols():
129103
) == 'beef1111111111111111111111111111111111111111cafe'
130104

131105

132-
@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
133-
reason='solc compiler not available')
106+
@skip_if_no_solidity
134107
def test_interop():
135108
serpent_contract = """
136-
extern solidity: [sub2:[]:i]
109+
extern solidity: [sub2:[]:i]
137110
138-
def main(a):
139-
return(a.sub2() * 2)
111+
def main(a):
112+
return(a.sub2() * 2)
140113
141-
def sub1():
142-
return(5)
114+
def sub1():
115+
return(5)
143116
"""
144117

145118
solidity_contract = """
@@ -158,9 +131,13 @@ def sub1():
158131
}
159132
"""
160133

161-
state = tester.state()
162-
serpent_abi = state.abi_contract(serpent_contract)
163-
solidity_abi = state.abi_contract(
134+
state = tester.Chain()
135+
136+
serpent_abi = state.contract(
137+
serpent_contract,
138+
language='serpent')
139+
140+
solidity_abi = state.contract(
164141
solidity_contract,
165142
language='solidity') # should be zoo
166143
solidity_address = utils.encode_hex(solidity_abi.address)
@@ -171,12 +148,11 @@ def sub1():
171148

172149
assert solidity_abi.sub2() == 7
173150
assert solidity_abi.sub3(utils.encode_hex(
174-
solidity_abi.address)) == solidity_address
151+
solidity_abi.address)) == '0x' + solidity_address
175152
assert solidity_abi.main(serpent_abi.address) == 10
176153

177154

178-
@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
179-
reason='solc compiler not available')
155+
@skip_if_no_solidity
180156
def test_constructor():
181157
constructor_contract = """
182158
contract testme {
@@ -190,19 +166,19 @@ def test_constructor():
190166
}
191167
"""
192168

193-
state = tester.state()
194-
contract = state.abi_contract(
169+
state = tester.Chain()
170+
171+
contract = state.contract(
195172
constructor_contract,
196173
language='solidity',
197-
constructor_parameters=(2, ),
174+
args=(2, ),
198175
)
199176

200177
# pylint: disable=no-member
201178
assert contract.getValue() == 2
202179

203180

204-
@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
205-
reason='solc compiler not available')
181+
@skip_if_no_solidity
206182
def test_solidity_compile_rich():
207183
compile_rich_contract = """
208184
contract contract_add {
@@ -239,8 +215,7 @@ def test_solidity_compile_rich():
239215
} == {'subtract7', 'subtract42'}
240216

241217

242-
@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
243-
reason='solc compiler not available')
218+
@skip_if_no_solidity
244219
def test_abi_contract():
245220
one_contract = """
246221
contract foo {
@@ -253,17 +228,16 @@ def test_abi_contract():
253228
}
254229
"""
255230

256-
state = tester.state()
257-
contract = state.abi_contract(one_contract, language='solidity')
231+
state = tester.Chain()
232+
contract = state.contract(one_contract, language='solidity')
258233

259234
# pylint: disable=no-member
260235
assert contract.seven() == 7
261236
assert contract.mul2(2) == 4
262237
assert contract.mul2(-2) == -4
263238

264239

265-
@pytest.mark.skipif(not SOLIDITY_AVAILABLE,
266-
reason='solc compiler not available')
240+
@skip_if_no_solidity
267241
def test_extra_args():
268242
src = """
269243
contract foo {

ethereum/tools/_solidity.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ def solc_arguments(libraries=None, combined='bin,abi',
5858
'--combined-json', combined,
5959
]
6060

61+
def str_of(address):
62+
"""cast address to string. py2/3 compatability. """
63+
try:
64+
return address.decode('utf8')
65+
except AttributeError:
66+
return address
67+
68+
6169
if optimize:
6270
args.append('--optimize')
6371

@@ -70,7 +78,7 @@ def solc_arguments(libraries=None, combined='bin,abi',
7078
if libraries is not None and len(libraries):
7179
addresses = [
7280
'{name}:{address}'.format(
73-
name=name, address=address.decode('utf8'))
81+
name=name, address=str_of(address))
7482
for name, address in libraries.items()
7583
]
7684
args.extend([

ethereum/tools/tester.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def last_gas_used(self, with_tx=False):
225225
self.head_state.receipts[-2].gas_used
226226
return diff - (not with_tx) * self.last_tx.intrinsic_gas_used
227227

228-
def contract(self, sourcecode, args=[], sender=k0, value=0,
228+
def contract(self, sourcecode, args=[], sender=k0, value=0, libraries=None,
229229
language=None, l=None, startgas=STARTGAS, gasprice=GASPRICE):
230230
assert not (l and language)
231231
language = l or language
@@ -238,7 +238,8 @@ def contract(self, sourcecode, args=[], sender=k0, value=0,
238238
interface = compiler.mk_full_signature(sourcecode)
239239
ct = ContractTranslator(interface)
240240
code = compiler.compile(
241-
sourcecode) + (ct.encode_constructor_arguments(args) if args else b'')
241+
sourcecode, libraries=libraries
242+
) + (ct.encode_constructor_arguments(args) if args else b'')
242243
addr = self.tx(
243244
sender=sender,
244245
to=b'',

0 commit comments

Comments
 (0)