-
Notifications
You must be signed in to change notification settings - Fork 178
/
utils.go
80 lines (69 loc) · 2.81 KB
/
utils.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
package cmd
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool/queue"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/network/p2p"
"github.com/onflow/flow-go/network/p2p/distributor"
"github.com/onflow/flow-go/state/protocol/inmem"
"github.com/onflow/flow-go/utils/io"
)
// loadRootProtocolSnapshot loads the root protocol snapshot from disk
func loadRootProtocolSnapshot(dir string) (*inmem.Snapshot, error) {
path := filepath.Join(dir, bootstrap.PathRootProtocolStateSnapshot)
data, err := io.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read root snapshot (path=%s): %w", path, err)
}
var snapshot inmem.EncodableSnapshot
err = json.Unmarshal(data, &snapshot)
if err != nil {
return nil, err
}
return inmem.SnapshotFromEncodable(snapshot), nil
}
// LoadPrivateNodeInfo the private info for this node from disk (e.g., private staking/network keys).
func LoadPrivateNodeInfo(dir string, myID flow.Identifier) (*bootstrap.NodeInfoPriv, error) {
path := filepath.Join(dir, fmt.Sprintf(bootstrap.PathNodeInfoPriv, myID))
data, err := io.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read private node info (path=%s): %w", path, err)
}
var info bootstrap.NodeInfoPriv
err = json.Unmarshal(data, &info)
return &info, err
}
// loadSecretsEncryptionKey loads the encryption key for the secrets database.
// If the file does not exist, returns os.ErrNotExist.
func loadSecretsEncryptionKey(dir string, myID flow.Identifier) ([]byte, error) {
path := filepath.Join(dir, fmt.Sprintf(bootstrap.PathSecretsEncryptionKey, myID))
data, err := io.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not read secrets db encryption key (path=%s): %w", path, err)
}
return data, nil
}
func rateLimiterPeerFilter(rateLimiter p2p.RateLimiter) p2p.PeerFilter {
return func(p peer.ID) error {
if rateLimiter.IsRateLimited(p) {
return fmt.Errorf("peer is rate limited")
}
return nil
}
}
// BuildDisallowListNotificationDisseminator builds the disallow list notification distributor.
func BuildDisallowListNotificationDisseminator(size uint32, metricsRegistry prometheus.Registerer, logger zerolog.Logger, metricsEnabled bool) p2p.DisallowListNotificationDistributor {
heroStoreOpts := []queue.HeroStoreConfigOption{queue.WithHeroStoreSizeLimit(size)}
if metricsEnabled {
collector := metrics.DisallowListNotificationQueueMetricFactory(metricsRegistry)
heroStoreOpts = append(heroStoreOpts, queue.WithHeroStoreCollector(collector))
}
return distributor.DefaultDisallowListNotificationDistributor(logger, heroStoreOpts...)
}