aboutsummaryrefslogtreecommitdiffstats
path: root/core/configuration-chain.go
blob: 31827f225ae67f5a3599bb37f95ba3e2a4af6c66 (plain) (blame)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library 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 Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.

package core

import (
    "fmt"
    "sync"
    "time"

    "github.com/dexon-foundation/dexon-consensus/common"
    "github.com/dexon-foundation/dexon-consensus/core/crypto"
    "github.com/dexon-foundation/dexon-consensus/core/db"
    "github.com/dexon-foundation/dexon-consensus/core/types"
    typesDKG "github.com/dexon-foundation/dexon-consensus/core/types/dkg"
    "github.com/dexon-foundation/dexon-consensus/core/utils"
)

// Errors for configuration chain..
var (
    ErrDKGNotRegistered = fmt.Errorf(
        "not yet registered in DKG protocol")
    ErrTSigAlreadyRunning = fmt.Errorf(
        "tsig is already running")
    ErrDKGNotReady = fmt.Errorf(
        "DKG is not ready")
    ErrSkipButNoError = fmt.Errorf("skip but no error")
)

type configurationChain struct {
    ID              types.NodeID
    recv            dkgReceiver
    gov             Governance
    dkg             *dkgProtocol
    dkgRunPhases    []func(round uint64, reset uint64) error
    logger          common.Logger
    dkgLock         sync.RWMutex
    dkgSigner       map[uint64]*dkgShareSecret
    npks            map[uint64]*typesDKG.NodePublicKeys
    dkgResult       sync.RWMutex
    tsig            map[common.Hash]*tsigProtocol
    tsigTouched     map[common.Hash]struct{}
    tsigReady       *sync.Cond
    cache           *utils.NodeSetCache
    db              db.Database
    dkgSet          map[types.NodeID]struct{}
    mpkReady        bool
    pendingPrvShare map[types.NodeID]*typesDKG.PrivateShare
    // TODO(jimmy-dexon): add timeout to pending psig.
    pendingPsig map[common.Hash][]*typesDKG.PartialSignature
    prevHash    common.Hash
}

func newConfigurationChain(
    ID types.NodeID,
    recv dkgReceiver,
    gov Governance,
    cache *utils.NodeSetCache,
    dbInst db.Database,
    logger common.Logger) *configurationChain {
    configurationChain := &configurationChain{
        ID:          ID,
        recv:        recv,
        gov:         gov,
        logger:      logger,
        dkgSigner:   make(map[uint64]*dkgShareSecret),
        npks:        make(map[uint64]*typesDKG.NodePublicKeys),
        tsig:        make(map[common.Hash]*tsigProtocol),
        tsigTouched: make(map[common.Hash]struct{}),
        tsigReady:   sync.NewCond(&sync.Mutex{}),
        cache:       cache,
        db:          dbInst,
        pendingPsig: make(map[common.Hash][]*typesDKG.PartialSignature),
    }
    configurationChain.initDKGPhasesFunc()
    return configurationChain
}

func (cc *configurationChain) registerDKG(round, reset uint64, threshold int) {
    cc.dkgLock.Lock()
    defer cc.dkgLock.Unlock()
    if cc.dkg != nil {
        cc.logger.Error("Previous DKG is not finished")
        // TODO(mission): return here and fix CI failure.
    }
    dkgSet, err := cc.cache.GetDKGSet(round)
    if err != nil {
        cc.logger.Error("Error getting DKG set from cache", "error", err)
        return
    }
    cc.dkgSet = dkgSet
    cc.pendingPrvShare = make(map[types.NodeID]*typesDKG.PrivateShare)
    cc.mpkReady = false
    cc.dkg, err = recoverDKGProtocol(cc.ID, cc.recv, round, reset, cc.db)
    if err != nil {
        panic(err)
    }

    if cc.dkg == nil {
        cc.dkg = newDKGProtocol(
            cc.ID,
            cc.recv,
            round,
            reset,
            threshold)

        err = cc.db.PutOrUpdateDKGProtocol(cc.dkg.toDKGProtocolInfo())
        if err != nil {
            cc.logger.Error("Error put or update DKG protocol", "error",
                err)
            return
        }
    }

    go func() {
        ticker := newTicker(cc.gov, round, TickerDKG)
        defer ticker.Stop()
        <-ticker.Tick()
        cc.dkgLock.Lock()
        defer cc.dkgLock.Unlock()
        cc.dkg.proposeMPKReady()
    }()
}

func (cc *configurationChain) runDKGPhaseOne(round uint64, reset uint64) error {
    if cc.dkg.round < round ||
        (cc.dkg.round == round && cc.dkg.reset < reset) {
        return ErrDKGNotRegistered
    }
    if cc.dkg.round != round || cc.dkg.reset != reset {
        cc.logger.Warn("DKG canceled", "round", round, "reset", reset)
        return ErrSkipButNoError
    }
    cc.logger.Debug("Calling Governance.IsDKGFinal", "round", round)
    if cc.gov.IsDKGFinal(round) {
        cc.logger.Warn("DKG already final", "round", round)
        return ErrSkipButNoError
    }
    cc.logger.Debug("Calling Governance.IsDKGMPKReady", "round", round)
    for !cc.gov.IsDKGMPKReady(round) {
        cc.logger.Debug("DKG MPKs are not ready yet. Try again later...",
            "nodeID", cc.ID)
        cc.dkgLock.Unlock()
        time.Sleep(500 * time.Millisecond)
        cc.dkgLock.Lock()
    }

    return nil
}

func (cc *configurationChain) runDKGPhaseTwoAndThree(round uint64, reset uint64) error {
    // Check if this node successfully join the protocol.
    cc.logger.Debug("Calling Governance.DKGMasterPublicKeys", "round", round)
    mpks := cc.gov.DKGMasterPublicKeys(round)
    inProtocol := false
    for _, mpk := range mpks {
        if mpk.ProposerID == cc.ID {
            inProtocol = true
            break
        }
    }
    if !inProtocol {
        cc.logger.Warn("Failed to join DKG protocol",
            "round", round,
            "reset", reset)
        return ErrSkipButNoError
    }
    // Phase 2(T = 0): Exchange DKG secret key share.
    if err := cc.dkg.processMasterPublicKeys(mpks); err != nil {
        cc.logger.Error("Failed to process master public key",
            "round", round,
            "reset", reset,
            "error", err)
    }
    cc.mpkReady = true
    for _, prvShare := range cc.pendingPrvShare {
        if err := cc.dkg.processPrivateShare(prvShare); err != nil {
            cc.logger.Error("Failed to process private share",
                "round", round,
                "reset", reset,
                "error", err)
        }
    }

    // Phase 3(T = 0~λ): Propose complaint.
    // Propose complaint is done in `processMasterPublicKeys`.
    return nil
}

func (cc *configurationChain) runDKGPhaseFour() {
    // Phase 4(T = λ): Propose nack complaints.
    cc.dkg.proposeNackComplaints()
}

func (cc *configurationChain) runDKGPhaseFiveAndSix(round uint64, reset uint64) {
    // Phase 5(T = 2λ): Propose Anti nack complaint.
    cc.logger.Debug("Calling Governance.DKGComplaints", "round", round)
    complaints := cc.gov.DKGComplaints(round)
    if err := cc.dkg.processNackComplaints(complaints); err != nil {
        cc.logger.Error("Failed to process NackComplaint",
            "round", round,
            "reset", reset,
            "error", err)
    }

    // Phase 6(T = 3λ): Rebroadcast anti nack complaint.
    // Rebroadcast is done in `processPrivateShare`.
}

func (cc *configurationChain) runDKGPhaseSeven(complaints []*typesDKG.Complaint) {
    // Phase 7(T = 4λ): Enforce complaints and nack complaints.
    cc.dkg.enforceNackComplaints(complaints)
    // Enforce complaint is done in `processPrivateShare`.
}

func (cc *configurationChain) runDKGPhaseEight() {
    // Phase 8(T = 5λ): DKG finalize.
    cc.dkg.proposeFinalize()
}

func (cc *configurationChain) runDKGPhaseNine(round uint64, reset uint64) error {
    // Phase 9(T = 6λ): DKG is ready.
    // Normally, IsDKGFinal would return true here. Use this for in case of
    // unexpected network fluctuation and ensure the robustness of DKG protocol.
    cc.logger.Debug("Calling Governance.IsDKGFinal", "round", round)
    for !cc.gov.IsDKGFinal(round) {
        cc.logger.Debug("DKG is not ready yet. Try again later...",
            "nodeID", cc.ID.String()[:6],
            "round", round,
            "reset", reset)
        time.Sleep(500 * time.Millisecond)
    }
    cc.logger.Debug("Calling Governance.DKGMasterPublicKeys", "round", round)
    cc.logger.Debug("Calling Governance.DKGComplaints", "round", round)
    npks, err := typesDKG.NewNodePublicKeys(round,
        cc.gov.DKGMasterPublicKeys(round),
        cc.gov.DKGComplaints(round),
        cc.dkg.threshold)
    if err != nil {
        return err
    }
    qualifies := ""
    for nID := range npks.QualifyNodeIDs {
        qualifies += fmt.Sprintf("%s ", nID.String()[:6])
    }
    cc.logger.Info("Qualify Nodes",
        "nodeID", cc.ID,
        "round", round,
        "reset", reset,
        "count", len(npks.QualifyIDs),
        "qualifies", qualifies)
    if _, exist := npks.QualifyNodeIDs[cc.ID]; !exist {
        cc.logger.Warn("Self is not in Qualify Nodes",
            "round", round,
            "reset", reset)
        return nil
    }
    signer, err := cc.dkg.recoverShareSecret(npks.QualifyIDs)
    if err != nil {
        return err
    }
    // Save private shares to DB.
    if err = cc.db.PutDKGPrivateKey(round, *signer.privateKey); err != nil {
        return err
    }
    cc.dkgResult.Lock()
    defer cc.dkgResult.Unlock()
    cc.dkgSigner[round] = signer
    cc.npks[round] = npks
    return nil
}

func (cc *configurationChain) runTick(ticker Ticker) {
    cc.dkgLock.Unlock()
    <-ticker.Tick()
    cc.dkgLock.Lock()
}

func (cc *configurationChain) initDKGPhasesFunc() {
    cc.dkgRunPhases = []func(round uint64, reset uint64) error{
        func(round uint64, reset uint64) error {
            return cc.runDKGPhaseOne(round, reset)
        },
        func(round uint64, reset uint64) error {
            return cc.runDKGPhaseTwoAndThree(round, reset)
        },
        func(round uint64, reset uint64) error {
            cc.runDKGPhaseFour()
            return nil
        },
        func(round uint64, reset uint64) error {
            cc.runDKGPhaseFiveAndSix(round, reset)
            return nil
        },
        func(round uint64, reset uint64) error {
            complaints := cc.gov.DKGComplaints(round)
            cc.runDKGPhaseSeven(complaints)
            return nil
        },
        func(round uint64, reset uint64) error {
            cc.runDKGPhaseEight()
            return nil
        },
        func(round uint64, reset uint64) error {
            return cc.runDKGPhaseNine(round, reset)
        },
    }
}

func (cc *configurationChain) runDKG(round uint64, reset uint64) error {
    // Check if corresponding DKG signer is ready.
    if _, _, err := cc.getDKGInfo(round); err == nil {
        return ErrSkipButNoError
    }
    cc.dkgLock.Lock()
    defer cc.dkgLock.Unlock()

    tickStartAt := 1
    var ticker Ticker
    defer func() {
        if ticker != nil {
            ticker.Stop()
        }
    }()

    if cc.dkg == nil {
        return ErrDKGNotRegistered
    }

    for i := cc.dkg.step; i < len(cc.dkgRunPhases); i++ {
        if i >= tickStartAt && ticker == nil {
            ticker = newTicker(cc.gov, round, TickerDKG)
        }

        if ticker != nil {
            cc.runTick(ticker)
        }

        switch err := cc.dkgRunPhases[i](round, reset); err {
        case ErrSkipButNoError, nil:
            cc.dkg.step = i + 1
            err := cc.db.PutOrUpdateDKGProtocol(cc.dkg.toDKGProtocolInfo())
            if err != nil {
                return fmt.Errorf("put or update DKG protocol error: %v", err)
            }

            if err == nil {
                continue
            } else {
                return nil
            }
        default:
            return err
        }
    }

    return nil
}

func (cc *configurationChain) isDKGFinal(round uint64) bool {
    if !cc.gov.IsDKGFinal(round) {
        return false
    }
    _, _, err := cc.getDKGInfo(round)
    return err == nil
}

func (cc *configurationChain) getDKGInfo(
    round uint64) (*typesDKG.NodePublicKeys, *dkgShareSecret, error) {
    getFromCache := func() (*typesDKG.NodePublicKeys, *dkgShareSecret) {
        cc.dkgResult.RLock()
        defer cc.dkgResult.RUnlock()
        npks := cc.npks[round]
        signer := cc.dkgSigner[round]
        return npks, signer
    }
    npks, signer := getFromCache()
    if npks == nil || signer == nil {
        if err := cc.recoverDKGInfo(round); err != nil {
            return nil, nil, err
        }
        npks, signer = getFromCache()
    }
    if npks == nil || signer == nil {
        return nil, nil, ErrDKGNotReady
    }
    return npks, signer, nil
}

func (cc *configurationChain) recoverDKGInfo(round uint64) error {
    var npksExists, signerExists bool
    func() {
        cc.dkgResult.Lock()
        defer cc.dkgResult.Unlock()
        _, signerExists = cc.dkgSigner[round]
        _, npksExists = cc.npks[round]
    }()
    if signerExists && npksExists {
        return nil
    }
    if !cc.gov.IsDKGFinal(round) {
        return ErrDKGNotReady
    }

    if !npksExists {
        threshold := utils.GetDKGThreshold(
            utils.GetConfigWithPanic(cc.gov, round, cc.logger))
        // Restore group public key.
        cc.logger.Debug("Calling Governance.DKGMasterPublicKeys for recoverDKGInfo",
            "round", round)
        cc.logger.Debug("Calling Governance.DKGComplaints for recoverDKGInfo",
            "round", round)
        npks, err := typesDKG.NewNodePublicKeys(round,
            cc.gov.DKGMasterPublicKeys(round),
            cc.gov.DKGComplaints(round),
            threshold)
        if err != nil {
            cc.logger.Warn("Failed to create DKGGroupPublicKey",
                "round", round, "error", err)
            return err
        }
        func() {
            cc.dkgResult.Lock()
            defer cc.dkgResult.Unlock()
            cc.npks[round] = npks
        }()
    }
    if !signerExists {
        // Check if we have private shares in DB.
        prvKey, err := cc.db.GetDKGPrivateKey(round)
        if err != nil {
            cc.logger.Warn("Failed to create DKGPrivateKey",
                "round", round, "error", err)
            return err
        }
        func() {
            cc.dkgResult.Lock()
            defer cc.dkgResult.Unlock()
            cc.dkgSigner[round] = &dkgShareSecret{
                privateKey: &prvKey,
            }
        }()
    }
    return nil
}

func (cc *configurationChain) preparePartialSignature(
    round uint64, hash common.Hash) (*typesDKG.PartialSignature, error) {
    _, signer, _ := cc.getDKGInfo(round)
    if signer == nil {
        return nil, ErrDKGNotReady
    }
    return &typesDKG.PartialSignature{
        ProposerID:       cc.ID,
        Round:            round,
        Hash:             hash,
        PartialSignature: signer.sign(hash),
    }, nil
}

func (cc *configurationChain) touchTSigHash(hash common.Hash) (first bool) {
    cc.tsigReady.L.Lock()
    defer cc.tsigReady.L.Unlock()
    _, exist := cc.tsigTouched[hash]
    cc.tsigTouched[hash] = struct{}{}
    return !exist
}

func (cc *configurationChain) untouchTSigHash(hash common.Hash) {
    cc.tsigReady.L.Lock()
    defer cc.tsigReady.L.Unlock()
    delete(cc.tsigTouched, hash)
}

func (cc *configurationChain) runTSig(
    round uint64, hash common.Hash) (
    crypto.Signature, error) {
    npks, _, _ := cc.getDKGInfo(round)
    if npks == nil {
        return crypto.Signature{}, ErrDKGNotReady
    }
    cc.tsigReady.L.Lock()
    defer cc.tsigReady.L.Unlock()
    if _, exist := cc.tsig[hash]; exist {
        return crypto.Signature{}, ErrTSigAlreadyRunning
    }
    cc.tsig[hash] = newTSigProtocol(npks, hash)
    pendingPsig := cc.pendingPsig[hash]
    delete(cc.pendingPsig, hash)
    go func() {
        for _, psig := range pendingPsig {
            if err := cc.processPartialSignature(psig); err != nil {
                cc.logger.Error("failed to process partial signature",
                    "nodeID", cc.ID,
                    "error", err)
            }
        }
    }()
    timeout := make(chan struct{}, 1)
    go func() {
        // TODO(jimmy-dexon): make timeout configurable.
        time.Sleep(5 * time.Second)
        timeout <- struct{}{}
        cc.tsigReady.Broadcast()
    }()
    var signature crypto.Signature
    var err error
    for func() bool {
        signature, err = cc.tsig[hash].signature()
        select {
        case <-timeout:
            return false
        default:
        }
        return err == ErrNotEnoughtPartialSignatures
    }() {
        cc.tsigReady.Wait()
    }
    delete(cc.tsig, hash)
    if err != nil {
        return crypto.Signature{}, err
    }
    return signature, nil
}

func (cc *configurationChain) runCRSTSig(
    round uint64, crs common.Hash) ([]byte, error) {
    sig, err := cc.runTSig(round, crs)
    cc.logger.Info("CRS",
        "nodeID", cc.ID,
        "round", round+1,
        "signature", sig)
    return sig.Signature[:], err
}

func (cc *configurationChain) processPrivateShare(
    prvShare *typesDKG.PrivateShare) error {
    cc.dkgLock.Lock()
    defer cc.dkgLock.Unlock()
    if cc.dkg == nil {
        return nil
    }
    if _, exist := cc.dkgSet[prvShare.ProposerID]; !exist {
        return ErrNotDKGParticipant
    }
    if !cc.mpkReady {
        // TODO(jimmy-dexon): remove duplicated signature check in dkg module.
        ok, err := utils.VerifyDKGPrivateShareSignature(prvShare)
        if err != nil {
            return err
        }
        if !ok {
            return ErrIncorrectPrivateShareSignature
        }
        cc.pendingPrvShare[prvShare.ProposerID] = prvShare
        return nil
    }
    return cc.dkg.processPrivateShare(prvShare)
}

func (cc *configurationChain) processPartialSignature(
    psig *typesDKG.PartialSignature) error {
    cc.tsigReady.L.Lock()
    defer cc.tsigReady.L.Unlock()
    if _, exist := cc.tsig[psig.Hash]; !exist {
        ok, err := utils.VerifyDKGPartialSignatureSignature(psig)
        if err != nil {
            return err
        }
        if !ok {
            return ErrIncorrectPartialSignatureSignature
        }
        cc.pendingPsig[psig.Hash] = append(cc.pendingPsig[psig.Hash], psig)
        return nil
    }
    if err := cc.tsig[psig.Hash].processPartialSignature(psig); err != nil {
        return err
    }
    cc.tsigReady.Broadcast()
    return nil
}