merkledag: retain cid types when roundtripping through a ProtoNode

License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
This commit is contained in:
Jeromy 2016-11-29 10:15:40 -08:00
parent f81cccc3fc
commit cd6d2d24f4
3 changed files with 17 additions and 2 deletions

View File

@ -7,7 +7,7 @@ import (
pb "github.com/ipfs/go-ipfs/merkledag/pb"
node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
)
@ -84,7 +84,18 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
}
if n.cached == nil {
n.cached = cid.NewCidV0(u.Hash(n.encoded))
if n.prefix.MhType == 0 { // unset
n.prefix.Codec = cid.DagProtobuf
n.prefix.MhLength = -1
n.prefix.MhType = mh.SHA2_256
n.prefix.Version = 0
}
c, err := n.prefix.Sum(n.encoded)
if err != nil {
return nil, err
}
n.cached = c
}
return n.encoded, nil

View File

@ -103,6 +103,7 @@ func decodeBlock(b blocks.Block) (node.Node, error) {
}
decnd.cached = b.Cid()
decnd.prefix = b.Cid().Prefix()
return decnd, nil
case cid.Raw:
return NewRawNode(b.RawData()), nil

View File

@ -22,6 +22,9 @@ type ProtoNode struct {
encoded []byte
cached *cid.Cid
// prefix specifies cid version and hashing function
prefix cid.Prefix
}
type LinkSlice []*node.Link