-
Notifications
You must be signed in to change notification settings - Fork 886
/
config.go
124 lines (105 loc) · 3.19 KB
/
config.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
package header
import (
"encoding/hex"
"fmt"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
libhead "github.com/celestiaorg/go-header"
p2p_exchange "github.com/celestiaorg/go-header/p2p"
"github.com/celestiaorg/go-header/store"
"github.com/celestiaorg/go-header/sync"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)
// MetricsEnabled will be set during runtime if metrics are enabled on the node.
var MetricsEnabled = false
// Config contains configuration parameters for header retrieval and management.
type Config struct {
// TrustedHash is the Block/Header hash that Nodes use as starting point for header synchronization.
// Only affects the node once on initial sync.
TrustedHash string
// TrustedPeers are the peers we trust to fetch headers from.
// Note: The trusted does *not* imply Headers are not verified, but trusted as reliable to fetch
// headers at any moment.
TrustedPeers []string
Store store.Parameters
Syncer sync.Parameters
Server p2p_exchange.ServerParameters
Client p2p_exchange.ClientParameters `toml:",omitempty"`
}
func DefaultConfig(tp node.Type) Config {
cfg := Config{
TrustedHash: "",
TrustedPeers: make([]string, 0),
Store: store.DefaultParameters(),
Syncer: sync.DefaultParameters(),
Server: p2p_exchange.DefaultServerParameters(),
Client: p2p_exchange.DefaultClientParameters(),
}
switch tp {
case node.Bridge:
return cfg
case node.Full:
return cfg
case node.Light:
cfg.Store.StoreCacheSize = 512
cfg.Store.IndexCacheSize = 2048
cfg.Store.WriteBatchSize = 512
return cfg
default:
panic("header: invalid node type")
}
}
func (cfg *Config) trustedPeers(bpeers p2p.Bootstrappers) (infos []peer.AddrInfo, err error) {
if len(cfg.TrustedPeers) == 0 {
log.Infof("No trusted peers in config, initializing with default bootstrappers as trusted peers")
return bpeers, nil
}
infos = make([]peer.AddrInfo, len(cfg.TrustedPeers))
for i, tpeer := range cfg.TrustedPeers {
ma, err := multiaddr.NewMultiaddr(tpeer)
if err != nil {
return nil, err
}
p, err := peer.AddrInfoFromP2pAddr(ma)
if err != nil {
return nil, err
}
infos[i] = *p
}
return
}
func (cfg *Config) trustedHash(net p2p.Network) (libhead.Hash, error) {
if cfg.TrustedHash == "" {
gen, err := p2p.GenesisFor(net)
if err != nil {
return nil, err
}
return hex.DecodeString(gen)
}
return hex.DecodeString(cfg.TrustedHash)
}
// Validate performs basic validation of the config.
func (cfg *Config) Validate(tp node.Type) error {
err := cfg.Store.Validate()
if err != nil {
return fmt.Errorf("module/header: misconfiguration of store: %w", err)
}
err = cfg.Syncer.Validate()
if err != nil {
return fmt.Errorf("module/header: misconfiguration of syncer: %w", err)
}
err = cfg.Server.Validate()
if err != nil {
return fmt.Errorf("module/header: misconfiguration of p2p exchange server: %w", err)
}
// we do not create a client for bridge nodes
if tp == node.Bridge {
return nil
}
err = cfg.Client.Validate()
if err != nil {
return fmt.Errorf("module/header: misconfiguration of p2p exchange client: %w", err)
}
return nil
}