-
Notifications
You must be signed in to change notification settings - Fork 59
continuation of conversion of multihash to string #84
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
Conversation
multihash.go
Outdated
return Multihash{v} | ||
} | ||
|
||
// Parts returns the components of the Multihash (Code, Name, Digest) |
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.
(Code, Length, Digest)
. However, really, we can just return (Code, Digest)
.
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.
Yeah your right since the length is primary useful in the binary representation as a sanity check and to know when the multihash ends in a byte stream.
multihash.go
Outdated
// representation. The string is assumed to be a valid multihash. | ||
// This function will not alloc and will panic if it is unable to | ||
// parse the string. | ||
func FromBinary(v string) Multihash { |
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.
Any reason not to have this return an error? I don't see this being called so often checking an error becomes onerous. For example, I expect we'll want to use this from go-cid when validating the CID's multihash.
(It may also be useful to have a FromBinaryUnchecked
that doesn't even check but that's a separate issue).
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.
I wanted something that was fast and convenient to use when we know the multihash is already valid. The error checking that is done are sanity checks. For example if the hash length is incorrect or bogus this will likely panic due to an out of bounds index check rather than return a proper error message.
I am somewhat against FromBinaryUnchecked if we can get away with (performance wise) always doing the checks. This way we know that the Multihash is valid if it is not empty.
See ipfs/go-cid#73 for how I use this. I am less against fixing this to return an error.
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.
Got it. I'd like to expose some FromBinary
function that returns an error. Currently, go-cid
will have to allocate to check the multihash. That is, it'll:
- Pass a
[]byte
intomh.Cast
. mh.Cast
will return astring
backed multihash (allocating).
A FromBinary
that returns an error would allow us to check without panicing or allocating.
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.
Okay. I created such a function and called it New
.
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.
Why New
? It doesn't really create a new multihash.
I am somewhat against FromBinaryUnchecked if we can get away with (performance wise) always doing the checks. This way we know that the Multihash is valid if it is not empty.
But we aren't always doing the checks! FromString
leaves out one of the checks. Let's either check everything or nothing, checking somethings buys us nothing and just adds a bunch of code and two confusing functions that do slightly different things.
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.
Okay fair enough. I renamed New to FromBinary when the Cid needs to return a Multihash it can just ignore the errors.
If necessary e can add an unchecked version if this becomes a performance problem (which I doubt).
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.
SGTM. In that case, we can also profile which checks may still be worth keeping (although, in the method contract, I'd still just say "may panic, may not, don't feed us bad data"; debug checks shouldn't be relied on).
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.
I'm confused, what are you advocating. A FromBinary() method function that returns an error or a FromBinary() function that panics?
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.
As you said, we can consider adding an unchecked variant later. My point was that we can actually put "checks" in the unchecked version (as long as they're fast enough) as long as we don't make any promises. My issue with the previous FromBinary
incarnation is that it sort of checked and made unclear guarantees. I'd rather have a fully checked version (that returns the error) and a version that makes absolutely no guarantees (but can choose to check under the covers).
TL;DR: I'm happy with the current version, we can take a look into a less checked variant later.
5ff5074
to
e430586
Compare
@Stebalien Do you want to get this is now with the Cid changes? |
If you feel secure in your gx-fu. The package list is quite long:
The package list for go-cid is only:
Regardless, let's be very careful not to get stuck half-way (i.e., no pushing to master until the end). |
@Stebalien I can try (the full gx-update with multihash). It may be helpful if I do it at a time when you are around so you can walk me though and make sure everything looks good. So that I am confident you are okay with all the changes please Approve this p.r., ipfs/go-cid#71 and ipfs/go-cid#73. |
Only @kevina should merge this. CI fails because this breaks go-libp2p-peer. @kevina modulo the one comment on go-cid, this all looks good to me. I'd:
|
If there are any uses that depends on Multihash being []byte this could be useful. The Encode() function already returns a []byte.
db8c21f
to
6111bf9
Compare
Closing due to being stale since 2018 + merge conflicts. |
Continuation of #83 using parts of #82.