forked from bluesky-social/indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.go
429 lines (344 loc) · 9.05 KB
/
repo.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package repo
import (
"bytes"
"context"
"fmt"
"io"
lexutil "github.com/bluesky-social/indigo/lex/util"
"github.com/bluesky-social/indigo/mst"
"github.com/bluesky-social/indigo/util"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/ipld/go-car/v2"
cbg "github.com/whyrusleeping/cbor-gen"
"go.opentelemetry.io/otel"
)
// current version of repo currently implemented
const ATP_REPO_VERSION int64 = 3
const ATP_REPO_VERSION_2 int64 = 2
type SignedCommit struct {
Did string `json:"did" cborgen:"did"`
Version int64 `json:"version" cborgen:"version"`
Prev *cid.Cid `json:"prev" cborgen:"prev"`
Data cid.Cid `json:"data" cborgen:"data"`
Sig []byte `json:"sig" cborgen:"sig"`
Rev string `json:"rev" cborgen:"rev,omitempty"`
}
type UnsignedCommit struct {
Did string `cborgen:"did"`
Version int64 `cborgen:"version"`
Prev *cid.Cid `cborgen:"prev"`
Data cid.Cid `cborgen:"data"`
Rev string `cborgen:"rev,omitempty"`
}
type Repo struct {
sc SignedCommit
cst cbor.IpldStore
bs blockstore.Blockstore
repoCid cid.Cid
mst *mst.MerkleSearchTree
dirty bool
}
// Returns a copy of commit without the Sig field. Helpful when verifying signature.
func (sc *SignedCommit) Unsigned() *UnsignedCommit {
return &UnsignedCommit{
Did: sc.Did,
Version: sc.Version,
Prev: sc.Prev,
Data: sc.Data,
Rev: sc.Rev,
}
}
// returns bytes of the DAG-CBOR representation of object. This is what gets
// signed; the `go-did` library will take the SHA-256 of the bytes and sign
// that.
func (uc *UnsignedCommit) BytesForSigning() ([]byte, error) {
buf := new(bytes.Buffer)
if err := uc.MarshalCBOR(buf); err != nil {
return []byte{}, err
}
return buf.Bytes(), nil
}
func IngestRepo(ctx context.Context, bs blockstore.Blockstore, r io.Reader) (cid.Cid, error) {
ctx, span := otel.Tracer("repo").Start(ctx, "Ingest")
defer span.End()
br, err := car.NewBlockReader(r)
if err != nil {
return cid.Undef, err
}
for {
blk, err := br.Next()
if err != nil {
if err == io.EOF {
break
}
return cid.Undef, err
}
if err := bs.Put(ctx, blk); err != nil {
return cid.Undef, err
}
}
return br.Roots[0], nil
}
func ReadRepoFromCar(ctx context.Context, r io.Reader) (*Repo, error) {
bs := blockstore.NewBlockstore(datastore.NewMapDatastore())
root, err := IngestRepo(ctx, bs, r)
if err != nil {
return nil, err
}
return OpenRepo(ctx, bs, root, false)
}
func NewRepo(ctx context.Context, did string, bs blockstore.Blockstore) *Repo {
cst := util.CborStore(bs)
t := mst.NewEmptyMST(cst)
sc := SignedCommit{
Did: did,
Version: 2,
}
return &Repo{
cst: cst,
bs: bs,
mst: t,
sc: sc,
dirty: true,
}
}
func OpenRepo(ctx context.Context, bs blockstore.Blockstore, root cid.Cid, fullRepo bool) (*Repo, error) {
cst := util.CborStore(bs)
var sc SignedCommit
if err := cst.Get(ctx, root, &sc); err != nil {
return nil, fmt.Errorf("loading root from blockstore: %w", err)
}
if sc.Version != ATP_REPO_VERSION && sc.Version != ATP_REPO_VERSION_2 {
return nil, fmt.Errorf("unsupported repo version: %d", sc.Version)
}
return &Repo{
sc: sc,
bs: bs,
cst: cst,
repoCid: root,
}, nil
}
type CborMarshaler interface {
MarshalCBOR(w io.Writer) error
}
func (r *Repo) RepoDid() string {
if r.sc.Did == "" {
panic("repo has unset did")
}
return r.sc.Did
}
// TODO(bnewbold): this could return just *cid.Cid
func (r *Repo) PrevCommit(ctx context.Context) (*cid.Cid, error) {
return r.sc.Prev, nil
}
func (r *Repo) DataCid() cid.Cid {
return r.sc.Data
}
func (r *Repo) SignedCommit() SignedCommit {
return r.sc
}
func (r *Repo) Blockstore() blockstore.Blockstore {
return r.bs
}
func (r *Repo) CreateRecord(ctx context.Context, nsid string, rec CborMarshaler) (cid.Cid, string, error) {
ctx, span := otel.Tracer("repo").Start(ctx, "CreateRecord")
defer span.End()
r.dirty = true
t, err := r.getMst(ctx)
if err != nil {
return cid.Undef, "", fmt.Errorf("failed to get mst: %w", err)
}
k, err := r.cst.Put(ctx, rec)
if err != nil {
return cid.Undef, "", err
}
tid := NextTID()
nmst, err := t.Add(ctx, nsid+"/"+tid, k, -1)
if err != nil {
return cid.Undef, "", fmt.Errorf("mst.Add failed: %w", err)
}
r.mst = nmst
return k, tid, nil
}
func (r *Repo) PutRecord(ctx context.Context, rpath string, rec CborMarshaler) (cid.Cid, error) {
ctx, span := otel.Tracer("repo").Start(ctx, "PutRecord")
defer span.End()
r.dirty = true
t, err := r.getMst(ctx)
if err != nil {
return cid.Undef, fmt.Errorf("failed to get mst: %w", err)
}
k, err := r.cst.Put(ctx, rec)
if err != nil {
return cid.Undef, err
}
nmst, err := t.Add(ctx, rpath, k, -1)
if err != nil {
return cid.Undef, fmt.Errorf("mst.Add failed: %w", err)
}
r.mst = nmst
return k, nil
}
func (r *Repo) DeleteRecord(ctx context.Context, rpath string) error {
ctx, span := otel.Tracer("repo").Start(ctx, "DeleteRecord")
defer span.End()
r.dirty = true
t, err := r.getMst(ctx)
if err != nil {
return fmt.Errorf("failed to get mst: %w", err)
}
nmst, err := t.Delete(ctx, rpath)
if err != nil {
return fmt.Errorf("mst.Add failed: %w", err)
}
r.mst = nmst
return nil
}
// truncates history while retaining the same data root
func (r *Repo) Truncate() {
r.sc.Prev = nil
r.repoCid = cid.Undef
}
// creates and writes a new SignedCommit for this repo, with `prev` pointing to old value
func (r *Repo) Commit(ctx context.Context, signer func(context.Context, string, []byte) ([]byte, error)) (cid.Cid, string, error) {
ctx, span := otel.Tracer("repo").Start(ctx, "Commit")
defer span.End()
t, err := r.getMst(ctx)
if err != nil {
return cid.Undef, "", err
}
rcid, err := t.GetPointer(ctx)
if err != nil {
return cid.Undef, "", err
}
ncom := UnsignedCommit{
Did: r.RepoDid(),
Version: ATP_REPO_VERSION,
Data: rcid,
Rev: NextTID(),
}
sb, err := ncom.BytesForSigning()
if err != nil {
return cid.Undef, "", fmt.Errorf("failed to serialize commit: %w", err)
}
sig, err := signer(ctx, ncom.Did, sb)
if err != nil {
return cid.Undef, "", fmt.Errorf("failed to sign root: %w", err)
}
nsc := SignedCommit{
Sig: sig,
Did: ncom.Did,
Version: ncom.Version,
Prev: ncom.Prev,
Data: ncom.Data,
Rev: ncom.Rev,
}
nsccid, err := r.cst.Put(ctx, &nsc)
if err != nil {
return cid.Undef, "", err
}
r.sc = nsc
r.dirty = false
return nsccid, nsc.Rev, nil
}
func (r *Repo) getMst(ctx context.Context) (*mst.MerkleSearchTree, error) {
if r.mst != nil {
return r.mst, nil
}
t := mst.LoadMST(r.cst, r.sc.Data)
r.mst = t
return t, nil
}
var ErrDoneIterating = fmt.Errorf("done iterating")
func (r *Repo) ForEach(ctx context.Context, prefix string, cb func(k string, v cid.Cid) error) error {
ctx, span := otel.Tracer("repo").Start(ctx, "ForEach")
defer span.End()
t := mst.LoadMST(r.cst, r.sc.Data)
if err := t.WalkLeavesFrom(ctx, prefix, cb); err != nil {
if err != ErrDoneIterating {
return err
}
}
return nil
}
func (r *Repo) GetRecord(ctx context.Context, rpath string) (cid.Cid, cbg.CBORMarshaler, error) {
ctx, span := otel.Tracer("repo").Start(ctx, "GetRecord")
defer span.End()
mst, err := r.getMst(ctx)
if err != nil {
return cid.Undef, nil, fmt.Errorf("getting repo mst: %w", err)
}
cc, err := mst.Get(ctx, rpath)
if err != nil {
return cid.Undef, nil, fmt.Errorf("resolving rpath within mst: %w", err)
}
blk, err := r.bs.Get(ctx, cc)
if err != nil {
return cid.Undef, nil, err
}
rec, err := lexutil.CborDecodeValue(blk.RawData())
if err != nil {
return cid.Undef, nil, err
}
return cc, rec, nil
}
func (r *Repo) DiffSince(ctx context.Context, oldrepo cid.Cid) ([]*mst.DiffOp, error) {
ctx, span := otel.Tracer("repo").Start(ctx, "DiffSince")
defer span.End()
var oldTree cid.Cid
if oldrepo.Defined() {
otherRepo, err := OpenRepo(ctx, r.bs, oldrepo, true)
if err != nil {
return nil, err
}
oldmst, err := otherRepo.getMst(ctx)
if err != nil {
return nil, err
}
oldptr, err := oldmst.GetPointer(ctx)
if err != nil {
return nil, err
}
oldTree = oldptr
}
curmst, err := r.getMst(ctx)
if err != nil {
return nil, err
}
curptr, err := curmst.GetPointer(ctx)
if err != nil {
return nil, err
}
return mst.DiffTrees(ctx, r.bs, oldTree, curptr)
}
func (r *Repo) CopyDataTo(ctx context.Context, bs blockstore.Blockstore) error {
return copyRecCbor(ctx, r.bs, bs, r.sc.Data, make(map[cid.Cid]struct{}))
}
func copyRecCbor(ctx context.Context, from, to blockstore.Blockstore, c cid.Cid, seen map[cid.Cid]struct{}) error {
if _, ok := seen[c]; ok {
return nil
}
seen[c] = struct{}{}
blk, err := from.Get(ctx, c)
if err != nil {
return err
}
if err := to.Put(ctx, blk); err != nil {
return err
}
var out []cid.Cid
if err := cbg.ScanForLinks(bytes.NewReader(blk.RawData()), func(c cid.Cid) {
out = append(out, c)
}); err != nil {
return err
}
for _, child := range out {
if err := copyRecCbor(ctx, from, to, child, seen); err != nil {
return err
}
}
return nil
}