Skip to content

Commit

Permalink
log with multibase
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann authored and petar committed Aug 14, 2020
1 parent ce4c8c8 commit b213268
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
19 changes: 12 additions & 7 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import (
"strings"

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

func lowercaseB32Encode(k []byte) string {
return strings.ToLower(base32.RawStdEncoding.EncodeToString(k))
func multibaseB32Encode(k []byte) string {
res, err := multibase.Encode(multibase.Base32, k)
if err != nil {
// Should be unreachable
panic(err)
}
return res
}

func tryFormatLoggableRecordKey(k string) (string, error) {
Expand All @@ -22,16 +27,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", lowercaseB32Encode([]byte(k)))
return "", fmt.Errorf("loggableRecordKey starts with '/' but is not a path: %s", multibaseB32Encode([]byte(k)))
}
proto = k[1 : protoEnd+1]
cstr = k[protoEnd+2:]

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

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

type loggableRecordKeyString string
Expand Down Expand Up @@ -71,7 +76,7 @@ func tryFormatLoggableProviderKey(k []byte) (string, error) {
return "", fmt.Errorf("loggableProviderKey is empty")
}

encodedKey := lowercaseB32Encode(k)
encodedKey := multibaseB32Encode(k)

// The DHT used to provide CIDs, but now provides multihashes
// TODO: Drop this when enough of the network has upgraded
Expand Down
4 changes: 2 additions & 2 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/"+lowercaseB32Encode(c.Bytes()) {
if k != "/proto/"+multibaseB32Encode(c.Bytes()) {
t.Error("expected path to be preserved as a loggable key")
}

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

// Test logging CIDv0 provider
b32MH := lowercaseB32Encode(c0.Hash())
b32MH := multibaseB32Encode(c0.Hash())
k, err := tryFormatLoggableProviderKey(c0.Bytes())
if err != nil {
t.Errorf("failed to format key: %s", err)
Expand Down

0 comments on commit b213268

Please sign in to comment.