This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
merkle.go
358 lines (299 loc) · 7.35 KB
/
merkle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package merkle
import (
"bytes"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io"
"math/bits"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs/core/coredag"
"github.com/ipfs/go-ipfs/plugin"
node "github.com/ipfs/go-ipld-format"
coreiface "github.com/ipfs/interface-go-ipfs-core"
mh "github.com/multiformats/go-multihash"
)
type TreePlugin struct{}
func (t TreePlugin) Start(api coreiface.CoreAPI) error {
fmt.Println("TreePlugin loaded")
return nil
}
var _ plugin.PluginIPLD = (*TreePlugin)(nil)
var _ plugin.PluginDaemon = (*TreePlugin)(nil)
// 0x87 seems to be free:
// https://github.com/multiformats/multicodec/blob/master/table.csv
const Tree = 0x87
func (t TreePlugin) Name() string {
return "ipld-Merkle"
}
func (t TreePlugin) Version() string {
return "0.0"
}
func (t TreePlugin) Init(env *plugin.Environment) error {
return nil
}
func (t TreePlugin) RegisterInputEncParsers(iec coredag.InputEncParsers) error {
iec.AddParser("json", "merkle-leaves", TreeLeavesJSONInputParser)
return nil
}
func (t TreePlugin) RegisterBlockDecoders(dec node.BlockDecoder) error {
dec.Register(Tree, TreeNodeParser)
return nil
}
func TreeNodeParser(block blocks.Block) (node.Node, error) {
data := block.RawData()
if len(data) == 0 {
return &LeafNode{
RawHash: emptyHash(),
Data: nil,
}, nil
}
firstByte := data[:1]
if bytes.Equal(firstByte, leafPrefix) {
h := block.Cid().Hash()
return &LeafNode{
RawHash: h[2:], // CID().Hash() returns hash-code||len(hash)||hash
Data: data[1:],
}, nil
} else if bytes.Equal(firstByte, innerPrefix) {
h := block.Cid().Hash()
return InnerNode{
rawHash: h[2:], // CID().Hash() returns hash-code||len(hash)||hash
l: data[1:33],
r: data[33:],
}, nil
}
return nil, errors.New("unknown err")
}
func TreeLeavesJSONInputParser(r io.Reader, _mhType uint64, _mhLen int) ([]node.Node, error) {
// this can be anything; just using JSON as it's easy to understand
// what's fed into the tree:
shares, err := parseSharesFromJSON(r)
if err != nil {
return nil, err
}
input := make([][]byte, len(shares))
for i, share := range shares {
input[i] = share.Data
}
_, nodes := ComputeNodes(input)
fmt.Println(fmt.Sprintf("length of nodes: %d", len(nodes)))
return nodes, nil
}
type InnerNode struct {
rawHash []byte
l, r []byte
}
func (i InnerNode) RawData() []byte {
// fmt.Sprintf("inner-node-Data: %#v\n", append(innerPrefix, append(i.l, i.r...)...))
return append(innerPrefix, append(i.l, i.r...)...)
}
func (i InnerNode) Cid() cid.Cid {
// fmt.Sprintf("inner-node-cid: %#v\n", cidFromSha256(i.RawHash))
return cidFromSha256(i.rawHash)
}
func cidFromSha256(rawHash []byte) cid.Cid {
buf, err := mh.Encode(rawHash, mh.SHA2_256)
if err != nil {
panic(err)
}
return cid.NewCidV1(Tree, mh.Multihash(buf))
}
func (i InnerNode) String() string {
return fmt.Sprintf(`
inner-node {
hash: %x,
l: %x,
r: %x"
}`, i.rawHash, i.l, i.r)
}
func (i InnerNode) Loggable() map[string]interface{} {
return nil
}
func (i InnerNode) Resolve(path []string) (interface{}, []string, error) {
switch path[0] {
case "0":
return &node.Link{Cid: cidFromSha256(i.l)}, path[1:], nil
case "1":
return &node.Link{Cid: cidFromSha256(i.r)}, path[1:], nil
default:
return nil, nil, errors.New("invalid path for inner node")
}
}
func (i InnerNode) Tree(path string, depth int) []string {
if path != "" || depth == 0 {
return nil
}
return []string{
"0",
"1",
}
}
func (i InnerNode) ResolveLink(path []string) (*node.Link, []string, error) {
obj, rest, err := i.Resolve(path)
if err != nil {
return nil, nil, err
}
lnk, ok := obj.(*node.Link)
if !ok {
return nil, nil, fmt.Errorf("was not a link")
}
return lnk, rest, nil
}
func (i InnerNode) Copy() node.Node {
panic("implement me")
}
func (i InnerNode) Links() []*node.Link {
return []*node.Link{{Cid: cidFromSha256(i.l)}, {Cid: cidFromSha256(i.r)}}
}
func (i InnerNode) Stat() (*node.NodeStat, error) {
return &node.NodeStat{}, nil
}
func (i InnerNode) Size() (uint64, error) {
return 0, nil
}
type LeafNode struct {
RawHash []byte
Data []byte
}
func (l LeafNode) RawData() []byte {
return append(leafPrefix, l.Data...)
}
func (l LeafNode) Cid() cid.Cid {
buf, err := mh.Encode(l.RawHash, mh.SHA2_256)
if err != nil {
panic(err)
}
cidV1 := cid.NewCidV1(Tree, mh.Multihash(buf))
return cidV1
}
func (l LeafNode) String() string {
return fmt.Sprintf(`
leaf-node {
hash: %x,
len(Data): %v
}`, l.RawHash, len(l.Data))
}
func (l LeafNode) Loggable() map[string]interface{} {
return nil
}
func (l LeafNode) Resolve(path []string) (interface{}, []string, error) {
if path[0] == "Data" {
// TODO: store the data separately
// currently Leaf{Data:} contains the actual data
// instead there should be a link in the leaf to the actual data
return nil, nil, nil
} else {
return nil, nil, errors.New("invalid path for leaf node")
}
}
func (l LeafNode) Tree(path string, depth int) []string {
if path != "" || depth == 0 {
return nil
}
return []string{
"Data",
}
}
func (l LeafNode) ResolveLink(path []string) (*node.Link, []string, error) {
obj, rest, err := l.Resolve(path)
if err != nil {
return nil, nil, err
}
lnk, ok := obj.(*node.Link)
if !ok {
return nil, nil, fmt.Errorf("was not a link")
}
return lnk, rest, nil
}
func (l LeafNode) Copy() node.Node {
panic("implement me")
}
func (l LeafNode) Links() []*node.Link {
return []*node.Link{{Cid: l.Cid()}}
}
func (l LeafNode) Stat() (*node.NodeStat, error) {
return &node.NodeStat{}, nil
}
func (l LeafNode) Size() (uint64, error) {
return 0, nil
}
var _ node.Node = (*InnerNode)(nil)
var _ node.Node = (*LeafNode)(nil)
type Share struct {
// TODO add namespace.ID
Data []byte
}
type JsonLeaves struct {
Leaves []Share
}
func parseSharesFromJSON(r io.Reader) ([]Share, error) {
var obj JsonLeaves
dec := json.NewDecoder(r)
err := dec.Decode(&obj)
if err != nil {
return nil, err
}
return obj.Leaves, nil
}
// ---- recursively compute the nodes (RFC-6962); used tendermint's implementation as a basis ---- //
func ComputeNodes(items [][]byte) ([]byte, []node.Node) {
switch len(items) {
case 0:
emptyHash := emptyHash()
return emptyHash, []node.Node{&LeafNode{
RawHash: emptyHash,
Data: nil,
}}
case 1:
hash := leafHash(items[0])
return hash, []node.Node{&LeafNode{
RawHash: hash,
Data: items[0],
}}
default:
k := getSplitPoint(int64(len(items)))
left, lnodes := ComputeNodes(items[:k])
right, rnodes := ComputeNodes(items[k:])
parentHash := innerHash(left, right)
parentNode := []node.Node{&InnerNode{
rawHash: parentHash,
l: left,
r: right,
}}
return parentHash, append(parentNode, append(lnodes, rnodes...)...)
}
}
func getSplitPoint(length int64) int64 {
if length < 1 {
panic("Trying to split a tree with size < 1")
}
uLength := uint(length)
bitlen := bits.Len(uLength)
k := int64(1 << uint(bitlen-1))
if k == length {
k >>= 1
}
return k
}
var (
leafPrefix = []byte{0}
innerPrefix = []byte{1}
)
func emptyHash() []byte {
h := sha256.New()
h.Write(nil)
return h.Sum(nil)
}
func leafHash(leaf []byte) []byte {
h := sha256.New()
h.Write(append(leafPrefix, leaf...))
return h.Sum(nil)
}
func innerHash(left []byte, right []byte) []byte {
h := sha256.New()
h.Write(append(innerPrefix, append(left, right...)...))
return h.Sum(nil)
}