-
Notifications
You must be signed in to change notification settings - Fork 199
/
subroundSignature.go
219 lines (178 loc) · 5.43 KB
/
subroundSignature.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
package bls
import (
"github.com/ElrondNetwork/elrond-go/consensus"
"github.com/ElrondNetwork/elrond-go/consensus/spos"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/statusHandler"
)
type subroundSignature struct {
*spos.Subround
appStatusHandler core.AppStatusHandler
}
// NewSubroundSignature creates a subroundSignature object
func NewSubroundSignature(
baseSubround *spos.Subround,
extend func(subroundId int),
) (*subroundSignature, error) {
err := checkNewSubroundSignatureParams(
baseSubround,
)
if err != nil {
return nil, err
}
srSignature := subroundSignature{
baseSubround,
statusHandler.NewNilStatusHandler(),
}
srSignature.Job = srSignature.doSignatureJob
srSignature.Check = srSignature.doSignatureConsensusCheck
srSignature.Extend = extend
return &srSignature, nil
}
// SetAppStatusHandler method set appStatusHandler
func (sr *subroundSignature) SetAppStatusHandler(ash core.AppStatusHandler) error {
if ash == nil || ash.IsInterfaceNil() {
return spos.ErrNilAppStatusHandler
}
sr.appStatusHandler = ash
return nil
}
func checkNewSubroundSignatureParams(
baseSubround *spos.Subround,
) error {
if baseSubround == nil {
return spos.ErrNilSubround
}
if baseSubround.ConsensusState == nil {
return spos.ErrNilConsensusState
}
err := spos.ValidateConsensusCore(baseSubround.ConsensusCoreHandler)
return err
}
// doSignatureJob method does the job of the subround Signature
func (sr *subroundSignature) doSignatureJob() bool {
if !sr.CanDoSubroundJob(SrSignature) {
return false
}
sigPart, err := sr.MultiSigner().CreateSignatureShare(sr.GetData(), nil)
if err != nil {
debugError("CreateSignatureShare", err)
return false
}
if !sr.IsSelfLeaderInCurrentRound() { // is NOT self leader in this round?
//TODO: Check if it is possible to send message only to leader with O(1) instead of O(n)
msg := consensus.NewConsensusMessage(
sr.GetData(),
sigPart,
[]byte(sr.SelfPubKey()),
nil,
int(MtSignature),
uint64(sr.Rounder().TimeStamp().Unix()),
sr.Rounder().Index())
err = sr.BroadcastMessenger().BroadcastConsensusMessage(msg)
if err != nil {
debugError("BroadcastConsensusMessage", err)
return false
}
log.Debug("step 2: signature has been sent",
"type", "spos/bls",
"time [s]", sr.SyncTimer().FormattedCurrentTime())
// Validator has finished its job for this round
sr.RoundCanceled = true
}
err = sr.SetSelfJobDone(SrSignature, true)
if err != nil {
debugError("SetSelfJobDone", err)
return false
}
return true
}
// receivedSignature method is called when a signature is received through the signature channel.
// If the signature is valid, than the jobDone map corresponding to the node which sent it,
// is set on true for the subround Signature
func (sr *subroundSignature) receivedSignature(cnsDta *consensus.Message) bool {
node := string(cnsDta.PubKey)
if !sr.IsConsensusDataSet() {
return false
}
if !sr.IsConsensusDataEqual(cnsDta.BlockHeaderHash) {
return false
}
if !sr.CanProcessReceivedMessage(cnsDta, sr.Rounder().Index(), SrSignature) {
return false
}
// if this node is leader in this round and it already received 2/3 + 1 of signatures
// it will ignore any others received later
if sr.IsSelfLeaderInCurrentRound() {
threshold := sr.Threshold(SrSignature)
if ok, _ := sr.signaturesCollected(threshold); ok {
return false
}
}
index, err := sr.ConsensusGroupIndex(node)
if err != nil {
debugError("ConsensusGroupIndex", err)
return false
}
currentMultiSigner := sr.MultiSigner()
err = currentMultiSigner.StoreSignatureShare(uint16(index), cnsDta.SubRoundData)
if err != nil {
debugError("StoreSignatureShare", err)
return false
}
err = sr.SetJobDone(node, SrSignature, true)
if err != nil {
debugError("SetJobDone", err)
return false
}
if sr.IsSelfLeaderInCurrentRound() {
threshold := sr.Threshold(SrSignature)
if ok, n := sr.signaturesCollected(threshold); ok {
log.Debug("step 2: signatures",
"type", "spos/bls",
"time [s]", sr.SyncTimer().FormattedCurrentTime(),
"received", n,
"total", len(sr.ConsensusGroup()))
}
}
sr.appStatusHandler.SetStringValue(core.MetricConsensusRoundState, "signed")
return true
}
// doSignatureConsensusCheck method checks if the consensus in the subround Signature is achieved
func (sr *subroundSignature) doSignatureConsensusCheck() bool {
if sr.RoundCanceled {
return false
}
if sr.Status(SrSignature) == spos.SsFinished {
sr.appStatusHandler.SetStringValue(core.MetricConsensusRoundState, "signed")
return true
}
threshold := sr.Threshold(SrSignature)
if ok, _ := sr.signaturesCollected(threshold); ok {
log.Debug("step 2: Subround has been finished",
"type", "spos/bls",
"subround", sr.Name(),
"time [s]", sr.SyncTimer().FormattedCurrentTime())
sr.SetStatus(SrSignature, spos.SsFinished)
sr.appStatusHandler.SetStringValue(core.MetricConsensusRoundState, "signed")
return true
}
return false
}
// signaturesCollected method checks if the signatures received from the nodes, belonging to the current
// jobDone group, are more than the necessary given threshold
func (sr *subroundSignature) signaturesCollected(threshold int) (bool, int) {
n := 0
for i := 0; i < len(sr.ConsensusGroup()); i++ {
node := sr.ConsensusGroup()[i]
isSignJobDone, err := sr.JobDone(node, SrSignature)
if err != nil {
debugError("JobDone", err)
continue
}
if isSignJobDone {
n++
}
}
return n >= threshold, n
}