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
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")
......@@ -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
......@@ -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)
}
......@@ -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")
}
......@@ -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)
}
}
......
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