Skip to content

Commit

Permalink
Merge pull request ipfs#108 from libp2p/fix/loggable-key
Browse files Browse the repository at this point in the history
handle paths as DHT keys
  • Loading branch information
Stebalien authored Dec 12, 2017
2 parents c9557b6 + df33f2b commit b81d571
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
3 changes: 0 additions & 3 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ var testCaseValues = map[string][]byte{}
var testCaseCids []*cid.Cid

func init() {
testCaseValues["hello"] = []byte("world")
for i := 0; i < 100; i++ {
k := fmt.Sprintf("%d -- key", i)
v := fmt.Sprintf("%d -- value", i)
testCaseValues[k] = []byte(v)

mhv := u.Hash([]byte(v))
testCaseCids = append(testCaseCids, cid.NewCidV0(mhv))
Expand Down
34 changes: 31 additions & 3 deletions lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dht

import (
"context"
"fmt"
"strings"

cid "github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
Expand Down Expand Up @@ -29,13 +31,39 @@ func toPeerInfos(ps []peer.ID) []*pstore.PeerInfo {
return out
}

func tryFormatLoggableKey(k string) (string, error) {
if len(k) == 0 {
return "", fmt.Errorf("loggableKey is empty")
}
var proto, cstr string
if k[0] == '/' {
// it's a path (probably)
protoEnd := strings.IndexByte(k[1:], '/')
if protoEnd < 0 {
return k, fmt.Errorf("loggableKey starts with '/' but is not a path: %x", k)
}
proto = k[1 : protoEnd+1]
cstr = k[protoEnd+2:]
} else {
proto = "provider"
cstr = k
}

c, err := cid.Cast([]byte(cstr))
if err != nil {
return "", fmt.Errorf("loggableKey could not cast key to a CID: %x %v", k, err)
}
return fmt.Sprintf("/%s/%s", proto, c.String()), nil
}

func loggableKey(k string) logging.LoggableMap {
cid, err := cid.Cast([]byte(k))
newKey, err := tryFormatLoggableKey(k)
if err != nil {
log.Errorf("loggableKey could not cast key: %x %v", k, err)
log.Error(err)
} else {
k = cid.String()
k = newKey
}

return logging.LoggableMap{
"key": k,
}
Expand Down
36 changes: 36 additions & 0 deletions lookup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dht

import (
"testing"

cid "github.com/ipfs/go-cid"
)

func TestLoggableKey(t *testing.T) {
c, err := cid.Decode("QmfUvYQhL2GinafMbPDYz7VFoZv4iiuLuR33aRsPurXGag")
if err != nil {
t.Fatal(err)
}

k, err := tryFormatLoggableKey("/proto/" + string(c.Bytes()))
if err != nil {
t.Errorf("failed to format key 1: %s", err)
}
if k != "/proto/"+c.String() {
t.Error("expected path to be preserved as a loggable key")
}

k, err = tryFormatLoggableKey(string(c.Bytes()))
if err != nil {
t.Errorf("failed to format key 2: %s", err)
}
if k != "/provider/"+c.String() {
t.Error("expected cid to be formatted as a loggable key")
}

for _, s := range []string{"bla bla", "/bla", "/bla/asdf", ""} {
if _, err := tryFormatLoggableKey(s); err == nil {
t.Errorf("expected to fail formatting: %s", s)
}
}
}

0 comments on commit b81d571

Please sign in to comment.