-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #682 from libp2p/fix/key-log-encoding
Fix key log encoding
- Loading branch information
Showing
9 changed files
with
201 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package dht | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/multiformats/go-multibase" | ||
"github.com/multiformats/go-multihash" | ||
) | ||
|
||
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) { | ||
if len(k) == 0 { | ||
return "", fmt.Errorf("loggableRecordKey is empty") | ||
} | ||
var proto, cstr string | ||
if k[0] == '/' { | ||
// 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", multibaseB32Encode([]byte(k))) | ||
} | ||
proto = k[1 : protoEnd+1] | ||
cstr = k[protoEnd+2:] | ||
|
||
encStr := multibaseB32Encode([]byte(cstr)) | ||
return fmt.Sprintf("/%s/%s", proto, encStr), nil | ||
} | ||
|
||
return "", fmt.Errorf("loggableRecordKey is not a path: %s", multibaseB32Encode([]byte(cstr))) | ||
} | ||
|
||
type loggableRecordKeyString string | ||
|
||
func (lk loggableRecordKeyString) String() string { | ||
k := string(lk) | ||
newKey, err := tryFormatLoggableRecordKey(k) | ||
if err == nil { | ||
return newKey | ||
} | ||
return err.Error() | ||
} | ||
|
||
type loggableRecordKeyBytes []byte | ||
|
||
func (lk loggableRecordKeyBytes) String() string { | ||
k := string(lk) | ||
newKey, err := tryFormatLoggableRecordKey(k) | ||
if err == nil { | ||
return newKey | ||
} | ||
return err.Error() | ||
} | ||
|
||
type loggableProviderRecordBytes []byte | ||
|
||
func (lk loggableProviderRecordBytes) String() string { | ||
newKey, err := tryFormatLoggableProviderKey(lk) | ||
if err == nil { | ||
return newKey | ||
} | ||
return err.Error() | ||
} | ||
|
||
func tryFormatLoggableProviderKey(k []byte) (string, error) { | ||
if len(k) == 0 { | ||
return "", fmt.Errorf("loggableProviderKey is empty") | ||
} | ||
|
||
encodedKey := multibaseB32Encode(k) | ||
|
||
// The DHT used to provide CIDs, but now provides multihashes | ||
// TODO: Drop this when enough of the network has upgraded | ||
if _, err := cid.Cast(k); err == nil { | ||
return encodedKey, nil | ||
} | ||
|
||
if _, err := multihash.Cast(k); err == nil { | ||
return encodedKey, nil | ||
} | ||
|
||
return "", fmt.Errorf("loggableProviderKey is not a Multihash or CID: %s", encodedKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dht | ||
|
||
import ( | ||
"testing" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
) | ||
|
||
func TestLoggableRecordKey(t *testing.T) { | ||
c, err := cid.Decode("QmfUvYQhL2GinafMbPDYz7VFoZv4iiuLuR33aRsPurXGag") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
k, err := tryFormatLoggableRecordKey("/proto/" + string(c.Bytes())) | ||
if err != nil { | ||
t.Errorf("failed to format key: %s", err) | ||
} | ||
if k != "/proto/"+multibaseB32Encode(c.Bytes()) { | ||
t.Error("expected path to be preserved as a loggable key") | ||
} | ||
|
||
for _, s := range []string{"/bla", "", "bla bla"} { | ||
if _, err := tryFormatLoggableRecordKey(s); err == nil { | ||
t.Errorf("expected to fail formatting: %s", s) | ||
} | ||
} | ||
|
||
for _, s := range []string{"/bla/asdf", "/a/b/c"} { | ||
if _, err := tryFormatLoggableRecordKey(s); err != nil { | ||
t.Errorf("expected to be formatable: %s", s) | ||
} | ||
} | ||
} | ||
|
||
func TestLoggableProviderKey(t *testing.T) { | ||
c0, err := cid.Decode("QmfUvYQhL2GinafMbPDYz7VFoZv4iiuLuR33aRsPurXGag") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Test logging CIDv0 provider | ||
b32MH := multibaseB32Encode(c0.Hash()) | ||
k, err := tryFormatLoggableProviderKey(c0.Bytes()) | ||
if err != nil { | ||
t.Errorf("failed to format key: %s", err) | ||
} | ||
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(c1.Hash()) | ||
if err != nil { | ||
t.Errorf("failed to format key: %s", err) | ||
} | ||
if k != b32MH { | ||
t.Error("expected cidv1 to be converted into base32 multihash") | ||
} | ||
|
||
// Test logging multihash provider | ||
k, err = tryFormatLoggableProviderKey(c1.Hash()) | ||
if err != nil { | ||
t.Errorf("failed to format key: %s", err) | ||
} | ||
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([]byte(s)); err == nil { | ||
t.Errorf("expected to fail formatting: %s", s) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.