Commit e5a1a075 authored by Jeromy's avatar Jeromy

merkledag: retain cid types when roundtripping through a ProtoNode

License: MIT
Signed-off-by: default avatarJeromy <why@ipfs.io>
parent 119ca42b
......@@ -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
......
......@@ -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
......
......@@ -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
......
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