-
Notifications
You must be signed in to change notification settings - Fork 199
/
subroundEndRound.go
230 lines (184 loc) · 5.38 KB
/
subroundEndRound.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
package bls
import (
"fmt"
"time"
"github.com/ElrondNetwork/elrond-go/consensus/spos"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/display"
"github.com/ElrondNetwork/elrond-go/statusHandler"
)
type subroundEndRound struct {
*spos.Subround
appStatusHandler core.AppStatusHandler
}
// SetAppStatusHandler method set appStatusHandler
func (sr *subroundEndRound) SetAppStatusHandler(ash core.AppStatusHandler) error {
if ash == nil || ash.IsInterfaceNil() {
return spos.ErrNilAppStatusHandler
}
sr.appStatusHandler = ash
return nil
}
// NewSubroundEndRound creates a subroundEndRound object
func NewSubroundEndRound(
baseSubround *spos.Subround,
extend func(subroundId int),
) (*subroundEndRound, error) {
err := checkNewSubroundEndRoundParams(
baseSubround,
)
if err != nil {
return nil, err
}
srEndRound := subroundEndRound{
baseSubround,
statusHandler.NewNilStatusHandler(),
}
srEndRound.Job = srEndRound.doEndRoundJob
srEndRound.Check = srEndRound.doEndRoundConsensusCheck
srEndRound.Extend = extend
return &srEndRound, nil
}
func checkNewSubroundEndRoundParams(
baseSubround *spos.Subround,
) error {
if baseSubround == nil {
return spos.ErrNilSubround
}
if baseSubround.ConsensusState == nil {
return spos.ErrNilConsensusState
}
err := spos.ValidateConsensusCore(baseSubround.ConsensusCoreHandler)
return err
}
// doEndRoundJob method does the job of the subround EndRound
func (sr *subroundEndRound) doEndRoundJob() bool {
if !sr.IsSelfLeaderInCurrentRound() { // is NOT self leader in this round?
return false
}
bitmap := sr.GenerateBitmap(SrSignature)
err := sr.checkSignaturesValidity(bitmap)
if err != nil {
debugError("checkSignaturesValidity", err)
return false
}
// Aggregate sig and add it to the block
sig, err := sr.MultiSigner().AggregateSigs(bitmap)
if err != nil {
debugError("multisigner.AggregateSigs", err)
return false
}
sr.Header.SetPubKeysBitmap(bitmap)
sr.Header.SetSignature(sig)
// Header is complete so the leader can sign it
leaderSignature, err := sr.signBlockHeader()
if err != nil {
log.Error(err.Error())
return false
}
sr.Header.SetLeaderSignature(leaderSignature)
startTime := time.Now()
err = sr.BlockProcessor().CommitBlock(sr.Blockchain(), sr.Header, sr.BlockBody)
elapsedTime := time.Since(startTime)
log.Debug("elapsed time to commit block",
"time [s]", elapsedTime,
)
if err != nil {
debugError("CommitBlock", err)
return false
}
sr.SetStatus(SrEndRound, spos.SsFinished)
// broadcast section
// broadcast block body and header
err = sr.BroadcastMessenger().BroadcastBlock(sr.BlockBody, sr.Header)
if err != nil {
debugError("BroadcastBlock", err)
}
// broadcast header to metachain
err = sr.BroadcastMessenger().BroadcastShardHeader(sr.Header)
if err != nil {
debugError("BroadcastShardHeader", err)
}
log.Debug("step 3: BlockBody and Header has been committed and broadcast",
"type", "spos/bls",
"time [s]", sr.SyncTimer().FormattedCurrentTime())
err = sr.broadcastMiniBlocksAndTransactions()
if err != nil {
debugError("broadcastMiniBlocksAndTransactions", err)
}
msg := fmt.Sprintf("Added proposed block with nonce %d in blockchain", sr.Header.GetNonce())
log.Debug(display.Headline(msg, sr.SyncTimer().FormattedCurrentTime(), "+"))
sr.updateMetricsForLeader()
return true
}
func (sr *subroundEndRound) signBlockHeader() ([]byte, error) {
headerClone := sr.Header.Clone()
headerClone.SetLeaderSignature(nil)
marshalizedHdr, err := sr.Marshalizer().Marshal(headerClone)
if err != nil {
return nil, err
}
return sr.SingleSigner().Sign(sr.PrivateKey(), marshalizedHdr)
}
func (sr *subroundEndRound) updateMetricsForLeader() {
sr.appStatusHandler.Increment(core.MetricCountAcceptedBlocks)
sr.appStatusHandler.SetStringValue(core.MetricConsensusRoundState,
fmt.Sprintf("valid block produced in %f sec", time.Now().Sub(sr.Rounder().TimeStamp()).Seconds()))
}
func (sr *subroundEndRound) broadcastMiniBlocksAndTransactions() error {
miniBlocks, transactions, err := sr.BlockProcessor().MarshalizedDataToBroadcast(sr.Header, sr.BlockBody)
if err != nil {
return err
}
err = sr.BroadcastMessenger().BroadcastMiniBlocks(miniBlocks)
if err != nil {
return err
}
err = sr.BroadcastMessenger().BroadcastTransactions(transactions)
if err != nil {
return err
}
return nil
}
// doEndRoundConsensusCheck method checks if the consensus is achieved
func (sr *subroundEndRound) doEndRoundConsensusCheck() bool {
if sr.RoundCanceled {
return false
}
if sr.Status(SrEndRound) == spos.SsFinished {
return true
}
return false
}
func (sr *subroundEndRound) checkSignaturesValidity(bitmap []byte) error {
nbBitsBitmap := len(bitmap) * 8
consensusGroup := sr.ConsensusGroup()
consensusGroupSize := len(consensusGroup)
size := consensusGroupSize
if consensusGroupSize > nbBitsBitmap {
size = nbBitsBitmap
}
for i := 0; i < size; i++ {
indexRequired := (bitmap[i/8] & (1 << uint16(i%8))) > 0
if !indexRequired {
continue
}
pubKey := consensusGroup[i]
isSigJobDone, err := sr.JobDone(pubKey, SrSignature)
if err != nil {
return err
}
if !isSigJobDone {
return spos.ErrNilSignature
}
signature, err := sr.MultiSigner().SignatureShare(uint16(i))
if err != nil {
return err
}
err = sr.MultiSigner().VerifySignatureShare(uint16(i), signature, sr.GetData(), bitmap)
if err != nil {
return err
}
}
return nil
}