Commit d8541735 authored by Janos Guljas's avatar Janos Guljas

add connectivity configuration for libp2p

parent 31381401
......@@ -22,12 +22,15 @@ import (
func (c *command) initStartCmd() (err error) {
const (
optionNameAPIAddr = "api-addr"
optionNameP2PAddr = "p2p-addr"
optionNameP2PDisableWS = "p2p-disable-ws"
optionNameP2PDisableQUIC = "p2p-disable-quic"
optionNameDebugAPIAddr = "debug-api-addr"
optionNameBootnodes = "bootnode"
optionNameAPIAddr = "api-addr"
optionNameP2PAddr = "p2p-addr"
optionNameP2PDisableWS = "p2p-disable-ws"
optionNameP2PDisableQUIC = "p2p-disable-quic"
optionNameDebugAPIAddr = "debug-api-addr"
optionNameBootnodes = "bootnode"
optionNameConnectionsLow = "connections-low"
optionNameConnectionsHigh = "connections-high"
optionNameConnectionsGrace = "connections-grace"
)
cmd := &cobra.Command{
......@@ -41,18 +44,15 @@ func (c *command) initStartCmd() (err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
//var idht *dht.IpfsDHT
// Construct P2P service.
p2ps, err := libp2p.New(ctx, libp2p.Options{
Addr: c.config.GetString(optionNameP2PAddr),
DisableWS: c.config.GetBool(optionNameP2PDisableWS),
DisableQUIC: c.config.GetBool(optionNameP2PDisableQUIC),
Bootnodes: c.config.GetStringSlice(optionNameBootnodes),
// Routing: func(h host.Host) (r routing.PeerRouting, err error) {
// idht, err = dht.New(ctx, h)
// return idht, err
// },
Addr: c.config.GetString(optionNameP2PAddr),
DisableWS: c.config.GetBool(optionNameP2PDisableWS),
DisableQUIC: c.config.GetBool(optionNameP2PDisableQUIC),
Bootnodes: c.config.GetStringSlice(optionNameBootnodes),
ConnectionsLow: c.config.GetInt(optionNameConnectionsLow),
ConnectionsHigh: c.config.GetInt(optionNameConnectionsHigh),
ConnectionsGrace: c.config.GetDuration(optionNameConnectionsGrace),
})
if err != nil {
return fmt.Errorf("p2p service: %w", err)
......@@ -175,6 +175,9 @@ func (c *command) initStartCmd() (err error) {
cmd.Flags().Bool(optionNameP2PDisableQUIC, false, "disable P2P QUIC protocol")
cmd.Flags().StringSlice(optionNameBootnodes, nil, "initial nodes to connect to")
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 {
return err
......
......@@ -31,12 +31,13 @@ type Service struct {
}
type Options struct {
Addr string
DisableWS bool
DisableQUIC bool
Bootnodes []string
// PrivKey []byte
// Routing func(host.Host) (routing.PeerRouting, error)
Addr string
DisableWS bool
DisableQUIC bool
Bootnodes []string
ConnectionsLow int
ConnectionsHigh int
ConnectionsGrace time.Duration
}
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),
// support secio connections
libp2p.Security(secio.ID, secio.New),
// support QUIC - experimental
libp2p.Transport(libp2pquic.NewTransport),
// support any other default transports (TCP)
libp2p.DefaultTransports,
// Let's prevent our peer from having too many
// connections by attaching a connection manager.
libp2p.ConnectionManager(connmgr.NewConnManager(
100, // Lowwater
400, // HighWater,
time.Minute, // GracePeriod
o.ConnectionsLow,
o.ConnectionsHigh,
o.ConnectionsGrace,
)),
// Attempt to open ports using uPNP for NATed hosts.
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
// it finds it is behind NAT. Use libp2p.Relay(options...) to
// enable active relays and more.
//libp2p.EnableAutoRelay(),
}
if !o.DisableQUIC {
opts = append(opts,
// support QUIC - experimental
libp2p.Transport(libp2pquic.NewTransport),
)
}
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