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

Jeromy's avatar
Jeromy committed
8
	"github.com/ipfs/go-block-format"
9

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

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

16 17 18 19 20
// 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
21 22 23
// for now, we use a PBNode intermediate thing.
// because native go objects are nice.

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

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

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

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

60
func (n *ProtoNode) getPBNode() *pb.PBNode {
61
	pbn := &pb.PBNode{}
62 63
	if len(n.links) > 0 {
		pbn.Links = make([]*pb.PBLink, len(n.links))
Jeromy's avatar
Jeromy committed
64
	}
65

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

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

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

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

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

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

107
// DecodeProtobuf decodes raw data and returns a new Node instance.
108 109
func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
	n := new(ProtoNode)
110
	err := n.unmarshal(encoded)
111 112 113 114
	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
115
}
116 117 118

// DecodeProtobufBlock is a block decoder for protobuf IPLD nodes conforming to
// node.DecodeBlockFunc
119
func DecodeProtobufBlock(b blocks.Block) (ipld.Node, error) {
120 121 122 123 124 125 126 127
	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
128
			return nil, fmt.Errorf("the block referred to by '%s' was not a valid merkledag node", c)
129
		}
Łukasz Magiera's avatar
Łukasz Magiera committed
130
		return nil, fmt.Errorf("failed to decode Protocol Buffers: %v", err)
131 132 133
	}

	decnd.cached = c
134
	decnd.SetCidBuilder(c.Prefix())
135 136 137 138
	return decnd, nil
}

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