-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserRepo.cs
More file actions
623 lines (501 loc) · 21.1 KB
/
Copy pathUserRepo.cs
File metadata and controls
623 lines (501 loc) · 21.1 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
using dnproto.fs;
using dnproto.log;
using dnproto.mst;
using dnproto.repo;
using System.Text.Json.Nodes;
namespace dnproto.pds;
/// <summary>
/// Managing the user's repository.
/// </summary>
public class UserRepo
{
private LocalFileSystem _lfs;
private IDnProtoLogger _logger;
private PdsDb _db;
private string? _did = null;
private Func<byte[], byte[]>? _signer = null;
private UserRepo(LocalFileSystem lfs, IDnProtoLogger logger, PdsDb db)
{
_lfs = lfs;
_logger = logger;
_db = db;
}
public static UserRepo ConnectUserRepo(LocalFileSystem lfs, IDnProtoLogger logger, PdsDb db)
{
return new UserRepo(lfs, logger, db);
}
#region SIGNER
/// <summary>
/// Lazily load signer func.
/// </summary>
/// <returns></returns>
private Func<byte[], byte[]> GetSigniningFunc()
{
if(_signer == null)
{
_signer = dnproto.auth.Signer.CreateCommitSigningFunction(_db.GetConfigProperty("UserPrivateKeyMultibase"), _db.GetConfigProperty("UserPublicKeyMultibase"));
}
return _signer;
}
#endregion
#region DID
private string GetUserDid()
{
if(string.IsNullOrEmpty(_did))
{
_did = _db.GetConfigProperty("UserDid");
}
return _did;
}
#endregion
#region APPLYWRITES
/// <summary>
/// Main entry point for making any changes to the repo (create, update, delete).
///
/// This method updates the following:
///
/// 1. Repo Record (RepoRecord)
/// 2. Repo Commit (RepoCommit)
/// 3. Repo Header (RepoHeader)
/// 4. Firehose (FirehoseEvent)
///
/// </summary>
/// <param name="writes"></param>
/// <returns></returns>
public List<ApplyWritesResult> ApplyWrites(List<ApplyWritesOperation> writes, string? ip, string? userAgent)
{
DateTime startTime = DateTime.UtcNow;
Pds.GLOBAL_PDS_LOCK.Wait();
try
{
List<ApplyWritesResult> results = new List<ApplyWritesResult>();
//
// FIREHOSE: some state
//
RepoCommit before_repoCommit = _db.GetRepoCommit();
RepoHeader before_repoHeader = _db.GetRepoHeader();
JsonArray firehoseState_Ops = new JsonArray();
//
// Loop through operations and do writes.
//
foreach(var write in writes)
{
string uri = $"at://{GetUserDid()}/{write.Collection}/{write.Rkey}";
string fullKey = $"{write.Collection}/{write.Rkey}";
_logger.LogInfo($"[REPO] ip={ip} type={write.Type} collection={write.Collection} rkey={write.Rkey} userAgent=\"{userAgent}\"");
_logger.LogTrace($"[REPO] ApplyWrites DagCbor: \n {(write.Record != null ? DagCborObject.GetRecursiveDebugString(write.Record, 0) : "null")}");
switch(write.Type)
{
//
// CREATE/UPDATE
//
case ApplyWritesType.Create:
case ApplyWritesType.Update:
if(write.Record is null)
{
_logger.LogError($"[REPO] Update operation missing record for collection: {write.Collection} with rkey: {write.Rkey}");
continue;
}
//
// REPO RECORD
//
write.Record.SetString(new string[] { "$type" }, write.Collection);
CidV1 recordCid = CidV1.ComputeCidForDagCbor(write.Record)!;
if(write.Type == ApplyWritesType.Update && _db.RecordExists(write.Collection, write.Rkey))
{
_db.DeleteRepoRecord(write.Collection, write.Rkey);
}
_db.InsertRepoRecord(write.Collection, write.Rkey, recordCid, write.Record);
//
// Add to return list
//
string resultType = write.Type == ApplyWritesType.Create ? ApplyWritesType.CreateResult : ApplyWritesType.UpdateResult;
results.Add(new ApplyWritesResult
{
Type = resultType,
Uri = uri,
Cid = recordCid,
ValidationStatus = "valid"
});
//
// FIREHOSE: add operation to state
//
firehoseState_Ops.Add(new JsonObject()
{
["cid"] = recordCid.ToString(),
["path"] = fullKey,
["action"] = write.Type == ApplyWritesType.Create ? "create" : "update"
});
break;
//
// DELETE
//
case ApplyWritesType.Delete:
if(! _db.RecordExists(write.Collection, write.Rkey))
{
_logger.LogWarning($"[REPO] Delete operation skipped: record does not exist for collection: {write.Collection} with rkey: {write.Rkey}");
break;
}
//
// REPO RECORD
//
RepoRecord originalRepoRecord = _db.GetRepoRecord(write.Collection, write.Rkey);
CidV1 originalRepoRecordCid = originalRepoRecord.Cid;
_db.DeleteRepoRecord(write.Collection, write.Rkey);
//
// Add to return list
//
results.Add(new ApplyWritesResult
{
Type = ApplyWritesType.DeleteResult,
Uri = uri,
Cid = null
});
//
// FIREHOSE: add operation to state
//
firehoseState_Ops.Add(new JsonObject()
{
["cid"] = "null",
["path"] = $"{write.Collection}/{write.Rkey}",
["prev"] = originalRepoRecordCid.ToString(),
["action"] = "delete",
});
break;
}
}
//
// Find the nodes that we need to send back
//
Mst mst = Mst.AssembleTreeFromItems(_db.GetAllRepoRecordMstItems());
HashSet<MstNode> nodesToSend = new HashSet<MstNode>();
Dictionary<MstNode, (CidV1, DagCborObject)> mstNodeCache = new Dictionary<MstNode, (CidV1, DagCborObject)>();
foreach(var write in writes)
{
string fullKey = $"{write.Collection}/{write.Rkey}";
List<MstNode> nodes = mst.FindNodesForKey(fullKey);
foreach(var node in nodes)
{
nodesToSend.Add(node);
RepoMst.ConvertMstNodeToDagCbor(mstNodeCache, node);
}
}
//
// REPO COMMIT
//
CidV1 newRootMstNodeCid = mstNodeCache[mst.Root].Item1;
var repoCommit = _db.GetRepoCommit()!;
repoCommit.SignAndRecomputeCid(newRootMstNodeCid, GetSigniningFunc());
_db.InsertUpdateRepoCommit(repoCommit);
//
// REPO HEADER
//
var repoHeader = _db.GetRepoHeader()!;
repoHeader.RepoCommitCid = repoCommit.Cid!;
_db.InsertUpdateRepoHeader(repoHeader);
//
// FIREHOSE: OBJECT 1 (header)
//
int header_op = 1;
string header_t = "#commit";
var object1Json = new JsonObject()
{
["t"] = header_t,
["op"] = header_op
};
DagCborObject object1DagCbor = DagCborObject.FromJsonString(object1Json.ToString());
//
// FIREHOSE: BLOCKS (header, commit, nodes, records)
//
/// Format from spec:
///
/// [--- header -------- ] [----------------- data ---------------------------------]
/// [varint | header block ] [varint | cid | data block]....[varint | cid | data block]
///
MemoryStream blockStream = new MemoryStream();
// header
var firehoseFinal_RepoHeader = _db.GetRepoHeader();
firehoseFinal_RepoHeader.WriteToStream(blockStream);
// mst nodes
// order descending by key depth so that root is written first
foreach(var mstNode in nodesToSend.OrderByDescending(n => n.KeyDepth))
{
var (cid, dagCbor) = mstNodeCache[mstNode];
DagCborObject.WriteToRepoStream(blockStream, cid, dagCbor);
}
// records
foreach(var write in writes)
{
if(_db.RecordExists(write.Collection, write.Rkey))
{
var record = _db.GetRepoRecord(write.Collection, write.Rkey);
DagCborObject.WriteToRepoStream(blockStream, record.Cid!, record.DataBlock);
}
}
// commit
var firehoseFinal_RepoCommit = _db.GetRepoCommit();
DagCborObject.WriteToRepoStream(blockStream, firehoseFinal_RepoCommit.Cid!, firehoseFinal_RepoCommit.ToDagCborObject());
//
// FIREHOSE: OBJECT 2
//
long sequenceNumber = _db.GetNewSequenceNumberForFirehose();
string createdDate = FirehoseEvent.GetNewCreatedDate();
var object2Json = new JsonObject()
{
["ops"] = firehoseState_Ops,
["rev"] = firehoseFinal_RepoCommit.Rev,
["seq"] = sequenceNumber,
["repo"] = GetUserDid(),
["time"] = createdDate,
["blobs"] = new JsonArray(),
["since"] = before_repoCommit.Rev,
["blocks"] = "", // placeholder - will be replaced with byte[] below
["commit"] = firehoseFinal_RepoCommit.Cid!.ToString(),
["rebase"] = false,
["tooBig"] = false,
["prevData"] = before_repoCommit.RootMstNodeCid!.ToString()
};
var object2DagCbor = DagCborObject.FromJsonString(object2Json.ToString());
// Replace fields with proper types (JSON serialization loses CID link and byte[] types)
var object2Dict = (Dictionary<string, DagCborObject>)object2DagCbor.Value;
// "blocks" - byte string
object2Dict["blocks"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_BYTE_STRING, AdditionalInfo = 0, OriginalByte = 0 },
Value = blockStream.ToArray()
};
// "commit" - CID link (TAG 42)
object2Dict["commit"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = firehoseFinal_RepoCommit.Cid!
};
// "prevData" - CID link (TAG 42)
object2Dict["prevData"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = before_repoCommit.RootMstNodeCid!
};
// "ops[].cid" - CID links (TAG 42)
var opsArray = (List<DagCborObject>)object2Dict["ops"].Value;
for (int i = 0; i < opsArray.Count; i++)
{
var opDict = (Dictionary<string, DagCborObject>)opsArray[i].Value;
if (opDict.ContainsKey("cid"))
{
var cidObj = opDict["cid"];
// Check if it's already a CID (shouldn't happen, but be safe)
if (cidObj.Value is CidV1 existingCid)
{
opDict["cid"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = existingCid
};
}
else if (cidObj.Value is string cidString && cidString != "null")
{
CidV1 cidValue = CidV1.FromBase32(cidString);
opDict["cid"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = cidValue
};
}
//
// This block of code is very important, do not remove it.
// Previously, we had a bug where "null" string was being sent as the cid,
// and during deletes it would crash the subscribeRepos connection and
// retry constantly. For a "delete" operation, the "cid" field should be a simple value "null".
//
else if (cidObj.Value is string cidStrNull && cidStrNull == "null")
{
// turn into simple value
opDict["cid"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_SIMPLE_VALUE, AdditionalInfo = 0x16, OriginalByte = 0 },
Value = "null"
};
}
}
if (opDict.ContainsKey("prev"))
{
var cidObj = opDict["prev"];
// Check if it's already a CID (shouldn't happen, but be safe)
if (cidObj.Value is CidV1 existingCid)
{
opDict["prev"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = existingCid
};
}
else if (cidObj.Value is string cidString && cidString != "null")
{
CidV1 cidValue = CidV1.FromBase32(cidString);
opDict["prev"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = cidValue
};
}
}
}
//
// FIREHOSE: database object
//
FirehoseEvent firehoseEvent = new FirehoseEvent()
{
SequenceNumber = sequenceNumber,
CreatedDate = createdDate,
Header_op = header_op,
Header_t = header_t,
Header_DagCborObject = object1DagCbor,
Body_DagCborObject = object2DagCbor
};
_db.InsertFirehoseEvent(firehoseEvent);
//
// Return
//
return results;
}
finally
{
Pds.GLOBAL_PDS_LOCK.Release();
}
}
public class ApplyWritesOperation
{
public required string Type;
public required string Collection;
public required string Rkey;
public DagCborObject? Record = null;
}
public class ApplyWritesResult
{
public required string Type;
public string? Uri = null;
public CidV1? Cid = null;
public string? ValidationStatus = null;
}
public class ApplyWritesType
{
public const string Create = "com.atproto.repo.applyWrites#create";
public const string Update = "com.atproto.repo.applyWrites#update";
public const string Delete = "com.atproto.repo.applyWrites#delete";
public const string CreateResult = "com.atproto.repo.applyWrites#createResult";
public const string UpdateResult = "com.atproto.repo.applyWrites#updateResult";
public const string DeleteResult = "com.atproto.repo.applyWrites#deleteResult";
}
#endregion
#region GET
/// <summary>
/// Gets record by collection and rkey.
/// </summary>
/// <param name="collection"></param>
/// <param name="rkey"></param>
/// <returns></returns>
public RepoRecord GetRecord(string collection, string rkey)
{
return _db.GetRepoRecord(collection, rkey);
}
public bool RecordExists(string collection, string rkey)
{
return _db.RecordExists(collection, rkey);
}
#endregion
#region STREAM
/// <summary>
/// Loads entire repo from our database and writes it to the stream.
/// For example, this is called by getRepo.
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public async Task WriteToStreamAsync(System.IO.Stream stream)
{
await Pds.GLOBAL_PDS_LOCK.WaitAsync();
try
{
//
// Header
//
var header = _db.GetRepoHeader();
var headerDagCbor = header.ToDagCborObject();
var headerDagCborBytes = headerDagCbor.ToBytes();
var headerLengthVarInt = VarInt.FromLong((long)headerDagCborBytes.Length);
await VarInt.WriteVarIntAsync(stream, headerLengthVarInt);
await stream.WriteAsync(headerDagCborBytes, 0, headerDagCborBytes.Length);
//
// Repo Commit
//
var repoCommit = _db.GetRepoCommit();
if (repoCommit == null)
{
_logger.LogError("Cannot write MST to stream: repo commit is null.");
return;
}
var repoCommitDagCbor = repoCommit.ToDagCborObject();
if (repoCommitDagCbor == null)
{
_logger.LogError("Cannot write MST to stream: failed to convert repo commit to DagCborObject.");
return;
}
var repoCommitCid = repoCommit.Cid;
if (repoCommitCid == null)
{
_logger.LogError("Cannot write MST to stream: repo commit CID is null.");
return;
}
await WriteBlockAsync(stream, repoCommitCid, repoCommitDagCbor);
//
// MST Nodes
//
Mst mst = Mst.AssembleTreeFromItems(_db.GetAllRepoRecordMstItems());
List<MstNode> allNodes = mst.FindAllNodes();
Dictionary<MstNode, (CidV1, DagCborObject)> mstNodeCache = new Dictionary<MstNode, (CidV1, DagCborObject)>();
foreach(var node in allNodes)
{
RepoMst.ConvertMstNodeToDagCbor(mstNodeCache, node);
}
foreach (MstNode mstNode in allNodes)
{
var (mstNodeCid, mstNodeDagCbor) = mstNodeCache[mstNode];
await WriteBlockAsync(stream, mstNodeCid, mstNodeDagCbor);
}
//
// Repo records (atproto records - like posts, profiles, etc)
//
var repoRecords = _db.GetAllRepoRecords();
foreach (var repoRecord in repoRecords)
{
if (repoRecord.DataBlock == null || repoRecord.Cid == null)
{
_logger.LogError($"Cannot write MST to stream: failed to convert repo record {repoRecord.Cid?.Base32} to DagCborObject.");
return;
}
await WriteBlockAsync(stream, repoRecord.Cid, repoRecord.DataBlock);
}
}
finally
{
Pds.GLOBAL_PDS_LOCK.Release();
}
}
/// <summary>
/// Writing one record. The format is [VarInt | CidV1 | DagCborObject] (see Repo.cs)
/// </summary>
/// <param name="stream"></param>
/// <param name="cid"></param>
/// <param name="dagCbor"></param>
/// <returns></returns>
public static async Task WriteBlockAsync(System.IO.Stream stream, CidV1 cid, DagCborObject dagCbor)
{
var cidBytes = cid.AllBytes;
var dagCborBytes = dagCbor.ToBytes();
var blockLengthVarInt = VarInt.FromLong((long)(cidBytes.Length + dagCborBytes.Length));
await VarInt.WriteVarIntAsync(stream, blockLengthVarInt);
await CidV1.WriteCidAsync(stream, cid);
await stream.WriteAsync(dagCborBytes, 0, dagCborBytes.Length);
}
#endregion
}