-
Notifications
You must be signed in to change notification settings - Fork 2
/
eth_5_stop.py
90 lines (74 loc) · 3.34 KB
/
eth_5_stop.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
import argparse
import json
import logging
from web3 import Web3, HTTPProvider
from web3.middleware import geth_poa_middleware # only for PoA and dev networks
from chirotonia.ethereum.contract import Contract
logger = logging.getLogger('ETH_5_STOP')
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(description='Simulate stop action for a Chirotonia session')
parser.add_argument("session", type=str, help="Session name to use")
parser.add_argument("-e", "--endpoint", type=str, help="Custom rpc endpoint at port 8545", default='localhost')
parser.add_argument("--no_sign", action="store_true")
vote_flag_group = parser.add_mutually_exclusive_group(required=True)
vote_flag_group.add_argument("-v", "--vote", type=str, help="Name of vote to start")
vote_flag_group.add_argument("-a", "--all", const=True, help="Start all votes specified in configuration", nargs='?')
args = parser.parse_args()
session_name = args.session
w3 = Web3(HTTPProvider('http://' + args.endpoint))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
# if not w3.isConnected():
# raise 'Connection to ethereum node failed'
with open("./runs/%s.json" % session_name) as session_file:
session_conf = json.load(session_file)
if 'mainContract' not in session_conf:
logger.error('mainContract address must be set in configuration')
exit(1)
if 'manager' not in session_conf:
logger.error("Manager address is required to be set in configuration. Start aborted.")
exit(1)
if "managerPassword" not in session_conf:
logger.error("Manager account password is required in configuration (field: managerPassword)")
exit(1)
w3.eth.defaultAccount = session_conf['manager']
if not args.no_sign:
w3.geth.personal.unlockAccount(w3.eth.defaultAccount, session_conf['managerPassword'])
logger.info('Manager account successfully unlocked')
chirotonia = Contract(w3, session_conf['mainContract'])
if args.all:
txs = {}
for vote in session_conf['votes']:
logger.info("Stopping vote %s", vote['name'])
try:
txs[vote["name"]] = chirotonia.stop_vote(vote["name"])
except:
logger.error('Error stopping vote %s', vote['name'])
for name, tx in txs.items():
try:
tx_rcpt = chirotonia.wait(tx)
logger.info("Gas spent %d", tx_rcpt.gasUsed)
logger.info("Vote %s successfully stopped", name)
for vote in session_conf['votes']:
if vote['name'] == name:
vote['status'] = 'stopped'
except:
logger.error('Error on transaction for vote %s', vote['name'])
elif args.vote:
found = False
for vote in session_conf['votes']:
if args.vote == vote['name']:
logger.info("Stopping vote %s", vote['name'])
tx_rcpt = chirotonia.stop_vote(args.vote, sync=True)
logger.info("Gas spent %d", tx_rcpt.gasUsed)
logger.info("Vote %s successfully stopped", vote['name'])
vote['status'] = 'stopped'
found = True
if not found:
logger.error("Specified vote not found in configuration")
exit(1)
else:
logger.error("No vote has been specified")
exit(1)
with open("./runs/%s.json" % session_name, "w") as session_file:
json.dump(session_conf, session_file, indent=4)
logger.info("Configuration updated at ./runs/%s.json", session_name)