github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/header/config.go (about) 1 package header 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 7 "github.com/libp2p/go-libp2p/core/peer" 8 "github.com/multiformats/go-multiaddr" 9 10 libhead "github.com/celestiaorg/go-header" 11 p2p_exchange "github.com/celestiaorg/go-header/p2p" 12 "github.com/celestiaorg/go-header/store" 13 "github.com/celestiaorg/go-header/sync" 14 15 "github.com/celestiaorg/celestia-node/nodebuilder/node" 16 "github.com/celestiaorg/celestia-node/nodebuilder/p2p" 17 ) 18 19 // MetricsEnabled will be set during runtime if metrics are enabled on the node. 20 var MetricsEnabled = false 21 22 // Config contains configuration parameters for header retrieval and management. 23 type Config struct { 24 // TrustedHash is the Block/Header hash that Nodes use as starting point for header synchronization. 25 // Only affects the node once on initial sync. 26 TrustedHash string 27 // TrustedPeers are the peers we trust to fetch headers from. 28 // Note: The trusted does *not* imply Headers are not verified, but trusted as reliable to fetch 29 // headers at any moment. 30 TrustedPeers []string 31 32 Store store.Parameters 33 Syncer sync.Parameters 34 35 Server p2p_exchange.ServerParameters 36 Client p2p_exchange.ClientParameters `toml:",omitempty"` 37 } 38 39 func DefaultConfig(tp node.Type) Config { 40 cfg := Config{ 41 TrustedHash: "", 42 TrustedPeers: make([]string, 0), 43 Store: store.DefaultParameters(), 44 Syncer: sync.DefaultParameters(), 45 Server: p2p_exchange.DefaultServerParameters(), 46 Client: p2p_exchange.DefaultClientParameters(), 47 } 48 49 switch tp { 50 case node.Bridge: 51 return cfg 52 case node.Full: 53 return cfg 54 case node.Light: 55 cfg.Store.StoreCacheSize = 512 56 cfg.Store.IndexCacheSize = 2048 57 cfg.Store.WriteBatchSize = 512 58 return cfg 59 default: 60 panic("header: invalid node type") 61 } 62 } 63 64 func (cfg *Config) trustedPeers(bpeers p2p.Bootstrappers) (infos []peer.AddrInfo, err error) { 65 if len(cfg.TrustedPeers) == 0 { 66 log.Infof("No trusted peers in config, initializing with default bootstrappers as trusted peers") 67 return bpeers, nil 68 } 69 70 infos = make([]peer.AddrInfo, len(cfg.TrustedPeers)) 71 for i, tpeer := range cfg.TrustedPeers { 72 ma, err := multiaddr.NewMultiaddr(tpeer) 73 if err != nil { 74 return nil, err 75 } 76 p, err := peer.AddrInfoFromP2pAddr(ma) 77 if err != nil { 78 return nil, err 79 } 80 infos[i] = *p 81 } 82 return 83 } 84 85 func (cfg *Config) trustedHash(net p2p.Network) (libhead.Hash, error) { 86 if cfg.TrustedHash == "" { 87 gen, err := p2p.GenesisFor(net) 88 if err != nil { 89 return nil, err 90 } 91 return hex.DecodeString(gen) 92 } 93 return hex.DecodeString(cfg.TrustedHash) 94 } 95 96 // Validate performs basic validation of the config. 97 func (cfg *Config) Validate(tp node.Type) error { 98 err := cfg.Store.Validate() 99 if err != nil { 100 return fmt.Errorf("module/header: misconfiguration of store: %w", err) 101 } 102 103 err = cfg.Syncer.Validate() 104 if err != nil { 105 return fmt.Errorf("module/header: misconfiguration of syncer: %w", err) 106 } 107 108 err = cfg.Server.Validate() 109 if err != nil { 110 return fmt.Errorf("module/header: misconfiguration of p2p exchange server: %w", err) 111 } 112 113 // we do not create a client for bridge nodes 114 if tp == node.Bridge { 115 return nil 116 } 117 118 err = cfg.Client.Validate() 119 if err != nil { 120 return fmt.Errorf("module/header: misconfiguration of p2p exchange client: %w", err) 121 } 122 123 return nil 124 }