-
Notifications
You must be signed in to change notification settings - Fork 138
/
MerkleTrie.Path.cs
194 lines (165 loc) · 6.92 KB
/
MerkleTrie.Path.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
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Security.Cryptography;
using Bencodex.Types;
using Libplanet.Store.Trie.Nodes;
namespace Libplanet.Store.Trie
{
public partial class MerkleTrie
{
private PathResolution ResolvePath(INode? node, in PathCursor cursor) =>
node switch
{
null => PathResolution.Unresolved(),
ValueNode valueNode => PathResolution.Resolved(valueNode.Value),
ShortNode shortNode => !cursor.RemainingNibblesStartWith(shortNode.Key)
? PathResolution.Unresolved()
: ResolvePath(shortNode.Value, cursor.Next(shortNode.Key.Length)),
FullNode fullNode => ResolvePath(
fullNode.Children[cursor.NextNibble],
cursor.Next(1)
),
HashNode hashNode => PathResolution.PartiallyResolved(hashNode.HashDigest, cursor),
_ => throw new InvalidTrieNodeException(
$"Invalid node value: {node.ToBencodex().Inspect(false)}"
),
};
internal readonly struct PathCursor
{
public readonly ImmutableArray<byte> Bytes;
public readonly int NibbleLength;
public readonly int NibbleOffset;
public PathCursor(in KeyBytes keyBytes, bool secure)
: this(
secure
? HashDigest<SHA256>.DeriveFrom(keyBytes.ByteArray).ByteArray
: keyBytes.ByteArray,
0,
(secure ? HashDigest<SHA256>.Size : keyBytes.Length) * 2)
{
}
private PathCursor(in ImmutableArray<byte> bytes, int nibbleOffset, int nibbleLength)
{
if (nibbleOffset < 0 || nibbleOffset > nibbleLength)
{
throw new ArgumentOutOfRangeException(nameof(nibbleOffset));
}
Bytes = bytes;
NibbleOffset = nibbleOffset;
NibbleLength = nibbleLength;
}
[Pure]
public int RemainingNibbleLength => NibbleLength - NibbleOffset;
[Pure]
public bool RemainingAnyNibbles => NibbleLength > NibbleOffset;
[Pure]
public byte NextNibble => NibbleOffset < NibbleLength
? GetNibble(Bytes[NibbleOffset / 2], NibbleOffset % 2 == 0)
: throw new IndexOutOfRangeException();
[Pure]
public static PathCursor FromNibbles(
in ImmutableArray<byte> nibbles,
int nibbleOffset = 0
)
{
if (nibbleOffset < 0 || nibbleOffset > nibbles.Length)
{
throw new ArgumentOutOfRangeException(nameof(nibbleOffset));
}
int oddNibble = nibbles.Length % 2;
int byteLength = nibbles.Length / 2 + oddNibble;
var builder = ImmutableArray.CreateBuilder<byte>(byteLength);
for (int i = 0, evenNibbles = nibbles.Length - oddNibble; i < evenNibbles; i += 2)
{
builder.Add((byte)(nibbles[i] << 4 | nibbles[i + 1]));
}
if (oddNibble > 0)
{
builder.Add((byte)(nibbles[nibbles.Length - 1] << 4));
}
return new PathCursor(builder.MoveToImmutable(), nibbleOffset, nibbles.Length);
}
[Pure]
public byte NibbleAt(int index)
{
int nibbleOffset = NibbleOffset + index;
if (index < 0 || nibbleOffset >= NibbleLength)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
return GetNibble(Bytes[nibbleOffset / 2], nibbleOffset % 2 == 0);
}
[Pure]
public ImmutableArray<byte> GetRemainingNibbles()
{
var builder = ImmutableArray.CreateBuilder<byte>(RemainingNibbleLength);
for (int i = NibbleOffset; i < NibbleLength; i++)
{
builder.Add(GetNibble(Bytes[i / 2], i % 2 == 0));
}
return builder.MoveToImmutable();
}
[Pure]
public PathCursor Next(int nibbleOffset) => nibbleOffset < 0
? throw new ArgumentOutOfRangeException(nameof(nibbleOffset))
: new PathCursor(Bytes, NibbleOffset + nibbleOffset, NibbleLength);
// FIXME: We should declare a custom value type to represent nibbles...
[Pure]
public bool RemainingNibblesStartWith(in ImmutableArray<byte> nibbles)
{
if (NibbleLength < nibbles.Length)
{
return false;
}
return CountCommonStartingNibbles(nibbles) >= nibbles.Length;
}
// FIXME: We should declare a custom value type to represent nibbles...
[Pure]
public int CountCommonStartingNibbles(in ImmutableArray<byte> nibbles)
{
for (int i = 0; i < nibbles.Length; i++)
{
int nibbleOffset = NibbleOffset + i;
int byteOffset = nibbleOffset / 2;
if (byteOffset >= Bytes.Length)
{
return i;
}
byte nibble = GetNibble(Bytes[byteOffset], nibbleOffset % 2 == 0);
if (nibble != nibbles[i])
{
return i;
}
}
return nibbles.Length;
}
// FIXME: We should declare a custom value type to represent nibbles...
[Pure]
private static byte GetNibble(byte octet, bool high) =>
high ? (byte)(octet >> 4) : (byte)(octet & 0b00001111);
}
private readonly struct PathResolution
{
public readonly IValue? Value;
public readonly (HashDigest<SHA256> NodeHash, PathCursor Cursor)? Next;
private PathResolution(IValue? value)
{
Value = value;
Next = null;
}
private PathResolution(in HashDigest<SHA256> nodeHash, in PathCursor cursor)
{
Value = null;
Next = (nodeHash, cursor);
}
public static PathResolution Unresolved() => default;
public static PathResolution Resolved(IValue value) => new PathResolution(value);
public static PathResolution PartiallyResolved(
in HashDigest<SHA256> nodeHash,
in PathCursor cursor
) =>
new PathResolution(nodeHash, cursor);
}
}
}