-
Notifications
You must be signed in to change notification settings - Fork 175
/
hotstuff.go
167 lines (147 loc) · 5.13 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
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"
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"
"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 {
log zerolog.Logger
me module.Local
db *badger.DB
protoState protocol.State
createMetrics HotStuffMetricsFunc
opts []consensus.Option
}
func NewHotStuffFactory(
log zerolog.Logger,
me module.Local,
db *badger.DB,
protoState protocol.State,
createMetrics HotStuffMetricsFunc,
opts ...consensus.Option,
) (*HotStuffFactory, error) {
factory := &HotStuffFactory{
log: log,
me: me,
db: db,
protoState: protoState,
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
metrics := f.createMetrics(cluster.ChainID())
notifier := pubsub.NewDistributor()
finalizationDistributor := pubsub.NewFinalizationDistributor()
notifier.AddConsumer(finalizationDistributor)
notifier.AddConsumer(notifications.NewLogConsumer(f.log))
notifier.AddConsumer(hotmetrics.NewMetricsConsumer(metrics))
notifier.AddConsumer(notifications.NewTelemetryConsumer(f.log, cluster.ChainID()))
var (
err error
committee hotstuff.Committee
)
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
}
qcDistributor := pubsub.NewQCCreatedDistributor()
verifier := verification.NewStakingVerifier()
validator := validatorImpl.NewMetricsWrapper(validatorImpl.New(committee, forks, verifier), metrics)
voteProcessorFactory := votecollector.NewStakingVoteProcessorFactory(committee, qcDistributor.OnQcConstructedFromVotes)
aggregator, err := consensus.NewVoteAggregator(
f.log,
// 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,
notifier,
voteProcessorFactory,
finalizationDistributor,
)
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()),
Aggregator: aggregator,
QCCreatedDistributor: qcDistributor,
FinalizationDistributor: finalizationDistributor,
}, metrics, nil
}
func (f *HotStuffFactory) Create(
clusterState cluster.State,
metrics module.HotstuffMetrics,
builder module.Builder,
headers storage.Headers,
communicator hotstuff.Communicator,
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
}
participant, err := consensus.NewParticipant(
f.log,
metrics,
builder,
communicator,
finalizedBlock,
pendingBlocks,
hotstuffModules,
f.opts...,
)
return participant, err
}