github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/p2p/misc.go (about) 1 package p2p 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/ipfs/go-datastore" 8 connmgri "github.com/libp2p/go-libp2p/core/connmgr" 9 "github.com/libp2p/go-libp2p/core/peerstore" 10 "github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds" //nolint:staticcheck 11 "github.com/libp2p/go-libp2p/p2p/net/conngater" 12 "github.com/libp2p/go-libp2p/p2p/net/connmgr" 13 14 "github.com/celestiaorg/celestia-node/nodebuilder/node" 15 ) 16 17 // connManagerConfig configures connection manager. 18 type connManagerConfig struct { 19 // Low and High are watermarks governing the number of connections that'll be maintained. 20 Low, High int 21 // GracePeriod is the amount of time a newly opened connection is given before it becomes subject 22 // to pruning. 23 GracePeriod time.Duration 24 } 25 26 // defaultConnManagerConfig returns defaults for ConnManagerConfig. 27 func defaultConnManagerConfig(tp node.Type) connManagerConfig { 28 switch tp { 29 case node.Light: 30 return connManagerConfig{ 31 Low: 50, 32 High: 100, 33 GracePeriod: time.Minute, 34 } 35 case node.Bridge, node.Full: 36 return connManagerConfig{ 37 Low: 800, 38 High: 1000, 39 GracePeriod: time.Minute, 40 } 41 default: 42 panic("unknown node type") 43 } 44 } 45 46 // connectionManager provides a constructor for ConnectionManager. 47 func connectionManager(cfg Config, bpeers Bootstrappers) (connmgri.ConnManager, error) { 48 fpeers, err := cfg.mutualPeers() 49 if err != nil { 50 return nil, err 51 } 52 cm, err := connmgr.NewConnManager( 53 cfg.ConnManager.Low, 54 cfg.ConnManager.High, 55 connmgr.WithGracePeriod(cfg.ConnManager.GracePeriod), 56 ) 57 if err != nil { 58 return nil, err 59 } 60 for _, info := range fpeers { 61 cm.Protect(info.ID, "protected-mutual") 62 } 63 for _, info := range bpeers { 64 cm.Protect(info.ID, "protected-bootstrap") 65 } 66 67 return cm, nil 68 } 69 70 // connectionGater constructs a BasicConnectionGater. 71 func connectionGater(ds datastore.Batching) (*conngater.BasicConnectionGater, error) { 72 return conngater.NewBasicConnectionGater(ds) 73 } 74 75 // peerStore constructs an on-disk PeerStore. 76 func peerStore(ctx context.Context, ds datastore.Batching) (peerstore.Peerstore, error) { 77 return pstoreds.NewPeerstore(ctx, ds, pstoreds.DefaultOpts()) 78 }