Skip to content

Commit

Permalink
docs: update readme with test results
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Aug 30, 2024
1 parent ef4ddd0 commit fd5cf04
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 13 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,38 @@ Note that the `multiaddr` can be:

### Check results

The server performs several checks given a CID. The results of the check are expressed by the `output` type:
The server performs several checks depending on whether you also pass a **multiaddr** or just a **cid**.

#### Results when only a `cid` is passed

The results of the check are expressed by the `cidCheckOutput` type:

```go
type cidCheckOutput *[]providerOutput

type providerOutput struct {
ID string
ConnectionError string
Addrs []string
ConnectionMaddrs []string
DataAvailableOverBitswap BitswapCheckOutput
}
```

The `providerOutput` type contains the following fields:

- `ID`: The peer ID of the provider.
- `ConnectionError`: An error message if the connection to the provider failed.
- `Addrs`: The multiaddrs of the provider from the DHT.
- `ConnectionMaddrs`: The multiaddrs that were used to connect to the provider.
- `DataAvailableOverBitswap`: The result of the Bitswap check.

#### Results when a `multiaddr` and a `cid` are passed

The results of the check are expressed by the `peerCheckOutput` type:

```go
type output struct {
type peerCheckOutput struct {
ConnectionError string
PeerFoundInDHT map[string]int
CidInDHT bool
Expand Down
69 changes: 58 additions & 11 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
Expand Down Expand Up @@ -90,15 +92,44 @@ func newDaemon(ctx context.Context, acceleratedDHT bool) (*daemon, error) {
return nil, err
}

return &daemon{h: h, dht: d, dhtMessenger: pm, createTestHost: func() (host.Host, error) {
return libp2p.New(
libp2p.ConnectionGater(&privateAddrFilterConnectionGater{}),
libp2p.DefaultMuxers,
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
libp2p.EnableHolePunching(),
libp2p.UserAgent(userAgent),
)
}}, nil
return &daemon{h: h, dht: d, dhtMessenger: pm,
createTestHost: func() (host.Host, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

Check warning on line 98 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L95-L98

Added lines #L95 - L98 were not covered by tests

testHost, err := libp2p.New(
libp2p.ConnectionGater(&privateAddrFilterConnectionGater{}),
libp2p.DefaultMuxers,
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
libp2p.EnableHolePunching(),

Check warning on line 104 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L100-L104

Added lines #L100 - L104 were not covered by tests
// libp2p.ResourceManager(rm),
// libp2p.ConnectionManager(c),
libp2p.Transport(libp2pquic.NewTransport),
libp2p.ListenAddrStrings("/ip4/0.0.0.0/udp/0/quic-v1"),
libp2p.UserAgent(userAgent),
)
if err != nil {
return nil, err

Check warning on line 112 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L107-L112

Added lines #L107 - L112 were not covered by tests
}

testHostDht, err := dht.New(ctx, testHost, dht.Mode(dht.ModeClient), dht.BootstrapPeers(dht.GetDefaultBootstrapPeerAddrInfos()...))
if err != nil {
return nil, err

Check warning on line 117 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L115-L117

Added lines #L115 - L117 were not covered by tests
}

err = testHostDht.Bootstrap(ctx)
if err != nil {
return nil, err

Check warning on line 122 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L120-L122

Added lines #L120 - L122 were not covered by tests
}

log.Printf("Created host %s with listen addrs %v", testHost.ID(), testHost.Network().ListenAddresses())
ids, ok := testHost.(interface{ IDService() identify.IDService })
if ok {
log.Printf("Own observed addrs: %v", ids.IDService().OwnObservedAddrs())

Check warning on line 128 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L125-L128

Added lines #L125 - L128 were not covered by tests
}

return testHost, nil

Check warning on line 131 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L131

Added line #L131 was not covered by tests
}}, nil
}

func (d *daemon) mustStart() {
Expand All @@ -111,6 +142,8 @@ func (d *daemon) mustStart() {

}

type cidCheckOutput *[]providerOutput

type providerOutput struct {
ID string
ConnectionError string
Expand All @@ -120,7 +153,7 @@ type providerOutput struct {
}

// runCidCheck looks up the DHT for providers of a given CID and then checks their connectivity and Bitswap availability
func (d *daemon) runCidCheck(ctx context.Context, cidStr string) (*[]providerOutput, error) {
func (d *daemon) runCidCheck(ctx context.Context, cidStr string) (cidCheckOutput, error) {
cid, err := cid.Decode(cidStr)
if err != nil {
return nil, err

Check warning on line 159 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L159

Added line #L159 was not covered by tests
Expand Down Expand Up @@ -256,13 +289,27 @@ func (d *daemon) runPeerCheck(ctx context.Context, maStr, cidStr string) (*peerC

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

testHost.Connect(dialCtx, *ai)
// Call NewStream to force NAT hole punching. see https://github.com/libp2p/go-libp2p/issues/2714
_, connErr := testHost.NewStream(dialCtx, ai.ID, "/ipfs/bitswap/1.2.0", "/ipfs/bitswap/1.1.0", "/ipfs/bitswap/1.0.0", "/ipfs/bitswap")
dialCancel()
if connErr != nil {
log.Printf("Error connecting to peer %s: %v", ai.ID, connErr)
ids, ok := testHost.(interface{ IDService() identify.IDService })
if ok {
log.Printf("Own observed addrs: %v", ids.IDService().OwnObservedAddrs())

Check warning on line 302 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L299-L302

Added lines #L299 - L302 were not covered by tests
}

// Log all open connections
for _, conn := range testHost.Network().Conns() {
log.Printf("Open connection: Peer ID: %s, Remote Addr: %s, Local Addr: %s",
conn.RemotePeer(),
conn.RemoteMultiaddr(),
conn.LocalMultiaddr(),
)

Check warning on line 311 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L306-L311

Added lines #L306 - L311 were not covered by tests
}
out.ConnectionError = connErr.Error()

Check warning on line 313 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L313

Added line #L313 was not covered by tests
connectionFailed = true
}
Expand Down

0 comments on commit fd5cf04

Please sign in to comment.