Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

limit Libp2p host to use only TCP #24

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/p2p/dht_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"

kaddht "github.com/libp2p/go-libp2p-kad-dht"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -104,6 +105,7 @@ func NewDHTHost(ctx context.Context, opts DHTHostOptions) (*DHTHost, error) {
libp2p.Identity(privKey),
libp2p.UserAgent(DefaultUserAgent),
libp2p.ResourceManager(rm),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
var err error
dhtOpts := make([]kaddht.Option, 0)
Expand Down
54 changes: 27 additions & 27 deletions pkg/p2p/host_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,36 @@ func (p *HostPool) OneMoreHost(hOpts DHTHostOptions) error {
}

func (p *HostPool) GetBestHost(newCid *models.CidInfo) (*DHTHost, error) {
// Previus method that balances the pings based on XOR distance
// Deprecated as it is not 100% tested
// Previus method that balances the pings based on XOR distance
// Deprecated as it is not 100% tested
/*
p.m.RLock()
xorDists := make([]*big.Int, len(p.hostArray))
for idx, dhtHost := range p.hostArray {
xorDist, idle := dhtHost.XORDistanceToOngoingCids(newCid.CID)
if idle { // if any host is idle, return it directly
return dhtHost, nil
p.m.RLock()
xorDists := make([]*big.Int, len(p.hostArray))
for idx, dhtHost := range p.hostArray {
xorDist, idle := dhtHost.XORDistanceToOngoingCids(newCid.CID)
if idle { // if any host is idle, return it directly
return dhtHost, nil
}
xorDists[idx] = xorDist
}
xorDists[idx] = xorDist
}
p.m.RUnlock()

// get max dist out of the closes ongoing one
var hid int
maxDist := big.NewInt(0)
for idx, xorDist := range xorDists {
i := maxDist.Cmp(xorDist)
if i == int(big.Below) {
maxDist = xorDist
hid = idx
p.m.RUnlock()

// get max dist out of the closes ongoing one
var hid int
maxDist := big.NewInt(0)
for idx, xorDist := range xorDists {
i := maxDist.Cmp(xorDist)
if i == int(big.Below) {
maxDist = xorDist
hid = idx
}
}
p.m.RLock()
defer p.m.RUnlock()
if hid == 0 && maxDist == big.NewInt(0) {
// return at least the first node in the list (is among the hosts with fewer ongoing cids)
return p.hostArray[hid], ErrorRetrievingBestHost
}
}
p.m.RLock()
defer p.m.RUnlock()
if hid == 0 && maxDist == big.NewInt(0) {
// return at least the first node in the list (is among the hosts with fewer ongoing cids)
return p.hostArray[hid], ErrorRetrievingBestHost
}
*/
if p.Len() <= 0 {
return nil, errors.New("trying to get host from a pool with 0 hosts")
Expand Down