-
Notifications
You must be signed in to change notification settings - Fork 65
/
binaryagreement.py
285 lines (249 loc) · 10.9 KB
/
binaryagreement.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
import gevent
from gevent.event import Event
from collections import defaultdict
import logging
from honeybadgerbft.exceptions import RedundantMessageError, AbandonedNodeError
logger = logging.getLogger(__name__)
def handle_conf_messages(*, sender, message, conf_values, pid, bv_signal):
_, r, v = message
assert v in ((0,), (1,), (0, 1))
if sender in conf_values[r][v]:
logger.warn(f'Redundant CONF received {message} by {sender}',
extra={'nodeid': pid, 'epoch': r})
# FIXME: Raise for now to simplify things & be consistent
# with how other TAGs are handled. Will replace the raise
# with a continue statement as part of
# https://github.com/initc3/HoneyBadgerBFT-Python/issues/10
raise RedundantMessageError(
'Redundant CONF received {}'.format(message))
conf_values[r][v].add(sender)
logger.debug(
f'add v = {v} to conf_value[{r}] = {conf_values[r]}',
extra={'nodeid': pid, 'epoch': r},
)
bv_signal.set()
def wait_for_conf_values(*, pid, N, f, epoch, conf_sent, bin_values,
values, conf_values, bv_signal, broadcast):
conf_sent[epoch][tuple(values)] = True
logger.debug(f"broadcast {('CONF', epoch, tuple(values))}",
extra={'nodeid': pid, 'epoch': epoch})
broadcast(('CONF', epoch, tuple(bin_values[epoch])))
while True:
logger.debug(
f'looping ... conf_values[epoch] is: {conf_values[epoch]}',
extra={'nodeid': pid, 'epoch': epoch},
)
if 1 in bin_values[epoch] and len(conf_values[epoch][(1,)]) >= N - f:
return set((1,))
if 0 in bin_values[epoch] and len(conf_values[epoch][(0,)]) >= N - f:
return set((0,))
if (sum(len(senders) for conf_value, senders in
conf_values[epoch].items() if senders and
set(conf_value).issubset(bin_values[epoch])) >= N - f):
return set((0, 1))
bv_signal.clear()
bv_signal.wait()
def binaryagreement(sid, pid, N, f, coin, input, decide, broadcast, receive):
"""Binary consensus from [MMR14]. It takes an input ``vi`` and will
finally write the decided value into ``decide`` channel.
:param sid: session identifier
:param pid: my id number
:param N: the number of parties
:param f: the number of byzantine parties
:param coin: a ``common coin(r)`` is called to block until receiving a bit
:param input: ``input()`` is called to receive an input
:param decide: ``decide(0)`` or ``output(1)`` is eventually called
:param broadcast: broadcast channel
:param receive: receive channel
:return: blocks until
"""
# Messages received are routed to either a shared coin, the broadcast, or AUX
est_values = defaultdict(lambda: [set(), set()])
aux_values = defaultdict(lambda: [set(), set()])
conf_values = defaultdict(lambda: {(0,): set(), (1,): set(), (0, 1): set()})
est_sent = defaultdict(lambda: [False, False])
conf_sent = defaultdict(lambda: {(0,): False, (1,): False, (0, 1): False})
bin_values = defaultdict(set)
# This event is triggered whenever bin_values or aux_values changes
bv_signal = Event()
def _recv():
while True: # not finished[pid]:
(sender, msg) = receive()
logger.debug(f'receive {msg} from node {sender}',
extra={'nodeid': pid, 'epoch': msg[1]})
assert sender in range(N)
if msg[0] == 'EST':
# BV_Broadcast message
_, r, v = msg
assert v in (0, 1)
if sender in est_values[r][v]:
# FIXME: raise or continue? For now will raise just
# because it appeared first, but maybe the protocol simply
# needs to continue.
print(f'Redundant EST received by {sender}', msg)
logger.warn(
f'Redundant EST message received by {sender}: {msg}',
extra={'nodeid': pid, 'epoch': msg[1]}
)
raise RedundantMessageError(
'Redundant EST received {}'.format(msg))
# continue
est_values[r][v].add(sender)
# Relay after reaching first threshold
if len(est_values[r][v]) >= f + 1 and not est_sent[r][v]:
est_sent[r][v] = True
broadcast(('EST', r, v))
logger.debug(f"broadcast {('EST', r, v)}",
extra={'nodeid': pid, 'epoch': r})
# Output after reaching second threshold
if len(est_values[r][v]) >= 2 * f + 1:
logger.debug(
f'add v = {v} to bin_value[{r}] = {bin_values[r]}',
extra={'nodeid': pid, 'epoch': r},
)
bin_values[r].add(v)
logger.debug(f'bin_values[{r}] is now: {bin_values[r]}',
extra={'nodeid': pid, 'epoch': r})
bv_signal.set()
elif msg[0] == 'AUX':
# Aux message
_, r, v = msg
assert v in (0, 1)
if sender in aux_values[r][v]:
# FIXME: raise or continue? For now will raise just
# because it appeared first, but maybe the protocol simply
# needs to continue.
print('Redundant AUX received', msg)
raise RedundantMessageError(
'Redundant AUX received {}'.format(msg))
logger.debug(
f'add sender = {sender} to aux_value[{r}][{v}] = {aux_values[r][v]}',
extra={'nodeid': pid, 'epoch': r},
)
aux_values[r][v].add(sender)
logger.debug(
f'aux_value[{r}][{v}] is now: {aux_values[r][v]}',
extra={'nodeid': pid, 'epoch': r},
)
bv_signal.set()
elif msg[0] == 'CONF':
handle_conf_messages(
sender=sender,
message=msg,
conf_values=conf_values,
pid=pid,
bv_signal=bv_signal,
)
# Translate mmr14 broadcast into coin.broadcast
# _coin_broadcast = lambda (r, sig): broadcast(('COIN', r, sig))
# _coin_recv = Queue()
# coin = shared_coin(sid+'COIN', pid, N, f, _coin_broadcast, _coin_recv.get)
# Run the receive loop in the background
_thread_recv = gevent.spawn(_recv)
# Block waiting for the input
vi = input()
assert vi in (0, 1)
est = vi
r = 0
already_decided = None
while True: # Unbounded number of rounds
logger.info(f'Starting with est = {est}',
extra={'nodeid': pid, 'epoch': r})
if not est_sent[r][est]:
est_sent[r][est] = True
broadcast(('EST', r, est))
while len(bin_values[r]) == 0:
# Block until a value is output
bv_signal.clear()
bv_signal.wait()
w = next(iter(bin_values[r])) # take an element
logger.debug(f"broadcast {('AUX', r, w)}",
extra={'nodeid': pid, 'epoch': r})
broadcast(('AUX', r, w))
values = None
logger.debug(
f'block until at least N-f ({N-f}) AUX values are received',
extra={'nodeid': pid, 'epoch': r})
while True:
logger.debug(f'bin_values[{r}]: {bin_values[r]}',
extra={'nodeid': pid, 'epoch': r})
logger.debug(f'aux_values[{r}]: {aux_values[r]}',
extra={'nodeid': pid, 'epoch': r})
# Block until at least N-f AUX values are received
if 1 in bin_values[r] and len(aux_values[r][1]) >= N - f:
values = set((1,))
# print('[sid:%s] [pid:%d] VALUES 1 %d' % (sid, pid, r))
break
if 0 in bin_values[r] and len(aux_values[r][0]) >= N - f:
values = set((0,))
# print('[sid:%s] [pid:%d] VALUES 0 %d' % (sid, pid, r))
break
if sum(len(aux_values[r][v]) for v in bin_values[r]) >= N - f:
values = set((0, 1))
# print('[sid:%s] [pid:%d] VALUES BOTH %d' % (sid, pid, r))
break
bv_signal.clear()
bv_signal.wait()
logger.debug(f'Completed AUX phase with values = {values}',
extra={'nodeid': pid, 'epoch': r})
# CONF phase
logger.debug(
f'block until at least N-f ({N-f}) CONF values are received',
extra={'nodeid': pid, 'epoch': r})
if not conf_sent[r][tuple(values)]:
values = wait_for_conf_values(
pid=pid,
N=N,
f=f,
epoch=r,
conf_sent=conf_sent,
bin_values=bin_values,
values=values,
conf_values=conf_values,
bv_signal=bv_signal,
broadcast=broadcast,
)
logger.debug(f'Completed CONF phase with values = {values}',
extra={'nodeid': pid, 'epoch': r})
logger.debug(
f'Block until receiving the common coin value',
extra={'nodeid': pid, 'epoch': r},
)
# Block until receiving the common coin value
s = coin(r)
logger.info(f'Received coin with value = {s}',
extra={'nodeid': pid, 'epoch': r})
try:
est, already_decided = set_new_estimate(
values=values,
s=s,
already_decided=already_decided,
decide=decide,
)
except AbandonedNodeError:
# print('[sid:%s] [pid:%d] QUITTING in round %d' % (sid,pid,r)))
logger.debug(f'QUIT!',
extra={'nodeid': pid, 'epoch': r})
_thread_recv.kill()
return
r += 1
def set_new_estimate(*, values, s, already_decided, decide):
if len(values) == 1:
v = next(iter(values))
if v == s:
if already_decided is None:
already_decided = v
decide(v)
elif already_decided == v:
# Here corresponds to a proof that if one party
# decides at round r, then in all the following
# rounds, everybody will propose r as an
# estimation. (Lemma 2, Lemma 1) An abandoned
# party is a party who has decided but no enough
# peers to help him end the loop. Lemma: # of
# abandoned party <= t
raise AbandonedNodeError
est = v
else:
est = s
return est, already_decided