-
Notifications
You must be signed in to change notification settings - Fork 139
/
MerkleTrie.cs
703 lines (609 loc) · 26.4 KB
/
MerkleTrie.cs
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Cryptography;
using Bencodex;
using Bencodex.Types;
using Libplanet.Store.Trie.Nodes;
namespace Libplanet.Store.Trie
{
/// <summary>
/// An <see cref="ITrie"/> implementation implemented
/// <see href="https://eth.wiki/fundamentals/patricia-tree">Merkle Patricia Trie</see>.
/// </summary>
// TODO: implement 'logs' for debugging.
public partial class MerkleTrie : ITrie
{
public static readonly HashDigest<SHA256> EmptyRootHash;
private const long OffloadThresholdBytes = 0x7fffffffffffffffL;
private static readonly ConcurrentDictionary<Fingerprint, WeakReference<IValue>> _valueCache
= new ConcurrentDictionary<Fingerprint, WeakReference<IValue>>();
private static readonly Codec _codec;
private readonly bool _secure;
static MerkleTrie()
{
_codec = new Codec();
var bxNull = _codec.Encode(Null.Value);
EmptyRootHash = HashDigest<SHA256>.DeriveFrom(bxNull);
}
/// <summary>
/// An <see cref="ITrie"/> implementation.
/// </summary>
/// <param name="keyValueStore">The <see cref="IKeyValueStore"/> storage to store
/// nodes.</param>
/// <param name="rootHash">The root <see cref="ITrie.Hash"/> of
/// <see cref="MerkleTrie"/>.</param>
/// <param name="secure">Whether to use <see cref="MerkleTrie"/> in
/// secure mode. If it is turned on, <see cref="MerkleTrie"/> internally stores hashed keys
/// instead of bare keys. Keys will be hashed with SHA-256.</param>
public MerkleTrie(
IKeyValueStore keyValueStore,
HashDigest<SHA256> rootHash,
bool secure = false)
: this(keyValueStore, new HashNode(rootHash), secure)
{
}
/// <summary>
/// An <see cref="ITrie"/> implementation.
/// </summary>
/// <param name="keyValueStore">The <see cref="IKeyValueStore"/> storage to store
/// nodes.</param>
/// <param name="root">The root node of <see cref="MerkleTrie"/>. If it is <c>null</c>,
/// it will be treated like empty trie.</param>
/// <param name="secure">Whether to use <see cref="MerkleTrie"/> in secure
/// mode. If it is true, <see cref="MerkleTrie"/> will stores the value with the hashed
/// result from the given key as the key. Keys will be hashed with SHA-256.</param>
internal MerkleTrie(IKeyValueStore keyValueStore, INode? root = null, bool secure = false)
{
KeyValueStore = keyValueStore;
Root = root is HashNode hashNode && hashNode.HashDigest.Equals(EmptyRootHash)
? null
: root;
_secure = secure;
}
/// <inheritdoc cref="ITrie.Hash"/>
public HashDigest<SHA256> Hash => Root?.Hash() ?? EmptyRootHash;
/// <inheritdoc cref="ITrie.Recorded"/>
public bool Recorded => Root is null || KeyValueStore.Exists(new KeyBytes(Hash.ByteArray));
internal INode? Root { get; }
private IKeyValueStore KeyValueStore { get; }
/// <inheritdoc/>
public ITrie Set(in KeyBytes key, IValue value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
INode newRootNode = Insert(
Root,
new PathCursor(key, _secure),
new ValueNode(value));
return new MerkleTrie(KeyValueStore, newRootNode, _secure);
}
/// <inheritdoc cref="ITrie.Get(IReadOnlyList{KeyBytes})"/>
public IReadOnlyList<IValue?> Get(IReadOnlyList<KeyBytes> keys)
{
PathResolution[] resolutions = keys
.Select(k => ResolvePath(Root, new PathCursor(k, _secure)))
.ToArray();
var nextNodeHashes = new List<KeyBytes>(resolutions.Length);
while (true)
{
nextNodeHashes.Clear();
for (int i = 0; i < resolutions.Length; i++)
{
PathResolution resolution = resolutions[i];
if (resolution.Next is (HashDigest<SHA256> nodeHash, _))
{
nextNodeHashes.Add(new KeyBytes(nodeHash.ByteArray));
}
}
if (!nextNodeHashes.Any())
{
break;
}
IReadOnlyDictionary<KeyBytes, byte[]> nValues = KeyValueStore.Get(nextNodeHashes);
for (int i = 0, j = 0; i < resolutions.Length; i++)
{
PathResolution resolution = resolutions[i];
if (resolution.Next is (_, PathCursor cursor))
{
byte[]? nodeValue = nValues[nextNodeHashes[j]];
j++;
if (nodeValue is { } v)
{
IValue intermediateEncoding = _codec.Decode(v, LoadIndirectValue);
INode? nextNode = NodeDecoder.Decode(intermediateEncoding);
resolutions[i] = ResolvePath(nextNode, cursor);
}
else
{
resolutions[i] = PathResolution.Unresolved();
}
}
}
}
return resolutions.Select(r => r.Value).ToArray();
}
/// <inheritdoc/>
public ITrie Commit()
{
if (Root is null)
{
return new MerkleTrie(KeyValueStore, new HashNode(EmptyRootHash));
}
var values = new ConcurrentDictionary<KeyBytes, byte[]>();
var newRoot = Commit(Root, values);
// It assumes embedded node if it's not HashNode.
if (!(newRoot is HashNode))
{
byte[] serialized = _codec.Encode(newRoot.ToBencodex());
values[new KeyBytes(SHA256.Create().ComputeHash(serialized))] = serialized;
}
KeyValueStore.Set(values);
var rv = new MerkleTrie(KeyValueStore, newRoot);
return rv;
}
internal IEnumerable<HashDigest<SHA256>> IterateHashNodes()
{
return IterateNodes().Where(pair => pair.Node is HashNode)
.Select(pair => ((HashNode)pair.Node).HashDigest);
}
internal IEnumerable<(INode Node, KeyBytes Path)> IterateNodes()
{
if (Root is null)
{
yield break;
}
var queue = new Queue<(INode, ImmutableArray<byte>)>();
queue.Enqueue((Root, ImmutableArray<byte>.Empty));
while (queue.Count > 0)
{
(INode node, ImmutableArray<byte> path) = queue.Dequeue();
yield return (node, new KeyBytes(path));
switch (node)
{
case FullNode fullNode:
foreach (int index in Enumerable.Range(0, FullNode.ChildrenCount - 1))
{
INode? child = fullNode.Children[index];
if (!(child is null))
{
queue.Enqueue((child, path.Add((byte)index)));
}
}
if (!(fullNode.Value is null))
{
queue.Enqueue((fullNode.Value, path));
}
break;
case ShortNode shortNode:
if (!(shortNode.Value is null))
{
queue.Enqueue((
shortNode.Value,
path.Concat(shortNode.Key).ToImmutableArray()));
}
break;
case HashNode hashNode:
INode? nn = GetNode(hashNode.HashDigest);
if (!(nn is null))
{
queue.Enqueue((nn, path));
}
break;
}
}
}
internal IEnumerable<(KeyBytes Key, byte[] Value)> IterateNodeKeyValuePairs()
{
if (Root is null)
{
yield break;
}
var queue =
new Queue<(KeyBytes Key, byte[] Value, ImmutableArray<byte> Path)>();
switch (Root)
{
case ValueNode valueNode:
var value = _codec.Encode(valueNode.ToBencodex());
var key = new KeyBytes(HashDigest<SHA256>.DeriveFrom(value).ByteArray);
yield return (key, value);
yield break;
case HashNode hashNode:
key = new KeyBytes(hashNode.HashDigest.ToByteArray());
queue.Enqueue((key, KeyValueStore.Get(key), ImmutableArray<byte>.Empty));
break;
case FullNode _:
case ShortNode _:
value = _codec.Encode(Root.ToBencodex());
key = new KeyBytes(HashDigest<SHA256>.DeriveFrom(value).ByteArray);
queue.Enqueue((key, value, ImmutableArray<byte>.Empty));
break;
}
bool GuessValueNodeByPath(in ImmutableArray<byte> path)
{
if (path.Length < 2)
{
return false;
}
bool isStartedWithUnderbar = (path[0] << 4) + path[1] == '_';
bool isStatePath = !isStartedWithUnderbar &&
path.Length == Address.Size * 2 * 2;
return isStatePath;
}
while (queue.Count > 0)
{
(KeyBytes key, byte[] value, ImmutableArray<byte> path) =
queue.Dequeue();
// It assumes every length of value nodes is same with Address' hexadecimal
// string's hexadecimal string's size.
bool isValueNode = GuessValueNodeByPath(path);
bool noFingerprint = value.All(x => x != '*');
if (noFingerprint)
{
yield return (key, value);
// To avoid decode value node, it decodes when only there is '*' character,
// fingerprint.
if (isValueNode)
{
continue;
}
}
var node = NodeDecoder.Decode(_codec.Decode(value, LoadIndirectValue));
if (!noFingerprint && !(node is null))
{
yield return (key, _codec.Encode(node.ToBencodex()));
}
if (isValueNode)
{
continue;
}
switch (node)
{
case FullNode fullNode:
foreach (int index in Enumerable.Range(0, FullNode.ChildrenCount - 1))
{
INode? child = fullNode.Children[index];
if (child is HashNode hashNode)
{
key = new KeyBytes(hashNode.HashDigest.ByteArray);
value = KeyValueStore.Get(key);
queue.Enqueue((key, value, path.Add((byte)index)));
}
}
switch (fullNode.Value)
{
case HashNode hashNode:
key = new KeyBytes(hashNode.HashDigest.ByteArray);
value = KeyValueStore.Get(key);
queue.Enqueue((key, value, path));
break;
}
break;
case ShortNode shortNode:
switch (shortNode.Value)
{
case HashNode hashNode:
key = new KeyBytes(hashNode.HashDigest.ByteArray);
value = KeyValueStore.Get(key);
queue.Enqueue((key, value, path.AddRange(shortNode.Key)));
break;
}
break;
case ValueNode _:
break;
default:
throw new InvalidOperationException();
}
}
}
private static void FreeValueCache()
{
foreach (KeyValuePair<Fingerprint, WeakReference<IValue>> kv in _valueCache)
{
if (!kv.Value.TryGetTarget(out _))
{
_valueCache.TryRemove(kv.Key, out _);
}
}
}
private INode Commit(INode node, IDictionary<KeyBytes, byte[]> values)
{
switch (node)
{
case HashNode _:
return node;
case FullNode fullNode:
return CommitFullNode(fullNode, values);
case ShortNode shortNode:
return CommitShortNode(shortNode, values);
case ValueNode valueNode:
return CommitValueNode(valueNode, values);
default:
throw new NotSupportedException("Not supported node came.");
}
}
private INode CommitFullNode(FullNode fullNode, IDictionary<KeyBytes, byte[]> values)
{
var virtualChildren = fullNode.Children
.Select(c => c is null ? null : Commit(c, values))
.ToImmutableArray();
fullNode = new FullNode(virtualChildren);
IValue encoded = fullNode.ToBencodex();
if (encoded.EncodingLength <= HashDigest<SHA256>.Size)
{
return fullNode;
}
return Encode(fullNode.ToBencodex(), values);
}
private INode CommitShortNode(ShortNode shortNode, IDictionary<KeyBytes, byte[]> values)
{
var committedValueNode = Commit(shortNode.Value!, values);
shortNode = new ShortNode(shortNode.Key, committedValueNode);
IValue encoded = shortNode.ToBencodex();
if (encoded.EncodingLength <= HashDigest<SHA256>.Size)
{
return shortNode;
}
return Encode(encoded, values);
}
private INode CommitValueNode(ValueNode valueNode, IDictionary<KeyBytes, byte[]> values)
{
IValue encoded = valueNode.ToBencodex();
var nodeSize = encoded.EncodingLength;
if (nodeSize <= HashDigest<SHA256>.Size)
{
return valueNode;
}
return Encode(encoded, values);
}
private HashNode Encode(IValue intermediateEncoding, IDictionary<KeyBytes, byte[]> values)
{
var offloadOptions = new OffloadOptions(OffloadThresholdBytes, values, KeyValueStore);
byte[] serialized = _codec.Encode(intermediateEncoding, offloadOptions);
byte[] fullEncoding = offloadOptions.Offloaded
? _codec.Encode(intermediateEncoding)
: serialized;
var nodeHash = HashDigest<SHA256>.DeriveFrom(fullEncoding);
values[new KeyBytes(nodeHash.ByteArray)] = serialized;
return new HashNode(nodeHash);
}
private INode Insert(INode? node, in PathCursor cursor, INode value)
{
// If path exists only last one
if (!cursor.RemainingAnyNibbles)
{
return value;
}
switch (node)
{
case ShortNode shortNode:
return InsertShortNode(shortNode, cursor, value);
case FullNode fullNode:
byte nextNibble = cursor.NextNibble;
var n = Insert(
fullNode.Children[nextNibble],
cursor.Next(1),
value);
return fullNode.SetChild(nextNibble, n);
case null:
return new ShortNode(cursor.GetRemainingNibbles(), value);
case HashNode hashNode:
var hn = GetNode(hashNode.HashDigest);
return Insert(hn, cursor, value);
default:
throw new InvalidTrieNodeException(
$"Unsupported node value: {node.ToBencodex().Inspect(false)}"
);
}
}
private INode InsertShortNode(ShortNode shortNode, in PathCursor cursor, INode value)
{
int commonPrefixLength = cursor.CountCommonStartingNibbles(shortNode.Key);
if (commonPrefixLength == shortNode.Key.Length)
{
INode nn = Insert(shortNode.Value, cursor.Next(commonPrefixLength), value);
return new ShortNode(shortNode.Key, nn);
}
var branch = new FullNode();
branch = branch.SetChild(
cursor.NibbleAt(commonPrefixLength),
Insert(null, cursor.Next(commonPrefixLength + 1), value)
);
PathCursor branchCursor =
PathCursor.FromNibbles(shortNode.Key, commonPrefixLength + 1);
branch = branch.SetChild(
shortNode.Key[commonPrefixLength],
Insert(null, branchCursor, shortNode.Value!)
);
if (commonPrefixLength == 0)
{
return branch;
}
// extension node
ImmutableArray<byte> commonPrefixNibbles = shortNode.Key.RemoveRange(
commonPrefixLength,
shortNode.Key.Length - commonPrefixLength
);
return new ShortNode(commonPrefixNibbles, branch);
}
/// <summary>
/// Gets the node corresponding to <paramref name="nodeHash"/> from storage,
/// (i.e., <see cref="KeyValueStore"/>).
/// </summary>
/// <param name="nodeHash">The hash of node to get.</param>
/// <returns>The node corresponding to <paramref name="nodeHash"/>.</returns>
/// <exception cref="KeyNotFoundException">Thrown when the <paramref name="nodeHash"/> is
/// not found.</exception>
private INode? GetNode(HashDigest<SHA256> nodeHash)
{
IValue intermediateEncoding = _codec.Decode(
KeyValueStore.Get(new KeyBytes(nodeHash.ByteArray)),
LoadIndirectValue
);
return NodeDecoder.Decode(intermediateEncoding);
}
private IValue LoadIndirectValue(Fingerprint fp)
{
if (fp.EncodingLength >= OffloadThresholdBytes &&
_valueCache.TryGetValue(fp, out WeakReference<IValue>? weakRef) &&
weakRef is { } w)
{
if (w.TryGetTarget(out IValue? cached) && cached is { } cachedValue)
{
return cachedValue;
}
_valueCache.TryRemove(fp, out _);
}
var key = new KeyBytes(fp.Serialize());
if (fp.Kind != ValueKind.Dictionary)
{
FreeValueCache();
IValue v = _codec.Decode(KeyValueStore.Get(key), LoadIndirectValue);
if (fp != v.Fingerprint)
{
throw new InvalidOperationException(
$"Failed to load an offloaded value." +
$"\nExpected: {fp}\nActual: {v.Fingerprint}\nLoaded: {v}"
);
}
else if (fp.EncodingLength >= OffloadThresholdBytes)
{
_valueCache[fp] = new WeakReference<IValue>(v);
}
return v;
}
var pair = (List)_codec.Decode(KeyValueStore.Get(key), LoadIndirectValue);
IEnumerable<IndirectValue> reprs =
pair.EnumerateIndirectValues(out IndirectValue.Loader? reprLoader);
Dictionary value;
if (reprLoader is { } l)
{
IndirectValue keysIv = reprs.First();
var keys = keysIv.LoadedValue is List lst ? lst : (List)keysIv.GetValue(l);
IEnumerable<KeyValuePair<IKey, IndirectValue>> indirectPairs = keys.Zip(
reprs.Skip(1).Select(group => (List)group.GetValue(l)).SelectMany(vs =>
vs.EnumerateIndirectValues(out _)
),
(k, v) => new KeyValuePair<IKey, IndirectValue>((IKey)k, v)
);
value = new Dictionary(indirectPairs, l);
}
else
{
var keys = (List)pair[0];
IEnumerable<KeyValuePair<IKey, IValue>> pairs = keys.Zip(
pair.Skip(1).SelectMany(group => (List)group),
(k, v) => new KeyValuePair<IKey, IValue>((IKey)k, v)
);
value = new Dictionary(pairs);
}
if (fp != value.Fingerprint)
{
throw new InvalidOperationException(
$"Failed to load an offloaded value." +
$"\nExpected: {fp}\nActual: {value.Fingerprint}\nLoaded: {value}"
);
}
FreeValueCache();
if (fp.EncodingLength >= OffloadThresholdBytes)
{
_valueCache[fp] = new WeakReference<IValue>(value);
}
return value;
}
private sealed class OffloadOptions : IOffloadOptions
{
[SuppressMessage(
"Microsoft.StyleCop.CSharp.CSharpRules",
"SA1401:FieldsMustBePrivate",
Justification = "It's a private class and we want to get rid of runtime overhead.")]
public bool Offloaded;
private readonly long _thresholdBytes;
private readonly IDictionary<KeyBytes, byte[]> _dirty;
private readonly IKeyValueStore _store;
public OffloadOptions(
long thresholdBytes,
IDictionary<KeyBytes, byte[]> dirty,
IKeyValueStore store
)
{
_thresholdBytes = thresholdBytes;
_dirty = dirty;
_store = store;
Offloaded = false;
}
public bool Embeds(in IndirectValue indirectValue) =>
indirectValue.EncodingLength < _thresholdBytes;
public void Offload(in IndirectValue indirectValue, IndirectValue.Loader? loader)
{
Offloaded = true;
var fp = new KeyBytes(indirectValue.Fingerprint.Serialize());
if (!_dirty.ContainsKey(fp) && !_store.Exists(fp))
{
IValue value = indirectValue.GetValue(loader);
IValue repr = value;
if (repr is Dictionary dict)
{
// For dictionaries, in order to reduce duplicate common keys (= schema),
// they are encoded in [keys, [v, v', ...], [v'', v''', ...], ...] where
// keys = [k, k', ...] instead of [k, v, k', v', ...].
// Keys and value groups can be offloaded too.
const int groupSize = 0x7fffffff;
var keys = new List<IKey>(dict.Count);
var valueGroups =
new IValue[1 + (int)Math.Ceiling((double)dict.Count / groupSize)];
IEnumerable<KeyValuePair<IKey, IndirectValue>> pairs =
dict.EnumerableIndirectPairs(out loader);
if (loader is { } l)
{
var group = new List<IndirectValue>(groupSize);
int g = 1;
foreach (KeyValuePair<IKey, IndirectValue> pair in pairs)
{
keys.Add(pair.Key);
group.Add(pair.Value);
if (group.Count >= groupSize || keys.Count >= dict.Count)
{
List valueGroup = new List(group, l);
group.Clear();
valueGroups[g] = valueGroup;
g++;
}
}
}
else
{
var group = new List<IValue>(groupSize);
int g = 1;
foreach (KeyValuePair<IKey, IValue> pair in dict)
{
keys.Add(pair.Key);
group.Add(pair.Value);
if (group.Count >= groupSize || keys.Count >= dict.Count)
{
List valueGroup = new List(group);
group.Clear();
valueGroups[g] = valueGroup;
g++;
}
}
}
valueGroups[0] = new List(keys);
repr = new List(valueGroups);
}
_dirty[fp] = _codec.Encode(repr, this);
if (!_valueCache.ContainsKey(indirectValue.Fingerprint))
{
FreeValueCache();
_valueCache[indirectValue.Fingerprint] = new WeakReference<IValue>(value);
}
}
}
}
}
}