-
Notifications
You must be signed in to change notification settings - Fork 179
/
hotstuff.go
207 lines (185 loc) · 6.86 KB
/
hotstuff.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
package factories
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/consensus"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/blockproducer"
"github.com/onflow/flow-go/consensus/hotstuff/committees"
"github.com/onflow/flow-go/consensus/hotstuff/notifications"
"github.com/onflow/flow-go/consensus/hotstuff/notifications/pubsub"
"github.com/onflow/flow-go/consensus/hotstuff/persister"
"github.com/onflow/flow-go/consensus/hotstuff/timeoutcollector"
validatorImpl "github.com/onflow/flow-go/consensus/hotstuff/validator"
"github.com/onflow/flow-go/consensus/hotstuff/verification"
"github.com/onflow/flow-go/consensus/hotstuff/votecollector"
recovery "github.com/onflow/flow-go/consensus/recovery/cluster"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
hotmetrics "github.com/onflow/flow-go/module/metrics/hotstuff"
msig "github.com/onflow/flow-go/module/signature"
"github.com/onflow/flow-go/state/cluster"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
type HotStuffMetricsFunc func(chainID flow.ChainID) module.HotstuffMetrics
type HotStuffFactory struct {
baseLogger zerolog.Logger
me module.Local
db *badger.DB
protoState protocol.State
engineMetrics module.EngineMetrics
mempoolMetrics module.MempoolMetrics
createMetrics HotStuffMetricsFunc
opts []consensus.Option
}
func NewHotStuffFactory(
log zerolog.Logger,
me module.Local,
db *badger.DB,
protoState protocol.State,
engineMetrics module.EngineMetrics,
mempoolMetrics module.MempoolMetrics,
createMetrics HotStuffMetricsFunc,
opts ...consensus.Option,
) (*HotStuffFactory, error) {
factory := &HotStuffFactory{
baseLogger: log,
me: me,
db: db,
protoState: protoState,
engineMetrics: engineMetrics,
mempoolMetrics: mempoolMetrics,
createMetrics: createMetrics,
opts: opts,
}
return factory, nil
}
func (f *HotStuffFactory) CreateModules(
epoch protocol.Epoch,
cluster protocol.Cluster,
clusterState cluster.State,
headers storage.Headers,
payloads storage.ClusterPayloads,
updater module.Finalizer,
) (*consensus.HotstuffModules, module.HotstuffMetrics, error) {
// setup metrics/logging with the new chain ID
log := f.createLogger(cluster)
metrics := f.createMetrics(cluster.ChainID())
telemetryConsumer := notifications.NewTelemetryConsumer(log)
slashingConsumer := notifications.NewSlashingViolationsConsumer(log)
notifier := pubsub.NewDistributor()
notifier.AddConsumer(notifications.NewLogConsumer(log))
notifier.AddConsumer(hotmetrics.NewMetricsConsumer(metrics))
notifier.AddParticipantConsumer(telemetryConsumer)
notifier.AddProposalViolationConsumer(slashingConsumer)
var (
err error
committee hotstuff.DynamicCommittee
)
committee, err = committees.NewClusterCommittee(f.protoState, payloads, cluster, epoch, f.me.NodeID())
if err != nil {
return nil, nil, fmt.Errorf("could not create cluster committee: %w", err)
}
committee = committees.NewMetricsWrapper(committee, metrics) // wrapper for measuring time spent determining consensus committee relations
// create a signing provider
var signer hotstuff.Signer = verification.NewStakingSigner(f.me)
signer = verification.NewMetricsWrapper(signer, metrics) // wrapper for measuring time spent with crypto-related operations
finalizedBlock, err := clusterState.Final().Head()
if err != nil {
return nil, nil, fmt.Errorf("could not get cluster finalized block: %w", err)
}
forks, err := consensus.NewForks(
finalizedBlock,
headers,
updater,
notifier,
cluster.RootBlock().Header,
cluster.RootQC(),
)
if err != nil {
return nil, nil, err
}
voteAggregationDistributor := pubsub.NewVoteAggregationDistributor()
voteAggregationDistributor.AddVoteCollectorConsumer(telemetryConsumer)
voteAggregationDistributor.AddVoteAggregationViolationConsumer(slashingConsumer)
verifier := verification.NewStakingVerifier()
validator := validatorImpl.NewMetricsWrapper(validatorImpl.New(committee, verifier), metrics)
voteProcessorFactory := votecollector.NewStakingVoteProcessorFactory(committee, voteAggregationDistributor.OnQcConstructedFromVotes)
voteAggregator, err := consensus.NewVoteAggregator(
log,
metrics,
f.engineMetrics,
f.mempoolMetrics,
// since we don't want to aggregate votes for finalized view,
// the lowest retained view starts with the next view of the last finalized view.
finalizedBlock.View+1,
voteAggregationDistributor,
voteProcessorFactory,
notifier.FollowerDistributor,
)
if err != nil {
return nil, nil, err
}
timeoutCollectorDistributor := pubsub.NewTimeoutAggregationDistributor()
timeoutCollectorDistributor.AddTimeoutCollectorConsumer(telemetryConsumer)
timeoutCollectorDistributor.AddTimeoutAggregationViolationConsumer(slashingConsumer)
timeoutProcessorFactory := timeoutcollector.NewTimeoutProcessorFactory(log, timeoutCollectorDistributor, committee, validator, msig.CollectorTimeoutTag)
timeoutAggregator, err := consensus.NewTimeoutAggregator(
log,
metrics,
f.engineMetrics,
f.mempoolMetrics,
notifier,
timeoutProcessorFactory,
timeoutCollectorDistributor,
finalizedBlock.View+1,
)
if err != nil {
return nil, nil, err
}
return &consensus.HotstuffModules{
Forks: forks,
Validator: validator,
Notifier: notifier,
Committee: committee,
Signer: signer,
Persist: persister.New(f.db, cluster.ChainID()),
VoteAggregator: voteAggregator,
TimeoutAggregator: timeoutAggregator,
VoteCollectorDistributor: voteAggregationDistributor.VoteCollectorDistributor,
TimeoutCollectorDistributor: timeoutCollectorDistributor.TimeoutCollectorDistributor,
}, metrics, nil
}
func (f *HotStuffFactory) Create(
cluster protocol.Cluster,
clusterState cluster.State,
metrics module.HotstuffMetrics,
builder module.Builder,
headers storage.Headers,
hotstuffModules *consensus.HotstuffModules,
) (module.HotStuff, error) {
// setup metrics/logging with the new chain ID
builder = blockproducer.NewMetricsWrapper(builder, metrics) // wrapper for measuring time spent building block payload component
finalizedBlock, pendingBlocks, err := recovery.FindLatest(clusterState, headers)
if err != nil {
return nil, err
}
log := f.createLogger(cluster)
participant, err := consensus.NewParticipant(
log,
metrics,
f.mempoolMetrics,
builder,
finalizedBlock,
pendingBlocks,
hotstuffModules,
f.opts...,
)
return participant, err
}
// createLogger creates a logger by wrapping base logger by decorating it will cluster ID
func (f *HotStuffFactory) createLogger(cluster protocol.Cluster) zerolog.Logger {
return f.baseLogger.With().Str("chain", cluster.ChainID().String()).Logger()
}