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
ld
go-ld-prime
Commits
98883737
Unverified
Commit
98883737
authored
Aug 29, 2020
by
Eric Myhre
Committed by
GitHub
Aug 29, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #70 from ipld/fix/byte-mem-test-rebased
fix(cidlink): check for byte buffer
parents
e2992c32
ead913a2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
12 deletions
+27
-12
linking/cid/cidLink.go
linking/cid/cidLink.go
+27
-12
No files found.
linking/cid/cidLink.go
View file @
98883737
...
...
@@ -19,6 +19,11 @@ type Link struct {
cid
.
Cid
}
// byteAccessor is a reader interface that can access underlying bytes
type
byteAccesor
interface
{
Bytes
()
[]
byte
}
func
(
lnk
Link
)
Load
(
ctx
context
.
Context
,
lnkCtx
ipld
.
LinkContext
,
na
ipld
.
NodeAssembler
,
loader
ipld
.
Loader
)
error
{
// Open the byte reader.
r
,
err
:=
loader
(
lnk
,
lnkCtx
)
...
...
@@ -30,20 +35,30 @@ func (lnk Link) Load(ctx context.Context, lnkCtx ipld.LinkContext, na ipld.NodeA
if
!
exists
{
return
fmt
.
Errorf
(
"no decoder registered for multicodec %d"
,
lnk
.
Prefix
()
.
Codec
)
}
var
hasher
bytes
.
Buffer
// multihash only exports bulk use, which is... really inefficient and should be fixed.
decodeErr
:=
mcDecoder
(
na
,
io
.
TeeReader
(
r
,
&
hasher
))
// Error checking order here is tricky.
// If decoding errored out, we should still run the reader to the end, to check the hash.
// (We still don't implement this by running the hash to the end first, because that would increase the high-water memory requirement.)
// ((Which we experience right now anyway because multihash's interface is silly, but we're acting as if that's fixed or will be soon.))
// If the hash is rejected, we should return that error (and even if there was a decodeErr, it becomes irrelevant).
if
decodeErr
!=
nil
{
_
,
err
:=
io
.
Copy
(
&
hasher
,
r
)
if
err
!=
nil
{
return
err
var
hasherBytes
[]
byte
var
decodeErr
error
byteBuf
,
ok
:=
r
.
(
byteAccesor
)
if
ok
{
hasherBytes
=
byteBuf
.
Bytes
()
decodeErr
=
mcDecoder
(
na
,
r
)
}
else
{
var
hasher
bytes
.
Buffer
// multihash only exports bulk use, which is... really inefficient and should be fixed.
decodeErr
=
mcDecoder
(
na
,
io
.
TeeReader
(
r
,
&
hasher
))
// Error checking order here is tricky.
// If decoding errored out, we should still run the reader to the end, to check the hash.
// (We still don't implement this by running the hash to the end first, because that would increase the high-water memory requirement.)
// ((Which we experience right now anyway because multihash's interface is silly, but we're acting as if that's fixed or will be soon.))
// If the hash is rejected, we should return that error (and even if there was a decodeErr, it becomes irrelevant).
if
decodeErr
!=
nil
{
_
,
err
:=
io
.
Copy
(
&
hasher
,
r
)
if
err
!=
nil
{
return
err
}
}
hasherBytes
=
hasher
.
Bytes
()
}
cid
,
err
:=
lnk
.
Prefix
()
.
Sum
(
hasher
.
Bytes
())
cid
,
err
:=
lnk
.
Prefix
()
.
Sum
(
hasherBytes
)
if
err
!=
nil
{
return
err
}
...
...
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