Commit 48143f3d authored by Hector Sanjuan's avatar Hector Sanjuan

Remove DsKeyToCid() and CidToDsKey(). Add DsKeyToCidV1Raw()

We are deprecating these functions as ipfs strives to store
multihashes directly on the datastore, rather than CIDs.

Thus multiple CIDs might map to the same multihash. Making this functions
reflect that behaviour might cause silent breakage in users. We prefer loud
breakage.

Users using CidToDsKey(c) should move to MultihashToDsKey(c.Hash()).

Users using DsKeyToCid() should move to DsKeyToCidV1Raw() or DsKeyToMultihash().

Note that datastore should handle migrations to replace existing CID-encoded
keys with raw multihashes, or preserve DsKeyToCid() and CidToDsKey() in their
own codebases.
parent 11890cc8
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
package dshelp package dshelp
import ( import (
cid "github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore"
"github.com/multiformats/go-base32" "github.com/multiformats/go-base32"
mh "github.com/multiformats/go-multihash" mh "github.com/multiformats/go-multihash"
...@@ -23,6 +23,9 @@ func BinaryFromDsKey(k datastore.Key) ([]byte, error) { ...@@ -23,6 +23,9 @@ func BinaryFromDsKey(k datastore.Key) ([]byte, error) {
} }
// MultihashToDsKey creates a Key from the given Multihash. // MultihashToDsKey creates a Key from the given Multihash.
// If working with Cids, you can call cid.Hash() to obtain
// the multihash. Note that different CIDs might represent
// the same multihash.
func MultihashToDsKey(k mh.Multihash) datastore.Key { func MultihashToDsKey(k mh.Multihash) datastore.Key {
return NewKeyFromBinary(k) return NewKeyFromBinary(k)
} }
...@@ -36,16 +39,12 @@ func DsKeyToMultihash(dsKey datastore.Key) (mh.Multihash, error) { ...@@ -36,16 +39,12 @@ func DsKeyToMultihash(dsKey datastore.Key) (mh.Multihash, error) {
return mh.Cast(kb) return mh.Cast(kb)
} }
// CidToDsKey creates a Key from the given Cid. // DsKeyToCidV1Raw converts the given Key (which should be a raw multihash
func CidToDsKey(k cid.Cid) datastore.Key { // key) to a Cid V1 of raw type.
return MultihashToDsKey(k.Hash()) func DsKeyToCidV1Raw(dsKey datastore.Key) (cid.Cid, error) {
}
// DsKeyToCid converts the given Key to its corresponding Cid.
func DsKeyToCid(dsKey datastore.Key) (cid.Cid, error) {
hash, err := DsKeyToMultihash(dsKey) hash, err := DsKeyToMultihash(dsKey)
if err != nil { if err != nil {
return cid.Cid{}, nil return cid.Cid{}, err
} }
return cid.NewCidV1(cid.Raw, hash), nil return cid.NewCidV1(cid.Raw, hash), nil
} }
...@@ -8,15 +8,17 @@ import ( ...@@ -8,15 +8,17 @@ import (
func TestKey(t *testing.T) { func TestKey(t *testing.T) {
c, _ := cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmq") c, _ := cid.Decode("QmP63DkAFEnDYNjDYBpyNDfttu1fvUw99x1brscPzpqmmq")
dsKey := CidToDsKey(c) dsKey := MultihashToDsKey(c.Hash())
c2, err := DsKeyToCid(dsKey) mh, err := DsKeyToMultihash(dsKey)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if string(c.Hash()) != string(c2.Hash()) { if string(c.Hash()) != string(mh) {
t.Fatal("should have parsed the same key") t.Fatal("should have parsed the same multihash")
} }
if c.Equals(c2) || c2.Type() != cid.Raw || c2.Version() != 1 {
c2, err := DsKeyToCidV1Raw(dsKey)
if err != nil || c.Equals(c2) || c2.Type() != cid.Raw || c2.Version() != 1 {
t.Fatal("should have been converted to CIDv1-raw") t.Fatal("should have been converted to CIDv1-raw")
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment