/
timeout_cert_manager.go
130 lines (113 loc) · 3.14 KB
/
timeout_cert_manager.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
// Copyright (c) 2020 The Meter.io developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package consensus
import (
"sync"
bls "github.com/saolacoincom/saolacoin/crypto/multi_sig"
cmn "github.com/saolacoincom/saolacoin/libs/common"
)
type timeoutID struct {
Height uint32
Round uint32
}
type timeoutVal struct {
Counter uint64
PeerID []byte
PeerIndex uint32
MsgHash [32]byte
Signature bls.Signature
}
type PMTimeoutCertManager struct {
sync.RWMutex
pacemaker *Pacemaker
cache map[timeoutID][]*timeoutVal
bitArrays map[timeoutID]*cmn.BitArray
}
func newPMTimeoutCertManager(pacemaker *Pacemaker) *PMTimeoutCertManager {
return &PMTimeoutCertManager{
pacemaker: pacemaker,
cache: make(map[timeoutID][]*timeoutVal),
bitArrays: make(map[timeoutID]*cmn.BitArray),
}
}
func (tm *PMTimeoutCertManager) collectSignature(newViewMsg *PMNewViewMessage) {
tm.Lock()
defer tm.Unlock()
if newViewMsg.Reason == RoundTimeout {
index := int(newViewMsg.PeerIndex)
// append signature only if it doesn't exist
height := newViewMsg.TimeoutHeight
round := newViewMsg.TimeoutRound
var bitArray *cmn.BitArray
id := timeoutID{Height: height, Round: round}
bitArray, ok := tm.bitArrays[id]
if !ok {
bitArray = cmn.NewBitArray(int(tm.pacemaker.csReactor.committeeSize))
tm.bitArrays[id] = bitArray
}
if bitArray.GetIndex(index) == false {
bitArray.SetIndex(index, true)
sig, err := tm.pacemaker.csReactor.csCommon.GetSystem().SigFromBytes(newViewMsg.PeerSignature)
if err != nil {
tm.pacemaker.logger.Error("error convert signature", "err", err)
}
var vals []*timeoutVal
vals, ok := tm.cache[id]
if !ok {
vals = make([]*timeoutVal, 0)
}
tm.cache[id] = append(vals, &timeoutVal{
// TODO: set counter
Counter: newViewMsg.TimeoutCounter,
PeerID: newViewMsg.PeerID,
PeerIndex: newViewMsg.PeerIndex,
MsgHash: newViewMsg.SignedMessageHash,
Signature: sig,
})
}
}
}
func (tm *PMTimeoutCertManager) count(height, round uint32) int {
tm.RLock()
defer tm.RUnlock()
if bitArray, ok := tm.bitArrays[timeoutID{height, round}]; ok {
return bitArray.Count()
}
return 0
}
func (tm *PMTimeoutCertManager) getTimeoutCert(height, round uint32) *PMTimeoutCert {
id := timeoutID{height, round}
vals, ok := tm.cache[id]
if !ok {
return nil
}
bitArray, ok := tm.bitArrays[id]
if !ok {
return nil
}
var sigs []bls.Signature
for _, v := range vals {
sigs = append(sigs, v.Signature)
}
aggSig := tm.pacemaker.csReactor.csCommon.AggregateSign(sigs)
aggSigBytes := tm.pacemaker.csReactor.csCommon.GetSystem().SigToBytes(aggSig)
return &PMTimeoutCert{
TimeoutHeight: height,
TimeoutRound: round,
//TODO: better way?
TimeoutCounter: uint32(vals[0].Counter),
TimeoutBitArray: bitArray,
TimeoutAggSig: aggSigBytes,
}
}
func (tm *PMTimeoutCertManager) cleanup(height, round uint32) {
tm.Lock()
defer tm.Unlock()
for k, _ := range tm.cache {
if k.Height <= height || k.Round <= k.Round {
delete(tm.cache, k)
delete(tm.bitArrays, k)
}
}
}