-
Notifications
You must be signed in to change notification settings - Fork 178
/
participant.go
231 lines (198 loc) · 7.96 KB
/
participant.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
package consensus
import (
"fmt"
"time"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/blockproducer"
"github.com/onflow/flow-go/consensus/hotstuff/eventhandler"
"github.com/onflow/flow-go/consensus/hotstuff/forks"
"github.com/onflow/flow-go/consensus/hotstuff/forks/finalizer"
"github.com/onflow/flow-go/consensus/hotstuff/forks/forkchoice"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/consensus/hotstuff/pacemaker"
"github.com/onflow/flow-go/consensus/hotstuff/pacemaker/timeout"
validatorImpl "github.com/onflow/flow-go/consensus/hotstuff/validator"
"github.com/onflow/flow-go/consensus/hotstuff/voteaggregator"
"github.com/onflow/flow-go/consensus/hotstuff/voter"
"github.com/onflow/flow-go/consensus/recovery"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/storage"
)
// NewParticipant initialize the EventLoop instance and recover the forks' state with all pending block
func NewParticipant(
log zerolog.Logger,
notifier hotstuff.Consumer,
metrics module.HotstuffMetrics,
headers storage.Headers,
committee hotstuff.Committee,
builder module.Builder,
updater module.Finalizer,
persist hotstuff.Persister,
signer hotstuff.SignerVerifier,
communicator hotstuff.Communicator,
rootHeader *flow.Header,
rootQC *flow.QuorumCertificate,
finalized *flow.Header,
pending []*flow.Header,
options ...Option,
) (*hotstuff.EventLoop, error) {
// initialize the default configuration
defTimeout := timeout.DefaultConfig
cfg := ParticipantConfig{
TimeoutInitial: time.Duration(defTimeout.ReplicaTimeout) * time.Millisecond,
TimeoutMinimum: time.Duration(defTimeout.MinReplicaTimeout) * time.Millisecond,
TimeoutAggregationFraction: defTimeout.VoteAggregationTimeoutFraction,
TimeoutIncreaseFactor: defTimeout.TimeoutIncrease,
TimeoutDecreaseFactor: defTimeout.TimeoutDecrease,
BlockRateDelay: time.Duration(defTimeout.BlockRateDelayMS) * time.Millisecond,
}
// apply the configuration options
for _, option := range options {
option(&cfg)
}
// initialize forks with only finalized block.
// pending blocks was not recovered yet
forks, err := initForks(finalized, headers, updater, notifier, rootHeader, rootQC)
if err != nil {
return nil, fmt.Errorf("could not recover forks: %w", err)
}
// initialize the validator
var validator hotstuff.Validator
validator = validatorImpl.New(committee, forks, signer)
validator = validatorImpl.NewMetricsWrapper(validator, metrics) // wrapper for measuring time spent in Validator component
// get the last view we started
started, err := persist.GetStarted()
if err != nil {
return nil, fmt.Errorf("could not recover last started: %w", err)
}
// get the last view we voted
voted, err := persist.GetVoted()
if err != nil {
return nil, fmt.Errorf("could not recover last voted: %w", err)
}
// initialize the vote aggregator
aggregator := voteaggregator.New(notifier, 0, committee, validator, signer)
// recover the hotstuff state, mainly to recover all pending blocks
// in forks
err = recovery.Participant(log, forks, aggregator, validator, finalized, pending)
if err != nil {
return nil, fmt.Errorf("could not recover hotstuff state: %w", err)
}
// initialize the timeout config
timeoutConfig, err := timeout.NewConfig(
cfg.TimeoutInitial,
cfg.TimeoutMinimum,
cfg.TimeoutAggregationFraction,
cfg.TimeoutIncreaseFactor,
cfg.TimeoutDecreaseFactor,
cfg.BlockRateDelay,
)
if err != nil {
return nil, fmt.Errorf("could not initialize timeout config: %w", err)
}
// initialize the pacemaker
controller := timeout.NewController(timeoutConfig)
pacemaker, err := pacemaker.New(started+1, controller, notifier)
if err != nil {
return nil, fmt.Errorf("could not initialize flow pacemaker: %w", err)
}
// initialize block producer
producer, err := blockproducer.New(signer, committee, builder)
if err != nil {
return nil, fmt.Errorf("could not initialize block producer: %w", err)
}
// initialize the voter
voter := voter.New(signer, forks, persist, voted)
// initialize the event handler
handler, err := eventhandler.New(log, pacemaker, producer, forks, persist, communicator, committee, aggregator, voter, validator, notifier)
if err != nil {
return nil, fmt.Errorf("could not initialize event handler: %w", err)
}
// initialize and return the event loop
loop, err := hotstuff.NewEventLoop(log, metrics, handler)
if err != nil {
return nil, fmt.Errorf("could not initialize event loop: %w", err)
}
return loop, nil
}
func initForks(final *flow.Header, headers storage.Headers, updater module.Finalizer, notifier hotstuff.Consumer, rootHeader *flow.Header, rootQC *flow.QuorumCertificate) (*forks.Forks, error) {
finalizer, err := initFinalizer(final, headers, updater, notifier, rootHeader, rootQC)
if err != nil {
return nil, fmt.Errorf("could not initialize finalizer: %w", err)
}
// initialize the fork choice
forkchoice, err := forkchoice.NewNewestForkChoice(finalizer, notifier)
if err != nil {
return nil, fmt.Errorf("could not initialize fork choice: %w", err)
}
// initialize the forks manager
forks := forks.New(finalizer, forkchoice)
return forks, nil
}
func initFinalizer(final *flow.Header, headers storage.Headers, updater module.Finalizer, notifier hotstuff.FinalizationConsumer, rootHeader *flow.Header, rootQC *flow.QuorumCertificate) (*finalizer.Finalizer, error) {
// recover the trusted root
trustedRoot, err := recoverTrustedRoot(final, headers, rootHeader, rootQC)
if err != nil {
return nil, fmt.Errorf("could not recover trusted root: %w", err)
}
// initialize the finalizer
finalizer, err := finalizer.New(trustedRoot, updater, notifier)
if err != nil {
return nil, fmt.Errorf("could not initialize finalizer: %w", err)
}
return finalizer, nil
}
func recoverTrustedRoot(final *flow.Header, headers storage.Headers, rootHeader *flow.Header, rootQC *flow.QuorumCertificate) (*forks.BlockQC, error) {
if final.View < rootHeader.View {
return nil, fmt.Errorf("finalized Block has older view than trusted root")
}
// if finalized view is genesis block, then use genesis block as the trustedRoot
if final.View == rootHeader.View {
if final.ID() != rootHeader.ID() {
return nil, fmt.Errorf("finalized Block conflicts with trusted root")
}
return makeRootBlockQC(rootHeader, rootQC), nil
}
// get the parent for the latest finalized block
parent, err := headers.ByBlockID(final.ParentID)
if err != nil {
return nil, fmt.Errorf("could not get parent for finalized: %w", err)
}
// find a valid child of the finalized block in order to get its QC
children, err := headers.ByParentID(final.ID())
if err != nil {
// a finalized block must have a valid child, if err happens, we exit
return nil, fmt.Errorf("could not get children for finalized block (ID: %v, view: %v): %w", final.ID(), final.View, err)
}
if len(children) == 0 {
return nil, fmt.Errorf("finalized block has no children")
}
child := model.BlockFromFlow(children[0], final.View)
// create the root block to use
trustedRoot := &forks.BlockQC{
Block: model.BlockFromFlow(final, parent.View),
QC: child.QC,
}
return trustedRoot, nil
}
func makeRootBlockQC(header *flow.Header, qc *flow.QuorumCertificate) *forks.BlockQC {
// By convention of Forks, the trusted root block does not need to have a qc
// (as is the case for the genesis block). For simplify of the implementation, we always omit
// the QC of the root block. Thereby, we have one algorithm which handles all cases,
// instead of having to distinguish between a genesis block without a qc
// and a later-finalized root block where we can retrieve the qc.
rootBlock := &model.Block{
View: header.View,
BlockID: header.ID(),
ProposerID: header.ProposerID,
QC: nil, // QC is omitted
PayloadHash: header.PayloadHash,
Timestamp: header.Timestamp,
}
return &forks.BlockQC{
QC: qc,
Block: rootBlock,
}
}