/
byzbcasts.go
323 lines (298 loc) · 13.2 KB
/
byzbcasts.go
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
github.com/tcrain/cons - Experimental project for testing and scaling consensus algorithms.
Copyright (C) 2020 The project authors - tcrain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package cons
import (
"github.com/tcrain/cons/consensus/auth/sig"
"github.com/tcrain/cons/consensus/channelinterface"
"github.com/tcrain/cons/consensus/consinterface"
"github.com/tcrain/cons/consensus/generalconfig"
"github.com/tcrain/cons/consensus/messages"
"github.com/tcrain/cons/consensus/messagetypes"
"github.com/tcrain/cons/consensus/types"
)
func BroadcastHalfHalfFixedBin(nextCoordPub sig.Pub,
ci *consinterface.ConsInterfaceItems,
msg messages.InternalSignedMsgHeader,
signMessage bool,
forwardFunc channelinterface.NewForwardFuncFilter,
mainChannel channelinterface.MainChannel,
gc *generalconfig.GeneralConfig,
additionalMsgs ...messages.MsgHeader) {
BroadcastHalfHalf(true, nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel,
gc, additionalMsgs...)
}
func BroadcastHalfHalfNormal(nextCoordPub sig.Pub,
ci *consinterface.ConsInterfaceItems,
msg messages.InternalSignedMsgHeader,
signMessage bool,
forwardFunc channelinterface.NewForwardFuncFilter,
mainChannel channelinterface.MainChannel,
gc *generalconfig.GeneralConfig,
additionalMsgs ...messages.MsgHeader) {
BroadcastHalfHalf(false, nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel,
gc, additionalMsgs...)
}
// BroadcastHalfHalf broadcast different messages to the nodes.
func BroadcastHalfHalf(fixedBin bool, nextCoordPub sig.Pub,
ci *consinterface.ConsInterfaceItems,
msg messages.InternalSignedMsgHeader,
signMessage bool,
forwardFunc channelinterface.NewForwardFuncFilter,
mainChannel channelinterface.MainChannel,
gc *generalconfig.GeneralConfig,
additionalMsgs ...messages.MsgHeader) {
if !generalconfig.CheckFaulty(ci.ConsItem.GetIndex(), gc) {
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
return
}
frontHalf, backHalf := ci.FwdChecker.GetHalfHalfForwardListFunc()
switch auxMsg := msg.(type) {
case *messagetypes.AuxProofMessage:
if fixedBin {
auxMsg.BinVal = 0
}
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, frontHalf, mainChannel, additionalMsgs...)
// Send the opposite bin val
if auxMsg.BinVal == types.Coin {
auxMsg.BinVal = 0 // TODO what should be done here?
} else {
auxMsg.BinVal = 1 - auxMsg.BinVal
}
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, backHalf, mainChannel, additionalMsgs...)
case *messagetypes.AuxBothMessage:
if fixedBin {
auxMsg.BinVal = 0
}
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, frontHalf, mainChannel, additionalMsgs...)
// Send the opposite bin val
if auxMsg.BinVal == types.Coin {
auxMsg.BinVal = 0 // TODO what should be done here?
} else {
auxMsg.BinVal = 1 - auxMsg.BinVal
}
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, backHalf, mainChannel, additionalMsgs...)
case *messagetypes.AuxStage0Message:
if fixedBin {
auxMsg.BinVal = 0
}
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, frontHalf, mainChannel, additionalMsgs...)
// Send the opposite bin val
auxMsg.BinVal = 1 - auxMsg.BinVal
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, backHalf, mainChannel, additionalMsgs...)
case *messagetypes.AuxStage1Message:
if fixedBin {
auxMsg.BinVal = 0
}
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, frontHalf, mainChannel, additionalMsgs...)
// Send the opposite bin val
if auxMsg.BinVal == types.Coin {
auxMsg.BinVal = 0 // TODO what should be done here?
} else {
auxMsg.BinVal = 1 - auxMsg.BinVal
}
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, backHalf, mainChannel, additionalMsgs...)
case *messagetypes.BVMessage0:
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, frontHalf, mainChannel, additionalMsgs...)
// Send the opposite bin val
msg = messagetypes.CreateBVMessage(1, auxMsg.Round)
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, backHalf, mainChannel, additionalMsgs...)
case *messagetypes.BVMessage1:
var binVal types.BinVal = 1
if fixedBin {
binVal = 0
}
msg = messagetypes.CreateBVMessage(binVal, auxMsg.Round)
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, frontHalf, mainChannel, additionalMsgs...)
// Send the opposite bin val
msg = messagetypes.CreateBVMessage(1-binVal, auxMsg.Round)
ci.ConsItem.Broadcast(nextCoordPub, msg, signMessage, backHalf, mainChannel, additionalMsgs...)
case *messagetypes.MvInitMessage, *messagetypes.MvInitSupportMessage:
// Broadcast normally to first half
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, frontHalf, mainChannel, gc, additionalMsgs...)
// Broadcast with byzantine proposal to back half
switch auxMsg := auxMsg.(type) {
case *messagetypes.MvInitMessage:
auxMsg.Proposal, auxMsg.ByzProposal = auxMsg.ByzProposal, auxMsg.Proposal
case *messagetypes.MvInitSupportMessage:
auxMsg.Proposal, auxMsg.ByzProposal = auxMsg.ByzProposal, auxMsg.Proposal
default:
panic(auxMsg)
}
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, backHalf, mainChannel, gc, additionalMsgs...)
switch auxMsg := auxMsg.(type) {
case *messagetypes.MvInitMessage:
auxMsg.Proposal, auxMsg.ByzProposal = auxMsg.ByzProposal, auxMsg.Proposal
case *messagetypes.MvInitSupportMessage:
auxMsg.Proposal, auxMsg.ByzProposal = auxMsg.ByzProposal, auxMsg.Proposal
default:
panic(auxMsg)
}
default:
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
}
}
// Broadcast an aux proof message, but flip the binary value.
func BroadcastBinFlip(nextCoordPub sig.Pub,
ci *consinterface.ConsInterfaceItems,
msg messages.InternalSignedMsgHeader,
signMessage bool,
forwardFunc channelinterface.NewForwardFuncFilter,
mainChannel channelinterface.MainChannel,
gc *generalconfig.GeneralConfig,
additionalMsgs ...messages.MsgHeader) {
if !generalconfig.CheckFaulty(ci.ConsItem.GetIndex(), gc) {
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
return
}
switch auxMsg := msg.(type) {
case *messagetypes.AuxBothMessage:
// Send the opposite bin val
if auxMsg.BinVal == types.Coin {
auxMsg.BinVal = 0 // TODO what should be done here?
} else {
// auxMsg.BinVal = 1 - auxMsg.BinVal
auxMsg.BinVal = types.Coin
}
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
case *messagetypes.AuxStage1Message:
// Send the opposite bin val
if auxMsg.BinVal == types.Coin {
auxMsg.BinVal = 0 // TODO what should be done here?
} else {
// auxMsg.BinVal = 1 - auxMsg.BinVal
auxMsg.BinVal = types.Coin
}
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
case *messagetypes.AuxStage0Message:
// Send the opposite bin val
auxMsg.BinVal = 1 - auxMsg.BinVal
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
case *messagetypes.AuxProofMessage:
// Send the opposite bin val
if auxMsg.BinVal == types.Coin {
auxMsg.BinVal = 0 // TODO what should be done here?
} else {
if auxMsg.AllowCoin {
auxMsg.BinVal = types.Coin
} else {
auxMsg.BinVal = 1 - auxMsg.BinVal
}
}
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
// ci.ConsItem.Broadcast(nextCoordPub, auxMsg, forwardFunc, mainChannel, additionalMsgs...)
case *messagetypes.BVMessage0:
// Send the opposite bin val
msg = messagetypes.CreateBVMessage(1, auxMsg.Round)
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
case *messagetypes.BVMessage1:
// Send the opposite bin val
msg = messagetypes.CreateBVMessage(0, auxMsg.Round)
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
default:
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
}
}
// Broadcast an aux proof message, but dont actually send it.
func BroadcastMute(nextCoordPub sig.Pub,
ci *consinterface.ConsInterfaceItems,
msg messages.InternalSignedMsgHeader,
signMessage bool,
forwardFunc channelinterface.NewForwardFuncFilter,
mainChannel channelinterface.MainChannel,
gc *generalconfig.GeneralConfig,
additionalMsgs ...messages.MsgHeader) {
if !generalconfig.CheckFaulty(ci.ConsItem.GetIndex(), gc) {
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
return
}
// Send nothing
}
// Broadcast two aux proof messages, one for 0 and one for 1.
func BroadcastBinBoth(nextCoordPub sig.Pub,
ci *consinterface.ConsInterfaceItems,
msg messages.InternalSignedMsgHeader,
signMessage bool,
forwardFunc channelinterface.NewForwardFuncFilter,
mainChannel channelinterface.MainChannel,
gc *generalconfig.GeneralConfig,
additionalMsgs ...messages.MsgHeader) {
if !generalconfig.CheckFaulty(ci.ConsItem.GetIndex(), gc) {
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
return
}
switch auxMsg := msg.(type) {
case *messagetypes.AuxStage0Message:
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
// Send the opposite bin val
// auxMsg.SetSigItems(nil)
BroadcastBinFlip(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
case *messagetypes.AuxStage1Message:
auxMsg.BinVal = types.Coin
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
auxMsg.BinVal = 0
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
auxMsg.BinVal = 1
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
case *messagetypes.AuxBothMessage:
auxMsg.BinVal = types.Coin
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
auxMsg.BinVal = 0
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
auxMsg.BinVal = 1
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
case *messagetypes.AuxProofMessage:
if auxMsg.AllowCoin { // Broadcast all 3 options
auxMsg.BinVal = types.Coin
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
auxMsg.BinVal = 0
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
auxMsg.BinVal = 1
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
} else {
// Send the normal bin val
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
// Send the opposite bin val
// auxMsg.SetSigItems(nil)
BroadcastBinFlip(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
}
case *messagetypes.BVMessage0, *messagetypes.BVMessage1:
// Send the normal bin val
consinterface.NormalBroadcast(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
// Send the opposite bin val
// auxMsg.SetSigItems(nil)
BroadcastBinFlip(nextCoordPub, ci, auxMsg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
default:
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
}
}
// BroadcastMuteExceptInit only broadcasts init messages
func BroadcastMuteExceptInit(nextCoordPub sig.Pub,
ci *consinterface.ConsInterfaceItems,
msg messages.InternalSignedMsgHeader,
signMessage bool,
forwardFunc channelinterface.NewForwardFuncFilter,
mainChannel channelinterface.MainChannel,
gc *generalconfig.GeneralConfig,
additionalMsgs ...messages.MsgHeader) {
if !generalconfig.CheckFaulty(ci.ConsItem.GetIndex(), gc) {
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
return
}
switch msg.GetID() {
case messages.HdrMvInit, messages.HdrMvInitSupport:
consinterface.NormalBroadcast(nextCoordPub, ci, msg, signMessage, forwardFunc, mainChannel, gc, additionalMsgs...)
default: // Do nothing
}
}