mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-03 23:38:07 +08:00
merkledag: retain cid types when roundtripping through a ProtoNode
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
This commit is contained in:
parent
f81cccc3fc
commit
cd6d2d24f4
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user