-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli.py
executable file
·496 lines (357 loc) · 15.8 KB
/
cli.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
#!/usr/bin/env python
import argparse
import sys
sys.path.append("lib")
sys.path.append("scripts")
import cmd
import misc
import libmysql
import config
import crontab
import cmd, sys
import govtypes
import random
import json
from datetime import datetime, date, time
from governance import GovernanceObject, GovernanceObjectMananger, Setting, Event
from classes import Proposal, Superblock
from dashd import CTransaction
# Enable only for testing:
crontab.CONFIRMATIONS_REQUIRED = 1
parent = GovernanceObject()
parent.init()
db = libmysql.connect(config.hostname, config.username, config.password, config.database)
commands = {}
"""
Sentinel - v1
--------------------------------
- this is an exact copy of our existing functionality, just reimplemented in python using sentinel
old commands:
mnbudget prepare beer-reimbursement2 www.dashwhale.org/p/beer-reimbursement2 1 481864 XfoGXXFJtobHvjwfszWnbMNZCBAHJWeN6G 50
mnbudget submit beer-reimbursement2 www.dashwhale.org/p/beer-reimbursement2 1 481864 XfoGXXFJtobHvjwfszWnbMNZCBAHJWeN6G 50 REPLACE_WITH_COLLATERAL_HASH
1. proposal --create --proposal_name="beer-reimbursement" --description_url="www.dashwhale.org/p/beer-reimbursement" --start-date="2017/1/1" --end-date="2017/6/1"
2. cron process (will automatically submit the proposal to the network)
"""
# ---------------------------------------------------------------------
""" SENTINEL AUTOCOMPLETE FOR CLI """
# proposal --create [...]
commands["proposal"] = [
"--create",
"--name",
"--description_url",
"--proposal_url",
"--start_date",
"--end_date"
]
# superblock --create [...]
commands["trigger"] = [
"--create",
"--date",
"--payments"
]
# crontab --task="(prepare_events|submit_events)"
commands["crontab"] = [
"--clear_events",
"--prepare_events",
"--submit_events"
]
# ---------------------------------------------------------------------
"""
Sentinel Shell (CLI)
"""
class SentinelShell(cmd.Cmd):
intro = 'Welcome to the sentinel shell. Type help or ? to list commands.\n'
prompt = '(sentinel) '
file = None
"""
Network Proposal Tasks
"""
def do_proposal(self, arg):
'proposal --create --proposal_name="sb-test" --description_url="www.dashwhale.org/p/sb-test" --start_date="2016/8/1" --end_date="2017/1/1" --payment_address="ydE7B1A7htNwSTvvER6xBdgpKZqNDbbEhPydE7B1A7htNwSTvvER6xBdgpKZqNDbbEhP" --payment_amount="23"'
parser = argparse.ArgumentParser(description='Create a dash proposal')
# desired action
parser.add_argument('-c', '--create', help="create", action='store_true')
# object identity (existentially... what does it mean to be a pubkey?)
parser.add_argument('-k', '--pubkey', help='your public key for this username (only required for --create)')
# meta data (create or amend)
parser.add_argument('-p', '--proposal_name', help='the proposal name (must be unique)')
parser.add_argument('-d', '--description_url', help='your proposals url where a description of the project can be found')
parser.add_argument('-s', '--start_date', help='starting data, must be the first of the month. Example : 2017/1/1')
parser.add_argument('-e', '--end_date', help='ending data, must be the first of the month. Example : 2017/6/1')
parser.add_argument('-x', '--payment_address', help='the payment address where you wish to receive the funds')
parser.add_argument('-a', '--payment_amount', help='how much to send in each payment to the payment address')
# process
args = None
try:
args = parser.parse_args(parse(arg))
except:
pass
if not args:
return
### ------ CREATE METHOD -------- ####
if args.create:
#--create --revision=1 --pubkey=XPubkey --username="user-cid"
if not args.proposal_name:
print "proposal creation requires a proposal name, use --proposal_name"
return
if not args.description_url:
print "proposal creation requires a description url, use --description_url"
return
if not args.start_date:
print "start creation requires a start date, use --start_date"
return
if not args.end_date:
print "end creation requires a end date, use --end_date"
return
if not args.payment_address:
print "payment creation requires a valid base58 payment address, use --payment_address"
return
if not args.payment_amount:
print "payment creation requires a valid payment amount, use --payment_amount"
return
### ---- CONVERT AND CHECK EPOCHS -----
start_epoch = 0
end_epoch = 0
try:
start_epoch = datetime.strptime(args.start_date, '"%d/%m/%y"').strftime('%s')
end_epoch = datetime.strptime(args.end_date, '"%d/%m/%y"').strftime('%s')
except:
try:
start_epoch = datetime.strptime(args.start_date, '"%Y/%m/%d"').strftime('%s')
end_epoch = datetime.strptime(args.end_date, '"%Y/%m/%d"').strftime('%s')
except:
pass
if start_epoch == 0 or end_epoch == 0:
print "start or end date has invalid format, YYYY/MM/DD or DD/MM/YY is required";
return
### ---- CHECK NAME UNIQUENESS -----
if GovernanceObjectMananger.object_with_name_exists(args.proposal_name):
print "governance object with that name already exists"
return
fee_tx = CTransaction()
newObj = GovernanceObject()
newObj.create_new(parent, args.proposal_name, govtypes.proposal, govtypes.FIRST_REVISION, fee_tx)
last_id = newObj.save()
print last_id
if last_id != None:
# ADD OUR PROPOSAL AS A SUB-OBJECT WITHIN GOVERNANCE OBJECT
c = Proposal()
c.set_field("governance_object_id", last_id)
c.set_field("type", govtypes.proposal)
c.set_field("proposal_name", args.proposal_name)
c.set_field("description_url", args.description_url)
c.set_field("start_epoch", start_epoch)
c.set_field("end_epoch", end_epoch)
c.set_field("payment_address", args.payment_address)
c.set_field("payment_amount", args.payment_amount)
# APPEND TO GOVERNANCE OBJECT
newObj.add_subclass("proposal", c)
newObj.save()
# CREATE EVENT TO TALK TO DASHD / PREPARE / SUBMIT OBJECT
event = Event()
event.create_new(last_id)
event.save()
libmysql.db.commit()
print "event queued successfully"
else:
print "error:", newObj.last_error()
# abort mysql commit
return
### ------- ELSE PRINT HELP --------------- ###
parser.print_help()
### ------ CREATE METHOD -------- ####
"""
Superblock
"""
def do_superblock(self, arg):
'superblock --create --event_block_height="28224" --payments="yLipDagwb1gM15RaUq3hpcaTxzDsFsSy9a=100"'
'superblock --create --event_date="2017/1/1" --payments="Addr1=amount,Addr2=amount,Addr3=amount"'
parser = argparse.ArgumentParser(description='Create a dash proposal')
# desired action
parser.add_argument('-c', '--create', help="create", action='store_true')
# meta data (create or amend)
parser.add_argument('-p', '--payments', help='the payments desired in the superblock, serialized as a list. example: {"Addr1": amount,"Addr2": amount}')
parser.add_argument('-b', '--event_block_height', help='block height to issue superblock')
# process
args = None
try:
args = parser.parse_args(parse(arg))
except:
pass
if not args:
return
### ------ CREATE METHOD -------- ####
if args.create:
#--create --revision=1 --pubkey=XPubkey --username="user-cid"
if not args.payments:
print "superblock creation requires a payment descriptions, use --payments"
return
if not args.event_block_height:
print "superblock creation requires a event_block_height, use --event_block_height"
return
### ---- CONVERT AND CHECK EPOCHS -----
payments = misc.normalize(args.payments).split(",")
if len(payments) > 0:
pass
# COMPILE LIST OF ADDRESSES AND AMOUNTS
list_addr = []
list_amount = []
for payment in payments:
print payment
addr,amount = payment.split("=")
list_addr.append(addr)
list_amount.append(amount)
print list_amount
print list_addr
# CREATE NAME ACCORDING TO STARTING DATE (NON-UNIQUE IS NOT AN ATTACK)
superblock_name = "sb" + str(random.randint(1000000, 9999999))
# DOES THIS ALREADY EXIST?
if GovernanceObjectMananger.object_with_name_exists(superblock_name):
print "governance object with that name already exists"
return
event_block_height = misc.normalize(args.event_block_height);
print event_block_height
fee_tx = CTransaction()
newObj = GovernanceObject()
newObj.create_new(parent, superblock_name, govtypes.trigger, govtypes.FIRST_REVISION, fee_tx)
last_id = newObj.save()
print last_id
if last_id != None:
# ADD OUR PROPOSAL AS A SUB-OBJECT WITHIN GOVERNANCE OBJECT
c = Superblock()
c.set_field("governance_object_id", last_id)
c.set_field("type", govtypes.trigger)
c.set_field("subtype", "superblock")
c.set_field("superblock_name", superblock_name)
c.set_field("event_block_height", event_block_height)
c.set_field("payment_addresses", "|".join(list_addr))
c.set_field("payment_amounts", "|".join(list_amount))
# APPEND TO GOVERNANCE OBJECT
newObj.add_subclass("trigger", c)
newObj.save()
# CREATE EVENT TO TALK TO DASHD / PREPARE / SUBMIT OBJECT
event = Event()
event.create_new(last_id)
event.save()
libmysql.db.commit()
print "event queued successfully"
else:
print "error:", newObj.last_error()
# abort mysql commit
return
### ------- ELSE PRINT HELP --------------- ###
parser.print_help()
### ------ CREATE METHOD -------- ####
"""
Crontab Tasks
"""
def do_crontab(self, arg):
' crontab [--clear_events --prepare_events --submit_events]"'
parser = argparse.ArgumentParser(description='Do a crontab task')
# meta data (create or amend)
parser.add_argument('-p', '--prepare_events', help="Submit any queued governance objects pending submission (stage 2: submission of colateral tx and governance object)", action="store_true")
parser.add_argument('-s', '--submit_events', help="Process any queued events pending creation (stage 1: prepare colateral tx)", action="store_true")
parser.add_argument('-b', '--process_budget', help="Process superblock for monthly budget")
parser.add_argument('-c', '--clear_events', help="Clear event queue (for testing only)", action="store_true")
parser.add_argument('-r', '--reset', help="Hard reset (for testing only)", action="store_true")
args = None
try:
args = parser.parse_args(parse(arg))
except:
pass
if not args:
return
### ------ NAME METHOD -------- ####
if args.clear_events:
count = crontab.clear_events()
print count, "events cleared"
return
if args.reset:
print "Hard Reset:"
count = crontab.clear_events()
print count, "events cleared"
count = crontab.clear_governance_objects()
print count, "governance objects cleared"
count = crontab.clear_superblocks()
print count, "superblocks cleared"
count = crontab.clear_proposals()
print count, "proposals cleared"
return
### --- EXECUTED DESIRED CRONTAB FOR USER --- ####
if args.process_budget:
print crontab.process_budget()
return
if args.prepare_events:
count = crontab.prepare_events()
print count, "events successfully prepared (stage 1)"
return
elif args.submit_events:
count = crontab.submit_events()
print count, "events successfully submitted (stage 2)"
return
else:
print "unknown command"
return
### ------- ELSE PRINT HELP --------------- ###
parser.print_help()
def complete_crontab(self, text, line, start_index, end_index):
if text:
return [command for command in commands["crontab"]
if command.startswith(text)]
else:
return commands
"""
Vote on a specific proposal
"""
# ----- (internal) vote on something -----
def do_vote(self, arg):
'Command action on the dash network'
' vote --times=22 --type=funding --outcome=yes [--hash=governance-hash --name=obj-name]'
parser = argparse.ArgumentParser(description='Vote on governance objects and signal what dash should do with them.')
#voting
parser.add_argument('-t', '--times')
parser.add_argument('-p', '--type')
parser.add_argument('-o', '--outcome')
parser.add_argument('-n', '--hash')
parser.add_argument('-k', '--pubkey')
### ------- ELSE PRINT HELP --------------- ###
parser.print_help()
# ----- quit the program -----
def do_quit(self, arg):
' bye, see you later!'
print "Goodbye! See you soon!"
sys.exit(0)
return
def emptyline(self):
pass
def parse(arg):
'Convert a series of zero or more numbers to an argument tuple'
return tuple(map(str, arg.split()))
"""
Run the main cli
"""
import sys
args = sys.argv[1:]
misc.add_sentinel_option("clear_events")
misc.add_sentinel_option("prepare_events")
misc.add_sentinel_option("submit_events")
misc.startup()
if __name__ == '__main__':
if len(args) > 1:
if args[0] == "proposal":
SentinelShell().do_user(" ".join(args[1:]))
elif args[0] == "vote":
SentinelShell().do_vote(" ".join(args[1:]))
elif args[0] == "crontab":
SentinelShell().do_crontab(" ".join(args[1:]))
elif args[0] == "superblock":
SentinelShell().do_superblock(" ".join(args[1:]))
else:
SentinelShell().cmdloop()
"""
Test Flow (to be moved into unit tests):
1.) create an example proposal
proposal --create --proposal_name="beer-reimbursement" --description_url="www.dashwhale.org/p/beer-reimbursement" --start_date="2017/1/1" --end_date="2017/6/1"
2.) vote on the funding proposal
vote --times=22 --type=funding --outcome=yes [--hash=governance-hash --name=obj-name]
"""