-
Notifications
You must be signed in to change notification settings - Fork 175
/
hotstuff.go
116 lines (102 loc) · 3.47 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
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/verification"
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
aggregator module.AggregatingSigner
db *badger.DB
protoState protocol.State
createMetrics HotStuffMetricsFunc
opts []consensus.Option
}
func NewHotStuffFactory(
log zerolog.Logger,
me module.Local,
aggregator module.AggregatingSigner,
db *badger.DB,
protoState protocol.State,
createMetrics HotStuffMetricsFunc,
opts ...consensus.Option,
) (*HotStuffFactory, error) {
factory := &HotStuffFactory{
log: log,
me: me,
aggregator: aggregator,
db: db,
protoState: protoState,
createMetrics: createMetrics,
opts: opts,
}
return factory, nil
}
func (f *HotStuffFactory) Create(
epoch protocol.Epoch,
cluster protocol.Cluster,
clusterState cluster.State,
headers storage.Headers,
payloads storage.ClusterPayloads,
builder module.Builder,
updater module.Finalizer,
communicator hotstuff.Communicator,
) (*hotstuff.EventLoop, error) {
// setup metrics/logging with the new chain ID
metrics := f.createMetrics(cluster.ChainID())
notifier := pubsub.NewDistributor()
notifier.AddConsumer(notifications.NewLogConsumer(f.log))
notifier.AddConsumer(hotmetrics.NewMetricsConsumer(metrics))
notifier.AddConsumer(notifications.NewTelemetryConsumer(f.log, cluster.ChainID()))
builder = blockproducer.NewMetricsWrapper(builder, metrics) // wrapper for measuring time spent building block payload component
var committee hotstuff.Committee
var err error
committee, err = committees.NewClusterCommittee(f.protoState, payloads, cluster, epoch, f.me.NodeID())
if err != nil {
return 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.SignerVerifier = verification.NewSingleSignerVerifier(committee, f.aggregator, f.me.NodeID())
signer = verification.NewMetricsWrapper(signer, metrics) // wrapper for measuring time spent with crypto-related operations
persist := persister.New(f.db, cluster.ChainID())
finalized, pending, err := recovery.FindLatest(clusterState, headers)
if err != nil {
return nil, err
}
participant, err := consensus.NewParticipant(
f.log,
notifier,
metrics,
headers,
committee,
builder,
updater,
persist,
signer,
communicator,
cluster.RootBlock().Header,
cluster.RootQC(),
finalized,
pending,
f.opts...,
)
return participant, err
}