coding.go 3.67 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package merkledag

import (
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
4
	"fmt"
5
	"sort"
6 7
	"strings"

postables's avatar
postables committed
8
	blocks "github.com/ipfs/go-block-format"
Jeromy's avatar
Jeromy committed
9
	pb "github.com/ipfs/go-merkledag/pb"
Jeromy's avatar
Jeromy committed
10

Jeromy's avatar
Jeromy committed
11 12
	cid "github.com/ipfs/go-cid"
	ipld "github.com/ipfs/go-ipld-format"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
)

15 16 17 18 19
// Make sure the user doesn't upgrade this file.
// We need to check *here* as well as inside the `pb` package *just* in case the
// user replaces *all* go files in that package.
const _ = pb.DoNotUpgradeFileEverItWillChangeYourHashes

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22
// for now, we use a PBNode intermediate thing.
// because native go objects are nice.

23
// unmarshal decodes raw data into a *Node instance.
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
24
// The conversion uses an intermediate PBNode.
25
func (n *ProtoNode) unmarshal(encoded []byte) error {
26
	var pbn pb.PBNode
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
27
	if err := pbn.Unmarshal(encoded); err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
28
		return fmt.Errorf("unmarshal failed. %v", err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
29 30 31
	}

	pbnl := pbn.GetLinks()
32
	n.links = make([]*ipld.Link, len(pbnl))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
33
	for i, l := range pbnl {
34
		n.links[i] = &ipld.Link{Name: l.GetName(), Size: l.GetTsize()}
35
		c, err := cid.Cast(l.GetHash())
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
36
		if err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
37
			return fmt.Errorf("link hash #%d is not valid multihash. %v", i, err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
38
		}
39
		n.links[i].Cid = c
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
40
	}
41
	sort.Stable(LinkSlice(n.links)) // keep links sorted
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
42

43 44
	n.data = pbn.GetData()
	n.encoded = encoded
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
45
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
48 49
// Marshal encodes a *Node instance into a new byte slice.
// The conversion uses an intermediate PBNode.
50
func (n *ProtoNode) Marshal() ([]byte, error) {
postables's avatar
postables committed
51
	pbn := n.GetPBNode()
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
52 53
	data, err := pbn.Marshal()
	if err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
54
		return data, fmt.Errorf("marshal failed. %v", err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
55 56
	}
	return data, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58
}

postables's avatar
postables committed
59 60 61 62
// GetPBNode converts *ProtoNode into it's protocol buffer variant.
// If you plan on mutating the data of the original node, it is recommended
// that you call ProtoNode.Copy() before calling ProtoNode.GetPBNode()
func (n *ProtoNode) GetPBNode() *pb.PBNode {
63
	pbn := &pb.PBNode{}
64 65
	if len(n.links) > 0 {
		pbn.Links = make([]*pb.PBLink, len(n.links))
Jeromy's avatar
Jeromy committed
66
	}
67

68 69
	sort.Stable(LinkSlice(n.links)) // keep links sorted
	for i, l := range n.links {
70
		pbn.Links[i] = &pb.PBLink{}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
71 72
		pbn.Links[i].Name = &l.Name
		pbn.Links[i].Tsize = &l.Size
73
		if l.Cid.Defined() {
Jeromy's avatar
Jeromy committed
74 75
			pbn.Links[i].Hash = l.Cid.Bytes()
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
76 77
	}

78 79
	if len(n.data) > 0 {
		pbn.Data = n.data
Jeromy's avatar
Jeromy committed
80
	}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
81
	return pbn
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83

84
// EncodeProtobuf returns the encoded raw data version of a Node instance.
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
85
// It may use a cached encoded version, unless the force flag is given.
86 87
func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
	sort.Stable(LinkSlice(n.links)) // keep links sorted
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
88
	if n.encoded == nil || force {
89
		n.cached = cid.Undef
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92
		var err error
		n.encoded, err = n.Marshal()
		if err != nil {
93
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
		}
95 96
	}

97
	if !n.cached.Defined() {
98
		c, err := n.CidBuilder().Sum(n.encoded)
99 100 101 102 103
		if err != nil {
			return nil, err
		}

		n.cached = c
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104 105 106 107
	}

	return n.encoded, nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108

109
// DecodeProtobuf decodes raw data and returns a new Node instance.
110 111
func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
	n := new(ProtoNode)
112
	err := n.unmarshal(encoded)
113 114 115 116
	if err != nil {
		return nil, fmt.Errorf("incorrectly formatted merkledag node: %s", err)
	}
	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117
}
118 119 120

// DecodeProtobufBlock is a block decoder for protobuf IPLD nodes conforming to
// node.DecodeBlockFunc
121
func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) {
122 123 124 125 126 127 128 129
	c := b.Cid()
	if c.Type() != cid.DagProtobuf {
		return nil, fmt.Errorf("this function can only decode protobuf nodes")
	}

	decnd, err := DecodeProtobuf(b.RawData())
	if err != nil {
		if strings.Contains(err.Error(), "Unmarshal failed") {
Łukasz Magiera's avatar
Łukasz Magiera committed
130
			return nil, fmt.Errorf("the block referred to by '%s' was not a valid merkledag node", c)
131
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
132
		return nil, fmt.Errorf("failed to decode Protocol Buffers: %v", err)
133 134 135
	}

	decnd.cached = c
136
	decnd.builder = c.Prefix()
137 138 139 140
	return decnd, nil
}

// Type assertion
141
var _ ipld.DecodeBlockFunc = DecodeProtobufBlock