Skip to content

Commit

Permalink
logging: more uniformly output lowercase b32 encoded keys
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Jul 17, 2020
1 parent db2389a commit 07952a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
50 changes: 18 additions & 32 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package dht

import (
"fmt"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
"strings"

"github.com/ipfs/go-cid"
"github.com/multiformats/go-base32"
"github.com/multiformats/go-multihash"
)

func lowercaseB32Encode(k []byte) string {
return strings.ToLower(base32.RawStdEncoding.EncodeToString(k))
}

func tryFormatLoggableRecordKey(k string) (string, error) {
if len(k) == 0 {
return "", fmt.Errorf("loggableRecordKey is empty")
Expand All @@ -19,23 +22,16 @@ func tryFormatLoggableRecordKey(k string) (string, error) {
// it's a path (probably)
protoEnd := strings.IndexByte(k[1:], '/')
if protoEnd < 0 {
return "", fmt.Errorf("loggableRecordKey starts with '/' but is not a path: %s", base32.RawStdEncoding.EncodeToString([]byte(k)))
return "", fmt.Errorf("loggableRecordKey starts with '/' but is not a path: %s", lowercaseB32Encode([]byte(k)))
}
proto = k[1 : protoEnd+1]
cstr = k[protoEnd+2:]

var encStr string
c, err := cid.Cast([]byte(cstr))
if err == nil {
encStr = c.String()
} else {
encStr = base32.RawStdEncoding.EncodeToString([]byte(cstr))
}

encStr := lowercaseB32Encode([]byte(cstr))
return fmt.Sprintf("/%s/%s", proto, encStr), nil
}

return "", fmt.Errorf("loggableRecordKey is not a path: %s", base32.RawStdEncoding.EncodeToString([]byte(k)))
return "", fmt.Errorf("loggableRecordKey is not a path: %s", lowercaseB32Encode([]byte(cstr)))
}

type loggableRecordKeyString string
Expand Down Expand Up @@ -63,39 +59,29 @@ func (lk loggableRecordKeyBytes) String() string {
type loggableProviderRecordBytes []byte

func (lk loggableProviderRecordBytes) String() string {
k := string(lk)
newKey, err := tryFormatLoggableProviderKey(k)
newKey, err := tryFormatLoggableProviderKey(lk)
if err == nil {
return newKey
}
return err.Error()
}

func tryFormatLoggableProviderKey(k string) (string, error) {
func tryFormatLoggableProviderKey(k []byte) (string, error) {
if len(k) == 0 {
return "", fmt.Errorf("loggableProviderKey is empty")
}

h, err := multihash.Cast([]byte(k))
if err == nil {
c := cid.NewCidV1(cid.Raw, h)
encStr, err := c.StringOfBase(multibase.Base32)
if err != nil {
panic(fmt.Errorf("should be impossible to reach here : %w", err))
}
return encStr, nil
}
encodedKey := lowercaseB32Encode(k)

// The DHT used to provide CIDs, but now provides multihashes
// TODO: Drop this when enough of the network has upgraded
c, err := cid.Cast([]byte(k))
if err == nil {
encStr, err := c.StringOfBase(multibase.Base32)
if err != nil {
panic(fmt.Errorf("should be impossible to reach here : %w", err))
}
return encStr, nil
if _, err := cid.Cast(k); err == nil {
return encodedKey, nil
}

if _, err := multihash.Cast(k); err == nil {
return encodedKey, nil
}

return "", fmt.Errorf("invalid provider record: %s : err %w", base32.RawStdEncoding.EncodeToString([]byte(k)), err)
return "", fmt.Errorf("loggableProviderKey is not a Multihash or CID: %s", encodedKey)
}
25 changes: 12 additions & 13 deletions logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestLoggableRecordKey(t *testing.T) {
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != "/proto/"+c.String() {
if k != "/proto/"+lowercaseB32Encode(c.Bytes()) {
t.Error("expected path to be preserved as a loggable key")
}

Expand All @@ -40,37 +40,36 @@ func TestLoggableProviderKey(t *testing.T) {
}

// Test logging CIDv0 provider
c0ascidv1Raw := cid.NewCidV1(cid.Raw, c0.Hash())
k, err := tryFormatLoggableProviderKey(string(c0.Bytes()))
b32MH := lowercaseB32Encode(c0.Hash())
k, err := tryFormatLoggableProviderKey(c0.Bytes())
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != c0ascidv1Raw.String() {
t.Error("expected cidv0 to be converted into CIDv1 b32 with Raw codec")
if k != b32MH {
t.Error("expected cidv0 to be converted into base32 multihash")
}

// Test logging CIDv1 provider (from older DHT implementations)
c1 := cid.NewCidV1(cid.DagProtobuf, c0.Hash())
k, err = tryFormatLoggableProviderKey(string(c1.Bytes()))
k, err = tryFormatLoggableProviderKey(c1.Hash())
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != c1.String() {
t.Error("expected cidv1 to be displayed normally")
if k != b32MH {
t.Error("expected cidv1 to be converted into base32 multihash")
}

// Test logging multihash provider
c1ascidv1Raw := cid.NewCidV1(cid.Raw, c1.Hash())
k, err = tryFormatLoggableProviderKey(string(c1.Hash()))
k, err = tryFormatLoggableProviderKey(c1.Hash())
if err != nil {
t.Errorf("failed to format key: %s", err)
}
if k != c1ascidv1Raw.String() {
t.Error("expected multihash to be converted into CIDv1 b32 with Raw codec")
if k != b32MH {
t.Error("expected multihash to be displayed in base32")
}

for _, s := range []string{"/bla", "", "bla bla", "/bla/asdf", "/a/b/c"} {
if _, err := tryFormatLoggableProviderKey(s); err == nil {
if _, err := tryFormatLoggableProviderKey([]byte(s)); err == nil {
t.Errorf("expected to fail formatting: %s", s)
}
}
Expand Down

0 comments on commit 07952a5

Please sign in to comment.