Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: reuse host #59

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 20 additions & 18 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,29 +171,26 @@
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 @@
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 @@
}
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
Loading