coding.go 3.27 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
)

// 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
	if err := pbn.Unmarshal(encoded); err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
24
		return fmt.Errorf("unmarshal failed. %v", err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
25 26 27
	}

	pbnl := pbn.GetLinks()
28
	n.links = make([]*ipld.Link, len(pbnl))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
29
	for i, l := range pbnl {
30
		n.links[i] = &ipld.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 {
Łukasz Magiera's avatar
Łukasz Magiera committed
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
	pbn := n.getPBNode()
	data, err := pbn.Marshal()
	if err != nil {
Łukasz Magiera's avatar
Łukasz Magiera committed
50
		return data, fmt.Errorf("marshal failed. %v", err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
51 52
	}
	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

105
// DecodeProtobuf 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

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

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

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