-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRepoMst.cs
More file actions
330 lines (272 loc) · 10.3 KB
/
Copy pathRepoMst.cs
File metadata and controls
330 lines (272 loc) · 10.3 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
using dnproto.log;
using dnproto.mst;
namespace dnproto.repo;
/// <summary>
/// Helper functions for working with Merkle Search Trees (MST) in Atproto repos.
/// The "dnproto.mst.*" namespace has the core MST in-memory structures and functions,
/// but it doesn't know about the rest of the namespaces in this project (repo, log, etc.)
/// This class helps transform the MST into things that atproto needs.
/// </summary>
public class RepoMst
{
#region LOADREPO
/// <summary>
/// Load a CAR repo stream and extract the MST items from it.
/// </summary>
/// <param name="s"></param>
/// <param name="logger"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static List<MstItem> LoadMstItemsFromRepo(string repoFile, IDnProtoLogger logger)
{
using(var fs = new FileStream(repoFile, FileMode.Open, FileAccess.Read))
{
return LoadMstItemsFromRepo(fs, logger);
}
}
/// <summary>
/// Load a CAR repo stream and extract the MST items from it.
/// </summary>
/// <param name="s"></param>
/// <param name="logger"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static List<MstItem> LoadMstItemsFromRepo(Stream s, IDnProtoLogger logger)
{
List<MstItem> mstItems = new List<MstItem>();
//
// Walk repo and get mst nodes
//
Repo.WalkRepo(s,
(header) =>
{
return true;
},
(record) =>
{
if(RepoMst.IsMstNode(record))
{
// Entries
var entriesObj = (List<DagCborObject>?)record.DataBlock.SelectObjectValue(new []{"e"});
if (entriesObj != null)
{
List<string> fullKeys = new List<string>();
List<CidV1> recordCids = new List<CidV1>();
for(int i = 0; i < entriesObj.Count; i++)
{
// "p" - prefix length
int prefixLength = entriesObj[i].SelectInt(new[] { "p" }) ?? 0;
// "k" - key suffix
var keyBytes = (byte[]?)entriesObj[i].SelectObjectValue(new[] { "k" });
string? keySuffix = keyBytes != null ? System.Text.Encoding.UTF8.GetString(keyBytes) : null;
// "v" - record CID
CidV1? cid = (CidV1?)entriesObj[i].SelectObjectValue(new[] { "v" });
if(cid is null || keySuffix is null)
{
throw new Exception("CID or key suffix is null");
}
string fullKey = (i == 0) ? keySuffix : fullKeys[i-1].Substring(0, prefixLength) + keySuffix;
fullKeys.Add(fullKey);
recordCids.Add(cid);
mstItems.Add(new MstItem() { Key = fullKey, Value = cid.Base32 });
}
}
}
return true;
});
//
// Return
//
return mstItems;
}
#endregion
#region DAG CBOR
/// <summary>
/// The main entry point for converting the MST into DAG CBOR objects.
///
/// Because this might be called recursively, we use a cache to avoid re-creating.
///
/// </summary>
/// <param name="cache"></param>
/// <param name="node"></param>
public static void ConvertMstNodeToDagCbor(Dictionary<MstNode, (CidV1, DagCborObject)> cache, MstNode node)
{
//
// If not cached, create it.
//
if(cache.ContainsKey(node) == false)
{
//
// Create empty dict
//
var nodeDict = new Dictionary<string, DagCborObject>();
//
// Add left link if present
//
if (node.LeftTree != null)
{
if(!cache.ContainsKey(node.LeftTree))
{
ConvertMstNodeToDagCbor(cache, node.LeftTree);
}
(CidV1 leftCid, DagCborObject leftObj) = cache[node.LeftTree];
nodeDict["l"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = leftCid
};
}
else
{
nodeDict["l"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_SIMPLE_VALUE, AdditionalInfo = 0x16, OriginalByte = 0 },
Value = "null"
};
}
//
// Add entries array
//
var entriesArray = new List<DagCborObject>();
List<int> prefixLengths = GetPrefixLengths(node.Entries);
List<string> keySuffixes = GetKeySuffixes(node.Entries);
for(int i = 0; i < node.Entries.Count; i++)
{
var entry = node.Entries[i];
var entryDict = new Dictionary<string, DagCborObject>();
// "p" - prefix length
entryDict["p"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_UNSIGNED_INT, AdditionalInfo = 0, OriginalByte = 0 },
Value = prefixLengths[i]
};
// "k" - key suffix (byte string)
entryDict["k"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_BYTE_STRING, AdditionalInfo = 0, OriginalByte = 0 },
Value = System.Text.Encoding.UTF8.GetBytes(keySuffixes[i])
};
// "v" - value CID
entryDict["v"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = CidV1.FromBase32(entry.Value)
};
// "t" - tree CID (nullable)
if (entry.RightTree != null)
{
if(!cache.ContainsKey(entry.RightTree))
{
ConvertMstNodeToDagCbor(cache, entry.RightTree);
}
(CidV1 rightCid, DagCborObject rightObj) = cache[entry.RightTree];
entryDict["t"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_TAG, AdditionalInfo = 42, OriginalByte = 0 },
Value = rightCid
};
}
else
{
entryDict["t"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_SIMPLE_VALUE, AdditionalInfo = 0x16, OriginalByte = 0 },
Value = "null"
};
}
entriesArray.Add(new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_MAP, AdditionalInfo = 0, OriginalByte = 0 },
Value = entryDict
});
}
nodeDict["e"] = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_ARRAY, AdditionalInfo = 0, OriginalByte = 0 },
Value = entriesArray
};
//
// Make enclosing MAP object.
//
var nodeObj = new DagCborObject
{
Type = new DagCborType { MajorType = DagCborType.TYPE_MAP, AdditionalInfo = 0, OriginalByte = 0 },
Value = nodeDict
};
//
// Write to cache (for other callers).
//
cache[node] = (CidV1.ComputeCidForDagCbor(nodeObj)!, nodeObj);
}
}
#endregion
#region ENTRIES
public static bool IsMstNode(RepoRecord record)
{
bool notNull = record.DataBlock != null;
bool isMap = record.DataBlock?.Type.MajorType == DagCborType.TYPE_MAP;
bool containsE = (record.DataBlock?.SelectObjectValue(new[]{"e"}) as List<DagCborObject>) != null;
return notNull && isMap && containsE;
}
public static List<int> GetPrefixLengths(List<MstEntry> entries)
{
var prefixLengths = new List<int>();
string previousFullKey = string.Empty;
for (int i = 0; i < entries.Count; i++)
{
if (i == 0)
{
prefixLengths.Add(0);
previousFullKey = entries[i].Key;
}
else
{
int prefixLen = GetCommonPrefixLength(previousFullKey, entries[i].Key);
prefixLengths.Add(prefixLen);
previousFullKey = entries[i].Key;
}
}
return prefixLengths;
}
public static List<string> GetKeySuffixes(List<MstEntry> entries)
{
var keySuffixes = new List<string>();
string previousFullKey = string.Empty;
for (int i = 0; i < entries.Count; i++)
{
if (i == 0)
{
keySuffixes.Add(entries[i].Key);
previousFullKey = entries[i].Key;
}
else
{
int prefixLen = GetCommonPrefixLength(previousFullKey, entries[i].Key);
keySuffixes.Add(entries[i].Key.Substring(prefixLen));
previousFullKey = entries[i].Key;
}
}
return keySuffixes;
}
/// <summary>
/// Get the length of the common prefix between two keys.
/// </summary>
public static int GetCommonPrefixLength(string a, string b)
{
int len = 0;
int minLen = Math.Min(a.Length, b.Length);
for (int i = 0; i < minLen; i++)
{
if (a[i] == b[i])
{
len++;
}
else
{
break;
}
}
return len;
}
#endregion
}