Skip to content
24 changes: 12 additions & 12 deletions src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public bool Delete(byte[] key)
throw new ArgumentException("could not be empty", nameof(key));
if (path.Length > Node.MaxKeyLength)
throw new ArgumentException("exceeds limit", nameof(key));
return TryDelete(ref root, path);
return TryDelete(ref _root, path);
}

private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
Expand All @@ -34,7 +34,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
{
if (path.IsEmpty)
{
if (!full) cache.DeleteNode(node.Hash);
if (!_full) _cache.DeleteNode(node.Hash);
node = new Node();
return true;
}
Expand All @@ -47,20 +47,20 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
var oldHash = node.Hash;
var result = TryDelete(ref node.Next, path[node.Key.Length..]);
if (!result) return false;
if (!full) cache.DeleteNode(oldHash);
if (!_full) _cache.DeleteNode(oldHash);
if (node.Next.IsEmpty)
{
node = node.Next;
return true;
}
if (node.Next.Type == NodeType.ExtensionNode)
{
if (!full) cache.DeleteNode(node.Next.Hash);
if (!_full) _cache.DeleteNode(node.Next.Hash);
node.Key = new([.. node.Key.Span, .. node.Next.Key.Span]);
node.Next = node.Next.Next;
}
node.SetDirty();
cache.PutNode(node);
_cache.PutNode(node);
return true;
}
return false;
Expand All @@ -78,7 +78,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
result = TryDelete(ref node.Children[path[0]], path[1..]);
}
if (!result) return false;
if (!full) cache.DeleteNode(oldHash);
if (!_full) _cache.DeleteNode(oldHash);
List<byte> childrenIndexes = new List<byte>(Node.BranchChildCount);
for (int i = 0; i < Node.BranchChildCount; i++)
{
Expand All @@ -88,7 +88,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
if (childrenIndexes.Count > 1)
{
node.SetDirty();
cache.PutNode(node);
_cache.PutNode(node);
return true;
}
var lastChildIndex = childrenIndexes[0];
Expand All @@ -100,20 +100,20 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
}
if (lastChild.Type == NodeType.HashNode)
{
lastChild = cache.Resolve(lastChild.Hash);
lastChild = _cache.Resolve(lastChild.Hash);
if (lastChild is null) throw new InvalidOperationException("Internal error, can't resolve hash");
}
if (lastChild.Type == NodeType.ExtensionNode)
{
if (!full) cache.DeleteNode(lastChild.Hash);
if (!_full) _cache.DeleteNode(lastChild.Hash);
lastChild.Key = new([.. childrenIndexes.ToArray(), .. lastChild.Key.Span]);
lastChild.SetDirty();
cache.PutNode(lastChild);
_cache.PutNode(lastChild);
node = lastChild;
return true;
}
node = Node.NewExtension(childrenIndexes.ToArray(), lastChild);
cache.PutNode(node);
_cache.PutNode(node);
return true;
}
case NodeType.Empty:
Expand All @@ -122,7 +122,7 @@ private bool TryDelete(ref Node node, ReadOnlySpan<byte> path)
}
case NodeType.HashNode:
{
var newNode = cache.Resolve(node.Hash);
var newNode = _cache.Resolve(node.Hash);
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt delete");
node = newNode;
return TryDelete(ref node, path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private ReadOnlySpan<byte> Seek(ref Node node, ReadOnlySpan<byte> path, out Node
break;
case NodeType.HashNode:
{
var newNode = cache.Resolve(node.Hash);
var newNode = _cache.Resolve(node.Hash);
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt seek");
node = newNode;
return Seek(ref node, path, out start);
Expand Down Expand Up @@ -84,7 +84,7 @@ private ReadOnlySpan<byte> Seek(ref Node node, ReadOnlySpan<byte> path, out Node
}
if (path.Length > Node.MaxKeyLength || from.Length > Node.MaxKeyLength)
throw new ArgumentException("exceeds limit");
path = Seek(ref root, path, out Node start).ToArray();
path = Seek(ref _root, path, out Node start).ToArray();
if (from.Length > 0)
{
for (int i = 0; i < from.Length && i < path.Length; i++)
Expand Down Expand Up @@ -120,7 +120,7 @@ private ReadOnlySpan<byte> Seek(ref Node node, ReadOnlySpan<byte> path, out Node
break;
case NodeType.HashNode:
{
var newNode = cache.Resolve(node.Hash);
var newNode = _cache.Resolve(node.Hash);
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt find");
node = newNode;
foreach (var item in Travers(node, path, from, offset))
Expand Down
6 changes: 3 additions & 3 deletions src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public byte[] this[byte[] key]
throw new ArgumentException("could not be empty", nameof(key));
if (path.Length > Node.MaxKeyLength)
throw new ArgumentException("exceeds limit", nameof(key));
var result = TryGet(ref root, path, out var value);
var result = TryGet(ref _root, path, out var value);
return result ? value.ToArray() : throw new KeyNotFoundException();
}
}
Expand All @@ -38,7 +38,7 @@ public bool TryGetValue(byte[] key, out byte[] value)
throw new ArgumentException("could not be empty", nameof(key));
if (path.Length > Node.MaxKeyLength)
throw new ArgumentException("exceeds limit", nameof(key));
var result = TryGet(ref root, path, out var val);
var result = TryGet(ref _root, path, out var val);
if (result)
value = val.ToArray();
return result;
Expand All @@ -61,7 +61,7 @@ private bool TryGet(ref Node node, ReadOnlySpan<byte> path, out ReadOnlySpan<byt
break;
case NodeType.HashNode:
{
var newNode = cache.Resolve(node.Hash);
var newNode = _cache.Resolve(node.Hash);
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt get");
node = newNode;
return TryGet(ref node, path, out value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public bool TryGetProof(byte[] key, out HashSet<byte[]> proof)
if (path.Length > Node.MaxKeyLength)
throw new ArgumentException("exceeds limit", nameof(key));
proof = new HashSet<byte[]>(ByteArrayEqualityComparer.Default);
return GetProof(ref root, path, proof);
return GetProof(ref _root, path, proof);
}

private bool GetProof(ref Node node, ReadOnlySpan<byte> path, HashSet<byte[]> set)
Expand All @@ -46,7 +46,7 @@ private bool GetProof(ref Node node, ReadOnlySpan<byte> path, HashSet<byte[]> se
break;
case NodeType.HashNode:
{
var newNode = cache.Resolve(node.Hash);
var newNode = _cache.Resolve(node.Hash);
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt getproof");
node = newNode;
return GetProof(ref node, path, set);
Expand Down
30 changes: 15 additions & 15 deletions src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.Put.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Put(byte[] key, byte[] value)
if (val.Length > Node.MaxValueLength)
throw new ArgumentException("exceed limit", nameof(value));
var n = Node.NewLeaf(val);
Put(ref root, path, n);
Put(ref _root, path, n);
}

private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
Expand All @@ -49,15 +49,15 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
{
if (path.IsEmpty)
{
if (!full) cache.DeleteNode(node.Hash);
if (!_full) _cache.DeleteNode(node.Hash);
node = val;
cache.PutNode(node);
_cache.PutNode(node);
return;
}
var branch = Node.NewBranch();
branch.Children[Node.BranchChildCount - 1] = node;
Put(ref branch.Children[path[0]], path[1..], val);
cache.PutNode(branch);
_cache.PutNode(branch);
node = branch;
break;
}
Expand All @@ -67,12 +67,12 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
{
var oldHash = node.Hash;
Put(ref node.Next, path[node.Key.Length..], val);
if (!full) cache.DeleteNode(oldHash);
if (!_full) _cache.DeleteNode(oldHash);
node.SetDirty();
cache.PutNode(node);
_cache.PutNode(node);
return;
}
if (!full) cache.DeleteNode(node.Hash);
if (!_full) _cache.DeleteNode(node.Hash);
var prefix = CommonPrefix(node.Key.Span, path);
var pathRemain = path[prefix.Length..];
var keyRemain = node.Key.Span[prefix.Length..];
Expand All @@ -85,7 +85,7 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
else
{
var exNode = Node.NewExtension(keyRemain[1..].ToArray(), node.Next);
cache.PutNode(exNode);
_cache.PutNode(exNode);
child.Children[keyRemain[0]] = exNode;
}
if (pathRemain.IsEmpty)
Expand All @@ -98,11 +98,11 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
Put(ref grandChild, pathRemain[1..], val);
child.Children[pathRemain[0]] = grandChild;
}
cache.PutNode(child);
_cache.PutNode(child);
if (prefix.Length > 0)
{
var exNode = Node.NewExtension(prefix.ToArray(), child);
cache.PutNode(exNode);
_cache.PutNode(exNode);
node = exNode;
}
else
Expand All @@ -122,9 +122,9 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
{
Put(ref node.Children[path[0]], path[1..], val);
}
if (!full) cache.DeleteNode(oldHash);
if (!_full) _cache.DeleteNode(oldHash);
node.SetDirty();
cache.PutNode(node);
_cache.PutNode(node);
break;
}
case NodeType.Empty:
Expand All @@ -137,15 +137,15 @@ private void Put(ref Node node, ReadOnlySpan<byte> path, Node val)
else
{
newNode = Node.NewExtension(path.ToArray(), val);
cache.PutNode(newNode);
_cache.PutNode(newNode);
}
node = newNode;
if (val.Type == NodeType.LeafNode) cache.PutNode(val);
if (val.Type == NodeType.LeafNode) _cache.PutNode(val);
break;
}
case NodeType.HashNode:
{
Node newNode = cache.Resolve(node.Hash);
Node newNode = _cache.Resolve(node.Hash);
if (newNode is null) throw new InvalidOperationException("Internal error, can't resolve hash when mpt put");
node = newNode;
Put(ref node, path, val);
Expand Down
24 changes: 12 additions & 12 deletions src/Neo.Cryptography.MPTTrie/Cryptography/MPTTrie/Trie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ namespace Neo.Cryptography.MPTTrie
public partial class Trie
{
private const byte Prefix = 0xf0;
private readonly bool full;
private readonly IStoreSnapshot store;
private Node root;
private readonly Cache cache;
public Node Root => root;
private readonly bool _full;
private readonly IStoreSnapshot _store;
private Node _root;
private readonly Cache _cache;
public Node Root => _root;

public Trie(IStoreSnapshot store, UInt256 root, bool full_state = false)
{
this.store = store ?? throw new ArgumentNullException(nameof(store));
cache = new Cache(store, Prefix);
this.root = root is null ? new Node() : Node.NewHash(root);
full = full_state;
_store = store ?? throw new ArgumentNullException(nameof(store));
_cache = new Cache(store, Prefix);
_root = root is null ? new Node() : Node.NewHash(root);
_full = full_state;
}

private static byte[] ToNibbles(ReadOnlySpan<byte> path)
{
var result = new byte[path.Length * 2];
for (int i = 0; i < path.Length; i++)
for (var i = 0; i < path.Length; i++)
{
result[i * 2] = (byte)(path[i] >> 4);
result[i * 2 + 1] = (byte)(path[i] & 0x0F);
Expand All @@ -46,7 +46,7 @@ private static byte[] FromNibbles(ReadOnlySpan<byte> path)
{
if (path.Length % 2 != 0) throw new FormatException($"MPTTrie.FromNibbles invalid path.");
var key = new byte[path.Length / 2];
for (int i = 0; i < key.Length; i++)
for (var i = 0; i < key.Length; i++)
{
key[i] = (byte)(path[i * 2] << 4);
key[i] |= path[i * 2 + 1];
Expand All @@ -56,7 +56,7 @@ private static byte[] FromNibbles(ReadOnlySpan<byte> path)

public void Commit()
{
cache.Commit();
_cache.Commit();
}
}
}
26 changes: 13 additions & 13 deletions src/Plugins/StateService/Network/StateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Cryptography.ECC;
using Neo.Extensions;
using Neo.IO;
using Neo.Json;
Expand Down Expand Up @@ -49,7 +48,7 @@ Witness[] IVerifiable.Witnesses
{
get
{
return new[] { Witness };
return [Witness];
}
set
{
Expand All @@ -70,12 +69,12 @@ Witness[] IVerifiable.Witnesses
void ISerializable.Deserialize(ref MemoryReader reader)
{
DeserializeUnsigned(ref reader);
Witness[] witnesses = reader.ReadSerializableArray<Witness>(1);
var witnesses = reader.ReadSerializableArray<Witness>(1);
Witness = witnesses.Length switch
{
0 => null,
1 => witnesses[0],
_ => throw new FormatException(),
_ => throw new FormatException($"Expected 1 witness, got {witnesses.Length}."),
};
}

Expand All @@ -92,7 +91,7 @@ void ISerializable.Serialize(BinaryWriter writer)
if (Witness is null)
writer.WriteVarInt(0);
else
writer.Write(new[] { Witness });
writer.Write([Witness]);
}

public void SerializeUnsigned(BinaryWriter writer)
Expand All @@ -109,19 +108,20 @@ public bool Verify(ProtocolSettings settings, DataCache snapshot)

public UInt160[] GetScriptHashesForVerifying(DataCache snapshot)
{
ECPoint[] validators = NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.StateValidator, Index);
var validators = NativeContract.RoleManagement.GetDesignatedByRole(snapshot, Role.StateValidator, Index);
if (validators.Length < 1) throw new InvalidOperationException("No script hash for state root verifying");
return new UInt160[] { Contract.GetBFTAddress(validators) };
return [Contract.GetBFTAddress(validators)];
}

public JObject ToJson()
{
var json = new JObject();
json["version"] = Version;
json["index"] = Index;
json["roothash"] = RootHash.ToString();
json["witnesses"] = Witness is null ? new JArray() : new JArray(Witness.ToJson());
return json;
return new()
{
["version"] = Version,
["index"] = Index,
["roothash"] = RootHash.ToString(),
["witnesses"] = Witness is null ? new JArray() : new JArray(Witness.ToJson()),
};
}
}
}
Loading