-
Notifications
You must be signed in to change notification settings - Fork 178
/
scaffold.go
1914 lines (1627 loc) · 75.8 KB
/
scaffold.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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cmd
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"math/rand"
"os"
"runtime"
"strings"
"time"
gcemd "cloud.google.com/go/compute/metadata"
"github.com/dgraph-io/badger/v2"
"github.com/hashicorp/go-multierror"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
"github.com/spf13/pflag"
"golang.org/x/time/rate"
"google.golang.org/api/option"
"github.com/onflow/flow-go/admin"
"github.com/onflow/flow-go/admin/commands"
"github.com/onflow/flow-go/admin/commands/common"
storageCommands "github.com/onflow/flow-go/admin/commands/storage"
"github.com/onflow/flow-go/cmd/build"
"github.com/onflow/flow-go/consensus/hotstuff/persister"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/environment"
"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/chainsync"
"github.com/onflow/flow-go/module/compliance"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/id"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/module/local"
"github.com/onflow/flow-go/module/mempool/herocache"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/module/profiler"
"github.com/onflow/flow-go/module/trace"
"github.com/onflow/flow-go/module/updatable_configs"
"github.com/onflow/flow-go/module/util"
"github.com/onflow/flow-go/network"
alspmgr "github.com/onflow/flow-go/network/alsp/manager"
netcache "github.com/onflow/flow-go/network/cache"
"github.com/onflow/flow-go/network/p2p"
"github.com/onflow/flow-go/network/p2p/cache"
"github.com/onflow/flow-go/network/p2p/conduit"
"github.com/onflow/flow-go/network/p2p/dns"
"github.com/onflow/flow-go/network/p2p/inspector/validation"
"github.com/onflow/flow-go/network/p2p/middleware"
"github.com/onflow/flow-go/network/p2p/p2pbuilder"
p2pconfig "github.com/onflow/flow-go/network/p2p/p2pbuilder/config"
"github.com/onflow/flow-go/network/p2p/p2pbuilder/inspector"
"github.com/onflow/flow-go/network/p2p/ping"
"github.com/onflow/flow-go/network/p2p/subscription"
"github.com/onflow/flow-go/network/p2p/unicast/protocols"
"github.com/onflow/flow-go/network/p2p/unicast/ratelimit"
"github.com/onflow/flow-go/network/p2p/utils/ratelimiter"
"github.com/onflow/flow-go/network/slashing"
"github.com/onflow/flow-go/network/topology"
"github.com/onflow/flow-go/state/protocol"
badgerState "github.com/onflow/flow-go/state/protocol/badger"
"github.com/onflow/flow-go/state/protocol/events"
"github.com/onflow/flow-go/state/protocol/events/gadgets"
"github.com/onflow/flow-go/storage"
bstorage "github.com/onflow/flow-go/storage/badger"
"github.com/onflow/flow-go/storage/badger/operation"
sutil "github.com/onflow/flow-go/storage/util"
"github.com/onflow/flow-go/utils/logging"
)
const (
NetworkComponent = "network"
ConduitFactoryComponent = "conduit-factory"
LibP2PNodeComponent = "libp2p-node"
)
type Metrics struct {
Network module.NetworkMetrics
Engine module.EngineMetrics
Compliance module.ComplianceMetrics
Cache module.CacheMetrics
Mempool module.MempoolMetrics
CleanCollector module.CleanerMetrics
Bitswap module.BitswapMetrics
}
type Storage = storage.All
type namedModuleFunc struct {
fn BuilderFunc
name string
}
type namedComponentFunc struct {
fn ReadyDoneFactory
name string
errorHandler component.OnError
dependencies *DependencyList
}
// FlowNodeBuilder is the default builder struct used for all flow nodes
// It runs a node process with following structure, in sequential order
// Base inits (network, storage, state, logger)
// PostInit handlers, if any
// Components handlers, if any, wait sequentially
// Run() <- main loop
// Components destructors, if any
// The initialization can be proceeded and succeeded with PreInit and PostInit functions that allow customization
// of the process in case of nodes such as the unstaked access node where the NodeInfo is not part of the genesis data
type FlowNodeBuilder struct {
*NodeConfig
flags *pflag.FlagSet
modules []namedModuleFunc
components []namedComponentFunc
postShutdownFns []func() error
preInitFns []BuilderFunc
postInitFns []BuilderFunc
extraRootSnapshotCheck func(protocol.Snapshot) error
extraFlagCheck func() error
adminCommandBootstrapper *admin.CommandRunnerBootstrapper
adminCommands map[string]func(config *NodeConfig) commands.AdminCommand
componentBuilder component.ComponentManagerBuilder
}
var _ NodeBuilder = (*FlowNodeBuilder)(nil)
func (fnb *FlowNodeBuilder) BaseFlags() {
defaultConfig := DefaultBaseConfig()
// bind configuration parameters
fnb.flags.StringVar(&fnb.BaseConfig.nodeIDHex, "nodeid", defaultConfig.nodeIDHex, "identity of our node")
fnb.flags.StringVar(&fnb.BaseConfig.BindAddr, "bind", defaultConfig.BindAddr, "address to bind on")
fnb.flags.StringVarP(&fnb.BaseConfig.BootstrapDir, "bootstrapdir", "b", defaultConfig.BootstrapDir, "path to the bootstrap directory")
fnb.flags.StringVarP(&fnb.BaseConfig.datadir, "datadir", "d", defaultConfig.datadir, "directory to store the public database (protocol state)")
fnb.flags.StringVar(&fnb.BaseConfig.secretsdir, "secretsdir", defaultConfig.secretsdir, "directory to store private database (secrets)")
fnb.flags.StringVarP(&fnb.BaseConfig.level, "loglevel", "l", defaultConfig.level, "level for logging output")
fnb.flags.Uint32Var(&fnb.BaseConfig.debugLogLimit, "debug-log-limit", defaultConfig.debugLogLimit, "max number of debug/trace log events per second")
fnb.flags.DurationVar(&fnb.BaseConfig.PeerUpdateInterval, "peerupdate-interval", defaultConfig.PeerUpdateInterval, "how often to refresh the peer connections for the node")
fnb.flags.DurationVar(&fnb.BaseConfig.UnicastMessageTimeout, "unicast-timeout", defaultConfig.UnicastMessageTimeout, "how long a unicast transmission can take to complete")
fnb.flags.UintVarP(&fnb.BaseConfig.metricsPort, "metricport", "m", defaultConfig.metricsPort, "port for /metrics endpoint")
fnb.flags.BoolVar(&fnb.BaseConfig.profilerConfig.Enabled, "profiler-enabled", defaultConfig.profilerConfig.Enabled, "whether to enable the auto-profiler")
fnb.flags.BoolVar(&fnb.BaseConfig.profilerConfig.UploaderEnabled, "profile-uploader-enabled", defaultConfig.profilerConfig.UploaderEnabled,
"whether to enable automatic profile upload to Google Cloud Profiler. "+
"For autoupload to work forllowing should be true: "+
"1) both -profiler-enabled=true and -profile-uploader-enabled=true need to be set. "+
"2) node is running in GCE. "+
"3) server or user has https://www.googleapis.com/auth/monitoring.write scope. ")
fnb.flags.StringVar(&fnb.BaseConfig.profilerConfig.Dir, "profiler-dir", defaultConfig.profilerConfig.Dir, "directory to create auto-profiler profiles")
fnb.flags.DurationVar(&fnb.BaseConfig.profilerConfig.Interval, "profiler-interval", defaultConfig.profilerConfig.Interval,
"the interval between auto-profiler runs")
fnb.flags.DurationVar(&fnb.BaseConfig.profilerConfig.Duration, "profiler-duration", defaultConfig.profilerConfig.Duration,
"the duration to run the auto-profile for")
fnb.flags.BoolVar(&fnb.BaseConfig.tracerEnabled, "tracer-enabled", defaultConfig.tracerEnabled,
"whether to enable tracer")
fnb.flags.UintVar(&fnb.BaseConfig.tracerSensitivity, "tracer-sensitivity", defaultConfig.tracerSensitivity,
"adjusts the level of sampling when tracing is enabled. 0 means capture everything, higher value results in less samples")
fnb.flags.StringVar(&fnb.BaseConfig.AdminAddr, "admin-addr", defaultConfig.AdminAddr, "address to bind on for admin HTTP server")
fnb.flags.StringVar(&fnb.BaseConfig.AdminCert, "admin-cert", defaultConfig.AdminCert, "admin cert file (for TLS)")
fnb.flags.StringVar(&fnb.BaseConfig.AdminKey, "admin-key", defaultConfig.AdminKey, "admin key file (for TLS)")
fnb.flags.StringVar(&fnb.BaseConfig.AdminClientCAs, "admin-client-certs", defaultConfig.AdminClientCAs, "admin client certs (for mutual TLS)")
fnb.flags.UintVar(&fnb.BaseConfig.AdminMaxMsgSize, "admin-max-response-size", defaultConfig.AdminMaxMsgSize, "admin server max response size in bytes")
fnb.flags.Float64Var(&fnb.BaseConfig.LibP2PResourceManagerConfig.FileDescriptorsRatio, "libp2p-fd-ratio", defaultConfig.LibP2PResourceManagerConfig.FileDescriptorsRatio, "ratio of available file descriptors to be used by libp2p (in (0,1])")
fnb.flags.Float64Var(&fnb.BaseConfig.LibP2PResourceManagerConfig.MemoryLimitRatio, "libp2p-memory-limit", defaultConfig.LibP2PResourceManagerConfig.MemoryLimitRatio, "ratio of available memory to be used by libp2p (in (0,1])")
fnb.flags.IntVar(&fnb.BaseConfig.LibP2PResourceManagerConfig.PeerBaseLimitConnsInbound, "libp2p-inbound-conns-limit", defaultConfig.LibP2PResourceManagerConfig.PeerBaseLimitConnsInbound, "the maximum amount of allowed inbound connections per peer")
fnb.flags.IntVar(&fnb.BaseConfig.ConnectionManagerConfig.LowWatermark, "libp2p-connmgr-low", defaultConfig.ConnectionManagerConfig.LowWatermark, "low watermarking for libp2p connection manager")
fnb.flags.IntVar(&fnb.BaseConfig.ConnectionManagerConfig.HighWatermark, "libp2p-connmgr-high", defaultConfig.ConnectionManagerConfig.HighWatermark, "high watermarking for libp2p connection manager")
fnb.flags.DurationVar(&fnb.BaseConfig.ConnectionManagerConfig.GracePeriod, "libp2p-connmgr-grace", defaultConfig.ConnectionManagerConfig.GracePeriod, "grace period for libp2p connection manager")
fnb.flags.DurationVar(&fnb.BaseConfig.ConnectionManagerConfig.SilencePeriod, "libp2p-connmgr-silence", defaultConfig.ConnectionManagerConfig.SilencePeriod, "silence period for libp2p connection manager")
fnb.flags.DurationVar(&fnb.BaseConfig.DNSCacheTTL, "dns-cache-ttl", defaultConfig.DNSCacheTTL, "time-to-live for dns cache")
fnb.flags.StringSliceVar(&fnb.BaseConfig.PreferredUnicastProtocols, "preferred-unicast-protocols", nil, "preferred unicast protocols in ascending order of preference")
fnb.flags.Uint32Var(&fnb.BaseConfig.NetworkReceivedMessageCacheSize, "networking-receive-cache-size", p2p.DefaultReceiveCacheSize,
"incoming message cache size at networking layer")
fnb.flags.BoolVar(&fnb.BaseConfig.NetworkConnectionPruning, "networking-connection-pruning", defaultConfig.NetworkConnectionPruning, "enabling connection trimming")
fnb.flags.BoolVar(&fnb.BaseConfig.GossipSubConfig.PeerScoring, "peer-scoring-enabled", defaultConfig.GossipSubConfig.PeerScoring, "enabling peer scoring on pubsub network")
fnb.flags.DurationVar(&fnb.BaseConfig.GossipSubConfig.LocalMeshLogInterval, "gossipsub-local-mesh-logging-interval", defaultConfig.GossipSubConfig.LocalMeshLogInterval, "logging interval for local mesh in gossipsub")
fnb.flags.DurationVar(&fnb.BaseConfig.GossipSubConfig.ScoreTracerInterval, "gossipsub-score-tracer-interval", defaultConfig.GossipSubConfig.ScoreTracerInterval, "logging interval for peer score tracer in gossipsub, set to 0 to disable")
fnb.flags.UintVar(&fnb.BaseConfig.guaranteesCacheSize, "guarantees-cache-size", bstorage.DefaultCacheSize, "collection guarantees cache size")
fnb.flags.UintVar(&fnb.BaseConfig.receiptsCacheSize, "receipts-cache-size", bstorage.DefaultCacheSize, "receipts cache size")
// dynamic node startup flags
fnb.flags.StringVar(&fnb.BaseConfig.DynamicStartupANPubkey, "dynamic-startup-access-publickey", "", "the public key of the trusted secure access node to connect to when using dynamic-startup, this access node must be staked")
fnb.flags.StringVar(&fnb.BaseConfig.DynamicStartupANAddress, "dynamic-startup-access-address", "", "the access address of the trusted secure access node to connect to when using dynamic-startup, this access node must be staked")
fnb.flags.StringVar(&fnb.BaseConfig.DynamicStartupEpochPhase, "dynamic-startup-epoch-phase", "EpochPhaseSetup", "the target epoch phase for dynamic startup <EpochPhaseStaking|EpochPhaseSetup|EpochPhaseCommitted")
fnb.flags.StringVar(&fnb.BaseConfig.DynamicStartupEpoch, "dynamic-startup-epoch", "current", "the target epoch for dynamic-startup, use \"current\" to start node in the current epoch")
fnb.flags.DurationVar(&fnb.BaseConfig.DynamicStartupSleepInterval, "dynamic-startup-sleep-interval", time.Minute, "the interval in which the node will check if it can start")
fnb.flags.BoolVar(&fnb.BaseConfig.InsecureSecretsDB, "insecure-secrets-db", false, "allow the node to start up without an secrets DB encryption key")
fnb.flags.BoolVar(&fnb.BaseConfig.HeroCacheMetricsEnable, "herocache-metrics-collector", false, "enables herocache metrics collection")
// sync core flags
fnb.flags.DurationVar(&fnb.BaseConfig.SyncCoreConfig.RetryInterval, "sync-retry-interval", defaultConfig.SyncCoreConfig.RetryInterval, "the initial interval before we retry a sync request, uses exponential backoff")
fnb.flags.UintVar(&fnb.BaseConfig.SyncCoreConfig.Tolerance, "sync-tolerance", defaultConfig.SyncCoreConfig.Tolerance, "determines how big of a difference in block heights we tolerate before actively syncing with range requests")
fnb.flags.UintVar(&fnb.BaseConfig.SyncCoreConfig.MaxAttempts, "sync-max-attempts", defaultConfig.SyncCoreConfig.MaxAttempts, "the maximum number of attempts we make for each requested block/height before discarding")
fnb.flags.UintVar(&fnb.BaseConfig.SyncCoreConfig.MaxSize, "sync-max-size", defaultConfig.SyncCoreConfig.MaxSize, "the maximum number of blocks we request in the same block request message")
fnb.flags.UintVar(&fnb.BaseConfig.SyncCoreConfig.MaxRequests, "sync-max-requests", defaultConfig.SyncCoreConfig.MaxRequests, "the maximum number of requests we send during each scanning period")
fnb.flags.Uint64Var(&fnb.BaseConfig.ComplianceConfig.SkipNewProposalsThreshold, "compliance-skip-proposals-threshold", defaultConfig.ComplianceConfig.SkipNewProposalsThreshold, "threshold at which new proposals are discarded rather than cached, if their height is this much above local finalized height")
// unicast stream handler rate limits
fnb.flags.IntVar(&fnb.BaseConfig.UnicastRateLimitersConfig.MessageRateLimit, "unicast-message-rate-limit", defaultConfig.UnicastRateLimitersConfig.MessageRateLimit, "maximum number of unicast messages that a peer can send per second")
fnb.flags.IntVar(&fnb.BaseConfig.UnicastRateLimitersConfig.BandwidthRateLimit, "unicast-bandwidth-rate-limit", defaultConfig.UnicastRateLimitersConfig.BandwidthRateLimit, "bandwidth size in bytes a peer is allowed to send via unicast streams per second")
fnb.flags.IntVar(&fnb.BaseConfig.UnicastRateLimitersConfig.BandwidthBurstLimit, "unicast-bandwidth-burst-limit", defaultConfig.UnicastRateLimitersConfig.BandwidthBurstLimit, "bandwidth size in bytes a peer is allowed to send at one time")
fnb.flags.DurationVar(&fnb.BaseConfig.UnicastRateLimitersConfig.LockoutDuration, "unicast-rate-limit-lockout-duration", defaultConfig.UnicastRateLimitersConfig.LockoutDuration, "the number of seconds a peer will be forced to wait before being allowed to successful reconnect to the node after being rate limited")
fnb.flags.BoolVar(&fnb.BaseConfig.UnicastRateLimitersConfig.DryRun, "unicast-rate-limit-dry-run", defaultConfig.UnicastRateLimitersConfig.DryRun, "disable peer disconnects and connections gating when rate limiting peers")
// gossipsub RPC control message validation limits used for validation configuration and rate limiting
fnb.flags.IntVar(&fnb.BaseConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.NumberOfWorkers, "gossipsub-rpc-validation-inspector-workers", defaultConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.NumberOfWorkers, "number of gossupsub RPC control message validation inspector component workers")
fnb.flags.Uint32Var(&fnb.BaseConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.CacheSize, "gossipsub-rpc-validation-inspector-cache-size", defaultConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.CacheSize, "cache size for gossipsub RPC validation inspector events worker pool queue.")
fnb.flags.StringToIntVar(&fnb.BaseConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.GraftLimits, "gossipsub-rpc-graft-limits", defaultConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.GraftLimits, fmt.Sprintf("hard threshold, safety and rate limits for gossipsub RPC GRAFT message validation e.g: %s=1000,%s=100,%s=1000", validation.HardThresholdMapKey, validation.SafetyThresholdMapKey, validation.RateLimitMapKey))
fnb.flags.StringToIntVar(&fnb.BaseConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.PruneLimits, "gossipsub-rpc-prune-limits", defaultConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.PruneLimits, fmt.Sprintf("hard threshold, safety and rate limits for gossipsub RPC PRUNE message validation e.g: %s=1000,%s=20,%s=1000", validation.HardThresholdMapKey, validation.SafetyThresholdMapKey, validation.RateLimitMapKey))
fnb.flags.StringToIntVar(&fnb.BaseConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.IHaveLimitsConfig.IHaveLimits, "gossipsub-rpc-ihave-limits", defaultConfig.GossipSubConfig.RpcInspector.ValidationInspectorConfigs.IHaveLimitsConfig.IHaveLimits, fmt.Sprintf("hard threshold and safety threshold limits for gossipsub RPC IHAVE message validation e.g: %s=1000,%s=20", validation.HardThresholdMapKey, validation.SafetyThresholdMapKey))
// gossipsub RPC control message metrics observer inspector configuration
fnb.flags.IntVar(&fnb.BaseConfig.GossipSubConfig.RpcInspector.MetricsInspectorConfigs.NumberOfWorkers, "gossipsub-rpc-metrics-inspector-workers", defaultConfig.GossipSubConfig.RpcInspector.MetricsInspectorConfigs.NumberOfWorkers, "cache size for gossipsub RPC metrics inspector events worker pool queue.")
fnb.flags.Uint32Var(&fnb.BaseConfig.GossipSubConfig.RpcInspector.MetricsInspectorConfigs.CacheSize, "gossipsub-rpc-metrics-inspector-cache-size", defaultConfig.GossipSubConfig.RpcInspector.MetricsInspectorConfigs.CacheSize, "cache size for gossipsub RPC metrics inspector events worker pool.")
// networking event notifications
fnb.flags.Uint32Var(&fnb.BaseConfig.GossipSubConfig.RpcInspector.GossipSubRPCInspectorNotificationCacheSize, "gossipsub-rpc-inspector-notification-cache-size", defaultConfig.GossipSubConfig.RpcInspector.GossipSubRPCInspectorNotificationCacheSize, "cache size for notification events from gossipsub rpc inspector")
fnb.flags.Uint32Var(&fnb.BaseConfig.DisallowListNotificationCacheSize, "disallow-list-notification-cache-size", defaultConfig.DisallowListNotificationCacheSize, "cache size for notification events from disallow list")
// unicast manager options
fnb.flags.DurationVar(&fnb.BaseConfig.UnicastCreateStreamRetryDelay, "unicast-manager-create-stream-retry-delay", defaultConfig.NetworkConfig.UnicastCreateStreamRetryDelay, "Initial delay between failing to establish a connection with another node and retrying. This delay increases exponentially (exponential backoff) with the number of subsequent failures to establish a connection.")
// application layer spam prevention (alsp) protocol
fnb.flags.BoolVar(&fnb.BaseConfig.AlspConfig.DisablePenalty, "alsp-disable", defaultConfig.AlspConfig.DisablePenalty, "disable the penalty mechanism of the alsp protocol. default value (recommended) is false")
fnb.flags.Uint32Var(&fnb.BaseConfig.AlspConfig.SpamRecordCacheSize, "alsp-spam-record-cache-size", defaultConfig.AlspConfig.SpamRecordCacheSize, "size of spam record cache, recommended to be 10x the number of authorized nodes")
fnb.flags.Uint32Var(&fnb.BaseConfig.AlspConfig.SpamReportQueueSize, "alsp-spam-report-queue-size", defaultConfig.AlspConfig.SpamReportQueueSize, "size of spam report queue, recommended to be 100x the number of authorized nodes")
fnb.flags.DurationVar(&fnb.BaseConfig.AlspConfig.HearBeatInterval, "alsp-heartbeat-interval", defaultConfig.AlspConfig.HearBeatInterval, "interval between two consecutive heartbeat events at alsp, recommended to leave it as default unless you know what you are doing.")
}
func (fnb *FlowNodeBuilder) EnqueuePingService() {
fnb.Component("ping service", func(node *NodeConfig) (module.ReadyDoneAware, error) {
pingLibP2PProtocolID := protocols.PingProtocolId(node.SporkID)
// setup the Ping provider to return the software version and the sealed block height
pingInfoProvider := &ping.InfoProvider{
SoftwareVersionFun: func() string {
return build.Semver()
},
SealedBlockHeightFun: func() (uint64, error) {
head, err := node.State.Sealed().Head()
if err != nil {
return 0, err
}
return head.Height, nil
},
HotstuffViewFun: func() (uint64, error) {
return 0, fmt.Errorf("hotstuff view reporting disabled")
},
}
// only consensus roles will need to report hotstuff view
if fnb.BaseConfig.NodeRole == flow.RoleConsensus.String() {
// initialize the persister
persist := persister.New(node.DB, node.RootChainID)
pingInfoProvider.HotstuffViewFun = func() (uint64, error) {
livenessData, err := persist.GetLivenessData()
if err != nil {
return 0, err
}
return livenessData.CurrentView, nil
}
}
pingService, err := node.Network.RegisterPingService(pingLibP2PProtocolID, pingInfoProvider)
node.PingService = pingService
return &module.NoopReadyDoneAware{}, err
})
}
func (fnb *FlowNodeBuilder) EnqueueResolver() {
fnb.Component("resolver", func(node *NodeConfig) (module.ReadyDoneAware, error) {
var dnsIpCacheMetricsCollector module.HeroCacheMetrics = metrics.NewNoopCollector()
var dnsTxtCacheMetricsCollector module.HeroCacheMetrics = metrics.NewNoopCollector()
if fnb.HeroCacheMetricsEnable {
dnsIpCacheMetricsCollector = metrics.NetworkDnsIpCacheMetricsFactory(fnb.MetricsRegisterer)
dnsTxtCacheMetricsCollector = metrics.NetworkDnsTxtCacheMetricsFactory(fnb.MetricsRegisterer)
}
cache := herocache.NewDNSCache(
dns.DefaultCacheSize,
node.Logger,
dnsIpCacheMetricsCollector,
dnsTxtCacheMetricsCollector,
)
resolver := dns.NewResolver(
node.Logger,
fnb.Metrics.Network,
cache,
dns.WithTTL(fnb.BaseConfig.DNSCacheTTL))
fnb.Resolver = resolver
return resolver, nil
})
}
func (fnb *FlowNodeBuilder) EnqueueNetworkInit() {
connGaterPeerDialFilters := make([]p2p.PeerFilter, 0)
connGaterInterceptSecureFilters := make([]p2p.PeerFilter, 0)
peerManagerFilters := make([]p2p.PeerFilter, 0)
fnb.UnicastRateLimiterDistributor = ratelimit.NewUnicastRateLimiterDistributor()
fnb.UnicastRateLimiterDistributor.AddConsumer(fnb.Metrics.Network)
// setup default rate limiter options
unicastRateLimiterOpts := []ratelimit.RateLimitersOption{
ratelimit.WithDisabledRateLimiting(fnb.BaseConfig.UnicastRateLimitersConfig.DryRun),
ratelimit.WithNotifier(fnb.UnicastRateLimiterDistributor),
}
// override noop unicast message rate limiter
if fnb.BaseConfig.UnicastRateLimitersConfig.MessageRateLimit > 0 {
unicastMessageRateLimiter := ratelimiter.NewRateLimiter(
rate.Limit(fnb.BaseConfig.UnicastRateLimitersConfig.MessageRateLimit),
fnb.BaseConfig.UnicastRateLimitersConfig.MessageRateLimit,
fnb.BaseConfig.UnicastRateLimitersConfig.LockoutDuration,
)
unicastRateLimiterOpts = append(unicastRateLimiterOpts, ratelimit.WithMessageRateLimiter(unicastMessageRateLimiter))
// avoid connection gating and pruning during dry run
if !fnb.BaseConfig.UnicastRateLimitersConfig.DryRun {
f := rateLimiterPeerFilter(unicastMessageRateLimiter)
// add IsRateLimited peerFilters to conn gater intercept secure peer and peer manager filters list
// don't allow rate limited peers to establishing incoming connections
connGaterInterceptSecureFilters = append(connGaterInterceptSecureFilters, f)
// don't create outbound connections to rate limited peers
peerManagerFilters = append(peerManagerFilters, f)
}
}
// override noop unicast bandwidth rate limiter
if fnb.BaseConfig.UnicastRateLimitersConfig.BandwidthRateLimit > 0 && fnb.BaseConfig.UnicastRateLimitersConfig.BandwidthBurstLimit > 0 {
unicastBandwidthRateLimiter := ratelimit.NewBandWidthRateLimiter(
rate.Limit(fnb.BaseConfig.UnicastRateLimitersConfig.BandwidthRateLimit),
fnb.BaseConfig.UnicastRateLimitersConfig.BandwidthBurstLimit,
fnb.BaseConfig.UnicastRateLimitersConfig.LockoutDuration,
)
unicastRateLimiterOpts = append(unicastRateLimiterOpts, ratelimit.WithBandwidthRateLimiter(unicastBandwidthRateLimiter))
// avoid connection gating and pruning during dry run
if !fnb.BaseConfig.UnicastRateLimitersConfig.DryRun {
f := rateLimiterPeerFilter(unicastBandwidthRateLimiter)
// add IsRateLimited peerFilters to conn gater intercept secure peer and peer manager filters list
connGaterInterceptSecureFilters = append(connGaterInterceptSecureFilters, f)
peerManagerFilters = append(peerManagerFilters, f)
}
}
// setup unicast rate limiters
unicastRateLimiters := ratelimit.NewRateLimiters(unicastRateLimiterOpts...)
uniCfg := &p2pconfig.UnicastConfig{
StreamRetryInterval: fnb.UnicastCreateStreamRetryDelay,
RateLimiterDistributor: fnb.UnicastRateLimiterDistributor,
}
connGaterCfg := &p2pconfig.ConnectionGaterConfig{
InterceptPeerDialFilters: connGaterPeerDialFilters,
InterceptSecuredFilters: connGaterInterceptSecureFilters,
}
peerManagerCfg := &p2pconfig.PeerManagerConfig{
ConnectionPruning: fnb.NetworkConnectionPruning,
UpdateInterval: fnb.PeerUpdateInterval,
}
fnb.Component(LibP2PNodeComponent, func(node *NodeConfig) (module.ReadyDoneAware, error) {
myAddr := fnb.NodeConfig.Me.Address()
if fnb.BaseConfig.BindAddr != NotSet {
myAddr = fnb.BaseConfig.BindAddr
}
metricsCfg := &p2pconfig.MetricsConfig{
Metrics: fnb.Metrics.Network,
HeroCacheFactory: fnb.HeroCacheMetricsFactory(),
}
rpcInspectorSuite, err := inspector.NewGossipSubInspectorBuilder(fnb.Logger, fnb.SporkID, fnb.GossipSubConfig.RpcInspector, fnb.IdentityProvider, fnb.Metrics.Network).
SetNetworkType(network.PrivateNetwork).
SetMetrics(metricsCfg).
Build()
if err != nil {
return nil, fmt.Errorf("failed to create gossipsub rpc inspectors for default libp2p node: %w", err)
}
fnb.GossipSubRpcInspectorSuite = rpcInspectorSuite
builder, err := p2pbuilder.DefaultNodeBuilder(
fnb.Logger,
myAddr,
fnb.NetworkKey,
fnb.SporkID,
fnb.IdentityProvider,
metricsCfg,
fnb.Resolver,
fnb.BaseConfig.NodeRole,
connGaterCfg,
peerManagerCfg,
fnb.GossipSubConfig,
fnb.GossipSubRpcInspectorSuite,
fnb.LibP2PResourceManagerConfig,
uniCfg)
if err != nil {
return nil, fmt.Errorf("could not create libp2p node builder: %w", err)
}
libp2pNode, err := builder.Build()
if err != nil {
return nil, fmt.Errorf("could not build libp2p node: %w", err)
}
fnb.LibP2PNode = libp2pNode
return libp2pNode, nil
})
fnb.Component(NetworkComponent, func(node *NodeConfig) (module.ReadyDoneAware, error) {
fnb.Logger.Info().Hex("node_id", logging.ID(fnb.NodeID)).Msg("default conduit factory initiated")
return fnb.InitFlowNetworkWithConduitFactory(
node,
conduit.NewDefaultConduitFactory(),
unicastRateLimiters,
peerManagerFilters)
})
fnb.Module("middleware dependency", func(node *NodeConfig) error {
fnb.middlewareDependable = module.NewProxiedReadyDoneAware()
fnb.PeerManagerDependencies.Add(fnb.middlewareDependable)
return nil
})
// peer manager won't be created until all PeerManagerDependencies are ready.
fnb.DependableComponent("peer manager", func(node *NodeConfig) (module.ReadyDoneAware, error) {
return fnb.LibP2PNode.PeerManagerComponent(), nil
}, fnb.PeerManagerDependencies)
}
// HeroCacheMetricsFactory returns a HeroCacheMetricsFactory based on the MetricsEnabled flag.
// If MetricsEnabled is true, it returns a HeroCacheMetricsFactory that will register metrics with the provided MetricsRegisterer.
// If MetricsEnabled is false, it returns a no-op HeroCacheMetricsFactory that will not register any metrics.
func (fnb *FlowNodeBuilder) HeroCacheMetricsFactory() metrics.HeroCacheMetricsFactory {
if fnb.MetricsEnabled {
return metrics.NewHeroCacheMetricsFactory(fnb.MetricsRegisterer)
}
return metrics.NewNoopHeroCacheMetricsFactory()
}
func (fnb *FlowNodeBuilder) InitFlowNetworkWithConduitFactory(node *NodeConfig, cf network.ConduitFactory, unicastRateLimiters *ratelimit.RateLimiters, peerManagerFilters []p2p.PeerFilter) (network.Network, error) {
var mwOpts []middleware.MiddlewareOption
if len(fnb.MsgValidators) > 0 {
mwOpts = append(mwOpts, middleware.WithMessageValidators(fnb.MsgValidators...))
}
// by default if no rate limiter configuration was provided in the CLI args the default
// noop rate limiter will be used.
mwOpts = append(mwOpts, middleware.WithUnicastRateLimiters(unicastRateLimiters))
mwOpts = append(mwOpts,
middleware.WithPreferredUnicastProtocols(protocols.ToProtocolNames(fnb.PreferredUnicastProtocols)),
)
// peerManagerFilters are used by the peerManager via the middleware to filter peers from the topology.
if len(peerManagerFilters) > 0 {
mwOpts = append(mwOpts, middleware.WithPeerManagerFilters(peerManagerFilters))
}
slashingViolationsConsumer := slashing.NewSlashingViolationsConsumer(fnb.Logger, fnb.Metrics.Network)
mw := middleware.NewMiddleware(
fnb.Logger,
fnb.LibP2PNode,
fnb.Me.NodeID(),
fnb.Metrics.Bitswap,
fnb.SporkID,
fnb.BaseConfig.UnicastMessageTimeout,
fnb.IDTranslator,
fnb.CodecFactory(),
slashingViolationsConsumer,
mwOpts...)
fnb.NodeDisallowListDistributor.AddConsumer(mw)
fnb.Middleware = mw
subscriptionManager := subscription.NewChannelSubscriptionManager(fnb.Middleware)
receiveCache := netcache.NewHeroReceiveCache(fnb.NetworkReceivedMessageCacheSize,
fnb.Logger,
metrics.NetworkReceiveCacheMetricsFactory(fnb.HeroCacheMetricsFactory(), network.PrivateNetwork))
err := node.Metrics.Mempool.Register(metrics.ResourceNetworkingReceiveCache, receiveCache.Size)
if err != nil {
return nil, fmt.Errorf("could not register networking receive cache metric: %w", err)
}
// creates network instance
net, err := p2p.NewNetwork(&p2p.NetworkConfig{
Logger: fnb.Logger,
Codec: fnb.CodecFactory(),
Me: fnb.Me,
MiddlewareFactory: func() (network.Middleware, error) { return fnb.Middleware, nil },
Topology: topology.NewFullyConnectedTopology(),
SubscriptionManager: subscriptionManager,
Metrics: fnb.Metrics.Network,
IdentityProvider: fnb.IdentityProvider,
ReceiveCache: receiveCache,
ConduitFactory: cf,
AlspCfg: &alspmgr.MisbehaviorReportManagerConfig{
Logger: fnb.Logger,
SpamRecordCacheSize: fnb.AlspConfig.SpamRecordCacheSize,
SpamReportQueueSize: fnb.AlspConfig.SpamReportQueueSize,
DisablePenalty: fnb.AlspConfig.DisablePenalty,
HeartBeatInterval: fnb.AlspConfig.HearBeatInterval,
AlspMetrics: fnb.Metrics.Network,
HeroCacheMetricsFactory: fnb.HeroCacheMetricsFactory(),
NetworkType: network.PrivateNetwork,
},
})
if err != nil {
return nil, fmt.Errorf("could not initialize network: %w", err)
}
fnb.Network = net
// register middleware's ReadyDoneAware interface so other components can depend on it for startup
if fnb.middlewareDependable != nil {
fnb.middlewareDependable.Init(fnb.Middleware)
}
idEvents := gadgets.NewIdentityDeltas(fnb.Middleware.UpdateNodeAddresses)
fnb.ProtocolEvents.AddConsumer(idEvents)
return net, nil
}
func (fnb *FlowNodeBuilder) EnqueueMetricsServerInit() {
fnb.Component("metrics server", func(node *NodeConfig) (module.ReadyDoneAware, error) {
server := metrics.NewServer(fnb.Logger, fnb.BaseConfig.metricsPort)
return server, nil
})
}
func (fnb *FlowNodeBuilder) EnqueueAdminServerInit() error {
if fnb.AdminAddr == NotSet {
return nil
}
if (fnb.AdminCert != NotSet || fnb.AdminKey != NotSet || fnb.AdminClientCAs != NotSet) &&
!(fnb.AdminCert != NotSet && fnb.AdminKey != NotSet && fnb.AdminClientCAs != NotSet) {
return fmt.Errorf("admin cert / key and client certs must all be provided to enable mutual TLS")
}
// create the updatable config manager
fnb.RegisterDefaultAdminCommands()
fnb.Component("admin server", func(node *NodeConfig) (module.ReadyDoneAware, error) {
// set up all admin commands
for commandName, commandFunc := range fnb.adminCommands {
command := commandFunc(fnb.NodeConfig)
fnb.adminCommandBootstrapper.RegisterHandler(commandName, command.Handler)
fnb.adminCommandBootstrapper.RegisterValidator(commandName, command.Validator)
}
opts := []admin.CommandRunnerOption{
admin.WithMaxMsgSize(int(fnb.AdminMaxMsgSize)),
}
if node.AdminCert != NotSet {
serverCert, err := tls.LoadX509KeyPair(node.AdminCert, node.AdminKey)
if err != nil {
return nil, err
}
clientCAs, err := os.ReadFile(node.AdminClientCAs)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(clientCAs)
config := &tls.Config{
MinVersion: tls.VersionTLS13,
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: certPool,
}
opts = append(opts, admin.WithTLS(config))
}
runner := fnb.adminCommandBootstrapper.Bootstrap(fnb.Logger, fnb.AdminAddr, opts...)
return runner, nil
})
return nil
}
func (fnb *FlowNodeBuilder) RegisterBadgerMetrics() error {
return metrics.RegisterBadgerMetrics()
}
func (fnb *FlowNodeBuilder) EnqueueTracer() {
fnb.Component("tracer", func(node *NodeConfig) (module.ReadyDoneAware, error) {
return fnb.Tracer, nil
})
}
func (fnb *FlowNodeBuilder) ParseAndPrintFlags() error {
// parse configuration parameters
pflag.Parse()
// print all flags
log := fnb.Logger.Info()
pflag.VisitAll(func(flag *pflag.Flag) {
log = log.Str(flag.Name, flag.Value.String())
})
log.Msg("flags loaded")
return fnb.extraFlagsValidation()
}
func (fnb *FlowNodeBuilder) ValidateRootSnapshot(f func(protocol.Snapshot) error) NodeBuilder {
fnb.extraRootSnapshotCheck = f
return fnb
}
func (fnb *FlowNodeBuilder) ValidateFlags(f func() error) NodeBuilder {
fnb.extraFlagCheck = f
return fnb
}
func (fnb *FlowNodeBuilder) PrintBuildVersionDetails() {
fnb.Logger.Info().Str("version", build.Semver()).Str("commit", build.Commit()).Msg("build details")
}
func (fnb *FlowNodeBuilder) initNodeInfo() error {
if fnb.BaseConfig.nodeIDHex == NotSet {
return fmt.Errorf("cannot start without node ID")
}
nodeID, err := flow.HexStringToIdentifier(fnb.BaseConfig.nodeIDHex)
if err != nil {
return fmt.Errorf("could not parse node ID from string (id: %v): %w", fnb.BaseConfig.nodeIDHex, err)
}
info, err := LoadPrivateNodeInfo(fnb.BaseConfig.BootstrapDir, nodeID)
if err != nil {
return fmt.Errorf("failed to load private node info: %w", err)
}
fnb.NodeID = nodeID
fnb.NetworkKey = info.NetworkPrivKey.PrivateKey
fnb.StakingKey = info.StakingPrivKey.PrivateKey
return nil
}
func (fnb *FlowNodeBuilder) initLogger() error {
// configure logger with standard level, node ID and UTC timestamp
zerolog.TimeFieldFormat = time.RFC3339Nano
zerolog.TimestampFunc = func() time.Time { return time.Now().UTC() }
// Drop all log events that exceed this rate limit
throttledSampler := logging.BurstSampler(fnb.BaseConfig.debugLogLimit, time.Second)
log := fnb.Logger.With().
Timestamp().
Str("node_role", fnb.BaseConfig.NodeRole).
Str("node_id", fnb.NodeID.String()).
Logger().
Sample(zerolog.LevelSampler{
TraceSampler: throttledSampler,
DebugSampler: throttledSampler,
})
log.Info().Msgf("flow %s node starting up", fnb.BaseConfig.NodeRole)
// parse config log level and apply to logger
lvl, err := zerolog.ParseLevel(strings.ToLower(fnb.BaseConfig.level))
if err != nil {
return fmt.Errorf("invalid log level: %w", err)
}
// Minimum log level is set to trace, then overridden by SetGlobalLevel.
// this allows admin commands to modify the level to any value during runtime
log = log.Level(zerolog.TraceLevel)
zerolog.SetGlobalLevel(lvl)
fnb.Logger = log
return nil
}
func (fnb *FlowNodeBuilder) initMetrics() error {
fnb.Tracer = trace.NewNoopTracer()
if fnb.BaseConfig.tracerEnabled {
nodeIdHex := fnb.NodeID.String()
if len(nodeIdHex) > 8 {
nodeIdHex = nodeIdHex[:8]
}
serviceName := fnb.BaseConfig.NodeRole + "-" + nodeIdHex
tracer, err := trace.NewTracer(
fnb.Logger,
serviceName,
fnb.RootChainID.String(),
fnb.tracerSensitivity,
)
if err != nil {
return fmt.Errorf("could not initialize tracer: %w", err)
}
fnb.Logger.Info().Msg("Tracer Started")
fnb.Tracer = tracer
}
fnb.Metrics = Metrics{
Network: metrics.NewNoopCollector(),
Engine: metrics.NewNoopCollector(),
Compliance: metrics.NewNoopCollector(),
Cache: metrics.NewNoopCollector(),
Mempool: metrics.NewNoopCollector(),
CleanCollector: metrics.NewNoopCollector(),
Bitswap: metrics.NewNoopCollector(),
}
if fnb.BaseConfig.MetricsEnabled {
fnb.MetricsRegisterer = prometheus.DefaultRegisterer
mempools := metrics.NewMempoolCollector(5 * time.Second)
fnb.Metrics = Metrics{
Network: metrics.NewNetworkCollector(fnb.Logger),
Engine: metrics.NewEngineCollector(),
Compliance: metrics.NewComplianceCollector(),
// CacheControl metrics has been causing memory abuse, disable for now
// Cache: metrics.NewCacheCollector(fnb.RootChainID),
Cache: metrics.NewNoopCollector(),
CleanCollector: metrics.NewCleanerCollector(),
Mempool: mempools,
Bitswap: metrics.NewBitswapCollector(),
}
// registers mempools as a Component so that its Ready method is invoked upon startup
fnb.Component("mempools metrics", func(node *NodeConfig) (module.ReadyDoneAware, error) {
return mempools, nil
})
// metrics enabled, report node info metrics as post init event
fnb.PostInit(func(nodeConfig *NodeConfig) error {
nodeInfoMetrics := metrics.NewNodeInfoCollector()
protocolVersion, err := fnb.RootSnapshot.Params().ProtocolVersion()
if err != nil {
return fmt.Errorf("could not query root snapshoot protocol version: %w", err)
}
nodeInfoMetrics.NodeInfo(build.Semver(), build.Commit(), nodeConfig.SporkID.String(), protocolVersion)
return nil
})
}
return nil
}
func (fnb *FlowNodeBuilder) createGCEProfileUploader(client *gcemd.Client, opts ...option.ClientOption) (profiler.Uploader, error) {
projectID, err := client.ProjectID()
if err != nil {
return &profiler.NoopUploader{}, fmt.Errorf("failed to get project ID: %w", err)
}
instance, err := client.InstanceID()
if err != nil {
return &profiler.NoopUploader{}, fmt.Errorf("failed to get instance ID: %w", err)
}
chainID := fnb.RootChainID.String()
if chainID == "" {
fnb.Logger.Warn().Msg("RootChainID is not set, using default value")
chainID = "unknown"
}
params := profiler.Params{
ProjectID: projectID,
ChainID: chainID,
Role: fnb.NodeConfig.NodeRole,
Version: build.Semver(),
Commit: build.Commit(),
Instance: instance,
}
fnb.Logger.Info().Msgf("creating pprof profile uploader with params: %+v", params)
return profiler.NewUploader(fnb.Logger, params, opts...)
}
func (fnb *FlowNodeBuilder) createProfileUploader() (profiler.Uploader, error) {
switch {
case fnb.BaseConfig.profilerConfig.UploaderEnabled && gcemd.OnGCE():
return fnb.createGCEProfileUploader(gcemd.NewClient(nil))
default:
fnb.Logger.Info().Msg("not running on GCE, setting pprof uploader to noop")
return &profiler.NoopUploader{}, nil
}
}
func (fnb *FlowNodeBuilder) initProfiler() error {
uploader, err := fnb.createProfileUploader()
if err != nil {
fnb.Logger.Warn().Err(err).Msg("failed to create pprof uploader, falling back to noop")
uploader = &profiler.NoopUploader{}
}
profiler, err := profiler.New(fnb.Logger, uploader, fnb.BaseConfig.profilerConfig)
if err != nil {
return fmt.Errorf("could not initialize profiler: %w", err)
}
// register the enabled state of the profiler for dynamic configuring
err = fnb.ConfigManager.RegisterBoolConfig("profiler-enabled", profiler.Enabled, profiler.SetEnabled)
if err != nil {
return fmt.Errorf("could not register profiler-enabled config: %w", err)
}
err = fnb.ConfigManager.RegisterDurationConfig(
"profiler-trigger",
func() time.Duration { return fnb.BaseConfig.profilerConfig.Duration },
func(d time.Duration) error { return profiler.TriggerRun(d) },
)
if err != nil {
return fmt.Errorf("could not register profiler-trigger config: %w", err)
}
err = fnb.ConfigManager.RegisterUintConfig(
"profiler-set-mem-profile-rate",
func() uint { return uint(runtime.MemProfileRate) },
func(r uint) error { runtime.MemProfileRate = int(r); return nil },
)
if err != nil {
return fmt.Errorf("could not register profiler-set-mem-profile-rate setting: %w", err)
}
// There is no way to get the current block profile rate so we keep track of it ourselves.
currentRate := new(uint)
err = fnb.ConfigManager.RegisterUintConfig(
"profiler-set-block-profile-rate",
func() uint { return *currentRate },
func(r uint) error { currentRate = &r; runtime.SetBlockProfileRate(int(r)); return nil },
)
if err != nil {
return fmt.Errorf("could not register profiler-set-block-profile-rate setting: %w", err)
}
err = fnb.ConfigManager.RegisterUintConfig(
"profiler-set-mutex-profile-fraction",
func() uint { return uint(runtime.SetMutexProfileFraction(-1)) },
func(r uint) error { _ = runtime.SetMutexProfileFraction(int(r)); return nil },
)
if err != nil {
return fmt.Errorf("could not register profiler-set-mutex-profile-fraction setting: %w", err)
}
// registering as a DependableComponent with no dependencies so that it's started immediately on startup
// without being blocked by other component's Ready()
fnb.DependableComponent("profiler", func(node *NodeConfig) (module.ReadyDoneAware, error) {
return profiler, nil
}, NewDependencyList())
return nil
}
func (fnb *FlowNodeBuilder) initDB() error {
// if a db has been passed in, use that instead of creating one
if fnb.BaseConfig.db != nil {
fnb.DB = fnb.BaseConfig.db
return nil
}
// Pre-create DB path (Badger creates only one-level dirs)
err := os.MkdirAll(fnb.BaseConfig.datadir, 0700)
if err != nil {
return fmt.Errorf("could not create datadir (path: %s): %w", fnb.BaseConfig.datadir, err)
}
log := sutil.NewLogger(fnb.Logger)
// we initialize the database with options that allow us to keep the maximum
// item size in the trie itself (up to 1MB) and where we keep all level zero
// tables in-memory as well; this slows down compaction and increases memory
// usage, but it improves overall performance and disk i/o
opts := badger.
DefaultOptions(fnb.BaseConfig.datadir).
WithKeepL0InMemory(true).
WithLogger(log).
// the ValueLogFileSize option specifies how big the value of a
// key-value pair is allowed to be saved into badger.
// exceeding this limit, will fail with an error like this:
// could not store data: Value with size <xxxx> exceeded 1073741824 limit
// Maximum value size is 10G, needed by execution node
// TODO: finding a better max value for each node type
WithValueLogFileSize(256 << 23).
WithValueLogMaxEntries(100000) // Default is 1000000
publicDB, err := bstorage.InitPublic(opts)
if err != nil {
return fmt.Errorf("could not open public db: %w", err)
}
fnb.DB = publicDB
fnb.ShutdownFunc(func() error {
if err := fnb.DB.Close(); err != nil {
return fmt.Errorf("error closing protocol database: %w", err)
}
return nil
})
fnb.Component("badger log cleaner", func(node *NodeConfig) (module.ReadyDoneAware, error) {
return bstorage.NewCleaner(node.Logger, node.DB, node.Metrics.CleanCollector, flow.DefaultValueLogGCWaitDuration), nil
})
return nil
}
func (fnb *FlowNodeBuilder) initSecretsDB() error {
// if the secrets DB is disabled (only applicable for Consensus Follower,
// which makes use of this same logic), skip this initialization
if !fnb.BaseConfig.secretsDBEnabled {
return nil
}
if fnb.BaseConfig.secretsdir == NotSet {
return fmt.Errorf("missing required flag '--secretsdir'")
}
err := os.MkdirAll(fnb.BaseConfig.secretsdir, 0700)
if err != nil {
return fmt.Errorf("could not create secrets db dir (path: %s): %w", fnb.BaseConfig.secretsdir, err)
}
log := sutil.NewLogger(fnb.Logger)
opts := badger.DefaultOptions(fnb.BaseConfig.secretsdir).WithLogger(log)
// NOTE: SN nodes need to explicitly set --insecure-secrets-db to true in order to
// disable secrets database encryption
if fnb.NodeRole == flow.RoleConsensus.String() && fnb.InsecureSecretsDB {
fnb.Logger.Warn().Msg("starting with secrets database encryption disabled")
} else {
encryptionKey, err := loadSecretsEncryptionKey(fnb.BootstrapDir, fnb.NodeID)
if errors.Is(err, os.ErrNotExist) {
if fnb.NodeRole == flow.RoleConsensus.String() {
// missing key is a fatal error for SN nodes
return fmt.Errorf("secrets db encryption key not found: %w", err)
}
fnb.Logger.Warn().Msg("starting with secrets database encryption disabled")
} else if err != nil {
return fmt.Errorf("failed to read secrets db encryption key: %w", err)
} else {
opts = opts.WithEncryptionKey(encryptionKey)
}
}
secretsDB, err := bstorage.InitSecret(opts)
if err != nil {
return fmt.Errorf("could not open secrets db: %w", err)
}
fnb.SecretsDB = secretsDB
fnb.ShutdownFunc(func() error {
if err := fnb.SecretsDB.Close(); err != nil {
return fmt.Errorf("error closing secrets database: %w", err)
}
return nil
})
return nil
}
func (fnb *FlowNodeBuilder) initStorage() error {
// in order to void long iterations with big keys when initializing with an
// already populated database, we bootstrap the initial maximum key size
// upon starting
err := operation.RetryOnConflict(fnb.DB.Update, func(tx *badger.Txn) error {
return operation.InitMax(tx)
})
if err != nil {
return fmt.Errorf("could not initialize max tracker: %w", err)
}
headers := bstorage.NewHeaders(fnb.Metrics.Cache, fnb.DB)