Commit d8541735 authored by Janos Guljas's avatar Janos Guljas

add connectivity configuration for libp2p

parent 31381401
...@@ -22,12 +22,15 @@ import ( ...@@ -22,12 +22,15 @@ import (
func (c *command) initStartCmd() (err error) { func (c *command) initStartCmd() (err error) {
const ( const (
optionNameAPIAddr = "api-addr" optionNameAPIAddr = "api-addr"
optionNameP2PAddr = "p2p-addr" optionNameP2PAddr = "p2p-addr"
optionNameP2PDisableWS = "p2p-disable-ws" optionNameP2PDisableWS = "p2p-disable-ws"
optionNameP2PDisableQUIC = "p2p-disable-quic" optionNameP2PDisableQUIC = "p2p-disable-quic"
optionNameDebugAPIAddr = "debug-api-addr" optionNameDebugAPIAddr = "debug-api-addr"
optionNameBootnodes = "bootnode" optionNameBootnodes = "bootnode"
optionNameConnectionsLow = "connections-low"
optionNameConnectionsHigh = "connections-high"
optionNameConnectionsGrace = "connections-grace"
) )
cmd := &cobra.Command{ cmd := &cobra.Command{
...@@ -41,18 +44,15 @@ func (c *command) initStartCmd() (err error) { ...@@ -41,18 +44,15 @@ func (c *command) initStartCmd() (err error) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
//var idht *dht.IpfsDHT
// Construct P2P service. // Construct P2P service.
p2ps, err := libp2p.New(ctx, libp2p.Options{ p2ps, err := libp2p.New(ctx, libp2p.Options{
Addr: c.config.GetString(optionNameP2PAddr), Addr: c.config.GetString(optionNameP2PAddr),
DisableWS: c.config.GetBool(optionNameP2PDisableWS), DisableWS: c.config.GetBool(optionNameP2PDisableWS),
DisableQUIC: c.config.GetBool(optionNameP2PDisableQUIC), DisableQUIC: c.config.GetBool(optionNameP2PDisableQUIC),
Bootnodes: c.config.GetStringSlice(optionNameBootnodes), Bootnodes: c.config.GetStringSlice(optionNameBootnodes),
// Routing: func(h host.Host) (r routing.PeerRouting, err error) { ConnectionsLow: c.config.GetInt(optionNameConnectionsLow),
// idht, err = dht.New(ctx, h) ConnectionsHigh: c.config.GetInt(optionNameConnectionsHigh),
// return idht, err ConnectionsGrace: c.config.GetDuration(optionNameConnectionsGrace),
// },
}) })
if err != nil { if err != nil {
return fmt.Errorf("p2p service: %w", err) return fmt.Errorf("p2p service: %w", err)
...@@ -175,6 +175,9 @@ func (c *command) initStartCmd() (err error) { ...@@ -175,6 +175,9 @@ func (c *command) initStartCmd() (err error) {
cmd.Flags().Bool(optionNameP2PDisableQUIC, false, "disable P2P QUIC protocol") cmd.Flags().Bool(optionNameP2PDisableQUIC, false, "disable P2P QUIC protocol")
cmd.Flags().StringSlice(optionNameBootnodes, nil, "initial nodes to connect to") cmd.Flags().StringSlice(optionNameBootnodes, nil, "initial nodes to connect to")
cmd.Flags().String(optionNameDebugAPIAddr, ":6060", "Debug HTTP API listen address") cmd.Flags().String(optionNameDebugAPIAddr, ":6060", "Debug HTTP API listen address")
cmd.Flags().Int(optionNameConnectionsLow, 200, "low watermark governing the number of connections that'll be maintained")
cmd.Flags().Int(optionNameConnectionsHigh, 400, "high watermark governing the number of connections that'll be maintained")
cmd.Flags().Duration(optionNameConnectionsGrace, time.Minute, "the amount of time a newly opened connection is given before it becomes subject to pruning")
if err := c.config.BindPFlags(cmd.Flags()); err != nil { if err := c.config.BindPFlags(cmd.Flags()); err != nil {
return err return err
......
...@@ -31,12 +31,13 @@ type Service struct { ...@@ -31,12 +31,13 @@ type Service struct {
} }
type Options struct { type Options struct {
Addr string Addr string
DisableWS bool DisableWS bool
DisableQUIC bool DisableQUIC bool
Bootnodes []string Bootnodes []string
// PrivKey []byte ConnectionsLow int
// Routing func(host.Host) (routing.PeerRouting, error) ConnectionsHigh int
ConnectionsGrace time.Duration
} }
func New(ctx context.Context, o Options) (*Service, error) { func New(ctx context.Context, o Options) (*Service, error) {
...@@ -90,25 +91,24 @@ func New(ctx context.Context, o Options) (*Service, error) { ...@@ -90,25 +91,24 @@ func New(ctx context.Context, o Options) (*Service, error) {
libp2p.Security(libp2ptls.ID, libp2ptls.New), libp2p.Security(libp2ptls.ID, libp2ptls.New),
// support secio connections // support secio connections
libp2p.Security(secio.ID, secio.New), libp2p.Security(secio.ID, secio.New),
// support QUIC - experimental
libp2p.Transport(libp2pquic.NewTransport),
// support any other default transports (TCP) // support any other default transports (TCP)
libp2p.DefaultTransports, libp2p.DefaultTransports,
// Let's prevent our peer from having too many // Let's prevent our peer from having too many
// connections by attaching a connection manager. // connections by attaching a connection manager.
libp2p.ConnectionManager(connmgr.NewConnManager( libp2p.ConnectionManager(connmgr.NewConnManager(
100, // Lowwater o.ConnectionsLow,
400, // HighWater, o.ConnectionsHigh,
time.Minute, // GracePeriod o.ConnectionsGrace,
)), )),
// Attempt to open ports using uPNP for NATed hosts. // Attempt to open ports using uPNP for NATed hosts.
libp2p.NATPortMap(), libp2p.NATPortMap(),
// Let this host use the DHT to find other hosts }
//libp2p.Routing(o.Routing),
// Let this host use relays and advertise itself on relays if if !o.DisableQUIC {
// it finds it is behind NAT. Use libp2p.Relay(options...) to opts = append(opts,
// enable active relays and more. // support QUIC - experimental
//libp2p.EnableAutoRelay(), libp2p.Transport(libp2pquic.NewTransport),
)
} }
h, err := libp2p.New(ctx, opts...) h, err := libp2p.New(ctx, opts...)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment