forked from blockchainsllc/DAO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
550 lines (457 loc) · 15.8 KB
/
utils.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#!/usr/bin/python2
import calendar
import random
import os
import json
import sys
import re
from sha3 import sha3_256 as sha3
from datetime import datetime
from jsutils import js_common_intro
def constrained_sum_sample_pos(n, total):
"""Return a randomly chosen list of n positive integers summing to total.
Each such list is equally likely to occur."""
dividers = sorted(random.sample(xrange(1, total), n - 1))
return [a - b for a, b in zip(dividers + [total], [0] + dividers)]
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def rm_file(f):
try:
os.remove(f)
except OSError:
pass
def which(program):
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def determine_binary(given_binary, name, is_mandatory):
"""
Determines if a path to a binary is correct and if not tries to
get a generic one by looking at the system PATH. If all fails, then
the tests will fail with an appropriate error message.
"""
ret_binary = None
if given_binary:
if is_exe(given_binary):
ret_binary = given_binary
else:
# try to find binary in the PATH
ret_binary = which(name)
if is_mandatory and not ret_binary:
print("ERROR: Could not find binary '{}'".format(given_binary))
sys.exit(1)
return ret_binary
def ts_now():
""" Return a unix timestamp representing the time in UTC right now"""
return calendar.timegm(datetime.utcnow().utctimetuple())
def seconds_in_future(secs):
return ts_now() + secs
def create_votes_array(amounts, approve, reverse):
"""
Create an array of votes out of the tokens holders to either pass or
reject a proposal.
Parameters
----------
amounts : list
The list of tokens each token holder has
approve : bool
True if we want to pass and false if we want to vote against
the proposal
reverse : bool
True if we need to iterate the list in reverse to give chance for
True votes to the last accounts
Returns
----------
The array of votes required
"""
votes = []
total = sum(amounts)
amounts_for_traversal = list(reversed(amounts)) if reverse else amounts
percentage = 0.0
if not approve:
for val in amounts_for_traversal:
ratio = val/float(total)
if (percentage + ratio < 0.5):
votes.append(True)
percentage += ratio
else:
votes.append(False)
else:
for val in amounts_for_traversal:
ratio = val/float(total)
if percentage <= 0.5:
votes.append(True)
percentage += ratio
else:
votes.append(False)
return list(reversed(votes)) if reverse else votes
def create_votes_array_for_quorum(amounts, targetQuorum, approve):
"""
Create an array of votes for a proposal that will reach a targetQuorum
Parameters
----------
amounts : list
The list of tokens each token holder has
targetQuorum : float
A target quorum represented by a float ranging from 0.0 to 1.0. It
represents percentage of the quorum we want to achieve
approve : bool
True if we want to pass and false if we want to vote against
the proposal
Returns
----------
The array of votes required
"""
votes = []
total = sum(amounts)
percentage = 0.0
for val in amounts:
ratio = val/float(total)
if (percentage + ratio < targetQuorum):
votes.append(approve)
percentage += ratio
else:
break
if not votes or percentage > targetQuorum:
print("ERROR: Could not satisfy the target quorum of '{}' with the "
"currrent way the token holders bought tokens. Please rerun the "
"test in order to get a different set of token holders")
sys.exit(1)
return votes
def arr_str(arr):
"""
Create a string representation of an array, ready to be imported to js.
Parameters
----------
arr : list
The list from which to create the array string. Can be an array of
ints, strings or booleans
Returns
----------
A string representation of the array ready to be imported in a js
template
"""
if type(arr) is not list or arr == []:
print("ERROR: 'arr_str()' expects a non-empty list")
sys.exit(1)
has_strings = isinstance(arr[0], basestring)
return "[ {} ]".format(
', '.join(['"{}"'.format(
str(x).lower()
) if has_strings else str(x).lower() for x in arr])
)
def extract_test_dict(name, output):
split = output.split('Test Results: ', 1)
if len(split) != 2:
print("ERROR: Could not parse '{}' output properly.\n"
"Output was:\n{}".format(
name, output
))
sys.exit(1)
try:
result = json.loads(split[1])
except:
print("ERROR: Could not parse '{}' output properly.\n"
"Output was:\n{}".format(
name, output
))
sys.exit(1)
return result
def is_int(v):
return isinstance(v, (int, long))
def cmp_floats(a, b):
return abs(a - b) <= 0.01
def cmp_string_to_int(string, num):
try:
return int(string) == num
except:
return cmp_floats(float(string), float(num))
def compare_values(got, expect):
if isinstance(got, float) ^ isinstance(expect, float):
if is_int(got) or is_int(expect):
return abs(expect - got) % 1 <= 0.01
else:
print("ERROR: float comparison failure")
return False
if isinstance(got, float):
return cmp_floats(got, expect)
elif isinstance(got, basestring) and is_int(expect):
return cmp_string_to_int(got, expect)
elif isinstance(expect, basestring) and is_int(got):
return cmp_string_to_int(expect, got)
elif isinstance(expect, basestring) and isinstance(got, float):
return cmp_floats(float(expect), got)
else:
return got == expect
def eval_test(name, output, expected_dict):
"""
Evaluate output of a scenario and compare with expected results
Parameters
----------
name : string
The name of the scenario to evaluate
output : string
The output of the script that was executed, from which we will
extract the results
expected_dict : dict
A dictionary containing all the expected output from the test
Returns
----------
results : dict
The dictionary that resulted from the parsing of the test output
"""
tests_fail = False
results = extract_test_dict(name, output)
for k, v in expected_dict.iteritems():
if k not in results:
tests_fail = True
print("ERROR: Did not find '{}' in the test results".format(k))
continue
if not compare_values(results[k], v):
tests_fail = True
print("ERROR: Expected {} for '{}' but got {}".format(
v, k, results[k]
))
if not tests_fail:
print("Tests for scenario '{}' PASSED!".format(name))
else:
print("Tests for scenario '{}' FAILED! Script output was:\n{}".format(
name, output)
)
sys.exit(1)
return results
def write_js(name, contents, accounts_num):
"""Write a javascript file from a template, prepending common intro"""
with open(name, "w") as f:
f.write("{}\n{}".format(js_common_intro(accounts_num), contents))
def create_genesis(accounts):
"""Create a genesis block with ether allocation for the given accounts"""
genesis = {}
config = {}
config["homesteadBlock"] = 0
genesis["config"] = config
genesis["nonce"] = "0xdeadbeefdeadbeef"
genesis["timestamp"] = "0x0"
# Start after homesteam
genesis["parentHash"] = (
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
genesis["extraData"] = "0x0"
genesis["gasLimit"] = "0x47e7c4"
genesis["difficulty"] = (
"0x0000000000000000000000000000000000000000000000000000000000000001"
)
genesis["mixhash"] = (
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
alloc = {}
for acc in accounts:
alloc[acc] = {"balance": "133700000000000000000000000000000000"}
genesis["alloc"] = alloc
with open('genesis_block.json', "w") as f:
f.write(json.dumps(genesis))
def count_token_votes(amounts, votes):
"""Returns how many tokens votes yay and how many voted nay"""
yay = 0
nay = 0
for idx, amount in enumerate(amounts):
if votes[idx]:
yay += amount
else:
nay += amount
return yay, nay
def str_replace_or_die(string, old, new):
if old not in string:
print(
"ERROR: Could not find '{}' during DAO's source "
"code editing for the tests.".format(old)
)
sys.exit(1)
return string.replace(old, new)
def re_replace_or_die(string, varname, value):
old_string = string
new_string = re.sub(
r"(uint constant *{} *=).*;".format(varname),
r"\1 {};".format(value),
string
)
if old_string == new_string:
print(
"ERROR: Could not match RE for '{}' during DAO's source "
"code editing for the tests.".format(varname)
)
sys.exit(1)
return new_string
def edit_dao_source(
contracts_dir,
keep_limits,
min_proposal_debate,
min_split_debate,
halve_minquorum,
split_exec_period,
normal_pricing,
extra_balance_refund,
offer_payment_period):
with open(os.path.join(contracts_dir, 'DAO.sol'), 'r') as f:
contents = f.read()
# remove all limits that would make testing impossible
contents = re_replace_or_die(
contents,
"minProposalDebatePeriod",
str(min_proposal_debate)
)
contents = re_replace_or_die(
contents,
"minSplitDebatePeriod",
"1"
)
contents = re_replace_or_die(
contents,
"splitExecutionPeriod",
str(split_exec_period)
)
if not extra_balance_refund:
contents = re_replace_or_die(contents, "creationGracePeriod", "1")
if halve_minquorum: # if we are testing halve_minquorum remove year limit
contents = re_replace_or_die(contents, "quorumHalvingPeriod", "1")
# add test query functions
contents = str_replace_or_die(
contents,
"contract DAO is DAOInterface, Token, TokenCreation {",
"""contract DAO is DAOInterface, Token, TokenCreation {
function splitProposalBalance(uint pid, uint sid) constant returns (uint _balance) {
Proposal p = proposals[pid];
if (!p.newCurator) throw;
SplitData s = p.splitData[sid];
return s.splitBalance;
}
function splitProposalSupply(uint pid, uint sid) constant returns (uint _supply) {
Proposal p = proposals[pid];
if (!p.newCurator) throw;
SplitData s = p.splitData[sid];
return s.totalSupply;
}
function splitProposalrewardToken(uint pid, uint sid) constant returns (uint _rewardToken) {
Proposal p = proposals[pid];
if (!p.newCurator) throw;
SplitData s = p.splitData[sid];
return s.rewardToken;
}
function splitProposalNewAddress(uint pid, uint sid) constant returns (address _DAO) {
Proposal p = proposals[pid];
if (!p.newCurator) throw;
SplitData s = p.splitData[sid];
return address(s.newDAO);
}
function extMinQuorum(uint _val) constant returns (uint _minQuorum) {
return minQuorum(_val);
}
"""
)
contents = str_replace_or_die(
contents,
'import "./TokenCreation.sol";',
'import "./TokenCreationCopy.sol";'
)
new_path = os.path.join(contracts_dir, "DAOcopy.sol")
with open(new_path, "w") as f:
f.write(contents)
# edit TokenCreation source
with open(os.path.join(contracts_dir, 'TokenCreation.sol'), 'r') as f:
contents = f.read()
if (not keep_limits) and (not normal_pricing):
contents = str_replace_or_die(
contents, 'closingTime - 2 weeks > now', 'true'
)
with open(os.path.join(contracts_dir, 'TokenCreationCopy.sol'), "w") as f:
f.write(contents)
# edit SampleOfferWithoutRewards.sol
with open(os.path.join(contracts_dir, 'SampleOfferWithoutReward.sol'), 'r') as f:
contents = f.read()
contents = str_replace_or_die(
contents,
'import "./DAO.sol";',
'import "./DAOcopy.sol";'
)
contents = str_replace_or_die(contents, '(1 days)', str(offer_payment_period))
with open(os.path.join(contracts_dir, 'SampleOfferWithoutRewardCopy.sol'), "w") as f:
f.write(contents)
# edit SampleOffer.sol
with open(os.path.join(contracts_dir, 'SampleOffer.sol'), 'r') as f:
contents = f.read()
contents = str_replace_or_die(
contents,
'import "./SampleOfferWithoutReward.sol";',
'import "./SampleOfferWithoutRewardCopy.sol";'
)
with open(os.path.join(contracts_dir, 'SampleOfferCopy.sol'), "w") as f:
f.write(contents)
# edit USNRewardPayOut.sol
with open(os.path.join(contracts_dir, 'USNRewardPayOut.sol'), 'r') as f:
contents = f.read()
contents = str_replace_or_die(
contents,
'import "./SampleOffer.sol";',
'import "./SampleOfferCopy.sol";'
)
with open(os.path.join(contracts_dir, 'USNRewardPayOutCopy.sol'), "w") as f:
f.write(contents)
return new_path
def argtype_is_int(t):
return t in ["uint256", "uint128"]
def calculate_bytecode(function_name, *args):
"""
Create the bytecode for calling function with `function_name` and the
given arguments as defined here:
https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#examples
Parameters
----------
function_hash : string
The first 4 bytes of the hash of the function signature.
args : list
A list of arguments to encode. Each argument consists of a tuple of
the argument type and its value.
Returns
----------
results : string
The encoded ABI for the function call with the given arguments
"""
# form the function's hash
types = []
for arg in args:
types.append(arg[0])
function_hash = "0x" + sha3("{}({})".format(
function_name,
','.join(types)
)).hexdigest()[:8]
bytecode = function_hash
for arg in args:
arg_type = arg[0]
arg_val = arg[1]
if arg_type == "bool" or argtype_is_int(arg_type):
if arg_type == "bool":
arg_val = 1 if arg[1] is True else 0
bytecode += "{0:0{1}x}".format(int(arg_val), 64)
elif arg_type == "address":
bytecode += arg_val.strip("0x").zfill(64)
else:
print(
"Error: Invalid argument type '{}' given at "
"'calculate_bytecode()`".format(arg_type)
)
sys.exit(1)
return bytecode
def available_scenarios():
dir = "scenarios"
return [name for name in os.listdir(dir)
if os.path.isdir(os.path.join(dir, name))]
def to_wei(val_in_ether):
return val_in_ether * 1000000000000000000