merkledag: retain cid types when roundtripping through a ProtoNode
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Nov 29, 2016 at 10:15 UTC
cd6d2d24f475dfac5001b46cde23c52cd15a8175
3 files changed
+17
-2
merkledag/coding.go
+13
-2
@@ -7,7 +7,7 @@ import (
7
pb "github.com/ipfs/go-ipfs/merkledag/pb"
8
9
node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
10
- u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
10
+ mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
11
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
12
)
13
@@ -84,7 +84,18 @@ func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
84
}
85
86
if n.cached == nil {
87
- n.cached = cid.NewCidV0(u.Hash(n.encoded))
87
+ if n.prefix.MhType == 0 { // unset
88
+ n.prefix.Codec = cid.DagProtobuf
89
+ n.prefix.MhLength = -1
90
+ n.prefix.MhType = mh.SHA2_256
91
+ n.prefix.Version = 0
92
+ }
93
+ c, err := n.prefix.Sum(n.encoded)
94
+ if err != nil {
95
+ return nil, err
96
+ }
97
+
98
+ n.cached = c
99
}
100
101
return n.encoded, nil
merkledag/merkledag.go
+1
@@ -103,6 +103,7 @@ func decodeBlock(b blocks.Block) (node.Node, error) {
103
}
104
105
decnd.cached = b.Cid()
106
+ decnd.prefix = b.Cid().Prefix()
107
return decnd, nil
108
case cid.Raw:
109
return NewRawNode(b.RawData()), nil
merkledag/node.go
+3
@@ -22,6 +22,9 @@ type ProtoNode struct {
22
encoded []byte
23
24
cached *cid.Cid
25
+
26
+ // prefix specifies cid version and hashing function
27
+ prefix cid.Prefix
28
}
29
30
type LinkSlice []*node.Link