-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathstep.go
300 lines (244 loc) · 8.6 KB
/
step.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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package firststep
import (
"bytes"
"context"
"time"
"github.com/dusk-network/dusk-blockchain/pkg/core/candidate"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus/header"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus/reduction"
"github.com/dusk-network/dusk-blockchain/pkg/core/data/block"
"github.com/dusk-network/dusk-blockchain/pkg/core/database"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/message"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/topics"
"github.com/dusk-network/dusk-blockchain/pkg/util"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
var lg = log.WithField("process", "consensus").
WithField("phase", "1th_reduction")
func getLog(r uint64, s uint8) *log.Entry {
return lg.WithFields(log.Fields{
"round": r,
"step": s,
})
}
// Phase is the implementation of the Selection step component.
type Phase struct {
*reduction.Reduction
db database.DB
handler *reduction.Handler
aggregator *reduction.Aggregator
selectionResult message.NewBlock
requestor *candidate.Requestor
next consensus.Phase
}
// New creates and launches the component which responsibility is to reduce the
// candidates gathered as winner of the selection of all nodes in the committee
// and reduce them to just one candidate obtaining 64% of the committee vote.
func New(next consensus.Phase, e *consensus.Emitter, verifyFn consensus.CandidateVerificationFunc, timeOut time.Duration, db database.DB, requestor *candidate.Requestor) *Phase {
return &Phase{
Reduction: &reduction.Reduction{
Emitter: e,
TimeOut: timeOut,
VerifyFn: verifyFn,
},
next: next,
db: db,
requestor: requestor,
}
}
// String returns the reduction.
func (p *Phase) String() string {
return "reduction-first-step"
}
// Initialize passes to this reduction step the best score collected during selection.
func (p *Phase) Initialize(re consensus.InternalPacket) consensus.PhaseFn {
p.selectionResult = re.(message.NewBlock)
return p
}
// Run the first reduction step until either there is a timeout, we reach 64%
// of votes, or we experience an unrecoverable error.
func (p *Phase) Run(ctx context.Context, queue *consensus.Queue, _, reductionChan chan message.Message, r consensus.RoundUpdate, step uint8) consensus.PhaseFn {
tlog := getLog(r.Round, step)
defer func() {
tlog.Traceln("ending first reduction step")
}()
if log.GetLevel() >= logrus.DebugLevel {
c := p.selectionResult.Candidate
tlog.WithField("hash", util.StringifyBytes(c.Header.Hash)).
Debug("initialized")
}
p.handler = reduction.NewHandler(p.Keys, r.P, r.Seed)
// send our own Selection
a := reduction.NewAsyncSend(p.Reduction, r.Round, step, &p.selectionResult.Candidate)
if p.handler.AmMember(r.Round, step) {
_ = a.Go(ctx, reductionChan, reduction.Republish)
} else {
if p.handler.AmMember(r.Round, step+1) {
_ = a.Go(ctx, reductionChan, reduction.ValidateOnly)
}
}
// Process queued reduction messages
timeoutChan := time.After(p.TimeOut)
p.aggregator = reduction.NewAggregator(p.handler)
for _, ev := range queue.GetEvents(r.Round, step) {
if ev.Category() == topics.Reduction {
rMsg := ev.Payload().(message.Reduction)
// if the sender is no member we discard the message
// XXX: the fact that a message from a non-committee member can end
// up in the Queue, is a vulnerability since an attacker could
// flood the queue with future non-committee reductions
if !p.handler.IsMember(rMsg.Sender(), r.Round, step) {
continue
}
// if collectReduction returns a StepVote, it means we reached
// consensus and can go to the next step
if sv := p.collectReduction(ctx, rMsg, r.Round, step, ev.Metadata()); sv != nil {
go func() {
<-timeoutChan
}()
return p.gotoNextPhase(sv)
}
}
}
for {
select {
case ev := <-reductionChan:
if reduction.ShouldProcess(ev, r.Round, step, queue) {
rMsg := ev.Payload().(message.Reduction)
if !p.handler.IsMember(rMsg.Sender(), r.Round, step) {
continue
}
sv := p.collectReduction(ctx, rMsg, r.Round, step, ev.Metadata())
if sv != nil {
// preventing timeout leakage
go func() {
<-timeoutChan
}()
return p.gotoNextPhase(sv)
}
}
case <-timeoutChan:
l := lg.WithField("event", "timeout").WithField("duration", p.TimeOut.String())
p.aggregator.Log(l, r.Round, step)
// in case of timeout we proceed in the consensus with an empty hash
sv := p.createStepVoteMessage(reduction.EmptyResult, r.Round, step, *block.NewBlock())
return p.gotoNextPhase(sv)
case <-ctx.Done():
// preventing timeout leakage
go func() {
<-timeoutChan
}()
return nil
}
}
}
func (p *Phase) gotoNextPhase(msg *message.StepVotesMsg) consensus.PhaseFn {
return p.next.Initialize(*msg)
}
func (p *Phase) collectReduction(ctx context.Context, r message.Reduction, round uint64, step uint8, metadata *message.Metadata) *message.StepVotesMsg {
if err := p.handler.VerifySignature(r.Copy().(message.Reduction)); err != nil {
lg.
WithError(err).
WithField("round", r.State().Round).
WithField("step", r.State().Step).
WithField("hash", util.StringifyBytes(r.State().BlockHash)).
Warn("error in verifying reduction, message discarded")
return nil
}
if log.GetLevel() >= logrus.DebugLevel {
log := consensus.WithFields(r.State().Round, r.State().Step, "1th_reduction_collected",
r.State().BlockHash, p.handler.BLSPubKey, nil, nil, nil)
log.WithField("signature", util.StringifyBytes(r.SignedHash)).
WithField("sender", util.StringifyBytes(r.Sender())).
Debug("")
}
m := message.NewWithMetadata(topics.Reduction, r, metadata)
// Once the event is verified, we can republish it.
if err := p.Emitter.Republish(m); err != nil {
lg.WithError(err).Error("could not republish reduction event")
}
hdr := r.State()
result := p.aggregator.CollectVote(r)
if result == nil {
return nil
}
// if the votes converged for an empty hash we invoke halt with no
// StepVotes
if bytes.Equal(hdr.BlockHash, block.EmptyHash[:]) {
return p.createStepVoteMessage(reduction.EmptyResult, round, step, *block.NewBlock())
}
if !bytes.Equal(hdr.BlockHash, p.selectionResult.Candidate.Header.Hash) {
var err error
p.selectionResult.Candidate, err = p.fetchCandidate(ctx, hdr.BlockHash)
if err != nil {
log.
WithError(err).
WithField("round", hdr.Round).
WithField("step", hdr.Step).
Warn("firststep_fetchCandidateBlock failed")
return p.createStepVoteMessage(reduction.EmptyResult, round, step, *block.NewBlock())
}
}
return p.createStepVoteMessage(result, round, step, p.selectionResult.Candidate)
}
func (p *Phase) fetchCandidate(ctx context.Context, hash []byte) (block.Block, error) {
// First, check to see if we have the candidate in the db.
var cm block.Block
err := p.db.View(func(t database.Transaction) error {
var err error
cm, err = t.FetchCandidateMessage(hash)
return err
})
if err == nil && !cm.Equals(&block.Block{}) {
return cm, nil
}
return p.requestCandidate(ctx, hash)
}
func (p *Phase) requestCandidate(ctx context.Context, hash []byte) (block.Block, error) {
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(2*time.Second))
// Ensure we release the resources associated to this context.
defer cancel()
lg.WithField("hash", util.StringifyBytes(hash)).Info("request candidate block")
cm, err := p.requestor.RequestCandidate(ctx, hash)
if err != nil {
lg.WithField("hash", util.StringifyBytes(hash)).WithError(err).
Warn("failed to receive candidate block")
return block.Block{}, err
}
lg.WithField("hash", util.StringifyBytes(hash)).Info("candidate block received")
// Store candidate for later use
if err := p.storeCandidate(cm); err != nil {
panic(err)
}
return cm, nil
}
func (p *Phase) createStepVoteMessage(r *reduction.Result, round uint64, step uint8, candidate block.Block) *message.StepVotesMsg {
if r.IsEmpty() {
p.IncreaseTimeout(round)
}
var cpy *block.Block
b := candidate.Copy().(block.Block)
cpy = &b
return &message.StepVotesMsg{
Header: header.Header{
Step: step,
Round: round,
BlockHash: r.Hash,
PubKeyBLS: p.Keys.BLSPubKey,
},
StepVotes: r.SV,
Candidate: cpy,
}
}
func (p *Phase) storeCandidate(cm block.Block) error {
return p.db.Update(func(t database.Transaction) error {
return t.StoreCandidateMessage(cm)
})
}