Skip to content

Commit 67e4975

Browse files
NGDAdminshargon
andauthored
[ut] 100% Coverage Trie.Get (#3952) (#3957)
* 100% Coverage Trie.Get * fix ut Co-authored-by: Shargon <[email protected]>
1 parent 7802202 commit 67e4975

File tree

7 files changed

+136
-121
lines changed

7 files changed

+136
-121
lines changed

src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Delete.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public bool Delete(byte[] key)
2323
throw new ArgumentException("could not be empty", nameof(key));
2424
if (path.Length > Node.MaxKeyLength)
2525
throw new ArgumentException("exceeds limit", nameof(key));
26-
return TryDelete(ref root, path);
26+
return TryDelete(ref _root, path);
2727
}
2828

2929
private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
@@ -34,7 +34,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
3434
{
3535
if (path.IsEmpty)
3636
{
37-
if (!full) cache.DeleteNode(node.Hash);
37+
if (!_full) _cache.DeleteNode(node.Hash);
3838
node = new Node();
3939
return true;
4040
}
@@ -47,20 +47,20 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
4747
var oldHash = node.Hash;
4848
var result = TryDelete(ref node.Next, path[node.Key.Length..]);
4949
if (!result) return false;
50-
if (!full) cache.DeleteNode(oldHash);
50+
if (!_full) _cache.DeleteNode(oldHash);
5151
if (node.Next.IsEmpty)
5252
{
5353
node = node.Next;
5454
return true;
5555
}
5656
if (node.Next.Type == NodeType.ExtensionNode)
5757
{
58-
if (!full) cache.DeleteNode(node.Next.Hash);
58+
if (!_full) _cache.DeleteNode(node.Next.Hash);
5959
node.Key = new([.. node.Key.Span, .. node.Next.Key.Span]);
6060
node.Next = node.Next.Next;
6161
}
6262
node.SetDirty();
63-
cache.PutNode(node);
63+
_cache.PutNode(node);
6464
return true;
6565
}
6666
return false;
@@ -78,7 +78,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
7878
result = TryDelete(ref node.Children[path[0]], path[1..]);
7979
}
8080
if (!result) return false;
81-
if (!full) cache.DeleteNode(oldHash);
81+
if (!_full) _cache.DeleteNode(oldHash);
8282
List<byte> childrenIndexes = new List<byte>(Node.BranchChildCount);
8383
for (int i = 0; i < Node.BranchChildCount; i++)
8484
{
@@ -88,7 +88,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
8888
if (childrenIndexes.Count > 1)
8989
{
9090
node.SetDirty();
91-
cache.PutNode(node);
91+
_cache.PutNode(node);
9292
return true;
9393
}
9494
var lastChildIndex = childrenIndexes[0];
@@ -100,20 +100,20 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
100100
}
101101
if (lastChild.Type == NodeType.HashNode)
102102
{
103-
lastChild = cache.Resolve(lastChild.Hash);
103+
lastChild = _cache.Resolve(lastChild.Hash);
104104
if (lastChild is null) throw new InvalidOperationException("Internal error, can't resolve hash");
105105
}
106106
if (lastChild.Type == NodeType.ExtensionNode)
107107
{
108-
if (!full) cache.DeleteNode(lastChild.Hash);
108+
if (!_full) _cache.DeleteNode(lastChild.Hash);
109109
lastChild.Key = new([.. childrenIndexes.ToArray(), .. lastChild.Key.Span]);
110110
lastChild.SetDirty();
111-
cache.PutNode(lastChild);
111+
_cache.PutNode(lastChild);
112112
node = lastChild;
113113
return true;
114114
}
115115
node = Node.NewExtension(childrenIndexes.ToArray(), lastChild);
116-
cache.PutNode(node);
116+
_cache.PutNode(node);
117117
return true;
118118
}
119119
case NodeType.Empty:
@@ -122,7 +122,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
122122
}
123123
case NodeType.HashNode:
124124
{
125-
var newNode = cache.Resolve(node.Hash);
125+
var newNode = _cache.Resolve(node.Hash);
126126
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt delete");
127127
node = newNode;
128128
return TryDelete(ref node, path);

src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Find.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private ReadOnlySpan<byte> Seek(ref Node node, ReadOnlySpan<byte> path, out Node
3434
break;
3535
case NodeType.HashNode:
3636
{
37-
var newNode = cache.Resolve(node.Hash);
37+
var newNode = _cache.Resolve(node.Hash);
3838
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt seek");
3939
node = newNode;
4040
return Seek(ref node, path, out start);
@@ -84,7 +84,7 @@ private ReadOnlySpan<byte> Seek(ref Node node, ReadOnlySpan<byte> path, out Node
8484
}
8585
if (path.Length > Node.MaxKeyLength || from.Length > Node.MaxKeyLength)
8686
throw new ArgumentException("exceeds limit");
87-
path = Seek(ref root, path, out Node start).ToArray();
87+
path = Seek(ref _root, path, out Node start).ToArray();
8888
if (from.Length > 0)
8989
{
9090
for (int i = 0; i < from.Length && i < path.Length; i++)
@@ -120,7 +120,7 @@ private ReadOnlySpan<byte> Seek(ref Node node, ReadOnlySpan<byte> path, out Node
120120
break;
121121
case NodeType.HashNode:
122122
{
123-
var newNode = cache.Resolve(node.Hash);
123+
var newNode = _cache.Resolve(node.Hash);
124124
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt find");
125125
node = newNode;
126126
foreach (var item in Travers(node, path, from, offset))

src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Get.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public byte[] this[byte[] key]
2525
throw new ArgumentException("could not be empty", nameof(key));
2626
if (path.Length > Node.MaxKeyLength)
2727
throw new ArgumentException("exceeds limit", nameof(key));
28-
var result = TryGet(ref root, path, out var value);
28+
var result = TryGet(ref _root, path, out var value);
2929
return result ? value.ToArray() : throw new KeyNotFoundException();
3030
}
3131
}
@@ -38,7 +38,7 @@ public bool TryGetValue(byte[] key, out byte[] value)
3838
throw new ArgumentException("could not be empty", nameof(key));
3939
if (path.Length > Node.MaxKeyLength)
4040
throw new ArgumentException("exceeds limit", nameof(key));
41-
var result = TryGet(ref root, path, out var val);
41+
var result = TryGet(ref _root, path, out var val);
4242
if (result)
4343
value = val.ToArray();
4444
return result;
@@ -61,7 +61,7 @@ private bool TryGet(ref Node node, ReadOnlySpan<byte> path, out ReadOnlySpan<byt
6161
break;
6262
case NodeType.HashNode:
6363
{
64-
var newNode = cache.Resolve(node.Hash);
64+
var newNode = _cache.Resolve(node.Hash);
6565
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt get");
6666
node = newNode;
6767
return TryGet(ref node, path, out value);

src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Proof.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public bool TryGetProof(byte[] key, out HashSet<byte[]> proof)
2626
if (path.Length > Node.MaxKeyLength)
2727
throw new ArgumentException("exceeds limit", nameof(key));
2828
proof = new HashSet<byte[]>(ByteArrayEqualityComparer.Default);
29-
return GetProof(ref root, path, proof);
29+
return GetProof(ref _root, path, proof);
3030
}
3131

3232
private bool GetProof(ref Node node, ReadOnlySpan<byte> path, HashSet<byte[]> set)
@@ -46,7 +46,7 @@ private bool GetProof(ref Node node, ReadOnlySpan<byte> path, HashSet<byte[]> se
4646
break;
4747
case NodeType.HashNode:
4848
{
49-
var newNode = cache.Resolve(node.Hash);
49+
var newNode = _cache.Resolve(node.Hash);
5050
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt getproof");
5151
node = newNode;
5252
return GetProof(ref node, path, set);

src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Put.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Put(byte[] key, byte[] value)
3838
if (val.Length > Node.MaxValueLength)
3939
throw new ArgumentException("exceed limit", nameof(value));
4040
var n = Node.NewLeaf(val);
41-
Put(ref root, path, n);
41+
Put(ref _root, path, n);
4242
}
4343

4444
private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
@@ -49,15 +49,15 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
4949
{
5050
if (path.IsEmpty)
5151
{
52-
if (!full) cache.DeleteNode(node.Hash);
52+
if (!_full) _cache.DeleteNode(node.Hash);
5353
node = val;
54-
cache.PutNode(node);
54+
_cache.PutNode(node);
5555
return;
5656
}
5757
var branch = Node.NewBranch();
5858
branch.Children[Node.BranchChildCount - 1] = node;
5959
Put(ref branch.Children[path[0]], path[1..], val);
60-
cache.PutNode(branch);
60+
_cache.PutNode(branch);
6161
node = branch;
6262
break;
6363
}
@@ -67,12 +67,12 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
6767
{
6868
var oldHash = node.Hash;
6969
Put(ref node.Next, path[node.Key.Length..], val);
70-
if (!full) cache.DeleteNode(oldHash);
70+
if (!_full) _cache.DeleteNode(oldHash);
7171
node.SetDirty();
72-
cache.PutNode(node);
72+
_cache.PutNode(node);
7373
return;
7474
}
75-
if (!full) cache.DeleteNode(node.Hash);
75+
if (!_full) _cache.DeleteNode(node.Hash);
7676
var prefix = CommonPrefix(node.Key.Span, path);
7777
var pathRemain = path[prefix.Length..];
7878
var keyRemain = node.Key.Span[prefix.Length..];
@@ -85,7 +85,7 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
8585
else
8686
{
8787
var exNode = Node.NewExtension(keyRemain[1..].ToArray(), node.Next);
88-
cache.PutNode(exNode);
88+
_cache.PutNode(exNode);
8989
child.Children[keyRemain[0]] = exNode;
9090
}
9191
if (pathRemain.IsEmpty)
@@ -98,11 +98,11 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
9898
Put(ref grandChild, pathRemain[1..], val);
9999
child.Children[pathRemain[0]] = grandChild;
100100
}
101-
cache.PutNode(child);
101+
_cache.PutNode(child);
102102
if (prefix.Length > 0)
103103
{
104104
var exNode = Node.NewExtension(prefix.ToArray(), child);
105-
cache.PutNode(exNode);
105+
_cache.PutNode(exNode);
106106
node = exNode;
107107
}
108108
else
@@ -122,9 +122,9 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
122122
{
123123
Put(ref node.Children[path[0]], path[1..], val);
124124
}
125-
if (!full) cache.DeleteNode(oldHash);
125+
if (!_full) _cache.DeleteNode(oldHash);
126126
node.SetDirty();
127-
cache.PutNode(node);
127+
_cache.PutNode(node);
128128
break;
129129
}
130130
case NodeType.Empty:
@@ -137,15 +137,15 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
137137
else
138138
{
139139
newNode = Node.NewExtension(path.ToArray(), val);
140-
cache.PutNode(newNode);
140+
_cache.PutNode(newNode);
141141
}
142142
node = newNode;
143-
if (val.Type == NodeType.LeafNode) cache.PutNode(val);
143+
if (val.Type == NodeType.LeafNode) _cache.PutNode(val);
144144
break;
145145
}
146146
case NodeType.HashNode:
147147
{
148-
Node newNode = cache.Resolve(node.Hash);
148+
Node newNode = _cache.Resolve(node.Hash);
149149
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt put");
150150
node = newNode;
151151
Put(ref node, path, val);

src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ namespace Neo.Cryptography.MPTTrie
1717
public partial class Trie
1818
{
1919
private const byte Prefix = 0xf0;
20-
private readonly bool full;
21-
private readonly IStoreSnapshot store;
22-
private Node root;
23-
private readonly Cache cache;
24-
public Node Root => root;
20+
private readonly bool _full;
21+
private readonly IStoreSnapshot _store;
22+
private Node _root;
23+
private readonly Cache _cache;
24+
public Node Root => _root;
2525

2626
public Trie(IStoreSnapshot store, UInt256 root, bool full_state = false)
2727
{
28-
this.store = store ?? throw new ArgumentNullException(nameof(store));
29-
cache = new Cache(store, Prefix);
30-
this.root = root is null ? new Node() : Node.NewHash(root);
31-
full = full_state;
28+
_store = store ?? throw new ArgumentNullException(nameof(store));
29+
_cache = new Cache(store, Prefix);
30+
_root = root is null ? new Node() : Node.NewHash(root);
31+
_full = full_state;
3232
}
3333

3434
private static byte[] ToNibbles(ReadOnlySpan<byte> path)
3535
{
3636
var result = new byte[path.Length * 2];
37-
for (int i = 0; i < path.Length; i++)
37+
for (var i = 0; i < path.Length; i++)
3838
{
3939
result[i * 2] = (byte)(path[i] >> 4);
4040
result[i * 2 + 1] = (byte)(path[i] & 0x0F);
@@ -46,7 +46,7 @@ private static byte[] FromNibbles(ReadOnlySpan<byte> path)
4646
{
4747
if (path.Length % 2 != 0) throw new FormatException($"MPTTrie.FromNibbles invalid path.");
4848
var key = new byte[path.Length / 2];
49-
for (int i = 0; i < key.Length; i++)
49+
for (var i = 0; i < key.Length; i++)
5050
{
5151
key[i] = (byte)(path[i * 2] << 4);
5252
key[i] |= path[i * 2 + 1];
@@ -56,7 +56,7 @@ private static byte[] FromNibbles(ReadOnlySpan<byte> path)
5656

5757
public void Commit()
5858
{
59-
cache.Commit();
59+
_cache.Commit();
6060
}
6161
}
6262
}

0 commit comments

Comments
 (0)