-
Notifications
You must be signed in to change notification settings - Fork 175
/
consensus_follower.go
268 lines (233 loc) · 8.07 KB
/
consensus_follower.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
package follower
import (
"context"
"fmt"
"sync"
"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/cmd"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/consensus/hotstuff/notifications/pubsub"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/chainsync"
"github.com/onflow/flow-go/module/compliance"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/module/util"
)
// ConsensusFollower is a standalone module run by third parties which provides
// a mechanism for observing the block chain. It maintains a set of subscribers
// and delivers block proposals broadcasted by the consensus nodes to each one.
type ConsensusFollower interface {
component.Component
// Run starts the consensus follower.
Run(context.Context)
// AddOnBlockFinalizedConsumer adds a new block finalization subscriber.
AddOnBlockFinalizedConsumer(pubsub.OnBlockFinalizedConsumer)
}
// Config contains the configurable fields for a `ConsensusFollower`.
type Config struct {
networkPrivKey crypto.PrivateKey // the network private key of this node
bootstrapNodes []BootstrapNodeInfo // the bootstrap nodes to use
bindAddr string // address to bind on
db *badger.DB // the badger DB storage to use for the protocol state
dataDir string // directory to store the protocol state (if the badger storage is not provided)
bootstrapDir string // path to the bootstrap directory
logLevel string // log level
exposeMetrics bool // whether to expose metrics
syncConfig *chainsync.Config // sync core configuration
complianceConfig *compliance.Config // follower engine configuration
}
type Option func(c *Config)
// WithDataDir sets the underlying directory to be used to store the database
// If a database is supplied, then data directory will be set to empty string
func WithDataDir(dataDir string) Option {
return func(cf *Config) {
if cf.db == nil {
cf.dataDir = dataDir
}
}
}
func WithBootstrapDir(bootstrapDir string) Option {
return func(cf *Config) {
cf.bootstrapDir = bootstrapDir
}
}
func WithLogLevel(level string) Option {
return func(cf *Config) {
cf.logLevel = level
}
}
// WithDB sets the underlying database that will be used to store the chain state
// WithDB takes precedence over WithDataDir and datadir will be set to empty if DB is set using this option
func WithDB(db *badger.DB) Option {
return func(cf *Config) {
cf.db = db
cf.dataDir = ""
}
}
func WithExposeMetrics(expose bool) Option {
return func(c *Config) {
c.exposeMetrics = expose
}
}
func WithSyncCoreConfig(config *chainsync.Config) Option {
return func(c *Config) {
c.syncConfig = config
}
}
func WithComplianceConfig(config *compliance.Config) Option {
return func(c *Config) {
c.complianceConfig = config
}
}
// BootstrapNodeInfo contains the details about the upstream bootstrap peer the consensus follower uses
type BootstrapNodeInfo struct {
Host string // ip or hostname
Port uint
NetworkPublicKey crypto.PublicKey // the network public key of the bootstrap peer
}
func bootstrapIdentities(bootstrapNodes []BootstrapNodeInfo) flow.IdentityList {
ids := make(flow.IdentityList, len(bootstrapNodes))
for i, b := range bootstrapNodes {
ids[i] = &flow.Identity{
Role: flow.RoleAccess,
NetworkPubKey: b.NetworkPublicKey,
Address: fmt.Sprintf("%s:%d", b.Host, b.Port),
StakingPubKey: nil,
}
}
return ids
}
func getFollowerServiceOptions(config *Config) []FollowerOption {
ids := bootstrapIdentities(config.bootstrapNodes)
return []FollowerOption{
WithBootStrapPeers(ids...),
WithBaseOptions(getBaseOptions(config)),
WithNetworkKey(config.networkPrivKey),
}
}
func getBaseOptions(config *Config) []cmd.Option {
options := []cmd.Option{
cmd.WithMetricsEnabled(false),
cmd.WithSecretsDBEnabled(false),
}
if config.bootstrapDir != "" {
options = append(options, cmd.WithBootstrapDir(config.bootstrapDir))
}
if config.dataDir != "" {
options = append(options, cmd.WithDataDir(config.dataDir))
}
if config.bindAddr != "" {
options = append(options, cmd.WithBindAddress(config.bindAddr))
}
if config.logLevel != "" {
options = append(options, cmd.WithLogLevel(config.logLevel))
}
if config.db != nil {
options = append(options, cmd.WithDB(config.db))
}
if config.exposeMetrics {
options = append(options, cmd.WithMetricsEnabled(config.exposeMetrics))
}
if config.syncConfig != nil {
options = append(options, cmd.WithSyncCoreConfig(*config.syncConfig))
}
if config.complianceConfig != nil {
options = append(options, cmd.WithComplianceConfig(*config.complianceConfig))
}
return options
}
func buildConsensusFollower(opts []FollowerOption) (*FollowerServiceBuilder, error) {
nodeBuilder := FlowConsensusFollowerService(opts...)
if err := nodeBuilder.Initialize(); err != nil {
return nil, err
}
return nodeBuilder, nil
}
type ConsensusFollowerImpl struct {
component.Component
*cmd.NodeConfig
logger zerolog.Logger
consumersMu sync.RWMutex
consumers []pubsub.OnBlockFinalizedConsumer
}
// NewConsensusFollower creates a new consensus follower.
func NewConsensusFollower(
networkPrivKey crypto.PrivateKey,
bindAddr string,
bootstapIdentities []BootstrapNodeInfo,
opts ...Option,
) (*ConsensusFollowerImpl, error) {
config := &Config{
networkPrivKey: networkPrivKey,
bootstrapNodes: bootstapIdentities,
bindAddr: bindAddr,
logLevel: "info",
exposeMetrics: false,
}
for _, opt := range opts {
opt(config)
}
accessNodeOptions := getFollowerServiceOptions(config)
anb, err := buildConsensusFollower(accessNodeOptions)
if err != nil {
return nil, err
}
cf := &ConsensusFollowerImpl{logger: anb.Logger}
anb.BaseConfig.NodeRole = "consensus_follower"
anb.FollowerDistributor.AddOnBlockFinalizedConsumer(cf.onBlockFinalized)
cf.NodeConfig = anb.NodeConfig
cf.Component, err = anb.Build()
if err != nil {
return nil, err
}
return cf, nil
}
// onBlockFinalized relays the block finalization event to all registered consumers.
func (cf *ConsensusFollowerImpl) onBlockFinalized(finalizedBlock *model.Block) {
cf.consumersMu.RLock()
for _, consumer := range cf.consumers {
cf.consumersMu.RUnlock()
consumer(finalizedBlock)
cf.consumersMu.RLock()
}
cf.consumersMu.RUnlock()
}
// AddOnBlockFinalizedConsumer adds a new block finalization subscriber.
func (cf *ConsensusFollowerImpl) AddOnBlockFinalizedConsumer(consumer pubsub.OnBlockFinalizedConsumer) {
cf.consumersMu.Lock()
defer cf.consumersMu.Unlock()
cf.consumers = append(cf.consumers, consumer)
}
// Run starts the consensus follower.
// This may also be implemented directly in a calling library to take advantage of error recovery
// possible with the irrecoverable error handling.
func (cf *ConsensusFollowerImpl) Run(ctx context.Context) {
if util.CheckClosed(ctx.Done()) {
return
}
// Start the consensus follower with an irrecoverable signaler context. The returned error channel
// will receive irrecoverable errors thrown by the consensus follower or any of its child components.
// This makes it possible to listen for irrecoverable errors and restart the consensus follower. In
// the default implementation, a fatal error is thrown.
signalerCtx, errChan := irrecoverable.WithSignaler(ctx)
cf.Start(signalerCtx)
// log when the follower has complete startup and when it's beginning to shut down
go func() {
if err := util.WaitClosed(ctx, cf.Ready()); err != nil {
return
}
cf.logger.Info().Msg("Consensus follower startup complete")
}()
go func() {
<-ctx.Done()
cf.logger.Info().Msg("Consensus follower shutting down")
}()
// Block here until all components have stopped or an irrecoverable error is received.
if err := util.WaitError(errChan, cf.Done()); err != nil {
cf.logger.Fatal().Err(err).Msg("A fatal error was encountered in consensus follower")
}
cf.logger.Info().Msg("Consensus follower shutdown complete")
}