Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move MPT into StateService from core #410

Merged
merged 2 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion neo-modules.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28729.10
MinimumVisualStudioVersion = 10.0.40219.1
Expand All @@ -24,6 +24,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Network.RPC.Tests", "te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Plugins.Storage.Tests", "tests\Neo.Plugins.Storage.Tests\Neo.Plugins.Storage.Tests.csproj", "{9E7EA895-302A-4C0C-BA9B-54F9A67AD75C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateService", "src\StateService\StateService.csproj", "{A0F4A66F-6F87-4B99-B8BE-A779BC002F47}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Plugins.StateService.Tests", "tests\Neo.Plugins.StateService.Tests\Neo.Plugins.StateService.Tests.csproj", "{149822EC-4E0C-425F-A032-4196B615BFEB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -66,6 +70,14 @@ Global
{9E7EA895-302A-4C0C-BA9B-54F9A67AD75C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E7EA895-302A-4C0C-BA9B-54F9A67AD75C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9E7EA895-302A-4C0C-BA9B-54F9A67AD75C}.Release|Any CPU.Build.0 = Release|Any CPU
{A0F4A66F-6F87-4B99-B8BE-A779BC002F47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0F4A66F-6F87-4B99-B8BE-A779BC002F47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0F4A66F-6F87-4B99-B8BE-A779BC002F47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0F4A66F-6F87-4B99-B8BE-A779BC002F47}.Release|Any CPU.Build.0 = Release|Any CPU
{149822EC-4E0C-425F-A032-4196B615BFEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{149822EC-4E0C-425F-A032-4196B615BFEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{149822EC-4E0C-425F-A032-4196B615BFEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{149822EC-4E0C-425F-A032-4196B615BFEB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +92,8 @@ Global
{1403FFE9-4265-4269-8E3D-5A79EFD108CA} = {97E81C78-1637-481F-9485-DA1225E94C23}
{D52460B3-AB5C-4D07-B400-9E7ADCB01FF5} = {59D802AB-C552-422A-B9C3-64D329FBCDCC}
{9E7EA895-302A-4C0C-BA9B-54F9A67AD75C} = {59D802AB-C552-422A-B9C3-64D329FBCDCC}
{A0F4A66F-6F87-4B99-B8BE-A779BC002F47} = {97E81C78-1637-481F-9485-DA1225E94C23}
{149822EC-4E0C-425F-A032-4196B615BFEB} = {59D802AB-C552-422A-B9C3-64D329FBCDCC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {61D3ADE6-BBFC-402D-AB42-1C71C9F9EDE3}
Expand Down
35 changes: 35 additions & 0 deletions src/StateService/MPT/BranchNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.IO;

namespace Neo.Plugins.MPT
{
public class BranchNode : MPTNode
{
public const int ChildCount = 17;
public readonly MPTNode[] Children = new MPTNode[ChildCount];

protected override NodeType Type => NodeType.BranchNode;

public BranchNode()
{
for (int i = 0; i < ChildCount; i++)
{
Children[i] = HashNode.EmptyNode;
}
}

internal override void EncodeSpecific(BinaryWriter writer)
{
for (int i = 0; i < ChildCount; i++)
WriteHash(writer, Children[i].Hash);
}

internal override void DecodeSpecific(BinaryReader reader)
{
for (int i = 0; i < ChildCount; i++)
{
Children[i] = new HashNode();
Children[i].DecodeSpecific(reader);
}
}
}
}
30 changes: 30 additions & 0 deletions src/StateService/MPT/ExtensionNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Neo.IO;
using Neo.SmartContract;
using System.IO;

namespace Neo.Plugins.MPT
{
public class ExtensionNode : MPTNode
{
//max lenght when store StorageKey
public const int MaxKeyLength = (ApplicationEngine.MaxStorageValueSize + sizeof(int)) * 2;

public byte[] Key;
public MPTNode Next;

protected override NodeType Type => NodeType.ExtensionNode;

internal override void EncodeSpecific(BinaryWriter writer)
{
writer.WriteVarBytes(Key);
WriteHash(writer, Next.Hash);
}

internal override void DecodeSpecific(BinaryReader reader)
{
Key = reader.ReadVarBytes(MaxKeyLength);
Next = new HashNode();
Next.DecodeSpecific(reader);
}
}
}
41 changes: 41 additions & 0 deletions src/StateService/MPT/HashNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Neo.IO;
using System;
using System.IO;

namespace Neo.Plugins.MPT
{
public class HashNode : MPTNode
{
private UInt256 hash;

public override UInt256 Hash => hash;
protected override NodeType Type => NodeType.HashNode;
public bool IsEmpty => Hash is null;
public static HashNode EmptyNode { get; } = new HashNode();

public HashNode()
{
}

public HashNode(UInt256 hash)
{
this.hash = hash;
}

internal override void EncodeSpecific(BinaryWriter writer)
{
WriteHash(writer, hash);
}

internal override void DecodeSpecific(BinaryReader reader)
{
byte[] buffer = reader.ReadVarBytes(UInt256.Length);
hash = buffer.Length switch
{
0 => null,
UInt256.Length => new UInt256(buffer),
_ => throw new FormatException()
};
}
}
}
36 changes: 36 additions & 0 deletions src/StateService/MPT/LeafNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Neo.IO;
using Neo.SmartContract;
using System;
using System.IO;

namespace Neo.Plugins.MPT
{
public class LeafNode : MPTNode
{
//the max size when store StorageItem
public const int MaxValueLength = 3 + ApplicationEngine.MaxStorageValueSize + sizeof(bool);

public byte[] Value;

protected override NodeType Type => NodeType.LeafNode;

public LeafNode()
{
}

public LeafNode(ReadOnlySpan<byte> value)
{
Value = value.ToArray();
}

internal override void EncodeSpecific(BinaryWriter writer)
{
writer.WriteVarBytes(Value);
}

internal override void DecodeSpecific(BinaryReader reader)
{
Value = reader.ReadVarBytes(MaxValueLength);
}
}
}
62 changes: 62 additions & 0 deletions src/StateService/MPT/MPTNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Neo.Cryptography;
using Neo.IO;
using Neo.IO.Caching;
using System;
using System.IO;

namespace Neo.Plugins.MPT
{
public abstract class MPTNode
{
private UInt256 hash;

public virtual UInt256 Hash => hash ??= new UInt256(Crypto.Hash256(Encode()));
protected abstract NodeType Type { get; }

public void SetDirty()
{
hash = null;
}

public byte[] Encode()
{
using MemoryStream ms = new MemoryStream();
using BinaryWriter writer = new BinaryWriter(ms);

writer.Write((byte)Type);
EncodeSpecific(writer);
writer.Flush();

return ms.ToArray();
}

internal abstract void EncodeSpecific(BinaryWriter writer);

public static unsafe MPTNode Decode(ReadOnlySpan<byte> data)
{
if (data.IsEmpty) return null;

fixed (byte* pointer = data)
{
using UnmanagedMemoryStream stream = new UnmanagedMemoryStream(pointer, data.Length);
using BinaryReader reader = new BinaryReader(stream);

MPTNode n = (MPTNode)ReflectionCache<NodeType>.CreateInstance((NodeType)reader.ReadByte());
if (n is null) throw new InvalidOperationException();

n.DecodeSpecific(reader);
return n;
}
}

internal abstract void DecodeSpecific(BinaryReader reader);

protected void WriteHash(BinaryWriter writer, UInt256 hash)
{
if (hash is null)
writer.Write((byte)0);
else
writer.WriteVarBytes(hash.ToArray());
}
}
}
16 changes: 16 additions & 0 deletions src/StateService/MPT/MPTNodeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Neo.IO.Caching;

namespace Neo.Plugins.MPT
{
public enum NodeType : byte
{
[ReflectionCache(typeof(BranchNode))]
BranchNode = 0x00,
[ReflectionCache(typeof(ExtensionNode))]
ExtensionNode = 0x01,
[ReflectionCache(typeof(HashNode))]
HashNode = 0x02,
[ReflectionCache(typeof(LeafNode))]
LeafNode = 0x03,
}
}
120 changes: 120 additions & 0 deletions src/StateService/MPT/MPTTrie.Delete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using Neo.IO;
using System;
using System.Collections.Generic;
using static Neo.Helper;

namespace Neo.Plugins.MPT
{
partial class MPTTrie<TKey, TValue>
{
public bool Delete(TKey key)
{
var path = ToNibbles(key.ToArray());
if (path.Length == 0) return false;
return TryDelete(ref root, path);
}

private bool TryDelete(ref MPTNode node, ReadOnlySpan<byte> path)
{
switch (node)
{
case LeafNode _:
{
if (path.IsEmpty)
{
node = HashNode.EmptyNode;
return true;
}
return false;
}
case ExtensionNode extensionNode:
{
if (path.StartsWith(extensionNode.Key))
{
var result = TryDelete(ref extensionNode.Next, path[extensionNode.Key.Length..]);
if (!result) return false;
if (extensionNode.Next is HashNode hashNode && hashNode.IsEmpty)
{
node = extensionNode.Next;
return true;
}
if (extensionNode.Next is ExtensionNode sn)
{
extensionNode.Key = Concat(extensionNode.Key, sn.Key);
extensionNode.Next = sn.Next;
}
extensionNode.SetDirty();
PutToStore(extensionNode);
return true;
}
return false;
}
case BranchNode branchNode:
{
bool result;
if (path.IsEmpty)
{
result = TryDelete(ref branchNode.Children[BranchNode.ChildCount - 1], path);
}
else
{
result = TryDelete(ref branchNode.Children[path[0]], path[1..]);
}
if (!result) return false;
List<byte> childrenIndexes = new List<byte>(BranchNode.ChildCount);
for (int i = 0; i < BranchNode.ChildCount; i++)
{
if (branchNode.Children[i] is HashNode hn && hn.IsEmpty) continue;
childrenIndexes.Add((byte)i);
}
if (childrenIndexes.Count > 1)
{
branchNode.SetDirty();
PutToStore(branchNode);
return true;
}
var lastChildIndex = childrenIndexes[0];
var lastChild = branchNode.Children[lastChildIndex];
if (lastChildIndex == BranchNode.ChildCount - 1)
{
node = lastChild;
return true;
}
if (lastChild is HashNode hashNode)
{
lastChild = Resolve(hashNode);
if (lastChild is null) return false;
}
if (lastChild is ExtensionNode exNode)
{
exNode.Key = Concat(childrenIndexes.ToArray(), exNode.Key);
exNode.SetDirty();
PutToStore(exNode);
node = exNode;
return true;
}
node = new ExtensionNode()
{
Key = childrenIndexes.ToArray(),
Next = lastChild,
};
PutToStore(node);
return true;
}
case HashNode hashNode:
{
if (hashNode.IsEmpty)
{
return true;
}
var newNode = Resolve(hashNode);
if (newNode is null) return false;
node = newNode;
return TryDelete(ref node, path);
}
default:
return false;
}
}
}
}
Loading