-
Notifications
You must be signed in to change notification settings - Fork 178
/
main.go
364 lines (331 loc) · 12.5 KB
/
main.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"fmt"
"time"
"github.com/spf13/pflag"
"github.com/onflow/flow-go/cmd"
"github.com/onflow/flow-go/consensus"
"github.com/onflow/flow-go/consensus/hotstuff/committee"
"github.com/onflow/flow-go/consensus/hotstuff/committee/leader"
"github.com/onflow/flow-go/consensus/hotstuff/notifications"
"github.com/onflow/flow-go/consensus/hotstuff/pacemaker/timeout"
"github.com/onflow/flow-go/consensus/hotstuff/verification"
recovery "github.com/onflow/flow-go/consensus/recovery/protocol"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/engine/collection/epochmgr"
"github.com/onflow/flow-go/engine/collection/epochmgr/factories"
"github.com/onflow/flow-go/engine/collection/ingest"
"github.com/onflow/flow-go/engine/collection/pusher"
followereng "github.com/onflow/flow-go/engine/common/follower"
"github.com/onflow/flow-go/engine/common/provider"
consync "github.com/onflow/flow-go/engine/common/synchronization"
"github.com/onflow/flow-go/model/encoding"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/buffer"
builder "github.com/onflow/flow-go/module/builder/collection"
"github.com/onflow/flow-go/module/epochs"
confinalizer "github.com/onflow/flow-go/module/finalizer/consensus"
"github.com/onflow/flow-go/module/ingress"
"github.com/onflow/flow-go/module/mempool"
"github.com/onflow/flow-go/module/mempool/stdmap"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/module/signature"
"github.com/onflow/flow-go/module/synchronization"
"github.com/onflow/flow-go/state/protocol/events/gadgets"
storagekv "github.com/onflow/flow-go/storage/badger"
)
func main() {
var (
txLimit uint
maxCollectionSize uint
builderExpiryBuffer uint
hotstuffTimeout time.Duration
hotstuffMinTimeout time.Duration
hotstuffTimeoutIncreaseFactor float64
hotstuffTimeoutDecreaseFactor float64
hotstuffTimeoutVoteAggregationFraction float64
blockRateDelay time.Duration
ingestConf ingest.Config
ingressConf ingress.Config
pool mempool.Transactions // shared tx pool
followerBuffer *buffer.PendingBlocks // pending block cache for follower
push *pusher.Engine
ing *ingest.Engine
mainChainSyncCore *synchronization.Core
followerEng *followereng.Engine
colMetrics module.CollectionMetrics
err error
)
cmd.FlowNode(flow.RoleCollection.String()).
ExtraFlags(func(flags *pflag.FlagSet) {
flags.UintVar(&txLimit, "tx-limit", 50000,
"maximum number of transactions in the memory pool")
flags.StringVarP(&ingressConf.ListenAddr, "ingress-addr", "i", "localhost:9000",
"the address the ingress server listens on")
flags.Uint64Var(&ingestConf.MaxGasLimit, "ingest-max-gas-limit", flow.DefaultMaxGasLimit,
"maximum per-transaction gas limit")
flags.BoolVar(&ingestConf.CheckScriptsParse, "ingest-check-scripts-parse", true,
"whether we check that inbound transactions are parse-able")
flags.BoolVar(&ingestConf.AllowUnknownReference, "ingest-allow-unknown-reference", true,
"whether we ingest transactions referencing an unknown block")
flags.UintVar(&ingestConf.ExpiryBuffer, "ingest-expiry-buffer", 30,
"expiry buffer for inbound transactions")
flags.UintVar(&ingestConf.PropagationRedundancy, "ingest-tx-propagation-redundancy", 10,
"how many additional cluster members we propagate transactions to")
flags.UintVar(&builderExpiryBuffer, "builder-expiry-buffer", 25,
"expiry buffer for transactions in proposed collections")
flags.UintVar(&maxCollectionSize, "builder-max-collection-size", 200,
"maximum number of transactions in proposed collections")
flags.DurationVar(&hotstuffTimeout, "hotstuff-timeout", 60*time.Second,
"the initial timeout for the hotstuff pacemaker")
flags.DurationVar(&hotstuffMinTimeout, "hotstuff-min-timeout", 2500*time.Millisecond,
"the lower timeout bound for the hotstuff pacemaker")
flags.Float64Var(&hotstuffTimeoutIncreaseFactor, "hotstuff-timeout-increase-factor",
timeout.DefaultConfig.TimeoutIncrease,
"multiplicative increase of timeout value in case of time out event")
flags.Float64Var(&hotstuffTimeoutDecreaseFactor, "hotstuff-timeout-decrease-factor",
timeout.DefaultConfig.TimeoutDecrease,
"multiplicative decrease of timeout value in case of progress")
flags.Float64Var(&hotstuffTimeoutVoteAggregationFraction, "hotstuff-timeout-vote-aggregation-fraction",
timeout.DefaultConfig.VoteAggregationTimeoutFraction,
"additional fraction of replica timeout that the primary will wait for votes")
flags.DurationVar(&blockRateDelay, "block-rate-delay", 250*time.Millisecond,
"the delay to broadcast block proposal in order to control block production rate")
}).
Module("transactions mempool", func(node *cmd.FlowNodeBuilder) error {
pool, err = stdmap.NewTransactions(txLimit)
return err
}).
Module("pending block cache", func(node *cmd.FlowNodeBuilder) error {
followerBuffer = buffer.NewPendingBlocks()
return nil
}).
Module("metrics", func(node *cmd.FlowNodeBuilder) error {
colMetrics = metrics.NewCollectionCollector(node.Tracer)
return nil
}).
Module("main chain sync core", func(node *cmd.FlowNodeBuilder) error {
mainChainSyncCore, err = synchronization.New(node.Logger, synchronization.DefaultConfig())
return err
}).
Component("follower engine", func(node *cmd.FlowNodeBuilder) (module.ReadyDoneAware, error) {
// initialize cleaner for DB
cleaner := storagekv.NewCleaner(node.Logger, node.DB, metrics.NewCleanerCollector(), flow.DefaultValueLogGCFrequency)
// create a finalizer that will handling updating the protocol
// state when the follower detects newly finalized blocks
finalizer := confinalizer.NewFinalizer(node.DB, node.Storage.Headers, node.State)
// initialize the staking & beacon verifiers, signature joiner
staking := signature.NewAggregationVerifier(encoding.ConsensusVoteTag)
beacon := signature.NewThresholdVerifier(encoding.RandomBeaconTag)
merger := signature.NewCombiner()
// initialize and pre-generate leader selections from the seed
selection, err := leader.NewSelectionForConsensus(leader.EstimatedSixMonthOfViews, node.RootBlock.Header, node.RootQC, node.State)
if err != nil {
return nil, fmt.Errorf("could not create leader selection for main consensus: %w", err)
}
// initialize consensus committee's membership state
// This committee state is for the HotStuff follower, which follows the MAIN CONSENSUS Committee
// Note: node.Me.NodeID() is not part of the consensus committee
mainConsensusCommittee, err := committee.NewMainConsensusCommitteeState(node.State, node.Me.NodeID(), selection)
if err != nil {
return nil, fmt.Errorf("could not create Committee state for main consensus: %w", err)
}
// initialize the verifier for the protocol consensus
verifier := verification.NewCombinedVerifier(mainConsensusCommittee, staking, beacon, merger)
// use proper engine for notifier to follower
notifier := notifications.NewNoopConsumer()
finalized, pending, err := recovery.FindLatest(node.State, node.Storage.Headers)
if err != nil {
return nil, fmt.Errorf("could not find latest finalized block and pending blocks to recover consensus follower: %w", err)
}
// creates a consensus follower with noop consumer as the notifier
followerCore, err := consensus.NewFollower(
node.Logger,
mainConsensusCommittee,
node.Storage.Headers,
finalizer,
verifier,
notifier,
node.RootBlock.Header,
node.RootQC,
finalized,
pending,
)
if err != nil {
return nil, fmt.Errorf("could not create follower core logic: %w", err)
}
followerEng, err = followereng.New(
node.Logger,
node.Network,
node.Me,
node.Metrics.Engine,
node.Metrics.Mempool,
cleaner,
node.Storage.Headers,
node.Storage.Payloads,
node.State,
followerBuffer,
followerCore,
mainChainSyncCore,
)
if err != nil {
return nil, fmt.Errorf("could not create follower engine: %w", err)
}
return followerEng, nil
}).
Component("main chain sync engine", func(node *cmd.FlowNodeBuilder) (module.ReadyDoneAware, error) {
// create a block synchronization engine to handle follower getting out of sync
sync, err := consync.New(
node.Logger,
node.Metrics.Engine,
node.Network,
node.Me,
node.State,
node.Storage.Blocks,
followerEng,
mainChainSyncCore,
)
if err != nil {
return nil, fmt.Errorf("could not create synchronization engine: %w", err)
}
return sync, nil
}).
Component("ingestion engine", func(node *cmd.FlowNodeBuilder) (module.ReadyDoneAware, error) {
ing, err = ingest.New(
node.Logger,
node.Network,
node.State,
node.Metrics.Engine,
colMetrics,
node.Me,
pool,
ingestConf,
)
return ing, err
}).
Component("transaction ingress server", func(node *cmd.FlowNodeBuilder) (module.ReadyDoneAware, error) {
server := ingress.New(ingressConf, ing, node.RootChainID)
return server, nil
}).
Component("provider engine", func(node *cmd.FlowNodeBuilder) (module.ReadyDoneAware, error) {
retrieve := func(collID flow.Identifier) (flow.Entity, error) {
coll, err := node.Storage.Collections.ByID(collID)
return coll, err
}
return provider.New(node.Logger, node.Metrics.Engine, node.Network, node.Me, node.State,
engine.ProvideCollections,
filter.HasRole(flow.RoleAccess, flow.RoleExecution),
retrieve,
)
}).
Component("pusher engine", func(node *cmd.FlowNodeBuilder) (module.ReadyDoneAware, error) {
push, err = pusher.New(
node.Logger,
node.Network,
node.State,
node.Metrics.Engine,
colMetrics,
node.Me,
pool,
node.Storage.Collections,
node.Storage.Transactions,
)
return push, err
}).
// Epoch manager encapsulates and manages epoch-dependent engines as we
// transition between epochs
Component("epoch manager", func(node *cmd.FlowNodeBuilder) (module.ReadyDoneAware, error) {
clusterStateFactory, err := factories.NewClusterStateFactory(node.DB, node.Metrics.Cache)
if err != nil {
return nil, err
}
builderFactory, err := factories.NewBuilderFactory(
node.DB,
node.Storage.Headers,
node.Tracer,
colMetrics,
push,
builder.WithMaxCollectionSize(maxCollectionSize),
builder.WithExpiryBuffer(builderExpiryBuffer),
)
if err != nil {
return nil, err
}
proposalFactory, err := factories.NewProposalEngineFactory(
node.Logger,
node.Network,
node.Me,
colMetrics,
node.Metrics.Engine,
node.Metrics.Mempool,
node.State,
pool,
node.Storage.Transactions,
)
if err != nil {
return nil, err
}
syncFactory, err := factories.NewSyncEngineFactory(
node.Logger,
node.Metrics.Engine,
node.Network,
node.Me,
synchronization.DefaultConfig(),
)
if err != nil {
return nil, err
}
hotstuffFactory, err := factories.NewHotStuffFactory(
node.Logger,
node.Me,
node.DB,
node.State,
consensus.WithBlockRateDelay(blockRateDelay),
consensus.WithInitialTimeout(hotstuffTimeout),
consensus.WithMinTimeout(hotstuffMinTimeout),
consensus.WithVoteAggregationTimeoutFraction(hotstuffTimeoutVoteAggregationFraction),
consensus.WithTimeoutIncreaseFactor(hotstuffTimeoutIncreaseFactor),
consensus.WithTimeoutDecreaseFactor(hotstuffTimeoutDecreaseFactor),
)
if err != nil {
return nil, err
}
staking := signature.NewAggregationProvider(encoding.CollectorVoteTag, node.Me)
signer := verification.NewSingleSigner(staking, node.Me.NodeID())
rootQCVoter := epochs.NewRootQCVoter(
node.Logger,
node.Me,
signer,
node.State,
nil, // TODO
)
factory := factories.NewEpochComponentsFactory(
node.Me,
pool,
builderFactory,
clusterStateFactory,
hotstuffFactory,
proposalFactory,
syncFactory,
)
heightEvents := gadgets.NewHeights()
node.ProtocolEvents.AddConsumer(heightEvents)
manager, err := epochmgr.New(
node.Logger,
node.Me,
node.State,
rootQCVoter,
factory,
heightEvents,
)
if err != nil {
return nil, fmt.Errorf("could not create epoch manager: %w", err)
}
// register the manager for protocol events
node.ProtocolEvents.AddConsumer(manager)
return manager, err
}).
Run()
}