Skip to content

Commit

Permalink
feat: reuse host
Browse files Browse the repository at this point in the history
Fixes #53
  • Loading branch information
2color committed Aug 30, 2024
1 parent a1985f1 commit ed87042
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,29 +171,26 @@ func (d *daemon) runCidCheck(ctx context.Context, cidStr string) (cidCheckOutput
DataAvailableOverBitswap: BitswapCheckOutput{},
}

testHost, err := d.createTestHost()
if err != nil {
log.Printf("Error creating test host: %v\n", err)
return
}
defer testHost.Close()

// Test Is the target connectable
dialCtx, dialCancel := context.WithTimeout(ctx, time.Second*15)
defer dialCancel()

testHost.Connect(dialCtx, provider)
// Call NewStream to force NAT hole punching. see https://github.com/libp2p/go-libp2p/issues/2714
_, connErr := testHost.NewStream(dialCtx, provider.ID, "/ipfs/bitswap/1.2.0", "/ipfs/bitswap/1.1.0", "/ipfs/bitswap/1.0.0", "/ipfs/bitswap")
connErr := d.h.Connect(dialCtx, provider)
if connErr != nil {
provOutput.ConnectionError = connErr.Error()

Check warning on line 180 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L180

Added line #L180 was not covered by tests
} else {
// Call NewStream to force NAT hole punching. see https://github.com/libp2p/go-libp2p/issues/2714
_, connErr = d.h.NewStream(dialCtx, provider.ID, "/ipfs/bitswap/1.2.0", "/ipfs/bitswap/1.1.0", "/ipfs/bitswap/1.0.0", "/ipfs/bitswap")
}

if connErr != nil {
provOutput.ConnectionError = connErr.Error()
} else {
// since we pass a libp2p host that's already connected to the peer the actual connection maddr we pass in doesn't matter
p2pAddr, _ := multiaddr.NewMultiaddr("/p2p/" + provider.ID.String())
provOutput.DataAvailableOverBitswap = checkBitswapCID(ctx, testHost, cid, p2pAddr)
provOutput.DataAvailableOverBitswap = checkBitswapCID(ctx, d.h, cid, p2pAddr)

for _, c := range testHost.Network().ConnsToPeer(provider.ID) {
for _, c := range d.h.Network().ConnsToPeer(provider.ID) {
provOutput.ConnectionMaddrs = append(provOutput.ConnectionMaddrs, c.RemoteMultiaddr().String())
}
}
Expand Down Expand Up @@ -247,12 +244,16 @@ func (d *daemon) runPeerCheck(ctx context.Context, maStr, cidStr string) (*peerC
addrMap, peerAddrDHTErr := peerAddrsInDHT(ctx, d.dht, d.dhtMessenger, ai.ID)
out.PeerFoundInDHT = addrMap

// Default to reusing the daemon libp2p host (which may already be connected to the peer through dht traversal)
testHost := d.h

// If peerID given,but no addresses check the DHT
if onlyPeerID {
if peerAddrDHTErr != nil {
// PeerID is not resolvable via the DHT
connectionFailed = true
out.ConnectionError = peerAddrDHTErr.Error()
return out, nil

Check warning on line 256 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L256

Added line #L256 was not covered by tests
}
for a := range addrMap {
ma, err := multiaddr.NewMultiaddr(a)
Expand All @@ -262,14 +263,15 @@ func (d *daemon) runPeerCheck(ctx context.Context, maStr, cidStr string) (*peerC
}
ai.Addrs = append(ai.Addrs, ma)
}
} else {
// create an ephemeral test host so that we check the passed multiaddr. See https://github.com/ipfs/ipfs-check/issues/53
testHost, err = d.createTestHost()
if err != nil {
return nil, fmt.Errorf("server error: %w", err)

Check warning on line 270 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L270

Added line #L270 was not covered by tests
}
defer testHost.Close()
}

testHost, err := d.createTestHost()
if err != nil {
return nil, fmt.Errorf("server error: %w", err)
}
defer testHost.Close()

if !connectionFailed {
// Test Is the target connectable
dialCtx, dialCancel := context.WithTimeout(ctx, time.Second*120)
Expand Down

0 comments on commit ed87042

Please sign in to comment.