This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
devnet.py
151 lines (111 loc) · 4.22 KB
/
devnet.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
import docker
import time
import tools
import random
import os
import pprint
from random import SystemRandom
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, wait, as_completed
### Create docker client
pp = pprint.PrettyPrinter(indent=4)
randcon = 8
client = docker.from_env()
advclient = docker.APIClient(base_url='unix://var/run/docker.sock')
tools.cleanUp(client.containers.list())
time.sleep(1)
print "Creating bootstrap container"
bootstrap = client.containers.run("spaceanton/spacemesh:spacemesh_p2p", detach=True, ports={9090:9999}, environment={"BOOTPARAMS": "--gossip --randcon 3 --bucketsize 100"})
print "Waiting for node to boot up"
tools.waitForRPC(bootstrap)
tools.registerProtocol(advclient, bootstrap, "gossip_test", 8081)
# collect id and ip.
bootIP = tools.getExternalIP(advclient, bootstrap)
bootID = tools.getPublicKey(bootstrap)
print "Bootnode is at - "+ bootstrap.name + ": " + bootIP + ":7513/" + bootID
# Start a network with size `netsize` boot each client and add to the list
# After that run the needed tests.
netsize = 30
idxes = {}
contlist = []
def createNodes(netsize, bootIP, bootID):
for i in range(netsize):
node = client.containers.run("spaceanton/spacemesh:spacemesh_p2p", detach=True, ports={"9090": None}, environment={"BOOTPARAMS": "--bootstrap --gossip --randcon " + str(randcon) + " --bucketsize 20 --bootnodes \"" + bootIP + ":7513/" + bootID + "\""})
contlist.append({"cont": node})
idxes[node.name] = i
def runMultiple(func, afterfunc):
futures = []
with ProcessPoolExecutor(max_workers=10) as pool:
# this code will run in multiple threads
for cont in contlist:
fut = pool.submit(func, cont["cont"].name)
futures.append(fut)
# As the jobs are completed, we process the data more
for res in as_completed(futures):
if afterfunc != None:
if isinstance(afterfunc, list):
# a list of function
for f in range(afterfunc):
f(res.result())
elif callable(afterfunc):
afterfunc(res.result())
def getpubkey(cont):
id = tools.getPublicKey(client.containers.get(cont))
return True, cont, id
def assignpubkey(packed):
suc, cont, id = packed
contlist[idxes[cont]]["id"] = id
createNodes(netsize, bootIP, bootID)
runMultiple(getpubkey, assignpubkey)
for cont in contlist:
assert "id" in cont
print "Spinned up " + str(netsize) + " more instances booting from bootnode"
def waitForNeighborhood(cont):
selected = False
selectcount = 0
selectedArr = []
line = "Neighborhood initialized"
print "[" + cont + "]"
start_time = time.time()
for i in client.containers.get(cont).logs(stream=True, stdout=True):
elapsed_time = time.time() - start_time
if line in i or selected:
if not selected:
print "found neighbors"
# switch to selected
return True
if elapsed_time >= 60 * randcon:
print "time passed :( for " + cont
return False
return False
runMultiple(waitForNeighborhood, None)
for cont in contlist:
tools.waitForRPC(cont["cont"])
tools.registerProtocol(advclient, cont["cont"], "gossip_test", 8081)
print " Broadcasting a gossip message ! "
def waitForGossipMessage(cont):
line = "Got gossip message!"
print "[" + cont + "]"
start_time = time.time()
for i in client.containers.get(cont).logs(stream=True, stdout=True):
elapsed_time = time.time() - start_time
if line in i:
print "I got it !"
return True
if elapsed_time > 60 * randcon:
return False
counting = 0
def count(success):
if success:
global counting
counting = counting + 1
cryptogen = SystemRandom()
num = cryptogen.randrange(len(contlist))
broadcaster = contlist[num]
tools.broadcast(advclient, broadcaster["cont"], "gossip_test", "Hello!")
contlist.remove(broadcaster)
runMultiple(waitForGossipMessage, count)
print str(counting) + " nodes got the gossip message"
print "Starting to shutdown nodes"
contlist.append({ "cont": bootstrap })
contlist.append({ "cont": broadcaster["cont"] })
tools.cleanUp(contlist)