Skip to content
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

continuation of conversion of multihash to string #84

Closed
wants to merge 8 commits into from

Conversation

kevina
Copy link
Contributor

@kevina kevina commented Aug 30, 2018

Continuation of #83 using parts of #82.

@kevina kevina changed the title continuration of conversion of multihash to string continuation of conversion of multihash to string Aug 30, 2018
@kevina kevina added the status/deferred Conscious decision to pause or backlog label Aug 30, 2018
multihash.go Outdated
return Multihash{v}
}

// Parts returns the components of the Multihash (Code, Name, Digest)
Copy link
Member

@Stebalien Stebalien Aug 30, 2018

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).

Copy link
Contributor Author

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 {
Copy link
Member

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).

Copy link
Contributor Author

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.

Copy link
Member

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:

  1. Pass a []byte into mh.Cast.
  2. mh.Cast will return a string backed multihash (allocating).

A FromBinary that returns an error would allow us to check without panicing or allocating.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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).

Copy link
Member

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).

Copy link
Contributor Author

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?

Copy link
Member

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.

@kevina
Copy link
Contributor Author

kevina commented Aug 31, 2018

@Stebalien Do you want to get this is now with the Cid changes?

@Stebalien
Copy link
Member

If you feel secure in your gx-fu. The package list is quite long:

go-libp2p-peer
go-multiaddr
go-multiaddr-net
mafmt
go-libp2p-peerstore
go-libp2p-net
go-conn-security
go-libp2p-secio
iptb
go-ipfs-util
go-libp2p-loggables
go-libp2p-transport
go-reuseport-transport
go-maddr-filter
go-libp2p-transport-upgrader
go-tcp-transport
go-testutil
go-libp2p-metrics
go-libp2p-interface-connmgr
go-libp2p-host
go-addr-util
go-conn-security-multistream
go-libp2p-swarm
go-libp2p-nat
go-libp2p-netutil
go-libp2p-blankhost
go-libp2p-circuit
go-multiaddr-dns
go-ws-transport
go-libp2p
go-cid
go-libp2p-record
go-libp2p-kbucket
go-libp2p-routing
go-libp2p-kad-dht
hang-fds
go-libp2p-floodsub
go-block-format
go-ipld-format
go-ipld-cbor
go-ipfs-cmds
go-ipld-git
go-libp2p-connmgr
go-ipfs-addr
go-ipfs-posinfo
go-ipfs-chunker
go-ipfs-ds-help
go-ipfs-routing
go-ipfs-blockstore
go-fs-lock
go-ipfs-exchange-interface
go-ipfs-blocksutil
go-ipfs-exchange-offline
go-libp2p-routing-helpers
go-libp2p-pubsub-router
go-ipns
go-ipfs

The package list for go-cid is only:

go-libp2p-routing
go-libp2p-kad-dht
go-block-format
go-ipld-format
go-ipld-cbor
go-ipld-git
go-ipfs-posinfo
go-ipfs-chunker
go-ipfs-ds-help
go-ipfs-routing
go-ipfs-blockstore
go-ipfs-exchange-interface
go-ipfs-blocksutil
go-ipfs-exchange-offline
go-libp2p-routing-helpers
go-libp2p-pubsub-router
go-ipfs

Regardless, let's be very careful not to get stuck half-way (i.e., no pushing to master until the end).

@kevina kevina requested a review from Stebalien August 31, 2018 04:54
@kevina
Copy link
Contributor Author

kevina commented Aug 31, 2018

@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.

@kevina kevina changed the base branch from feat/steb-mh-string to master August 31, 2018 05:15
@Stebalien
Copy link
Member

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:

  1. Start by not using gx. Instead, just fix all the appropriate packages (picking up where I left off), testing with go.
  2. Once that all works, we can use gx-workspace to make this happen (if it looks too gnarly, I can take a stab at this; this'll be interesting...).

@lidel lidel marked this pull request as draft September 15, 2021 14:04
@lidel
Copy link
Member

lidel commented Sep 15, 2021

Closing due to being stale since 2018 + merge conflicts.

@lidel lidel closed this Sep 15, 2021
@lidel lidel deleted the kevina/steb-mh-string branch September 15, 2021 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/deferred Conscious decision to pause or backlog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants