/
vss_protocol.go
408 lines (345 loc) · 11.4 KB
/
vss_protocol.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
This file is part of go-palletone.
go-palletone 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.
go-palletone 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 go-palletone. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* @author PalletOne core developer Albert·Gou <dev@pallet.one>
* @date 2018
*/
package mediatorplugin
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/event"
"github.com/palletone/go-palletone/common"
"github.com/palletone/go-palletone/common/log"
"go.dedis.ch/kyber/v3/share/dkg/pedersen"
"go.dedis.ch/kyber/v3/share/vss/pedersen"
)
func (mp *MediatorPlugin) newDKGAndInitVSSBuf() {
log.Debugf("initialize all mediator's dkgs, dealBufs, and responseBufs")
// 初始化dkg,并初始化与完成vss相关的buf
mp.dkgLock.Lock()
//log.Debugf("dkgLock.Lock()")
//defer log.Debugf("dkgLock.Unlock()")
defer mp.dkgLock.Unlock()
mp.vssBufLock.Lock()
//log.Debugf("vssBufLock.Lock()")
//defer log.Debugf("vssBufLock.Unlock()")
defer mp.vssBufLock.Unlock()
dag := mp.dag
lams := mp.GetLocalActiveMediators()
initPubs := dag.GetActiveMediatorInitPubs()
curThreshold := dag.ChainThreshold()
lamc := len(lams)
mp.activeDKGs = make(map[common.Address]*dkg.DistKeyGenerator, lamc)
ams := dag.GetActiveMediators()
aSize := len(ams)
for _, localMed := range lams {
// 初始化本地所有mediator的dkg
initSec := mp.mediators[localMed].InitPrivKey
dkgr, err := dkg.NewDistKeyGenerator(mp.suite, initSec, initPubs, curThreshold)
if err != nil {
log.Debugf("New the mediator(%v)'s DistKeyGenerator get err: %v", localMed.Str(), err.Error())
continue
}
mp.activeDKGs[localMed] = dkgr
// 初始化所有与完成vss相关的buf
mp.dealBuf[localMed] = make(chan *dkg.Deal, aSize-1)
mp.respBuf[localMed] = make(map[common.Address]chan *dkg.Response, aSize)
//mp.respBuf[localMed] = make(map[common.Address]chan *dkg.Response, aSize-1)
for _, vrfrMed := range ams {
//if vrfrMed == localMed {
// continue
//}
mp.respBuf[localMed][vrfrMed] = make(chan *dkg.Response, aSize)
//mp.respBuf[localMed][vrfrMed] = make(chan *dkg.Response, aSize-1)
}
}
}
func (mp *MediatorPlugin) startVSSProtocol() {
log.Debugf("start completing the VSS protocol")
// 开启处理其他 mediator 的 deals 的循环,准备完成vss协议
mp.launchVSSDealLoops()
cp := mp.dag.GetGlobalProp().ChainParameters
margin := (cp.MediatorInterval + 1) / 2
// 隔半个生产间隔,等待其他节点接收新unit,并做好vss协议相关准备工作
select {
case <-mp.quit:
return
case <-time.After(time.Second * time.Duration(margin)):
// 广播 vss deal 给其他节点,并处理来自其他节点的deal
go mp.broadcastVSSDeals()
}
// 再隔 MaintenanceSkipSlots 个生产间隔,才处理response,防止对应的 deal 还没收到的情况
select {
case <-mp.quit:
return
case <-time.After(time.Second * time.Duration(cp.MaintenanceSkipSlots*cp.MediatorInterval)):
go mp.launchVSSRespLoops()
}
// 再隔半个生产间隔,验证vss协议是否完成,并开始群签名
select {
case <-mp.quit:
return
case <-time.After(time.Second * time.Duration(cp.MediatorInterval-margin)):
go mp.completeVSSProtocol()
}
}
func (mp *MediatorPlugin) completeVSSProtocol() {
log.Debugf("to complete vss protocol")
// 删除vss相关缓存
mp.vssBufLock.Lock()
//log.Debugf("vssBufLock.Lock()")
lamc := len(mp.mediators)
mp.dealBuf = make(map[common.Address]chan *dkg.Deal, lamc)
mp.respBuf = make(map[common.Address]map[common.Address]chan *dkg.Response, lamc)
//log.Debugf("vssBufLock.Unlock()")
mp.vssBufLock.Unlock()
// 停止所有vss相关的循环
go func() {
select {
case <-mp.quit:
return
default:
mp.stopVSS <- struct{}{}
}
}()
// 验证vss是否完成,并开启群签名
go mp.launchGroupSignLoops()
}
func (mp *MediatorPlugin) launchGroupSignLoops() {
lams := mp.GetLocalActiveMediators()
mp.dkgLock.Lock()
//log.Debugf("dkgLock.Lock()")
//defer log.Debugf("dkgLock.Unlock()")
defer mp.dkgLock.Unlock()
for _, localMed := range lams {
dkgr, ok := mp.activeDKGs[localMed]
if !ok || dkgr == nil {
log.Debugf("the mediator(%v)'s dkg is not existed, or it is not active", localMed.String())
continue
}
if dkgr.Certified() {
log.Debugf("the mediator(%v)'s DKG verification passed, we can start group-signing",
localMed.Str())
go mp.signUnitsTBLS(localMed)
go mp.recoverUnitsTBLS(localMed)
} else {
log.Debugf("the mediator(%v)'s DKG verification failed", localMed.Str())
}
}
}
func (mp *MediatorPlugin) launchVSSDealLoops() {
lams := mp.GetLocalActiveMediators()
for _, localMed := range lams {
go mp.processDealLoop(localMed)
}
}
//func (mp *MediatorPlugin) launchDealAndRespLoops() {
func (mp *MediatorPlugin) launchVSSRespLoops() {
lams := mp.GetLocalActiveMediators()
ams := mp.dag.GetActiveMediators()
for _, localMed := range lams {
//go mp.processDealLoop(localMed)
for _, vrfrMed := range ams {
go mp.processResponseLoop(localMed, vrfrMed)
}
}
}
func (mp *MediatorPlugin) processDealLoop(localMed common.Address) {
log.Debugf("the local active mediator(%v) run the loop to process deal", localMed.Str())
mp.vssBufLock.RLock()
dealCh, ok := mp.dealBuf[localMed]
mp.vssBufLock.RUnlock()
if !ok {
log.Debugf("the mediator(%v)'s dealBuf has not initialized yet", localMed.Str())
return
}
for {
select {
case <-mp.quit:
return
case <-mp.stopVSS:
return
case deal := <-dealCh:
// 处理deal,并广播response
go mp.processVSSDeal(localMed, deal)
}
}
}
func (mp *MediatorPlugin) processVSSDeal(localMed common.Address, deal *dkg.Deal) {
mp.dkgLock.Lock()
//log.Debugf("dkgLock.Lock()")
//defer log.Debugf("dkgLock.Unlock()")
defer mp.dkgLock.Unlock()
dkgr, ok := mp.activeDKGs[localMed]
if !ok || dkgr == nil {
log.Debugf("the mediator(%v)'s dkg is not existed, or it is not active mediator", localMed.String())
return
}
vrfrMed := mp.dag.GetActiveMediatorAddr(int(deal.Index))
log.Debugf("the mediator(%v) process the vss deal from the mediator(%v)",
localMed.Str(), vrfrMed.Str())
resp, err := dkgr.ProcessDeal(deal)
if err != nil {
log.Debugf("the mediator(%v)'s dkg cannot process the mediator(%v)'s deal: %v",
localMed.Str(), vrfrMed.Str(), err.Error())
return
}
if resp.Response.Status != vss.StatusApproval {
errStr := fmt.Errorf("the mediator(%v)'s dkg gave the mediator(%v)'s deal a complaint: %v",
localMed.Str(), vrfrMed.Str(), resp.Response.Status)
log.Debugf(errStr.Error())
return
}
respEvent := VSSResponseEvent{
Resp: resp,
Deadline: mp.getGroupSignMessageDeadline(),
}
go mp.vssResponseFeed.Send(respEvent)
log.Debugf("the mediator(%v) broadcast the vss response to the mediator(%v)",
localMed.Str(), vrfrMed.Str())
}
func (mp *MediatorPlugin) broadcastVSSDeals() {
mp.dkgLock.Lock()
//log.Debugf("dkgLock.Lock()")
//defer log.Debugf("dkgLock.Unlock()")
defer mp.dkgLock.Unlock()
// 将deal广播给其他节点
for localMed, dkg := range mp.activeDKGs {
deals, err := dkg.Deals()
if err != nil {
log.Debugf("the mediator(%v)'s dkg get deals err: %v", localMed.Str(), err.Error())
continue
}
log.Debugf("the mediator(%v) broadcast vss deals", localMed.Str())
for index, deal := range deals {
event := VSSDealEvent{
DstIndex: uint32(index),
Deal: deal,
Deadline: mp.getGroupSignMessageDeadline(),
}
go mp.vssDealFeed.Send(event)
}
}
}
func (mp *MediatorPlugin) SubscribeVSSDealEvent(ch chan<- VSSDealEvent) event.Subscription {
return mp.vssDealScope.Track(mp.vssDealFeed.Subscribe(ch))
}
func (mp *MediatorPlugin) AddToDealBuf(dealEvent *VSSDealEvent) {
if !mp.groupSigningEnabled {
return
}
dag := mp.dag
localMed := dag.GetActiveMediatorAddr(int(dealEvent.DstIndex))
deal := dealEvent.Deal
vrfrMed := dag.GetActiveMediatorAddr(int(deal.Index))
log.Debugf("the mediator(%v) received the vss deal from the mediator(%v)",
localMed.Str(), vrfrMed.Str())
// 判断是否本地mediator的deal
mp.vssBufLock.Lock()
//log.Debugf("vssBufLock.Lock()")
dealCh, ok := mp.dealBuf[localMed]
if ok {
dealCh <- deal
} else {
log.Debugf("the mediator(%v)'s dealBuf is cleared, or it's not local mediator", localMed.Str())
}
//log.Debugf("vssBufLock.Unlock()")
mp.vssBufLock.Unlock()
}
func (mp *MediatorPlugin) SubscribeVSSResponseEvent(ch chan<- VSSResponseEvent) event.Subscription {
return mp.vssResponseScope.Track(mp.vssResponseFeed.Subscribe(ch))
}
func (mp *MediatorPlugin) AddToResponseBuf(respEvent *VSSResponseEvent) {
if !mp.groupSigningEnabled {
return
}
resp := respEvent.Resp
lams := mp.GetLocalActiveMediators()
for _, localMed := range lams {
dag := mp.dag
srcIndex := resp.Response.Index
srcMed := dag.GetActiveMediatorAddr(int(srcIndex))
if srcMed == localMed {
//log.Debugf("ignore the vss response message from myself(mediator: %v)", srcMed.Str())
continue
}
vrfrMed := dag.GetActiveMediatorAddr(int(resp.Index))
log.Debugf("the mediator(%v) received the vss response from the mediator(%v) to the mediator(%v)",
localMed.Str(), srcMed.Str(), vrfrMed.Str())
mp.vssBufLock.Lock()
//log.Debugf("vssBufLock.Lock()")
respCh, ok := mp.respBuf[localMed][vrfrMed]
if ok {
respCh <- resp
} else {
log.Debugf("the mediator(%v)'s respBuf regarding the mediator(%v) is cleared,"+
" or the mediator(%v) is not local mediator", localMed.Str(), vrfrMed.Str(), localMed.Str())
}
//log.Debugf("vssBufLock.Unlock()")
mp.vssBufLock.Unlock()
}
}
func (mp *MediatorPlugin) processResponseLoop(localMed, vrfrMed common.Address) {
log.Debugf("the mediator(%v) run the loop to process response regarding the mediator(%v)",
localMed.Str(), vrfrMed.Str())
mp.vssBufLock.RLock()
respCh, ok := mp.respBuf[localMed][vrfrMed]
mp.vssBufLock.RUnlock()
if !ok {
log.Debugf("the mediator(%v)'s respBuf corresponding the mediator(%v) has not initialized yet",
localMed.Str(), vrfrMed.Str())
return
}
for {
select {
case <-mp.quit:
return
case <-mp.stopVSS:
return
case resp := <-respCh:
go mp.processVSSResp(localMed, vrfrMed, resp)
}
}
}
func (mp *MediatorPlugin) processVSSResp(localMed, vrfrMed common.Address, resp *dkg.Response) {
mp.dkgLock.Lock()
//log.Debugf("dkgLock.Lock()")
//defer log.Debugf("dkgLock.Unlock()")
defer mp.dkgLock.Unlock()
dkgr, ok := mp.activeDKGs[localMed]
if !ok || dkgr == nil {
log.Debugf("the mediator(%v)'s dkg is not existed, or it is not active", localMed.String())
return
}
jstf, err := dkgr.ProcessResponse(resp)
if err != nil {
log.Debugf("the mediator(%v)'s dkg process response regarding the mediator(%v) err: %v",
localMed.Str(), vrfrMed.Str(), err.Error())
return
}
if jstf != nil {
log.Debugf("DKG: wrong Process Response: %v", localMed.String())
return
}
}
func (mp *MediatorPlugin) getGroupSignMessageDeadline() uint64 {
cp := mp.dag.GetGlobalProp().ChainParameters
slots := cp.MaintenanceSkipSlots
if slots == 0 {
slots = 1
}
deadline := time.Now().Unix() + int64(cp.MediatorInterval*slots)
return uint64(deadline)
}