-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun_socket_adkr.py
More file actions
162 lines (136 loc) · 6.22 KB
/
run_socket_adkr.py
File metadata and controls
162 lines (136 loc) · 6.22 KB
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
import gevent
from gevent import monkey;
import network
monkey.patch_all(thread=False)
import time
import random
import traceback
from typing import List, Callable
from gevent import Greenlet
from myexperiements.sockettest.adkg_adp_high_node import ADKGADPHIGHNode
from myexperiements.sockettest.adkr_hbacss_high_node import ADKRHBNode
from myexperiements.sockettest.smvba_high_node import MVBAHIGHNode
from myexperiements.sockettest.spbc_high_node import SPBCHIGHNode
from myexperiements.sockettest.spbc_ec_node import SPBCECNode
from myexperiements.sockettest.tmvba_high_node import TthresholdmvbaNode
from myexperiements.sockettest.adkg_adp_op_node import ADKGADPOPNode
from network.socket_server import NetworkServer
from network.socket_client import NetworkClient
from network.socket_client_ng import NetworkClient
from multiprocessing import Value as mpValue, Queue as mpQueue
from ctypes import c_bool
from charm.toolbox.ecgroup import ECGroup,G
def instantiate_bft_node(sid, i, N, f, K, pf, bft_from_server: Callable, bft_to_client: Callable, ready: mpValue,
stop: mpValue, protocol="adkr-high", mute=False, debug=False):
bft = None
# if protocol == 'dumbo':
# bft = DumboBFTNode(sid, i, B, N, f, bft_from_server, bft_to_client, ready, stop, K, mute=mute, debug=debug)
if protocol == 'adkg-adp':
bft = ADKGADPHIGHNode(sid, i, f, N, K, bft_from_server, bft_to_client, ready, stop, mute, debug)
elif protocol == 'adkr-hb':
bft = ADKRHBNode(sid, i, f, N, K, bft_from_server, bft_to_client, ready, stop, mute, debug)
elif protocol == 'adkr-op':
bft = ADKGADPOPNode(sid, i, f, N, K, bft_from_server, bft_to_client, ready, stop, mute, debug)
elif protocol == 'mvba-high':
bft = MVBAHIGHNode(sid, i, f, N, K, bft_from_server, bft_to_client, ready, stop, mute, debug)
elif protocol == 'spbc-high':
bft = SPBCHIGHNode(sid, i, f, N, K, bft_from_server, bft_to_client, ready, stop, mute, debug)
elif protocol == 'spbc-ec':
bft = SPBCECNode(sid, i, f, N, K, bft_from_server, bft_to_client, ready, stop, mute, debug)
elif protocol == 'ttmvba':
bft = TthresholdmvbaNode(sid, i, f, N, K, bft_from_server, bft_to_client, ready, stop, mute, debug)
# elif protocol == 'ng':
# bft = NGSNode(sid, i, S, B, F, N, f, bft_from_server, bft_to_client, ready, stop, mute=mute, countpoint=countpoint)
else:
print("Only support ADKR/ADKG")
return bft
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--sid', metavar='sid', required=True,
help='identifier of node', type=str)
parser.add_argument('--id', metavar='id', required=True,
help='identifier of node', type=int)
parser.add_argument('--N', metavar='N', required=True,
help='number of parties', type=int)
parser.add_argument('--f', metavar='f', required=True,
help='number of faulties', type=int)
parser.add_argument('--K', metavar='K', required=True,
help='instance to be select', type=int)
parser.add_argument('--P', metavar='P', required=False,
help='protocol to execute', type=str, default="ng")
parser.add_argument('--M', metavar='M', required=False,
help='whether to mute a third of nodes', type=bool, default=False)
parser.add_argument('--D', metavar='D', required=False,
help='whether to debug mode', type=bool, default=False)
parser.add_argument('--pf', metavar='pf', required=False,
help='whether to debug mode', type=int, default=1)
args = parser.parse_args()
# Some parameters
sid = args.sid
i = args.id
N = args.N
f = args.f
K = args.K
P = args.P
M = args.M
D = args.D
pf = args.pf
# Random generator
rnd = random.Random(sid)
# Nodes list
addresses = [None] * N
try:
with open('hosts.config', 'r') as hosts:
for line in hosts:
params = line.split()
pid = int(params[0])
priv_ip = params[1]
pub_ip = params[2]
port = int(params[3])
if pid not in range(N):
continue
if pid == i:
# print(pid, priv_ip, port)
my_address = (priv_ip, port)
addresses[pid] = (pub_ip, port)
assert all([node is not None for node in addresses])
# print("hosts.config is correctly read")
client_bft_mpq = mpQueue()
#client_from_bft = client_bft_mpq.get
client_from_bft = lambda: client_bft_mpq.get(timeout=0.00001)
bft_to_client = client_bft_mpq.put_nowait
server_bft_mpq = mpQueue()
#bft_from_server = server_bft_mpq.get
bft_from_server = lambda: server_bft_mpq.get(timeout=0.00001)
server_to_bft = server_bft_mpq.put_nowait
client_ready = mpValue(c_bool, False)
server_ready = mpValue(c_bool, False)
net_ready = mpValue(c_bool, False)
stop = mpValue(c_bool, False)
net_client = network.socket_client.NetworkClient(my_address[1], my_address[0], i, addresses, client_from_bft, client_ready, stop)
net_server = NetworkServer(my_address[1], my_address[0], i, addresses, server_to_bft, server_ready, stop)
print("here debug = ", D)
print("here N = ", N)
bft = instantiate_bft_node(sid, i, N, f, K, pf, bft_from_server, bft_to_client, net_ready, stop, P, M, D)
net_server.start()
net_client.start()
while not client_ready.value or not server_ready.value:
time.sleep(1)
print("waiting for network ready...")
# gevent.sleep(3)
time.sleep(3)
with net_ready.get_lock():
net_ready.value = True
bft_thread = Greenlet(bft.run)
bft_thread.start()
bft_thread.join()
with stop.get_lock():
stop.value = True
net_client.terminate()
net_client.join()
time.sleep(1)
net_server.terminate()
net_server.join()
except FileNotFoundError or AssertionError as e:
traceback.print_exc()