|
|
@ -128,7 +128,7 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
ctx, cancel := context.WithCancel(context.Background()) |
|
|
|
ctx, cancel := context.WithCancel(context.Background()) |
|
|
|
// TODO: move low and high to configs
|
|
|
|
// TODO: move low and high to configs
|
|
|
|
connmgr, err := connmgr.NewConnManager( |
|
|
|
connmgr, err := connmgr.NewConnManager( |
|
|
|
int(10), // Lowwater
|
|
|
|
int(10), // LowWater
|
|
|
|
int(10000)*cfg.MaxConnPerIP, // HighWater,
|
|
|
|
int(10000)*cfg.MaxConnPerIP, // HighWater,
|
|
|
|
connmgr.WithGracePeriod(time.Minute), |
|
|
|
connmgr.WithGracePeriod(time.Minute), |
|
|
|
) |
|
|
|
) |
|
|
@ -137,6 +137,7 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
var idht *dht.IpfsDHT |
|
|
|
var idht *dht.IpfsDHT |
|
|
|
|
|
|
|
var opt discovery.DHTConfig |
|
|
|
p2pHost, err := libp2p.New( |
|
|
|
p2pHost, err := libp2p.New( |
|
|
|
listenAddr, |
|
|
|
listenAddr, |
|
|
|
libp2p.Identity(key), |
|
|
|
libp2p.Identity(key), |
|
|
@ -152,33 +153,41 @@ func NewHost(cfg HostConfig) (Host, error) { |
|
|
|
// Attempt to open ports using uPNP for NATed hosts.
|
|
|
|
// Attempt to open ports using uPNP for NATed hosts.
|
|
|
|
libp2p.NATPortMap(), |
|
|
|
libp2p.NATPortMap(), |
|
|
|
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) { |
|
|
|
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) { |
|
|
|
idht, err = dht.New(ctx, h) |
|
|
|
opt = discovery.DHTConfig{ |
|
|
|
|
|
|
|
BootNodes: cfg.BootNodes, |
|
|
|
|
|
|
|
DataStoreFile: cfg.DataStoreFile, |
|
|
|
|
|
|
|
DiscConcurrency: cfg.DiscConcurrency, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
opts, err := opt.GetLibp2pRawOptions() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
idht, err = dht.New(ctx, h, opts...) |
|
|
|
return idht, err |
|
|
|
return idht, err |
|
|
|
}), |
|
|
|
}), |
|
|
|
|
|
|
|
|
|
|
|
// to help other peers to figure out if they are behind
|
|
|
|
// to help other peers to figure out if they are behind
|
|
|
|
// NATs, launch the server-side of AutoNAT too (AutoRelay
|
|
|
|
// NATs, launch the server-side of AutoNAT too (AutoRelay
|
|
|
|
// already runs the client)
|
|
|
|
// already runs the client)
|
|
|
|
// This service is highly rate-limited and should not cause any
|
|
|
|
// This service is highly rate-limited and should not cause any
|
|
|
|
// performance issues.
|
|
|
|
// performance issues.
|
|
|
|
libp2p.EnableNATService(), |
|
|
|
libp2p.EnableNATService(), |
|
|
|
|
|
|
|
// Bandwidth Reporter
|
|
|
|
libp2p.BandwidthReporter(newCounter()), |
|
|
|
libp2p.BandwidthReporter(newCounter()), |
|
|
|
// ForceReachabilityPublic overrides automatic reachability detection in the AutoNAT subsystem,
|
|
|
|
// ForceReachabilityPublic overrides automatic reachability detection in the AutoNAT subsystem,
|
|
|
|
// forcing the local node to believe it is reachable externally.
|
|
|
|
// forcing the local node to believe it is reachable externally.
|
|
|
|
// libp2p.ForceReachabilityPublic(),
|
|
|
|
// libp2p.ForceReachabilityPublic(),
|
|
|
|
|
|
|
|
// libp2p.DisableRelay(),
|
|
|
|
|
|
|
|
libp2p.EnableRelayService(), |
|
|
|
// prevent dialing of public addresses
|
|
|
|
// prevent dialing of public addresses
|
|
|
|
libp2p.ConnectionGater(NewGater(cfg.DisablePrivateIPScan)), |
|
|
|
// libp2p.ConnectionGater(NewGater(cfg.DisablePrivateIPScan)),
|
|
|
|
) |
|
|
|
) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
cancel() |
|
|
|
cancel() |
|
|
|
return nil, errors.Wrapf(err, "cannot initialize libp2p host") |
|
|
|
return nil, errors.Wrapf(err, "cannot initialize libp2p host") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
disc, err := discovery.NewDHTDiscovery(p2pHost, discovery.DHTConfig{ |
|
|
|
disc, err := discovery.NewDHTDiscovery(ctx, cancel, p2pHost, idht, opt) |
|
|
|
BootNodes: cfg.BootNodes, |
|
|
|
|
|
|
|
DataStoreFile: cfg.DataStoreFile, |
|
|
|
|
|
|
|
DiscConcurrency: cfg.DiscConcurrency, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
cancel() |
|
|
|
cancel() |
|
|
|
return nil, errors.Wrap(err, "cannot create DHT discovery") |
|
|
|
return nil, errors.Wrap(err, "cannot create DHT discovery") |
|
|
@ -319,6 +328,10 @@ func (host *HostV2) AddStreamProtocol(protocols ...sttypes.Protocol) { |
|
|
|
for _, proto := range protocols { |
|
|
|
for _, proto := range protocols { |
|
|
|
host.streamProtos = append(host.streamProtos, proto) |
|
|
|
host.streamProtos = append(host.streamProtos, proto) |
|
|
|
host.h.SetStreamHandlerMatch(protocol.ID(proto.ProtoID()), proto.Match, proto.HandleStream) |
|
|
|
host.h.SetStreamHandlerMatch(protocol.ID(proto.ProtoID()), proto.Match, proto.HandleStream) |
|
|
|
|
|
|
|
// TODO: do we need to add handler match for shard proto id?
|
|
|
|
|
|
|
|
// if proto.IsBeaconNode() {
|
|
|
|
|
|
|
|
// host.h.SetStreamHandlerMatch(protocol.ID(proto.ShardProtoID()), proto.Match, proto.HandleStream)
|
|
|
|
|
|
|
|
// }
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|