Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kad-dht
Commits
ce4c8c88
Commit
ce4c8c88
authored
Jul 17, 2020
by
Adin Schmahmann
Committed by
Petar Maymounkov
Aug 14, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logging: more uniformly output lowercase b32 encoded keys
parent
4bae0138
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
45 deletions
+30
-45
logging.go
logging.go
+18
-32
logging_test.go
logging_test.go
+12
-13
No files found.
logging.go
View file @
ce4c8c88
...
...
@@ -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"
,
b
ase32
.
RawStdEncoding
.
EncodeToString
([]
byte
(
k
)))
return
""
,
fmt
.
Errorf
(
"loggableRecordKey starts with '/' but is not a path: %s"
,
lowerc
ase
B
32
Encode
([]
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"
,
b
ase32
.
RawStdEncoding
.
EncodeToString
([]
byte
(
k
)))
return
""
,
fmt
.
Errorf
(
"loggableRecordKey is not a path: %s"
,
lowerc
ase
B
32
Encode
([]
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
)
}
logging_test.go
View file @
ce4c8c88
...
...
@@ -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
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment