-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix key log encoding #682
Merged
Merged
Fix key log encoding #682
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1071ec3
feat: refactor key logging
aschmahmann 4bae013
refactor: rename lookup_test.go to logging_test.go since it now only …
aschmahmann ce4c8c8
logging: more uniformly output lowercase b32 encoded keys
aschmahmann b213268
log with multibase
aschmahmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously we always returned the key whenever there was an error (e.g. empty string, invalid path, etc.) now we return error messages. |
||
} | ||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe tryParseAndFormat...