forked from AugurProject/augur-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_contracts.py
executable file
·215 lines (199 loc) · 6.43 KB
/
load_contracts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python2
import warnings; warnings.simplefilter('ignore')
import serpent
from pyrpctools import RPC_Client, DB
from collections import defaultdict
import os
import sys
import json
import time
FROM_DB = False
RPC = None
COINBASE = None
TRIES = 10
BLOCKTIME = 12
ROOT = os.path.dirname(os.path.realpath(__file__))
SRCPATH = os.path.join(ROOT, 'src')
GAS = hex(3*10**6)
USE_EXTERNS = False
INFO = {}
os.chdir(ROOT)
def get_fullname(name):
'''
Takes a short name from an import statement and
returns a real path to that contract.
'''
for directory, subdirs, files in os.walk(SRCPATH):
for f in files:
if f[:-3] == name:
return os.path.join(directory, f)
raise ValueError('No such name: '+name)
def get_shortname(fullname):
return os.path.split(fullname)[-1][:-3]
def wait(seconds):
sys.stdout.write('Waiting %f seconds' % seconds)
sys.stdout.flush()
for i in range(10):
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(seconds/10.)
print
def broadcast_code(evm, address=None):
'''Sends compiled code to the network, and returns the address.'''
while True:
response = RPC.eth_sendTransaction(sender=COINBASE, data=evm, gas=GAS)
if 'result' in response:
address = response['result']
break
else:
assert 'error' in response and response['error']['code'] == -32603, 'Weird JSONRPC response: ' + str(response)
if address is None:
wait(BLOCKTIME)
else:
break
tries = 0
while tries < TRIES:
wait(BLOCKTIME)
check = RPC.eth_getCode(address)['result']
if check != '0x' and check[2:] in evm:
return address
tries += 1
return broadcast_code(evm, address)
def get_compile_order():
# topological sorting! :3
nodes = {}
nodes_copy = {}
avail = set()
# for each node, build a list of it's incoming edges
# incoming edges are dependencies
for directory, subdirs, files in os.walk('src'):
for f in files:
if not f.endswith('.se'): continue
incoming_edges = set()
for line in open(os.path.join(directory, f)):
if USE_EXTERNS and line.startswith('extern'):
name = line[line.find(' ')+1:line.find(':')]
incoming_edges.add(name)
if not USE_EXTERNS and line.startswith('import'):
name = line.split(' ')[1]
incoming_edges.add(name)
nodes_copy[f[:-3]] = incoming_edges.copy()
if incoming_edges:
nodes[f[:-3]] = incoming_edges
else:
avail.add(f[:-3])
sorted_nodes = []
while avail:
curr = avail.pop()
sorted_nodes.append(curr)
for item, edges in nodes.items():
if curr in edges:
edges.remove(curr)
if not edges:
avail.add(item)
nodes.pop(item)
return sorted_nodes, nodes_copy
def get_info(name):
if FROM_DB:
return json.loads(DB.Get(name))
else:
return INFO[name]
def set_info(name, val):
if FROM_DB:
DB.Put(name, json.dumps(val))
else:
INFO[name] = val
def translate_code_with_imports(fullname):
new_code = []
for line in open(fullname):
line = line.rstrip()
if line.startswith('import'):
line = line.split(' ')
name, sub = line[1], line[3]
info = get_info(name)
new_code.append(info['sig'])
new_code.append(sub + ' = ' + info['address'])
else:
new_code.append(line)
return '\n'.join(new_code)
def translate_code_with_externs(fullname):
new_code = []
last_extern = float('+inf')
for i, line in enumerate(open(fullname)):
line = line.rstrip()
if line.startswith('extern'):
print line
last_extern = i
name = line[line.find(' ')+1:line.find(':')][:-3]
info = get_info(name)
new_code.append(info['sig'])
elif i == last_extern + 1:
sub = line.split(' ')[0]
new_code.append(sub + ' = ' + info['address'])
else:
new_code.append(line)
return '\n'.join(new_code)
def compile(fullname):
if USE_EXTERNS:
new_code = translate_code_with_externs(fullname)
else:
new_code = translate_code_with_imports(fullname)
print new_code
evm = '0x' + serpent.compile(new_code).encode('hex')
new_address = broadcast_code(evm)
short_name = os.path.split(fullname)[-1][:-3]
new_sig = serpent.mk_signature(new_code).replace('main', short_name, 1).replace(':,', ':_,').replace(':]', ':_]')
fullsig = serpent.mk_full_signature(new_code)
new_info = {'address':new_address, 'sig':new_sig, 'fullsig':fullsig}
set_info(short_name, new_info)
def optimize_deps(deps, contract_nodes, contract):
new_deps = [contract]
for i in range(deps.index(contract) + 1, len(deps)):
node = deps[i]
for new_dep in new_deps:
if new_dep in contract_nodes[node]:
new_deps.append(node)
break
return new_deps
def main():
global BLOCKTIME
global USE_EXTERNS
global RPC
global COINBASE
global FROM_DB
start = 0
verbose = False
debug = False
for arg in sys.argv:
if arg.startswith('--BLOCKTIME='):
BLOCKTIME = float(arg.split('=')[1])
if arg.startswith('--contract='):
FROM_DB = True
start = arg.split('=')[1]
if arg == '--use-externs':
USE_EXTERNS = True
if arg == '--verbose':
verbose = True
if arg == '--debug':
debug = True
deps, nodes = get_compile_order()
if type(start) == str:
deps = optimize_deps(deps, nodes, start)
start = 0
RPC = RPC_Client(default='GETH', verbose=verbose, debug=debug)
COINBASE = RPC.eth_coinbase()['result']
for i in range(start, len(deps)):
fullname = get_fullname(deps[i])
print "compiling", fullname
compile(fullname)
if not FROM_DB:
sys.stdout.write('dumping new addresses to DB')
sys.stdout.flush()
for k, v in INFO.items():
DB.Put(k, json.dumps(v))
sys.stdout.write('.')
sys.stdout.flush()
print
return 0
if __name__ == '__main__':
main()