Commit ce4c8c88 authored by Adin Schmahmann's avatar Adin Schmahmann Committed by Petar Maymounkov

logging: more uniformly output lowercase b32 encoded keys

parent 4bae0138
...@@ -2,14 +2,17 @@ package dht ...@@ -2,14 +2,17 @@ package dht
import ( import (
"fmt" "fmt"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
"strings" "strings"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/multiformats/go-base32" "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) { func tryFormatLoggableRecordKey(k string) (string, error) {
if len(k) == 0 { if len(k) == 0 {
return "", fmt.Errorf("loggableRecordKey is empty") return "", fmt.Errorf("loggableRecordKey is empty")
...@@ -19,23 +22,16 @@ func tryFormatLoggableRecordKey(k string) (string, error) { ...@@ -19,23 +22,16 @@ func tryFormatLoggableRecordKey(k string) (string, error) {
// it's a path (probably) // it's a path (probably)
protoEnd := strings.IndexByte(k[1:], '/') protoEnd := strings.IndexByte(k[1:], '/')
if protoEnd < 0 { 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] proto = k[1 : protoEnd+1]
cstr = k[protoEnd+2:] cstr = k[protoEnd+2:]
var encStr string encStr := lowercaseB32Encode([]byte(cstr))
c, err := cid.Cast([]byte(cstr))
if err == nil {
encStr = c.String()
} else {
encStr = base32.RawStdEncoding.EncodeToString([]byte(cstr))
}
return fmt.Sprintf("/%s/%s", proto, encStr), nil 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 type loggableRecordKeyString string
...@@ -63,39 +59,29 @@ func (lk loggableRecordKeyBytes) String() string { ...@@ -63,39 +59,29 @@ func (lk loggableRecordKeyBytes) String() string {
type loggableProviderRecordBytes []byte type loggableProviderRecordBytes []byte
func (lk loggableProviderRecordBytes) String() string { func (lk loggableProviderRecordBytes) String() string {
k := string(lk) newKey, err := tryFormatLoggableProviderKey(lk)
newKey, err := tryFormatLoggableProviderKey(k)
if err == nil { if err == nil {
return newKey return newKey
} }
return err.Error() return err.Error()
} }
func tryFormatLoggableProviderKey(k string) (string, error) { func tryFormatLoggableProviderKey(k []byte) (string, error) {
if len(k) == 0 { if len(k) == 0 {
return "", fmt.Errorf("loggableProviderKey is empty") return "", fmt.Errorf("loggableProviderKey is empty")
} }
h, err := multihash.Cast([]byte(k)) encodedKey := lowercaseB32Encode(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
}
// The DHT used to provide CIDs, but now provides multihashes // The DHT used to provide CIDs, but now provides multihashes
// TODO: Drop this when enough of the network has upgraded // TODO: Drop this when enough of the network has upgraded
c, err := cid.Cast([]byte(k)) if _, err := cid.Cast(k); err == nil {
if err == nil { return encodedKey, nil
encStr, err := c.StringOfBase(multibase.Base32) }
if err != nil {
panic(fmt.Errorf("should be impossible to reach here : %w", err)) if _, err := multihash.Cast(k); err == nil {
} return encodedKey, nil
return encStr, 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)
} }
...@@ -16,7 +16,7 @@ func TestLoggableRecordKey(t *testing.T) { ...@@ -16,7 +16,7 @@ func TestLoggableRecordKey(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("failed to format key: %s", err) 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") t.Error("expected path to be preserved as a loggable key")
} }
...@@ -40,37 +40,36 @@ func TestLoggableProviderKey(t *testing.T) { ...@@ -40,37 +40,36 @@ func TestLoggableProviderKey(t *testing.T) {
} }
// Test logging CIDv0 provider // Test logging CIDv0 provider
c0ascidv1Raw := cid.NewCidV1(cid.Raw, c0.Hash()) b32MH := lowercaseB32Encode(c0.Hash())
k, err := tryFormatLoggableProviderKey(string(c0.Bytes())) k, err := tryFormatLoggableProviderKey(c0.Bytes())
if err != nil { if err != nil {
t.Errorf("failed to format key: %s", err) t.Errorf("failed to format key: %s", err)
} }
if k != c0ascidv1Raw.String() { if k != b32MH {
t.Error("expected cidv0 to be converted into CIDv1 b32 with Raw codec") t.Error("expected cidv0 to be converted into base32 multihash")
} }
// Test logging CIDv1 provider (from older DHT implementations) // Test logging CIDv1 provider (from older DHT implementations)
c1 := cid.NewCidV1(cid.DagProtobuf, c0.Hash()) c1 := cid.NewCidV1(cid.DagProtobuf, c0.Hash())
k, err = tryFormatLoggableProviderKey(string(c1.Bytes())) k, err = tryFormatLoggableProviderKey(c1.Hash())
if err != nil { if err != nil {
t.Errorf("failed to format key: %s", err) t.Errorf("failed to format key: %s", err)
} }
if k != c1.String() { if k != b32MH {
t.Error("expected cidv1 to be displayed normally") t.Error("expected cidv1 to be converted into base32 multihash")
} }
// Test logging multihash provider // Test logging multihash provider
c1ascidv1Raw := cid.NewCidV1(cid.Raw, c1.Hash()) k, err = tryFormatLoggableProviderKey(c1.Hash())
k, err = tryFormatLoggableProviderKey(string(c1.Hash()))
if err != nil { if err != nil {
t.Errorf("failed to format key: %s", err) t.Errorf("failed to format key: %s", err)
} }
if k != c1ascidv1Raw.String() { if k != b32MH {
t.Error("expected multihash to be converted into CIDv1 b32 with Raw codec") t.Error("expected multihash to be displayed in base32")
} }
for _, s := range []string{"/bla", "", "bla bla", "/bla/asdf", "/a/b/c"} { 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) t.Errorf("expected to fail formatting: %s", s)
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment