-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoobcoin_starter
executable file
·76 lines (57 loc) · 1.96 KB
/
noobcoin_starter
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
#!/usr/bin/env python
import sys
import subprocess
import requests
import time
PIDS_FILE = 'logs/running.pids.log'
def run(cmd, logfile):
'''
Run the cmd and output to the logfile, returns the ret_code, pid
'''
logfile = open(logfile, 'w')
proc = subprocess.Popen(cmd.split(),
universal_newlines=True,
stdout=logfile)
# proc.wait()
return proc.pid
def start():
nodes = sys.argv[2]
pids = open(PIDS_FILE, 'w')
# Start the bootstrapper
cmd = 'python node.py -p 5000 -b -n ' + nodes
pid = run(cmd, 'logs/n0.log')
print('Started Bootstrapper | pid: ' + str(pid))
pids.write('bootstrapper: ' + str(pid) + '\n')
# Make sure the bootstrap has been successfully started
time.sleep(1)
# All the nodes must register themselves at the Bootstrap Node
for node in range(int(nodes))[1:]:
port = str(5000 + node)
pid = run('python node.py -p ' + port, 'logs/n' + str(node) + '.log')
print('Started Miner | pid: ' + str(pid))
pids.write('node' + str(node) + ': ' + str(pid) + '\n')
# Make sure all the nodes have registered
time.sleep(2)
# Then the boostrap node must inform everyone about the networks structure
resp = requests.get('http://localhost:5000/broadcast-ring').json()
print(resp)
resp2 = requests.post('http://localhost:5000/first-transaction').json()
def stop():
'''
Delete all the processes that start from the 'start' step
'''
pids_f = open(PIDS_FILE, 'r')
pids = [pid.split(": ")[-1] for pid in pids_f.read().split('\n')]
for pid in pids:
if pid != '':
run("kill -KILL " + pid, 'stop.log')
if __name__ == "__main__":
if len(sys.argv) == 1:
print('Available commands: \n' +
' start <n>: Start the Chain with <n> nodes')
exit()
cmd = sys.argv[1]
if cmd == 'start':
start()
elif cmd == 'stop':
stop()