Skip to content

Commit

Permalink
merkledag: retain cid types when roundtripping through a ProtoNode
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Nov 29, 2016
1 parent f81cccc commit cd6d2d2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
15 changes: 13 additions & 2 deletions merkledag/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions merkledag/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd6d2d2

Please sign in to comment.