-
Notifications
You must be signed in to change notification settings - Fork 976
/
Copy pathnmt.go
290 lines (242 loc) · 6.43 KB
/
nmt.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
package plugin
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
mh "github.com/multiformats/go-multihash"
"github.com/tendermint/tendermint/pkg/consts"
"github.com/celestiaorg/nmt"
)
const (
// Below used multiformats (one codec, one multihash) seem free:
// https://github.com/multiformats/multicodec/blob/master/table.csv
// nmtCodec is the codec used for leaf and inner nodes of a Namespaced Merkle Tree.
nmtCodec = 0x7700
// Sha256Namespace8Flagged is the multihash code used to hash blocks
// that contain an NMT node (inner and leaf nodes).
sha256Namespace8Flagged = 0x7701
// nmtHashSize is the size of a digest created by an NMT in bytes.
nmtHashSize = 2*consts.NamespaceSize + sha256.Size
// typeSize defines the size of the serialized NMT Node type
typeSize = 1
)
func GetNode(ctx context.Context, bGetter blockservice.BlockGetter, root cid.Cid) (ipld.Node, error) {
block, err := bGetter.GetBlock(ctx, root)
if err != nil {
var errNotFound *ipld.ErrNotFound
if errors.As(err, &errNotFound) {
return nil, errNotFound
}
return nil, err
}
return decodeBlock(block)
}
func decodeBlock(block blocks.Block) (ipld.Node, error) {
var (
leafPrefix = []byte{nmt.LeafPrefix}
innerPrefix = []byte{nmt.NodePrefix}
)
data := block.RawData()
if len(data) == 0 {
return &nmtLeafNode{
cid: cid.Undef,
Data: nil,
}, nil
}
domainSeparator := data[:typeSize]
if bytes.Equal(domainSeparator, leafPrefix) {
return &nmtLeafNode{
cid: block.Cid(),
Data: data[typeSize:],
}, nil
}
if bytes.Equal(domainSeparator, innerPrefix) {
return &nmtNode{
cid: block.Cid(),
l: data[typeSize : typeSize+nmtHashSize],
r: data[typeSize+nmtHashSize:],
}, nil
}
return nil, fmt.Errorf(
"expected first byte of block to be either the leaf or inner node prefix: (%x, %x), got: %x)",
leafPrefix,
innerPrefix,
domainSeparator,
)
}
var _ ipld.Node = (*nmtNode)(nil)
var _ ipld.Node = (*nmtLeafNode)(nil)
type nmtNode struct {
cid cid.Cid
l, r []byte
}
func NewNMTNode(id cid.Cid, l, r []byte) ipld.Node {
return nmtNode{id, l, r}
}
func (n nmtNode) RawData() []byte {
return append([]byte{nmt.NodePrefix}, append(n.l, n.r...)...)
}
func (n nmtNode) Cid() cid.Cid {
return n.cid
}
func (n nmtNode) String() string {
return fmt.Sprintf(`
node {
hash: %x,
l: %x,
r: %x"
}`, n.cid.Hash(), n.l, n.r)
}
func (n nmtNode) Loggable() map[string]interface{} {
return nil
}
func (n nmtNode) Resolve(path []string) (interface{}, []string, error) {
switch path[0] {
case "0":
left, err := CidFromNamespacedSha256(n.l)
if err != nil {
return nil, nil, err
}
return &ipld.Link{Cid: left}, path[1:], nil
case "1":
right, err := CidFromNamespacedSha256(n.r)
if err != nil {
return nil, nil, err
}
return &ipld.Link{Cid: right}, path[1:], nil
default:
return nil, nil, errors.New("invalid path for inner node")
}
}
func (n nmtNode) Tree(path string, depth int) []string {
if path != "" || depth != -1 {
panic("proper tree not yet implemented")
}
return []string{
"0",
"1",
}
}
func (n nmtNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
obj, rest, err := n.Resolve(path)
if err != nil {
return nil, nil, err
}
lnk, ok := obj.(*ipld.Link)
if !ok {
return nil, nil, errors.New("was not a link")
}
return lnk, rest, nil
}
func (n nmtNode) Copy() ipld.Node {
l := make([]byte, len(n.l))
copy(l, n.l)
r := make([]byte, len(n.r))
copy(r, n.r)
return &nmtNode{
cid: n.cid,
l: l,
r: r,
}
}
func (n nmtNode) Links() []*ipld.Link {
leftCid := MustCidFromNamespacedSha256(n.l)
rightCid := MustCidFromNamespacedSha256(n.r)
return []*ipld.Link{{Cid: leftCid}, {Cid: rightCid}}
}
func (n nmtNode) Stat() (*ipld.NodeStat, error) {
return &ipld.NodeStat{}, nil
}
func (n nmtNode) Size() (uint64, error) {
return 0, nil
}
type nmtLeafNode struct {
cid cid.Cid
Data []byte
}
func NewNMTLeafNode(id cid.Cid, data []byte) ipld.Node {
return &nmtLeafNode{id, data}
}
func (l nmtLeafNode) RawData() []byte {
return append([]byte{nmt.LeafPrefix}, l.Data...)
}
func (l nmtLeafNode) Cid() cid.Cid {
return l.cid
}
func (l nmtLeafNode) String() string {
return fmt.Sprintf(`
leaf {
hash: %x,
len(Data): %v
}`, l.cid.Hash(), len(l.Data))
}
func (l nmtLeafNode) Loggable() map[string]interface{} {
return nil
}
func (l nmtLeafNode) Resolve(path []string) (interface{}, []string, error) {
return nil, nil, errors.New("invalid path for leaf node")
}
func (l nmtLeafNode) Tree(_path string, _depth int) []string {
return nil
}
func (l nmtLeafNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
obj, rest, err := l.Resolve(path)
if err != nil {
return nil, nil, err
}
lnk, ok := obj.(*ipld.Link)
if !ok {
return nil, nil, errors.New("was not a link")
}
return lnk, rest, nil
}
func (l nmtLeafNode) Copy() ipld.Node {
panic("implement me")
}
func (l nmtLeafNode) Links() []*ipld.Link {
return []*ipld.Link{{Cid: l.Cid()}}
}
func (l nmtLeafNode) Stat() (*ipld.NodeStat, error) {
return &ipld.NodeStat{}, nil
}
func (l nmtLeafNode) Size() (uint64, error) {
return 0, nil
}
// CidFromNamespacedSha256 uses a hash from an nmt tree to create a CID
func CidFromNamespacedSha256(namespacedHash []byte) (cid.Cid, error) {
if got, want := len(namespacedHash), nmtHashSize; got != want {
return cid.Cid{}, fmt.Errorf("invalid namespaced hash length, got: %v, want: %v", got, want)
}
buf, err := mh.Encode(namespacedHash, sha256Namespace8Flagged)
if err != nil {
return cid.Undef, err
}
return cid.NewCidV1(nmtCodec, buf), nil
}
// MustCidFromNamespacedSha256 is a wrapper around cidFromNamespacedSha256 that panics
// in case of an error. Use with care and only in places where no error should occur.
func MustCidFromNamespacedSha256(hash []byte) cid.Cid {
cidFromHash, err := CidFromNamespacedSha256(hash)
if err != nil {
panic(
fmt.Sprintf("malformed hash: %s, codec: %v",
err,
mh.Codes[sha256Namespace8Flagged]),
)
}
return cidFromHash
}
// cidPrefixSize is the size of the prepended buffer of the CID encoding
// for NamespacedSha256. For more information, see:
// https://multiformats.io/multihash/#the-multihash-format
const cidPrefixSize = 4
// NamespacedSha256FromCID derives the Namespaced hash from the given CID.
func NamespacedSha256FromCID(cid cid.Cid) []byte {
return cid.Hash()[cidPrefixSize:]
}