coding.go 3.38 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"

Steven Allen's avatar
Steven Allen committed
8
	"gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
9

10
	pb "github.com/ipfs/go-ipfs/merkledag/pb"
Jeromy's avatar
Jeromy committed
11

Steven Allen's avatar
Steven Allen committed
12 13
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
	node "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16 17 18
)

// for now, we use a PBNode intermediate thing.
// because native go objects are nice.

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

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

39 40
	n.data = pbn.GetData()
	n.encoded = encoded
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
41
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43
}

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

55
func (n *ProtoNode) getPBNode() *pb.PBNode {
56
	pbn := &pb.PBNode{}
57 58
	if len(n.links) > 0 {
		pbn.Links = make([]*pb.PBLink, len(n.links))
Jeromy's avatar
Jeromy committed
59
	}
60

61 62
	sort.Stable(LinkSlice(n.links)) // keep links sorted
	for i, l := range n.links {
63
		pbn.Links[i] = &pb.PBLink{}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
64 65
		pbn.Links[i].Name = &l.Name
		pbn.Links[i].Tsize = &l.Size
Jeromy's avatar
Jeromy committed
66 67 68
		if l.Cid != nil {
			pbn.Links[i].Hash = l.Cid.Bytes()
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
69 70
	}

71 72
	if len(n.data) > 0 {
		pbn.Data = n.data
Jeromy's avatar
Jeromy committed
73
	}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
74
	return pbn
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76

77
// EncodeProtobuf returns the encoded raw data version of a Node instance.
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
78
// It may use a cached encoded version, unless the force flag is given.
79 80
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
81
	if n.encoded == nil || force {
82
		n.cached = nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84 85
		var err error
		n.encoded, err = n.Marshal()
		if err != nil {
86
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
		}
88 89 90
	}

	if n.cached == nil {
91
		if n.Prefix.Codec == 0 { // unset
92
			n.Prefix = v0CidPrefix
93
		}
94
		c, err := n.Prefix.Sum(n.encoded)
95 96 97 98 99
		if err != nil {
			return nil, err
		}

		n.cached = c
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100 101 102 103
	}

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
105
// Decoded decodes raw data and returns a new Node instance.
106 107
func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
	n := new(ProtoNode)
108
	err := n.unmarshal(encoded)
109 110 111 112
	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
113
}
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

// DecodeProtobufBlock is a block decoder for protobuf IPLD nodes conforming to
// node.DecodeBlockFunc
func DecodeProtobufBlock(b blocks.Block) (node.Node, error) {
	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") {
			return nil, fmt.Errorf("The block referred to by '%s' was not a valid merkledag node", c)
		}
		return nil, fmt.Errorf("Failed to decode Protocol Buffers: %v", err)
	}

	decnd.cached = c
	decnd.Prefix = c.Prefix()
	return decnd, nil
}

// Type assertion
var _ node.DecodeBlockFunc = DecodeProtobufBlock