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

Commit f89042b

Browse files
committed
add solc wrapper
1 parent 1b94e89 commit f89042b

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

ethereum/_solidity.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import subprocess
2+
import os
3+
4+
5+
class CompileError(Exception):
6+
pass
7+
8+
9+
class solc_wrapper(object):
10+
11+
"wraps solc binary"
12+
13+
@classmethod
14+
def compiler_available(cls):
15+
program = 'solc'
16+
17+
def is_exe(fpath):
18+
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
19+
20+
fpath, fname = os.path.split(program)
21+
if fpath:
22+
if is_exe(program):
23+
return program
24+
else:
25+
for path in os.environ["PATH"].split(os.pathsep):
26+
path = path.strip('"')
27+
exe_file = os.path.join(path, program)
28+
if is_exe(exe_file):
29+
return exe_file
30+
31+
return None
32+
33+
@classmethod
34+
def split_contracts(cls, code):
35+
contracts = []
36+
contract = None
37+
for line in code.split('\n'):
38+
if line.lstrip().startswith('contract '): # FIXME
39+
if contract:
40+
contracts.append('\n'.join(contract))
41+
contract = [line]
42+
elif contract:
43+
contract.append(line)
44+
if contract:
45+
contracts.append('\n'.join(contract))
46+
return contracts
47+
48+
@classmethod
49+
def compile(cls, code):
50+
p = subprocess.Popen(['solc', '--binary', 'stdout'],
51+
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
52+
stdoutdata, stderrdata = p.communicate(input=code)
53+
if p.returncode:
54+
raise CompileError('compilation failed')
55+
56+
hex_code = stdoutdata.rsplit('Binary: \n')[-1].strip()
57+
return hex_code.decode('hex')
58+
59+
@classmethod
60+
def mk_full_signature(cls, code):
61+
p = subprocess.Popen(['solc', '--json-abi', 'stdout'],
62+
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
63+
stdoutdata, stderrdata = p.communicate(input=code)
64+
if p.returncode:
65+
raise CompileError('compilation failed')
66+
jsonabi = stdoutdata.rsplit('Contract JSON ABI\n')[-1].strip()
67+
return jsonabi
68+
69+
70+
def get_solidity():
71+
try:
72+
import solidity
73+
tester.languages['solidity'] = solidity
74+
except ImportError:
75+
if not solc_wrapper.compiler_available():
76+
return None
77+
return solc_wrapper
78+
79+
80+
if __name__ == '__main__':
81+
import tester
82+
assert 'solidity' in tester.languages
83+
84+
one_contract = """
85+
86+
contract foo {
87+
function seven() returns (int256 y) {
88+
y = 7;
89+
}
90+
function mul2(int256 x) returns (int256 y) {
91+
y = x * 2;
92+
}
93+
}
94+
"""
95+
96+
two_contracts = one_contract + """
97+
contract baz {
98+
function echo(address a) returns (address b) {
99+
b = a;
100+
return b;
101+
}
102+
function eight() returns (int256 y) {
103+
y = 8;
104+
}
105+
}
106+
"""
107+
108+
# test
109+
assert 'solidity' in tester.languages
110+
111+
s = tester.state()
112+
113+
c1 = s.abi_contract(one_contract, language='solidity')
114+
assert c1.seven() == 7
115+
assert c1.mul2(2) == 4
116+
assert c1.mul2(-2) == -4
117+
118+
two_codes = solc_wrapper.split_contracts(two_contracts)
119+
assert len(two_codes) == 2
120+
121+
for code in two_codes:
122+
bytecode = solc_wrapper.compile(code)
123+
jsonabi = solc_wrapper.mk_full_signature(code)
124+
c = s.abi_contract(code, language='solidity')
125+
126+
c1 = s.abi_contract(two_codes[0], language='solidity')
127+
assert c1.seven() == 7
128+
assert c1.mul2(2) == 4
129+
assert c1.mul2(-2) == -4
130+
131+
c2 = s.abi_contract(two_codes[1], language='solidity')
132+
a = '\0' * 20
133+
assert c2.echo(a).decode('hex') == a
134+
assert c2.eight() == 8

ethereum/tester.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ethereum.abi as abi
99
from ethereum.slogging import LogRecorder, configure_logging, set_level
1010
from ethereum.utils import to_string
11+
from ethereum._solidity import get_solidity
1112
import rlp
1213
from rlp.utils import decode_hex, encode_hex, ascii_chr
1314

@@ -31,6 +32,11 @@
3132

3233
languages = {}
3334

35+
_solidity = get_solidity()
36+
if _solidity:
37+
languages['solidity'] = _solidity
38+
39+
3440
seed = 3 ** 160
3541

3642

0 commit comments

Comments
 (0)