-
Notifications
You must be signed in to change notification settings - Fork 175
/
access_node_builder.go
657 lines (578 loc) · 27.2 KB
/
access_node_builder.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package node_builder
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/onflow/flow/protobuf/go/flow/access"
"github.com/rs/zerolog"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/onflow/flow-go/cmd"
"github.com/onflow/flow-go/consensus"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/committees"
"github.com/onflow/flow-go/consensus/hotstuff/notifications/pubsub"
"github.com/onflow/flow-go/consensus/hotstuff/verification"
recovery "github.com/onflow/flow-go/consensus/recovery/protocol"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/engine/access/ingestion"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
"github.com/onflow/flow-go/engine/common/follower"
followereng "github.com/onflow/flow-go/engine/common/follower"
"github.com/onflow/flow-go/engine/common/requester"
synceng "github.com/onflow/flow-go/engine/common/synchronization"
"github.com/onflow/flow-go/model/encodable"
"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"
finalizer "github.com/onflow/flow-go/module/finalizer/consensus"
"github.com/onflow/flow-go/module/id"
"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/network"
cborcodec "github.com/onflow/flow-go/network/codec/cbor"
"github.com/onflow/flow-go/network/p2p"
"github.com/onflow/flow-go/network/validator"
"github.com/onflow/flow-go/state/protocol"
badgerState "github.com/onflow/flow-go/state/protocol/badger"
"github.com/onflow/flow-go/state/protocol/blocktimer"
storage "github.com/onflow/flow-go/storage/badger"
"github.com/onflow/flow-go/utils/grpcutils"
)
// AccessNodeBuilder extends cmd.NodeBuilder and declares additional functions needed to bootstrap an Access node
// These functions are shared by staked and unstaked access node builders.
// The Staked network allows the staked nodes to communicate among themselves, while the unstaked network allows the
// unstaked nodes and a staked Access node to communicate.
//
// unstaked network staked network
// +------------------------+
// | Unstaked Access Node 1 |<--------------------------|
// +------------------------+ v
// +------------------------+ +--------------------+ +------------------------+
// | Unstaked Access Node 2 |<----------------------->| Staked Access Node |<--------------->| All other staked Nodes |
// +------------------------+ +--------------------+ +------------------------+
// +------------------------+ ^
// | Unstaked Access Node 3 |<--------------------------|
// +------------------------+
type AccessNodeBuilder interface {
cmd.NodeBuilder
// IsStaked returns True is this is a staked Access Node, False otherwise
IsStaked() bool
// Build defines all of the Access node's components and modules.
Build() AccessNodeBuilder
}
// AccessNodeConfig defines all the user defined parameters required to bootstrap an access node
// For a node running as a standalone process, the config fields will be populated from the command line params,
// while for a node running as a library, the config fields are expected to be initialized by the caller.
type AccessNodeConfig struct {
staked bool
bootstrapNodeAddresses []string
bootstrapNodePublicKeys []string
bootstrapIdentities flow.IdentityList // the identity list of bootstrap peers the node uses to discover other nodes
NetworkKey crypto.PrivateKey // the networking key passed in by the caller when being used as a library
supportsUnstakedFollower bool // True if this is a staked Access node which also supports unstaked access nodes/unstaked consensus follower engines
collectionGRPCPort uint
executionGRPCPort uint
pingEnabled bool
nodeInfoFile string
apiRatelimits map[string]int
apiBurstlimits map[string]int
rpcConf rpc.Config
ExecutionNodeAddress string // deprecated
HistoricalAccessRPCs []access.AccessAPIClient
logTxTimeToFinalized bool
logTxTimeToExecuted bool
logTxTimeToFinalizedExecuted bool
retryEnabled bool
rpcMetricsEnabled bool
baseOptions []cmd.Option
}
// DefaultAccessNodeConfig defines all the default values for the AccessNodeConfig
func DefaultAccessNodeConfig() *AccessNodeConfig {
return &AccessNodeConfig{
collectionGRPCPort: 9000,
executionGRPCPort: 9000,
rpcConf: rpc.Config{
UnsecureGRPCListenAddr: "0.0.0.0:9000",
SecureGRPCListenAddr: "0.0.0.0:9001",
HTTPListenAddr: "0.0.0.0:8000",
CollectionAddr: "",
HistoricalAccessAddrs: "",
CollectionClientTimeout: 3 * time.Second,
ExecutionClientTimeout: 3 * time.Second,
MaxHeightRange: backend.DefaultMaxHeightRange,
PreferredExecutionNodeIDs: nil,
FixedExecutionNodeIDs: nil,
},
ExecutionNodeAddress: "localhost:9000",
logTxTimeToFinalized: false,
logTxTimeToExecuted: false,
logTxTimeToFinalizedExecuted: false,
pingEnabled: false,
retryEnabled: false,
rpcMetricsEnabled: false,
nodeInfoFile: "",
apiRatelimits: nil,
apiBurstlimits: nil,
staked: true,
bootstrapNodeAddresses: []string{},
bootstrapNodePublicKeys: []string{},
supportsUnstakedFollower: false,
}
}
// FlowAccessNodeBuilder provides the common functionality needed to bootstrap a Flow staked and unstaked access node
// It is composed of the FlowNodeBuilder, the AccessNodeConfig and contains all the components and modules needed for the
// staked and unstaked access nodes
type FlowAccessNodeBuilder struct {
*cmd.FlowNodeBuilder
*AccessNodeConfig
// components
LibP2PNode *p2p.Node
FollowerState protocol.MutableState
SyncCore *synchronization.Core
RpcEng *rpc.Engine
FinalizationDistributor *pubsub.FinalizationDistributor
FinalizedHeader *synceng.FinalizedHeaderCache
CollectionRPC access.AccessAPIClient
TransactionTimings *stdmap.TransactionTimings
CollectionsToMarkFinalized *stdmap.Times
CollectionsToMarkExecuted *stdmap.Times
BlocksToMarkExecuted *stdmap.Times
TransactionMetrics module.TransactionMetrics
PingMetrics module.PingMetrics
Committee hotstuff.Committee
Finalized *flow.Header
Pending []*flow.Header
FollowerCore module.HotStuffFollower
// for the untsaked access node, the sync engine participants provider is the libp2p peer store which is not
// available until after the network has started. Hence, a factory function that needs to be called just before
// creating the sync engine
SyncEngineParticipantsProviderFactory func() id.IdentifierProvider
// engines
IngestEng *ingestion.Engine
RequestEng *requester.Engine
FollowerEng *followereng.Engine
SyncEng *synceng.Engine
}
func (builder *FlowAccessNodeBuilder) buildFollowerState() *FlowAccessNodeBuilder {
builder.Module("mutable follower state", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) error {
// For now, we only support state implementations from package badger.
// If we ever support different implementations, the following can be replaced by a type-aware factory
state, ok := node.State.(*badgerState.State)
if !ok {
return fmt.Errorf("only implementations of type badger.State are currenlty supported but read-only state has type %T", node.State)
}
followerState, err := badgerState.NewFollowerState(
state,
node.Storage.Index,
node.Storage.Payloads,
node.Tracer,
node.ProtocolEvents,
blocktimer.DefaultBlockTimer,
)
builder.FollowerState = followerState
return err
})
return builder
}
func (builder *FlowAccessNodeBuilder) buildSyncCore() *FlowAccessNodeBuilder {
builder.Module("sync core", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) error {
syncCore, err := synchronization.New(node.Logger, synchronization.DefaultConfig())
builder.SyncCore = syncCore
return err
})
return builder
}
func (builder *FlowAccessNodeBuilder) buildCommittee() *FlowAccessNodeBuilder {
builder.Module("committee", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) error {
// 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
committee, err := committees.NewConsensusCommittee(node.State, node.Me.NodeID())
builder.Committee = committee
return err
})
return builder
}
func (builder *FlowAccessNodeBuilder) buildLatestHeader() *FlowAccessNodeBuilder {
builder.Module("latest header", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) error {
finalized, pending, err := recovery.FindLatest(node.State, node.Storage.Headers)
builder.Finalized, builder.Pending = finalized, pending
return err
})
return builder
}
func (builder *FlowAccessNodeBuilder) buildFollowerCore() *FlowAccessNodeBuilder {
builder.Component("follower core", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
// create a finalizer that will handle updating the protocol
// state when the follower detects newly finalized blocks
final := finalizer.NewFinalizer(node.DB, node.Storage.Headers, builder.FollowerState)
// initialize the staking & beacon verifiers, signature joiner
staking := signature.NewAggregationVerifier(encoding.ConsensusVoteTag)
beacon := signature.NewThresholdVerifier(encoding.RandomBeaconTag)
merger := signature.NewCombiner(encodable.ConsensusVoteSigLen, encodable.RandomBeaconSigLen)
// initialize the verifier for the protocol consensus
verifier := verification.NewCombinedVerifier(builder.Committee, staking, beacon, merger)
followerCore, err := consensus.NewFollower(node.Logger, builder.Committee, node.Storage.Headers, final, verifier,
builder.FinalizationDistributor, node.RootBlock.Header, node.RootQC, builder.Finalized, builder.Pending)
if err != nil {
return nil, fmt.Errorf("could not initialize follower core: %w", err)
}
builder.FollowerCore = followerCore
return builder.FollowerCore, nil
})
return builder
}
func (builder *FlowAccessNodeBuilder) buildFollowerEngine() *FlowAccessNodeBuilder {
builder.Component("follower engine", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
// initialize cleaner for DB
cleaner := storage.NewCleaner(node.Logger, node.DB, builder.Metrics.CleanCollector, flow.DefaultValueLogGCFrequency)
conCache := buffer.NewPendingBlocks()
followerEng, err := follower.New(
node.Logger,
node.Network,
node.Me,
node.Metrics.Engine,
node.Metrics.Mempool,
cleaner,
node.Storage.Headers,
node.Storage.Payloads,
builder.FollowerState,
conCache,
builder.FollowerCore,
builder.SyncCore,
)
if err != nil {
return nil, fmt.Errorf("could not create follower engine: %w", err)
}
builder.FollowerEng = followerEng
return builder.FollowerEng, nil
})
return builder
}
func (builder *FlowAccessNodeBuilder) buildFinalizedHeader() *FlowAccessNodeBuilder {
builder.Component("finalized snapshot", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
finalizedHeader, err := synceng.NewFinalizedHeaderCache(node.Logger, node.State, builder.FinalizationDistributor)
if err != nil {
return nil, fmt.Errorf("could not create finalized snapshot cache: %w", err)
}
builder.FinalizedHeader = finalizedHeader
return builder.FinalizedHeader, nil
})
return builder
}
func (builder *FlowAccessNodeBuilder) buildSyncEngine() *FlowAccessNodeBuilder {
builder.Component("sync engine", func(_ cmd.NodeBuilder, node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
sync, err := synceng.New(
node.Logger,
node.Metrics.Engine,
node.Network,
node.Me,
node.Storage.Blocks,
builder.FollowerEng,
builder.SyncCore,
builder.FinalizedHeader,
builder.SyncEngineParticipantsProviderFactory(),
)
if err != nil {
return nil, fmt.Errorf("could not create synchronization engine: %w", err)
}
builder.SyncEng = sync
return builder.SyncEng, nil
})
return builder
}
func (builder *FlowAccessNodeBuilder) BuildConsensusFollower() AccessNodeBuilder {
builder.
buildFollowerState().
buildSyncCore().
buildCommittee().
buildLatestHeader().
buildFollowerCore().
buildFollowerEngine().
buildFinalizedHeader().
buildSyncEngine()
return builder
}
func (anb *FlowAccessNodeBuilder) Build() AccessNodeBuilder {
anb.
BuildConsensusFollower().
Module("collection node client", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) error {
// collection node address is optional (if not specified, collection nodes will be chosen at random)
if strings.TrimSpace(anb.rpcConf.CollectionAddr) == "" {
node.Logger.Info().Msg("using a dynamic collection node address")
return nil
}
node.Logger.Info().
Str("collection_node", anb.rpcConf.CollectionAddr).
Msg("using the static collection node address")
collectionRPCConn, err := grpc.Dial(
anb.rpcConf.CollectionAddr,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(grpcutils.DefaultMaxMsgSize)),
grpc.WithInsecure(),
backend.WithClientUnaryInterceptor(anb.rpcConf.CollectionClientTimeout))
if err != nil {
return err
}
anb.CollectionRPC = access.NewAccessAPIClient(collectionRPCConn)
return nil
}).
Module("historical access node clients", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) error {
addrs := strings.Split(anb.rpcConf.HistoricalAccessAddrs, ",")
for _, addr := range addrs {
if strings.TrimSpace(addr) == "" {
continue
}
node.Logger.Info().Str("access_nodes", addr).Msg("historical access node addresses")
historicalAccessRPCConn, err := grpc.Dial(
addr,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(grpcutils.DefaultMaxMsgSize)),
grpc.WithInsecure())
if err != nil {
return err
}
anb.HistoricalAccessRPCs = append(anb.HistoricalAccessRPCs, access.NewAccessAPIClient(historicalAccessRPCConn))
}
return nil
}).
Module("transaction timing mempools", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) error {
var err error
anb.TransactionTimings, err = stdmap.NewTransactionTimings(1500 * 300) // assume 1500 TPS * 300 seconds
if err != nil {
return err
}
anb.CollectionsToMarkFinalized, err = stdmap.NewTimes(50 * 300) // assume 50 collection nodes * 300 seconds
if err != nil {
return err
}
anb.CollectionsToMarkExecuted, err = stdmap.NewTimes(50 * 300) // assume 50 collection nodes * 300 seconds
if err != nil {
return err
}
anb.BlocksToMarkExecuted, err = stdmap.NewTimes(1 * 300) // assume 1 block per second * 300 seconds
return err
}).
Module("transaction metrics", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) error {
anb.TransactionMetrics = metrics.NewTransactionCollector(anb.TransactionTimings, node.Logger, anb.logTxTimeToFinalized,
anb.logTxTimeToExecuted, anb.logTxTimeToFinalizedExecuted)
return nil
}).
Module("ping metrics", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) error {
anb.PingMetrics = metrics.NewPingCollector()
return nil
}).
Module("server certificate", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) error {
// generate the server certificate that will be served by the GRPC server
x509Certificate, err := grpcutils.X509Certificate(node.NetworkKey)
if err != nil {
return err
}
tlsConfig := grpcutils.DefaultServerTLSConfig(x509Certificate)
anb.rpcConf.TransportCredentials = credentials.NewTLS(tlsConfig)
return nil
}).
Component("RPC engine", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
anb.RpcEng = rpc.New(
node.Logger,
node.State,
anb.rpcConf,
anb.CollectionRPC,
anb.HistoricalAccessRPCs,
node.Storage.Blocks,
node.Storage.Headers,
node.Storage.Collections,
node.Storage.Transactions,
node.Storage.Receipts,
node.Storage.Results,
node.RootChainID,
anb.TransactionMetrics,
anb.collectionGRPCPort,
anb.executionGRPCPort,
anb.retryEnabled,
anb.rpcMetricsEnabled,
anb.apiRatelimits,
anb.apiBurstlimits,
)
return anb.RpcEng, nil
}).
Component("ingestion engine", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
var err error
anb.RequestEng, err = requester.New(
node.Logger,
node.Metrics.Engine,
node.Network,
node.Me,
node.State,
engine.RequestCollections,
filter.HasRole(flow.RoleCollection),
func() flow.Entity { return &flow.Collection{} },
)
if err != nil {
return nil, fmt.Errorf("could not create requester engine: %w", err)
}
anb.IngestEng, err = ingestion.New(node.Logger, node.Network, node.State, node.Me, anb.RequestEng, node.Storage.Blocks, node.Storage.Headers, node.Storage.Collections, node.Storage.Transactions, node.Storage.Results, node.Storage.Receipts, anb.TransactionMetrics,
anb.CollectionsToMarkFinalized, anb.CollectionsToMarkExecuted, anb.BlocksToMarkExecuted, anb.RpcEng)
anb.RequestEng.WithHandle(anb.IngestEng.OnCollection)
anb.FinalizationDistributor.AddConsumer(anb.IngestEng)
return anb.IngestEng, err
}).
Component("requester engine", func(builder cmd.NodeBuilder, node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
// We initialize the requester engine inside the ingestion engine due to the mutual dependency. However, in
// order for it to properly start and shut down, we should still return it as its own engine here, so it can
// be handled by the scaffold.
return anb.RequestEng, nil
})
return anb
}
type Option func(*AccessNodeConfig)
func WithBootStrapPeers(bootstrapNodes ...*flow.Identity) Option {
return func(config *AccessNodeConfig) {
config.bootstrapIdentities = bootstrapNodes
}
}
func SupportsUnstakedNode(enable bool) Option {
return func(config *AccessNodeConfig) {
config.supportsUnstakedFollower = enable
}
}
func WithNetworkKey(key crypto.PrivateKey) Option {
return func(config *AccessNodeConfig) {
config.NetworkKey = key
}
}
func WithBaseOptions(baseOptions []cmd.Option) Option {
return func(config *AccessNodeConfig) {
config.baseOptions = baseOptions
}
}
func FlowAccessNode(opts ...Option) *FlowAccessNodeBuilder {
config := DefaultAccessNodeConfig()
for _, opt := range opts {
opt(config)
}
return &FlowAccessNodeBuilder{
AccessNodeConfig: config,
FlowNodeBuilder: cmd.FlowNode(flow.RoleAccess.String(), config.baseOptions...),
FinalizationDistributor: pubsub.NewFinalizationDistributor(),
}
}
func (builder *FlowAccessNodeBuilder) IsStaked() bool {
return builder.staked
}
func (builder *FlowAccessNodeBuilder) ParseFlags() {
builder.BaseFlags()
builder.extraFlags()
builder.ParseAndPrintFlags()
}
func (builder *FlowAccessNodeBuilder) extraFlags() {
builder.ExtraFlags(func(flags *pflag.FlagSet) {
defaultConfig := DefaultAccessNodeConfig()
flags.UintVar(&builder.collectionGRPCPort, "collection-ingress-port", defaultConfig.collectionGRPCPort, "the grpc ingress port for all collection nodes")
flags.UintVar(&builder.executionGRPCPort, "execution-ingress-port", defaultConfig.executionGRPCPort, "the grpc ingress port for all execution nodes")
flags.StringVarP(&builder.rpcConf.UnsecureGRPCListenAddr, "rpc-addr", "r", defaultConfig.rpcConf.UnsecureGRPCListenAddr, "the address the unsecured gRPC server listens on")
flags.StringVar(&builder.rpcConf.SecureGRPCListenAddr, "secure-rpc-addr", defaultConfig.rpcConf.SecureGRPCListenAddr, "the address the secure gRPC server listens on")
flags.StringVarP(&builder.rpcConf.HTTPListenAddr, "http-addr", "h", defaultConfig.rpcConf.HTTPListenAddr, "the address the http proxy server listens on")
flags.StringVarP(&builder.rpcConf.CollectionAddr, "static-collection-ingress-addr", "", defaultConfig.rpcConf.CollectionAddr, "the address (of the collection node) to send transactions to")
flags.StringVarP(&builder.ExecutionNodeAddress, "script-addr", "s", defaultConfig.ExecutionNodeAddress, "the address (of the execution node) forward the script to")
flags.StringVarP(&builder.rpcConf.HistoricalAccessAddrs, "historical-access-addr", "", defaultConfig.rpcConf.HistoricalAccessAddrs, "comma separated rpc addresses for historical access nodes")
flags.DurationVar(&builder.rpcConf.CollectionClientTimeout, "collection-client-timeout", defaultConfig.rpcConf.CollectionClientTimeout, "grpc client timeout for a collection node")
flags.DurationVar(&builder.rpcConf.ExecutionClientTimeout, "execution-client-timeout", defaultConfig.rpcConf.ExecutionClientTimeout, "grpc client timeout for an execution node")
flags.UintVar(&builder.rpcConf.MaxHeightRange, "rpc-max-height-range", defaultConfig.rpcConf.MaxHeightRange, "maximum size for height range requests")
flags.StringSliceVar(&builder.rpcConf.PreferredExecutionNodeIDs, "preferred-execution-node-ids", defaultConfig.rpcConf.PreferredExecutionNodeIDs, "comma separated list of execution nodes ids to choose from when making an upstream call e.g. b4a4dbdcd443d...,fb386a6a... etc.")
flags.StringSliceVar(&builder.rpcConf.FixedExecutionNodeIDs, "fixed-execution-node-ids", defaultConfig.rpcConf.FixedExecutionNodeIDs, "comma separated list of execution nodes ids to choose from when making an upstream call if no matching preferred execution id is found e.g. b4a4dbdcd443d...,fb386a6a... etc.")
flags.BoolVar(&builder.logTxTimeToFinalized, "log-tx-time-to-finalized", defaultConfig.logTxTimeToFinalized, "log transaction time to finalized")
flags.BoolVar(&builder.logTxTimeToExecuted, "log-tx-time-to-executed", defaultConfig.logTxTimeToExecuted, "log transaction time to executed")
flags.BoolVar(&builder.logTxTimeToFinalizedExecuted, "log-tx-time-to-finalized-executed", defaultConfig.logTxTimeToFinalizedExecuted, "log transaction time to finalized and executed")
flags.BoolVar(&builder.pingEnabled, "ping-enabled", defaultConfig.pingEnabled, "whether to enable the ping process that pings all other peers and report the connectivity to metrics")
flags.BoolVar(&builder.retryEnabled, "retry-enabled", defaultConfig.retryEnabled, "whether to enable the retry mechanism at the access node level")
flags.BoolVar(&builder.rpcMetricsEnabled, "rpc-metrics-enabled", defaultConfig.rpcMetricsEnabled, "whether to enable the rpc metrics")
flags.StringVarP(&builder.nodeInfoFile, "node-info-file", "", defaultConfig.nodeInfoFile, "full path to a json file which provides more details about nodes when reporting its reachability metrics")
flags.StringToIntVar(&builder.apiRatelimits, "api-rate-limits", defaultConfig.apiRatelimits, "per second rate limits for Access API methods e.g. Ping=300,GetTransaction=500 etc.")
flags.StringToIntVar(&builder.apiBurstlimits, "api-burst-limits", defaultConfig.apiBurstlimits, "burst limits for Access API methods e.g. Ping=100,GetTransaction=100 etc.")
flags.BoolVar(&builder.staked, "staked", defaultConfig.staked, "whether this node is a staked access node or not")
flags.StringSliceVar(&builder.bootstrapNodeAddresses, "bootstrap-node-addresses", defaultConfig.bootstrapNodeAddresses, "the network addresses of the bootstrap access node if this is an unstaked access node e.g. access-001.mainnet.flow.org:9653,access-002.mainnet.flow.org:9653")
flags.StringSliceVar(&builder.bootstrapNodePublicKeys, "bootstrap-node-public-keys", defaultConfig.bootstrapNodePublicKeys, "the networking public key of the bootstrap access node if this is an unstaked access node (in the same order as the bootstrap node addresses) e.g. \"d57a5e9c5.....\",\"44ded42d....\"")
flags.BoolVar(&builder.supportsUnstakedFollower, "supports-unstaked-node", defaultConfig.supportsUnstakedFollower, "true if this staked access node supports unstaked node")
})
}
// initNetwork creates the network.Network implementation with the given metrics, middleware, initial list of network
// participants and topology used to choose peers from the list of participants. The list of participants can later be
// updated by calling network.SetIDs.
func (builder *FlowAccessNodeBuilder) initNetwork(nodeID module.Local,
networkMetrics module.NetworkMetrics,
middleware network.Middleware,
topology network.Topology,
) (*p2p.Network, error) {
codec := cborcodec.NewCodec()
// creates network instance
net, err := p2p.NewNetwork(
builder.Logger,
codec,
nodeID,
builder.Middleware,
p2p.DefaultCacheSize,
topology,
p2p.NewChannelSubscriptionManager(middleware),
networkMetrics,
builder.IdentityProvider,
)
if err != nil {
return nil, fmt.Errorf("could not initialize network: %w", err)
}
return net, nil
}
func unstakedNetworkMsgValidators(log zerolog.Logger, idProvider id.IdentityProvider, selfID flow.Identifier) []network.MessageValidator {
return []network.MessageValidator{
// filter out messages sent by this node itself
validator.ValidateNotSender(selfID),
validator.NewAnyValidator(
// message should be either from a valid staked node
validator.NewOriginValidator(
id.NewFilteredIdentifierProvider(filter.IsValidCurrentEpochParticipant, idProvider),
),
// or the message should be specifically targeted for this node
validator.ValidateTarget(log, selfID),
),
}
}
// BootstrapIdentities converts the bootstrap node addresses and keys to a Flow Identity list where
// each Flow Identity is initialized with the passed address, the networking key
// and the Node ID set to ZeroID, role set to Access, 0 stake and no staking key.
func BootstrapIdentities(addresses []string, keys []string) (flow.IdentityList, error) {
if len(addresses) != len(keys) {
return nil, fmt.Errorf("number of addresses and keys provided for the boostrap nodes don't match")
}
ids := make([]*flow.Identity, len(addresses))
for i, address := range addresses {
key := keys[i]
// json unmarshaller needs a quotes before and after the string
// the pflags.StringSliceVar does not retain quotes for the command line arg even if escaped with \"
// hence this additional check to ensure the key is indeed quoted
if !strings.HasPrefix(key, "\"") {
key = fmt.Sprintf("\"%s\"", key)
}
// networking public key
var networkKey encodable.NetworkPubKey
err := json.Unmarshal([]byte(key), &networkKey)
if err != nil {
return nil, err
}
// create the identity of the peer by setting only the relevant fields
id := &flow.Identity{
NodeID: flow.ZeroID, // the NodeID is the hash of the staking key and for the unstaked network it does not apply
Address: address,
Role: flow.RoleAccess, // the upstream node has to be an access node
NetworkPubKey: networkKey,
}
ids = append(ids, id)
}
return ids, nil
}