-
Notifications
You must be signed in to change notification settings - Fork 175
/
observer_builder.go
1687 lines (1501 loc) · 65.5 KB
/
observer_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
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 node_builder
import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/ipfs/boxo/bitswap"
badger "github.com/ipfs/go-ds-badger2"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/rs/zerolog"
"github.com/spf13/pflag"
"google.golang.org/grpc/credentials"
"github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow-go/admin/commands"
stateSyncCommands "github.com/onflow/flow-go/admin/commands/state_synchronization"
"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"
"github.com/onflow/flow-go/consensus/hotstuff/notifications/pubsub"
hotsignature "github.com/onflow/flow-go/consensus/hotstuff/signature"
hotstuffvalidator "github.com/onflow/flow-go/consensus/hotstuff/validator"
"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/apiproxy"
"github.com/onflow/flow-go/engine/access/rest"
restapiproxy "github.com/onflow/flow-go/engine/access/rest/apiproxy"
"github.com/onflow/flow-go/engine/access/rest/routes"
"github.com/onflow/flow-go/engine/access/rpc"
"github.com/onflow/flow-go/engine/access/rpc/backend"
rpcConnection "github.com/onflow/flow-go/engine/access/rpc/connection"
"github.com/onflow/flow-go/engine/access/state_stream"
statestreambackend "github.com/onflow/flow-go/engine/access/state_stream/backend"
"github.com/onflow/flow-go/engine/common/follower"
synceng "github.com/onflow/flow-go/engine/common/synchronization"
"github.com/onflow/flow-go/engine/execution/computation/query"
"github.com/onflow/flow-go/engine/protocol"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/complete/wal"
"github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/encodable"
"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/blobs"
"github.com/onflow/flow-go/module/chainsync"
"github.com/onflow/flow-go/module/execution"
"github.com/onflow/flow-go/module/executiondatasync/execution_data"
execdatacache "github.com/onflow/flow-go/module/executiondatasync/execution_data/cache"
finalizer "github.com/onflow/flow-go/module/finalizer/consensus"
"github.com/onflow/flow-go/module/grpcserver"
"github.com/onflow/flow-go/module/id"
"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/state_synchronization"
"github.com/onflow/flow-go/module/state_synchronization/indexer"
edrequester "github.com/onflow/flow-go/module/state_synchronization/requester"
consensus_follower "github.com/onflow/flow-go/module/upstream"
"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/channels"
"github.com/onflow/flow-go/network/converter"
"github.com/onflow/flow-go/network/p2p"
"github.com/onflow/flow-go/network/p2p/blob"
p2pbuilder "github.com/onflow/flow-go/network/p2p/builder"
p2pbuilderconfig "github.com/onflow/flow-go/network/p2p/builder/config"
"github.com/onflow/flow-go/network/p2p/cache"
"github.com/onflow/flow-go/network/p2p/conduit"
p2pdht "github.com/onflow/flow-go/network/p2p/dht"
"github.com/onflow/flow-go/network/p2p/keyutils"
p2plogging "github.com/onflow/flow-go/network/p2p/logging"
"github.com/onflow/flow-go/network/p2p/subscription"
"github.com/onflow/flow-go/network/p2p/translator"
"github.com/onflow/flow-go/network/p2p/unicast/protocols"
"github.com/onflow/flow-go/network/p2p/utils"
"github.com/onflow/flow-go/network/slashing"
"github.com/onflow/flow-go/network/underlay"
"github.com/onflow/flow-go/network/validator"
stateprotocol "github.com/onflow/flow-go/state/protocol"
badgerState "github.com/onflow/flow-go/state/protocol/badger"
"github.com/onflow/flow-go/state/protocol/blocktimer"
"github.com/onflow/flow-go/state/protocol/events/gadgets"
"github.com/onflow/flow-go/storage"
bstorage "github.com/onflow/flow-go/storage/badger"
pStorage "github.com/onflow/flow-go/storage/pebble"
"github.com/onflow/flow-go/utils/grpcutils"
"github.com/onflow/flow-go/utils/io"
)
// ObserverBuilder extends cmd.NodeBuilder and declares additional functions needed to bootstrap an Access node
// These functions are shared by observer builders.
// The Staked network allows the access nodes to communicate among themselves, while the public network allows the
// observers and an Access node to communicate.
//
// public network private network
// +------------------------+
// | observer 1 |<--------------------------|
// +------------------------+ v
// +------------------------+ +----------------------+ +------------------------+
// | observer 2 |<----------------------->| Access Node (staked) |<------------>| All other staked Nodes |
// +------------------------+ +----------------------+ +------------------------+
// +------------------------+ ^
// | observer 3 |<--------------------------|
// +------------------------+
// ObserverServiceConfig 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 ObserverServiceConfig struct {
bootstrapNodeAddresses []string
bootstrapNodePublicKeys []string
observerNetworkingKeyPath string
bootstrapIdentities flow.IdentityList // the identity list of bootstrap peers the node uses to discover other nodes
apiRatelimits map[string]int
apiBurstlimits map[string]int
rpcConf rpc.Config
rpcMetricsEnabled bool
registersDBPath string
checkpointFile string
apiTimeout time.Duration
stateStreamConf statestreambackend.Config
stateStreamFilterConf map[string]int
upstreamNodeAddresses []string
upstreamNodePublicKeys []string
upstreamIdentities flow.IdentityList // the identity list of upstream peers the node uses to forward API requests to
scriptExecutorConfig query.QueryConfig
executionDataSyncEnabled bool
executionDataIndexingEnabled bool
executionDataDir string
executionDataStartHeight uint64
executionDataConfig edrequester.ExecutionDataConfig
}
// DefaultObserverServiceConfig defines all the default values for the ObserverServiceConfig
func DefaultObserverServiceConfig() *ObserverServiceConfig {
homedir, _ := os.UserHomeDir()
return &ObserverServiceConfig{
rpcConf: rpc.Config{
UnsecureGRPCListenAddr: "0.0.0.0:9000",
SecureGRPCListenAddr: "0.0.0.0:9001",
HTTPListenAddr: "0.0.0.0:8000",
CollectionAddr: "",
HistoricalAccessAddrs: "",
BackendConfig: backend.Config{
CollectionClientTimeout: 3 * time.Second,
ExecutionClientTimeout: 3 * time.Second,
ConnectionPoolSize: backend.DefaultConnectionPoolSize,
MaxHeightRange: backend.DefaultMaxHeightRange,
PreferredExecutionNodeIDs: nil,
FixedExecutionNodeIDs: nil,
},
RestConfig: rest.Config{
ListenAddress: "",
WriteTimeout: rest.DefaultWriteTimeout,
ReadTimeout: rest.DefaultReadTimeout,
IdleTimeout: rest.DefaultIdleTimeout,
},
MaxMsgSize: grpcutils.DefaultMaxMsgSize,
CompressorName: grpcutils.NoCompressor,
},
stateStreamConf: statestreambackend.Config{
MaxExecutionDataMsgSize: grpcutils.DefaultMaxMsgSize,
ExecutionDataCacheSize: state_stream.DefaultCacheSize,
ClientSendTimeout: state_stream.DefaultSendTimeout,
ClientSendBufferSize: state_stream.DefaultSendBufferSize,
MaxGlobalStreams: state_stream.DefaultMaxGlobalStreams,
EventFilterConfig: state_stream.DefaultEventFilterConfig,
ResponseLimit: state_stream.DefaultResponseLimit,
HeartbeatInterval: state_stream.DefaultHeartbeatInterval,
RegisterIDsRequestLimit: state_stream.DefaultRegisterIDsRequestLimit,
},
stateStreamFilterConf: nil,
rpcMetricsEnabled: false,
apiRatelimits: nil,
apiBurstlimits: nil,
bootstrapNodeAddresses: []string{},
bootstrapNodePublicKeys: []string{},
observerNetworkingKeyPath: cmd.NotSet,
apiTimeout: 3 * time.Second,
upstreamNodeAddresses: []string{},
upstreamNodePublicKeys: []string{},
registersDBPath: filepath.Join(homedir, ".flow", "execution_state"),
checkpointFile: cmd.NotSet,
scriptExecutorConfig: query.NewDefaultConfig(),
executionDataSyncEnabled: false,
executionDataIndexingEnabled: false,
executionDataDir: filepath.Join(homedir, ".flow", "execution_data"),
executionDataStartHeight: 0,
executionDataConfig: edrequester.ExecutionDataConfig{
InitialBlockHeight: 0,
MaxSearchAhead: edrequester.DefaultMaxSearchAhead,
FetchTimeout: edrequester.DefaultFetchTimeout,
MaxFetchTimeout: edrequester.DefaultMaxFetchTimeout,
RetryDelay: edrequester.DefaultRetryDelay,
MaxRetryDelay: edrequester.DefaultMaxRetryDelay,
},
}
}
// ObserverServiceBuilder provides the common functionality needed to bootstrap a Flow observer service
// It is composed of the FlowNodeBuilder, the ObserverServiceConfig and contains all the components and modules needed for the observers
type ObserverServiceBuilder struct {
*cmd.FlowNodeBuilder
*ObserverServiceConfig
// components
LibP2PNode p2p.LibP2PNode
FollowerState stateprotocol.FollowerState
SyncCore *chainsync.Core
RpcEng *rpc.Engine
FollowerDistributor *pubsub.FollowerDistributor
Committee hotstuff.DynamicCommittee
Finalized *flow.Header
Pending []*flow.Header
FollowerCore module.HotStuffFollower
ExecutionIndexer *indexer.Indexer
ExecutionIndexerCore *indexer.IndexerCore
IndexerDependencies *cmd.DependencyList
ExecutionDataDownloader execution_data.Downloader
ExecutionDataRequester state_synchronization.ExecutionDataRequester
ExecutionDataStore execution_data.ExecutionDataStore
RegistersAsyncStore *execution.RegistersAsyncStore
EventsIndex *backend.EventsIndex
// available until after the network has started. Hence, a factory function that needs to be called just before
// creating the sync engine
SyncEngineParticipantsProviderFactory func() module.IdentifierProvider
// engines
FollowerEng *follower.ComplianceEngine
SyncEng *synceng.Engine
StateStreamEng *statestreambackend.Engine
// Public network
peerID peer.ID
RestMetrics *metrics.RestCollector
AccessMetrics module.AccessMetrics
// grpc servers
secureGrpcServer *grpcserver.GrpcServer
unsecureGrpcServer *grpcserver.GrpcServer
stateStreamGrpcServer *grpcserver.GrpcServer
stateStreamBackend *statestreambackend.StateStreamBackend
}
// deriveBootstrapPeerIdentities derives the Flow Identity of the bootstrap peers from the parameters.
// These are the identities of the observers also acting as the DHT bootstrap server
func (builder *ObserverServiceBuilder) deriveBootstrapPeerIdentities() error {
// if bootstrap identities already provided (as part of alternate initialization as a library the skip reading command
// line params)
if builder.bootstrapIdentities != nil {
return nil
}
ids, err := cmd.BootstrapIdentities(builder.bootstrapNodeAddresses, builder.bootstrapNodePublicKeys)
if err != nil {
return fmt.Errorf("failed to derive bootstrap peer identities: %w", err)
}
builder.bootstrapIdentities = ids
return nil
}
// deriveBootstrapPeerIdentities derives the Flow Identity of the bootstrap peers from the parameters.
// These are the identities of the observers also acting as the DHT bootstrap server
func (builder *ObserverServiceBuilder) deriveUpstreamIdentities() error {
// if bootstrap identities already provided (as part of alternate initialization as a library the skip reading command
// line params)
if builder.upstreamIdentities != nil {
return nil
}
// 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.
addresses := builder.upstreamNodeAddresses
keys := builder.upstreamNodePublicKeys
if len(addresses) != len(keys) {
return 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)
}
// create the identity of the peer by setting only the relevant fields
ids[i] = &flow.Identity{
NodeID: flow.ZeroID, // the NodeID is the hash of the staking key and for the public network it does not apply
Address: address,
Role: flow.RoleAccess, // the upstream node has to be an access node
NetworkPubKey: nil,
}
// networking public key
var networkKey encodable.NetworkPubKey
err := json.Unmarshal([]byte(key), &networkKey)
if err == nil {
ids[i].NetworkPubKey = networkKey
}
}
builder.upstreamIdentities = ids
return nil
}
func (builder *ObserverServiceBuilder) buildFollowerState() *ObserverServiceBuilder {
builder.Module("mutable follower state", func(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 currently supported but read-only state has type %T", node.State)
}
followerState, err := badgerState.NewFollowerState(
node.Logger,
node.Tracer,
node.ProtocolEvents,
state,
node.Storage.Index,
node.Storage.Payloads,
blocktimer.DefaultBlockTimer,
)
builder.FollowerState = followerState
return err
})
return builder
}
func (builder *ObserverServiceBuilder) buildSyncCore() *ObserverServiceBuilder {
builder.Module("sync core", func(node *cmd.NodeConfig) error {
syncCore, err := chainsync.New(node.Logger, node.SyncCoreConfig, metrics.NewChainSyncCollector(node.RootChainID), node.RootChainID)
builder.SyncCore = syncCore
return err
})
return builder
}
func (builder *ObserverServiceBuilder) buildCommittee() *ObserverServiceBuilder {
builder.Component("committee", func(node *cmd.NodeConfig) (module.ReadyDoneAware, 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())
node.ProtocolEvents.AddConsumer(committee)
builder.Committee = committee
return committee, err
})
return builder
}
func (builder *ObserverServiceBuilder) buildLatestHeader() *ObserverServiceBuilder {
builder.Module("latest header", func(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 *ObserverServiceBuilder) buildFollowerCore() *ObserverServiceBuilder {
builder.Component("follower core", func(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, node.Tracer)
followerCore, err := consensus.NewFollower(
node.Logger,
node.Metrics.Mempool,
node.Storage.Headers,
final,
builder.FollowerDistributor,
node.FinalizedRootBlock.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 *ObserverServiceBuilder) buildFollowerEngine() *ObserverServiceBuilder {
builder.Component("follower engine", func(node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
var heroCacheCollector module.HeroCacheMetrics = metrics.NewNoopCollector()
if node.HeroCacheMetricsEnable {
heroCacheCollector = metrics.FollowerCacheMetrics(node.MetricsRegisterer)
}
packer := hotsignature.NewConsensusSigDataPacker(builder.Committee)
verifier := verification.NewCombinedVerifier(builder.Committee, packer) // verifier for HotStuff signature constructs (QCs, TCs, votes)
val := hotstuffvalidator.New(builder.Committee, verifier)
core, err := follower.NewComplianceCore(
node.Logger,
node.Metrics.Mempool,
heroCacheCollector,
builder.FollowerDistributor,
builder.FollowerState,
builder.FollowerCore,
val,
builder.SyncCore,
node.Tracer,
)
if err != nil {
return nil, fmt.Errorf("could not create follower core: %w", err)
}
builder.FollowerEng, err = follower.NewComplianceLayer(
node.Logger,
node.EngineRegistry,
node.Me,
node.Metrics.Engine,
node.Storage.Headers,
builder.Finalized,
core,
builder.ComplianceConfig,
follower.WithChannel(channels.PublicReceiveBlocks),
)
if err != nil {
return nil, fmt.Errorf("could not create follower engine: %w", err)
}
builder.FollowerDistributor.AddOnBlockFinalizedConsumer(builder.FollowerEng.OnFinalizedBlock)
return builder.FollowerEng, nil
})
return builder
}
func (builder *ObserverServiceBuilder) buildSyncEngine() *ObserverServiceBuilder {
builder.Component("sync engine", func(node *cmd.NodeConfig) (module.ReadyDoneAware, error) {
spamConfig, err := synceng.NewSpamDetectionConfig()
if err != nil {
return nil, fmt.Errorf("could not initialize spam detection config: %w", err)
}
sync, err := synceng.New(
node.Logger,
node.Metrics.Engine,
node.EngineRegistry,
node.Me,
node.State,
node.Storage.Blocks,
builder.FollowerEng,
builder.SyncCore,
builder.SyncEngineParticipantsProviderFactory(),
spamConfig,
)
if err != nil {
return nil, fmt.Errorf("could not create synchronization engine: %w", err)
}
builder.SyncEng = sync
builder.FollowerDistributor.AddFinalizationConsumer(sync)
return builder.SyncEng, nil
})
return builder
}
func (builder *ObserverServiceBuilder) BuildConsensusFollower() cmd.NodeBuilder {
builder.
buildFollowerState().
buildSyncCore().
buildCommittee().
buildLatestHeader().
buildFollowerCore().
buildFollowerEngine().
buildSyncEngine()
return builder
}
type Option func(*ObserverServiceConfig)
func NewFlowObserverServiceBuilder(opts ...Option) *ObserverServiceBuilder {
config := DefaultObserverServiceConfig()
for _, opt := range opts {
opt(config)
}
anb := &ObserverServiceBuilder{
ObserverServiceConfig: config,
FlowNodeBuilder: cmd.FlowNode("observer"),
FollowerDistributor: pubsub.NewFollowerDistributor(),
IndexerDependencies: cmd.NewDependencyList(),
}
anb.FollowerDistributor.AddProposalViolationConsumer(notifications.NewSlashingViolationsConsumer(anb.Logger))
// the observer gets a version of the root snapshot file that does not contain any node addresses
// hence skip all the root snapshot validations that involved an identity address
anb.FlowNodeBuilder.SkipNwAddressBasedValidations = true
return anb
}
func (builder *ObserverServiceBuilder) ParseFlags() error {
builder.BaseFlags()
builder.extraFlags()
return builder.ParseAndPrintFlags()
}
func (builder *ObserverServiceBuilder) extraFlags() {
builder.ExtraFlags(func(flags *pflag.FlagSet) {
defaultConfig := DefaultObserverServiceConfig()
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.StringVar(&builder.rpcConf.RestConfig.ListenAddress,
"rest-addr",
defaultConfig.rpcConf.RestConfig.ListenAddress,
"the address the REST server listens on (if empty the REST server will not be started)")
flags.DurationVar(&builder.rpcConf.RestConfig.WriteTimeout,
"rest-write-timeout",
defaultConfig.rpcConf.RestConfig.WriteTimeout,
"timeout to use when writing REST response")
flags.DurationVar(&builder.rpcConf.RestConfig.ReadTimeout,
"rest-read-timeout",
defaultConfig.rpcConf.RestConfig.ReadTimeout,
"timeout to use when reading REST request headers")
flags.DurationVar(&builder.rpcConf.RestConfig.IdleTimeout, "rest-idle-timeout", defaultConfig.rpcConf.RestConfig.IdleTimeout, "idle timeout for REST connections")
flags.UintVar(&builder.rpcConf.MaxMsgSize,
"rpc-max-message-size",
defaultConfig.rpcConf.MaxMsgSize,
"the maximum message size in bytes for messages sent or received over grpc")
flags.UintVar(&builder.rpcConf.BackendConfig.ConnectionPoolSize,
"connection-pool-size",
defaultConfig.rpcConf.BackendConfig.ConnectionPoolSize,
"maximum number of connections allowed in the connection pool, size of 0 disables the connection pooling, and anything less than the default size will be overridden to use the default size")
flags.UintVar(&builder.rpcConf.BackendConfig.MaxHeightRange,
"rpc-max-height-range",
defaultConfig.rpcConf.BackendConfig.MaxHeightRange,
"maximum size for height range requests")
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.StringVar(&builder.observerNetworkingKeyPath,
"observer-networking-key-path",
defaultConfig.observerNetworkingKeyPath,
"path to the networking key for observer")
flags.StringSliceVar(&builder.bootstrapNodeAddresses,
"bootstrap-node-addresses",
defaultConfig.bootstrapNodeAddresses,
"the network addresses of the bootstrap access node if this is an observer 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 observer (in the same order as the bootstrap node addresses) e.g. \"d57a5e9c5.....\",\"44ded42d....\"")
flags.DurationVar(&builder.apiTimeout, "upstream-api-timeout", defaultConfig.apiTimeout, "tcp timeout for Flow API gRPC sockets to upstrem nodes")
flags.StringSliceVar(&builder.upstreamNodeAddresses,
"upstream-node-addresses",
defaultConfig.upstreamNodeAddresses,
"the gRPC network addresses of the upstream access node. e.g. access-001.mainnet.flow.org:9000,access-002.mainnet.flow.org:9000")
flags.StringSliceVar(&builder.upstreamNodePublicKeys,
"upstream-node-public-keys",
defaultConfig.upstreamNodePublicKeys,
"the networking public key of the upstream access node (in the same order as the upstream node addresses) e.g. \"d57a5e9c5.....\",\"44ded42d....\"")
flags.BoolVar(&builder.rpcMetricsEnabled, "rpc-metrics-enabled", defaultConfig.rpcMetricsEnabled, "whether to enable the rpc metrics")
flags.BoolVar(&builder.executionDataIndexingEnabled,
"execution-data-indexing-enabled",
defaultConfig.executionDataIndexingEnabled,
"whether to enable the execution data indexing")
flags.StringVar(&builder.registersDBPath, "execution-state-dir", defaultConfig.registersDBPath, "directory to use for execution-state database")
flags.StringVar(&builder.checkpointFile, "execution-state-checkpoint", defaultConfig.checkpointFile, "execution-state checkpoint file")
// ExecutionDataRequester config
flags.BoolVar(&builder.executionDataSyncEnabled,
"execution-data-sync-enabled",
defaultConfig.executionDataSyncEnabled,
"whether to enable the execution data sync protocol")
flags.StringVar(&builder.executionDataDir,
"execution-data-dir",
defaultConfig.executionDataDir,
"directory to use for Execution Data database")
flags.Uint64Var(&builder.executionDataStartHeight,
"execution-data-start-height",
defaultConfig.executionDataStartHeight,
"height of first block to sync execution data from when starting with an empty Execution Data database")
flags.Uint64Var(&builder.executionDataConfig.MaxSearchAhead,
"execution-data-max-search-ahead",
defaultConfig.executionDataConfig.MaxSearchAhead,
"max number of heights to search ahead of the lowest outstanding execution data height")
flags.DurationVar(&builder.executionDataConfig.FetchTimeout,
"execution-data-fetch-timeout",
defaultConfig.executionDataConfig.FetchTimeout,
"initial timeout to use when fetching execution data from the network. timeout increases using an incremental backoff until execution-data-max-fetch-timeout. e.g. 30s")
flags.DurationVar(&builder.executionDataConfig.MaxFetchTimeout,
"execution-data-max-fetch-timeout",
defaultConfig.executionDataConfig.MaxFetchTimeout,
"maximum timeout to use when fetching execution data from the network e.g. 300s")
flags.DurationVar(&builder.executionDataConfig.RetryDelay,
"execution-data-retry-delay",
defaultConfig.executionDataConfig.RetryDelay,
"initial delay for exponential backoff when fetching execution data fails e.g. 10s")
flags.DurationVar(&builder.executionDataConfig.MaxRetryDelay,
"execution-data-max-retry-delay",
defaultConfig.executionDataConfig.MaxRetryDelay,
"maximum delay for exponential backoff when fetching execution data fails e.g. 5m")
// Streaming API
flags.StringVar(&builder.stateStreamConf.ListenAddr,
"state-stream-addr",
defaultConfig.stateStreamConf.ListenAddr,
"the address the state stream server listens on (if empty the server will not be started)")
flags.Uint32Var(&builder.stateStreamConf.ExecutionDataCacheSize,
"execution-data-cache-size",
defaultConfig.stateStreamConf.ExecutionDataCacheSize,
"block execution data cache size")
flags.Uint32Var(&builder.stateStreamConf.MaxGlobalStreams,
"state-stream-global-max-streams", defaultConfig.stateStreamConf.MaxGlobalStreams,
"global maximum number of concurrent streams")
flags.UintVar(&builder.stateStreamConf.MaxExecutionDataMsgSize,
"state-stream-max-message-size",
defaultConfig.stateStreamConf.MaxExecutionDataMsgSize,
"maximum size for a gRPC message containing block execution data")
flags.StringToIntVar(&builder.stateStreamFilterConf,
"state-stream-event-filter-limits",
defaultConfig.stateStreamFilterConf,
"event filter limits for ExecutionData SubscribeEvents API e.g. EventTypes=100,Addresses=100,Contracts=100 etc.")
flags.DurationVar(&builder.stateStreamConf.ClientSendTimeout,
"state-stream-send-timeout",
defaultConfig.stateStreamConf.ClientSendTimeout,
"maximum wait before timing out while sending a response to a streaming client e.g. 30s")
flags.UintVar(&builder.stateStreamConf.ClientSendBufferSize,
"state-stream-send-buffer-size",
defaultConfig.stateStreamConf.ClientSendBufferSize,
"maximum number of responses to buffer within a stream")
flags.Float64Var(&builder.stateStreamConf.ResponseLimit,
"state-stream-response-limit",
defaultConfig.stateStreamConf.ResponseLimit,
"max number of responses per second to send over streaming endpoints. this helps manage resources consumed by each client querying data not in the cache e.g. 3 or 0.5. 0 means no limit")
flags.Uint64Var(&builder.stateStreamConf.HeartbeatInterval,
"state-stream-heartbeat-interval",
defaultConfig.stateStreamConf.HeartbeatInterval,
"default interval in blocks at which heartbeat messages should be sent. applied when client did not specify a value.")
flags.Uint32Var(&builder.stateStreamConf.RegisterIDsRequestLimit,
"state-stream-max-register-values",
defaultConfig.stateStreamConf.RegisterIDsRequestLimit,
"maximum number of register ids to include in a single request to the GetRegisters endpoint")
flags.StringVar(&builder.rpcConf.BackendConfig.EventQueryMode,
"event-query-mode",
defaultConfig.rpcConf.BackendConfig.EventQueryMode,
"mode to use when querying events. one of [local-only, execution-nodes-only(default), failover]")
}).ValidateFlags(func() error {
if builder.executionDataSyncEnabled {
if builder.executionDataConfig.FetchTimeout <= 0 {
return errors.New("execution-data-fetch-timeout must be greater than 0")
}
if builder.executionDataConfig.MaxFetchTimeout < builder.executionDataConfig.FetchTimeout {
return errors.New("execution-data-max-fetch-timeout must be greater than execution-data-fetch-timeout")
}
if builder.executionDataConfig.RetryDelay <= 0 {
return errors.New("execution-data-retry-delay must be greater than 0")
}
if builder.executionDataConfig.MaxRetryDelay < builder.executionDataConfig.RetryDelay {
return errors.New("execution-data-max-retry-delay must be greater than or equal to execution-data-retry-delay")
}
if builder.executionDataConfig.MaxSearchAhead == 0 {
return errors.New("execution-data-max-search-ahead must be greater than 0")
}
}
if builder.stateStreamConf.ListenAddr != "" {
if builder.stateStreamConf.ExecutionDataCacheSize == 0 {
return errors.New("execution-data-cache-size must be greater than 0")
}
if builder.stateStreamConf.ClientSendBufferSize == 0 {
return errors.New("state-stream-send-buffer-size must be greater than 0")
}
if len(builder.stateStreamFilterConf) > 3 {
return errors.New("state-stream-event-filter-limits must have at most 3 keys (EventTypes, Addresses, Contracts)")
}
for key, value := range builder.stateStreamFilterConf {
switch key {
case "EventTypes", "Addresses", "Contracts":
if value <= 0 {
return fmt.Errorf("state-stream-event-filter-limits %s must be greater than 0", key)
}
default:
return errors.New("state-stream-event-filter-limits may only contain the keys EventTypes, Addresses, Contracts")
}
}
if builder.stateStreamConf.ResponseLimit < 0 {
return errors.New("state-stream-response-limit must be greater than or equal to 0")
}
if builder.stateStreamConf.RegisterIDsRequestLimit <= 0 {
return errors.New("state-stream-max-register-values must be greater than 0")
}
}
return nil
})
}
func publicNetworkMsgValidators(log zerolog.Logger, idProvider module.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.NewIdentityFilterIdentifierProvider(filter.IsValidCurrentEpochParticipant, idProvider),
),
// or the message should be specifically targeted for this node
validator.ValidateTarget(log, selfID),
),
}
}
func (builder *ObserverServiceBuilder) initNodeInfo() error {
// use the networking key that was loaded from the configured file
networkingKey, err := loadNetworkingKey(builder.observerNetworkingKeyPath)
if err != nil {
return fmt.Errorf("could not load networking private key: %w", err)
}
pubKey, err := keyutils.LibP2PPublicKeyFromFlow(networkingKey.PublicKey())
if err != nil {
return fmt.Errorf("could not load networking public key: %w", err)
}
builder.peerID, err = peer.IDFromPublicKey(pubKey)
if err != nil {
return fmt.Errorf("could not get peer ID from public key: %w", err)
}
builder.NodeID, err = translator.NewPublicNetworkIDTranslator().GetFlowID(builder.peerID)
if err != nil {
return fmt.Errorf("could not get flow node ID: %w", err)
}
builder.NodeConfig.NetworkKey = networkingKey // copy the key to NodeConfig
builder.NodeConfig.StakingKey = nil // no staking key for the observer
return nil
}
func (builder *ObserverServiceBuilder) InitIDProviders() {
builder.Module("id providers", func(node *cmd.NodeConfig) error {
idCache, err := cache.NewProtocolStateIDCache(node.Logger, node.State, builder.ProtocolEvents)
if err != nil {
return fmt.Errorf("could not initialize ProtocolStateIDCache: %w", err)
}
builder.IDTranslator = translator.NewHierarchicalIDTranslator(idCache, translator.NewPublicNetworkIDTranslator())
// The following wrapper allows to black-list byzantine nodes via an admin command:
// the wrapper overrides the 'Ejected' flag of disallow-listed nodes to true
builder.IdentityProvider, err = cache.NewNodeDisallowListWrapper(idCache, node.DB, func() network.DisallowListNotificationConsumer {
return builder.NetworkUnderlay
})
if err != nil {
return fmt.Errorf("could not initialize NodeBlockListWrapper: %w", err)
}
// use the default identifier provider
builder.SyncEngineParticipantsProviderFactory = func() module.IdentifierProvider {
return id.NewCustomIdentifierProvider(func() flow.IdentifierList {
pids := builder.LibP2PNode.GetPeersForProtocol(protocols.FlowProtocolID(builder.SporkID))
result := make(flow.IdentifierList, 0, len(pids))
for _, pid := range pids {
// exclude own Identifier
if pid == builder.peerID {
continue
}
if flowID, err := builder.IDTranslator.GetFlowID(pid); err != nil {
// TODO: this is an instance of "log error and continue with best effort" anti-pattern
builder.Logger.Err(err).Str("peer", p2plogging.PeerId(pid)).Msg("failed to translate to Flow ID")
} else {
result = append(result, flowID)
}
}
return result
})
}
return nil
})
}
func (builder *ObserverServiceBuilder) Initialize() error {
if err := builder.deriveBootstrapPeerIdentities(); err != nil {
return err
}
if err := builder.deriveUpstreamIdentities(); err != nil {
return err
}
if err := builder.validateParams(); err != nil {
return err
}
if err := builder.initNodeInfo(); err != nil {
return err
}
builder.InitIDProviders()
builder.enqueuePublicNetworkInit()
builder.enqueueConnectWithStakedAN()
if builder.executionDataSyncEnabled {
builder.BuildExecutionSyncComponents()
}
builder.enqueueRPCServer()
if builder.BaseConfig.MetricsEnabled {
builder.EnqueueMetricsServerInit()
if err := builder.RegisterBadgerMetrics(); err != nil {
return err
}
}
builder.PreInit(builder.initObserverLocal())
return nil
}
func (builder *ObserverServiceBuilder) validateParams() error {
if builder.BaseConfig.BindAddr == cmd.NotSet || builder.BaseConfig.BindAddr == "" {
return errors.New("bind address not specified")
}
if builder.ObserverServiceConfig.observerNetworkingKeyPath == cmd.NotSet {
return errors.New("networking key not provided")
}
if len(builder.bootstrapIdentities) > 0 {
return nil
}
if len(builder.bootstrapNodeAddresses) == 0 {
return errors.New("no bootstrap node address provided")
}
if len(builder.bootstrapNodeAddresses) != len(builder.bootstrapNodePublicKeys) {
return errors.New("number of bootstrap node addresses and public keys should match")
}
if len(builder.upstreamNodePublicKeys) > 0 && len(builder.upstreamNodeAddresses) != len(builder.upstreamNodePublicKeys) {
return errors.New("number of upstream node addresses and public keys must match if public keys given")
}
return nil
}
// initPublicLibp2pNode creates a libp2p node for the observer service in the public (unstaked) network.
// The factory function is later passed into the initMiddleware function to eventually instantiate the p2p.LibP2PNode instance
// The LibP2P host is created with the following options:
// * DHT as client and seeded with the given bootstrap peers
// * The specified bind address as the listen address
// * The passed in private key as the libp2p key
// * No connection gater
// * No connection manager
// * No peer manager
// * Default libp2p pubsub options.
// Args:
// - networkKey: the private key to use for the libp2p node
// Returns:
// - p2p.LibP2PNode: the libp2p node
// - error: if any error occurs. Any error returned is considered irrecoverable.
func (builder *ObserverServiceBuilder) initPublicLibp2pNode(networkKey crypto.PrivateKey) (p2p.LibP2PNode, error) {
var pis []peer.AddrInfo
for _, b := range builder.bootstrapIdentities {
pi, err := utils.PeerAddressInfo(*b)
if err != nil {
return nil, fmt.Errorf("could not extract peer address info from bootstrap identity %v: %w", b, err)
}
pis = append(pis, pi)
}
node, err := p2pbuilder.NewNodeBuilder(
builder.Logger,
&builder.FlowConfig.NetworkConfig.GossipSub,
&p2pbuilderconfig.MetricsConfig{
HeroCacheFactory: builder.HeroCacheMetricsFactory(),
Metrics: builder.Metrics.Network,
},
network.PublicNetwork,
builder.BaseConfig.BindAddr,
networkKey,
builder.SporkID,
builder.IdentityProvider,
&builder.FlowConfig.NetworkConfig.ResourceManager,
p2pbuilderconfig.PeerManagerDisableConfig(), // disable peer manager for observer node.
&p2p.DisallowListCacheConfig{
MaxSize: builder.FlowConfig.NetworkConfig.DisallowListNotificationCacheSize,
Metrics: metrics.DisallowListCacheMetricsFactory(builder.HeroCacheMetricsFactory(), network.PublicNetwork),
},
&p2pbuilderconfig.UnicastConfig{
Unicast: builder.FlowConfig.NetworkConfig.Unicast,
}).
SetSubscriptionFilter(
subscription.NewRoleBasedFilter(
subscription.UnstakedRole, builder.IdentityProvider,
),
).
SetRoutingSystem(func(ctx context.Context, h host.Host) (routing.Routing, error) {
return p2pdht.NewDHT(ctx, h, protocols.FlowPublicDHTProtocolID(builder.SporkID),
builder.Logger,
builder.Metrics.Network,
p2pdht.AsClient(),
dht.BootstrapPeers(pis...),
)
}).
Build()
if err != nil {
return nil, fmt.Errorf("could not initialize libp2p node for observer: %w", err)
}
builder.LibP2PNode = node
return builder.LibP2PNode, nil
}
// initObserverLocal initializes the observer's ID, network key and network address
// Currently, it reads a node-info.priv.json like any other node.
// TODO: read the node ID from the special bootstrap files
func (builder *ObserverServiceBuilder) initObserverLocal() func(node *cmd.NodeConfig) error {
return func(node *cmd.NodeConfig) error {
// for an observer, set the identity here explicitly since it will not be found in the protocol state
self := &flow.Identity{
NodeID: node.NodeID,
NetworkPubKey: node.NetworkKey.PublicKey(),
StakingPubKey: nil, // no staking key needed for the observer
Role: flow.RoleAccess, // observer can only run as an access node
Address: builder.BindAddr,
}
var err error
node.Me, err = local.NewNoKey(self)
if err != nil {
return fmt.Errorf("could not initialize local: %w", err)
}
return nil
}
}