Skip to content

Commit

Permalink
fix: rename output field
Browse files Browse the repository at this point in the history
and simplify code
fixes #43
  • Loading branch information
2color committed Aug 26, 2024
1 parent e968620 commit ac2e96d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 35 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ The server performs several checks given a CID. The results of the check are exp

```go
type output struct {
ConnectionError string
PeerFoundInDHT map[string]int
CidInDHT bool
ConnectionMaddrs string[]
DataAvailableOverBitswap BitswapCheckOutput
ConnectionError string
PeerFoundInDHT map[string]int
ProviderRecordFromPeerInDHT bool
ConnectionMaddrs []string
DataAvailableOverBitswap BitswapCheckOutput
}

type BitswapCheckOutput struct {
Expand All @@ -90,9 +90,9 @@ type BitswapCheckOutput struct {
}
```

1. Is the CID (really multihash) advertised in the DHT (or later IPNI)?
1. Is the CID (really multihash) advertised in the DHT by the Passed PeerID (or later IPNI)?

- `CidInDHT`
- `ProviderRecordFromPeerInDHT`

2. Are the peer's addresses discoverable (particularly useful if the announcements are DHT based, but also independently useful)

Expand Down
31 changes: 13 additions & 18 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (d *daemon) runCheck(query url.Values) (*output, error) {

connectionFailed := false

out.CidInDHT = providerRecordInDHT(ctx, d.dht, c, ai.ID)
out.ProviderRecordFromPeerInDHT = ProviderRecordFromPeerInDHT(ctx, d.dht, c, ai.ID)

addrMap, peerAddrDHTErr := peerAddrsInDHT(ctx, d.dht, d.dhtMessenger, ai.ID)
out.PeerFoundInDHT = addrMap
Expand Down Expand Up @@ -183,21 +183,16 @@ func (d *daemon) runCheck(query url.Values) (*output, error) {
dialCancel()
if connErr != nil {
out.ConnectionError = fmt.Sprintf("error dialing to peer: %s", connErr.Error())
connectionFailed = true
return out, nil

Check warning on line 186 in daemon.go

View check run for this annotation

Codecov / codecov/patch

daemon.go#L186

Added line #L186 was not covered by tests
}
}

if connectionFailed {
out.DataAvailableOverBitswap.Error = "could not connect to peer"
} else {
// If so is the data available over Bitswap?
out.DataAvailableOverBitswap = checkBitswapCID(ctx, testHost, c, ma)
// If so is the data available over Bitswap?
out.DataAvailableOverBitswap = checkBitswapCID(ctx, testHost, c, ma)

// Get the direct connection in case it was hole punched and we have both a limited connection
// directMaddr := getDirectMaddr()
for _, c := range testHost.Network().ConnsToPeer(ai.ID) {
out.ConnectionMaddrs = append(out.ConnectionMaddrs, c.RemoteMultiaddr().String())
}
// Get all connection maddrs to the peer (in case we hole punched, there will usually be two: limited relay and direct)
for _, c := range testHost.Network().ConnsToPeer(ai.ID) {
out.ConnectionMaddrs = append(out.ConnectionMaddrs, c.RemoteMultiaddr().String())
}

return out, nil
Expand Down Expand Up @@ -232,11 +227,11 @@ type BitswapCheckOutput struct {
}

type output struct {
ConnectionError string
PeerFoundInDHT map[string]int
CidInDHT bool
ConnectionMaddrs []string
DataAvailableOverBitswap BitswapCheckOutput
ConnectionError string
PeerFoundInDHT map[string]int
ProviderRecordFromPeerInDHT bool
ConnectionMaddrs []string
DataAvailableOverBitswap BitswapCheckOutput
}

func peerAddrsInDHT(ctx context.Context, d kademlia, messenger *dhtpb.ProtocolMessenger, p peer.ID) (map[string]int, error) {
Expand Down Expand Up @@ -282,7 +277,7 @@ func peerAddrsInDHT(ctx context.Context, d kademlia, messenger *dhtpb.ProtocolMe
return addrMap, nil
}

func providerRecordInDHT(ctx context.Context, d kademlia, c cid.Cid, p peer.ID) bool {
func ProviderRecordFromPeerInDHT(ctx context.Context, d kademlia, c cid.Cid, p peer.ID) bool {
queryCtx, cancel := context.WithCancel(ctx)
defer cancel()
provsCh := d.FindProvidersAsync(queryCtx, c, 0)
Expand Down
6 changes: 3 additions & 3 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestBasicIntegration(t *testing.T) {

obj := test.Query(t, "http://localhost:1234", testCid.String(), hostAddr.String())

obj.Value("CidInDHT").Boolean().IsTrue()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsTrue()
obj.Value("ConnectionError").String().IsEmpty()
obj.Value("ConnectionMaddrs").Array().ContainsAll(h.Addrs()[0])
obj.Value("DataAvailableOverBitswap").Object().Value("Error").String().IsEmpty()
Expand All @@ -132,7 +132,7 @@ func TestBasicIntegration(t *testing.T) {

obj := test.Query(t, "http://localhost:1234", testCid.String(), hostAddr.String())

obj.Value("CidInDHT").Boolean().IsFalse()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsFalse()
obj.Value("ConnectionError").String().IsEmpty()
obj.Value("ConnectionMaddrs").Array().ContainsAll(h.Addrs()[0])
obj.Value("DataAvailableOverBitswap").Object().Value("Error").String().IsEmpty()
Expand All @@ -150,7 +150,7 @@ func TestBasicIntegration(t *testing.T) {

obj := test.Query(t, "http://localhost:1234", testCid.String(), hostAddr.String())

obj.Value("CidInDHT").Boolean().IsTrue()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsTrue()
obj.Value("ConnectionError").String().IsEmpty()
obj.Value("ConnectionMaddrs").Array().ContainsAll(h.Addrs()[0])
obj.Value("DataAvailableOverBitswap").Object().Value("Error").String().IsEmpty()
Expand Down
10 changes: 5 additions & 5 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestEmptyDirOnBoostrapPeer(t *testing.T) {
}
obj := Q(t, EMPTY_DIR_CID, BOOTSTRAP_PEER_ADDR)

obj.Value("CidInDHT").Boolean().IsTrue()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsTrue()
obj.Value("ConnectionError").String().IsEmpty()
obj.Value("DataAvailableOverBitswap").Object().Value("Error").String().IsEmpty()
obj.Value("DataAvailableOverBitswap").Object().Value("Found").Boolean().IsTrue()
Expand All @@ -51,7 +51,7 @@ func TestWikipediaOnSomeProviderPeer(t *testing.T) {
t.Skip("Skipping e2e tests")
}
obj := Q(t, WIKIPEDIA_CID, WIKIPEDIA_PEER_ADDR)
obj.Value("CidInDHT").Boolean().IsTrue()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsTrue()
// It seems that most peers do not provide over bitswap:
// obj.Value("ConnectionError").String().IsEmpty()
// obj.Value("DataAvailableOverBitswap").Object().Value("Error").String().IsEmpty()
Expand All @@ -72,7 +72,7 @@ func TestRandomFileOnBootstrapPeer(t *testing.T) {
time.Sleep(60 * time.Second)
obj := Q(t, randomFileCid, BOOTSTRAP_PEER_ADDR)

obj.Value("CidInDHT").Boolean().IsTrue()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsTrue()
obj.Value("ConnectionError").String().IsEmpty()
obj.Value("DataAvailableOverBitswap").Object().Value("Error").String().IsEmpty()
obj.Value("DataAvailableOverBitswap").Object().Value("Found").Boolean().IsTrue()
Expand All @@ -97,7 +97,7 @@ func TestRandomFileOnLocalPeer(t *testing.T) {
time.Sleep(25 * time.Second)
obj := Q(t, randomFileCid, localAddr)

obj.Value("CidInDHT").Boolean().IsTrue()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsTrue()
obj.Value("ConnectionError").String().IsEmpty()
obj.Value("DataAvailableOverBitswap").Object().Value("Error").String().IsEmpty()
obj.Value("DataAvailableOverBitswap").Object().Value("Found").Boolean().IsTrue()
Expand All @@ -115,7 +115,7 @@ func TestRandomFileNeverUploadedOnBootstrapPeer(t *testing.T) {

obj := Q(t, randomFileCid, BOOTSTRAP_PEER_ADDR)

obj.Value("CidInDHT").Boolean().IsFalse()
obj.Value("ProviderRecordFromPeerInDHT").Boolean().IsFalse()
obj.Value("DataAvailableOverBitswap").Object().Value("Found").Boolean().IsFalse()
obj.Value("DataAvailableOverBitswap").Object().Value("Responded").Boolean().IsTrue()
}
2 changes: 1 addition & 1 deletion test/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetEnv(key string, fallback string) string {
Example of outputs:
```json
{
"CidInDHT": true,
"ProviderRecordFromPeerInDHT": true,
"ConnectionError": "no addresses",
"DataAvailableOverBitswap": {
"Duration": 0,
Expand Down
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ <h2 class="f4">What does it mean if I get an error?</h2>
}
}

if (respObj.CidInDHT === true) {
if (respObj.ProviderRecordFromPeerInDHT === true) {
outText += "✅ Found multihash advertised in the dht\n"
} else {
outText += "❌ Could not find the multihash in the dht\n"
Expand Down

0 comments on commit ac2e96d

Please sign in to comment.