Skip to content

Commit

Permalink
Name conventions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodoufu committed Nov 11, 2018
1 parent 90ac9a0 commit 0017b5d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 59 deletions.
103 changes: 54 additions & 49 deletions NeoDataStructure/MerklePatricia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ namespace com.github.neoresearch.NeoDataStructure
/// </summary>
public class MerklePatricia
{
private const int LEAF_SIZE = 3;
private const int NODE_SIZE = 18;
private readonly Dictionary<string, string[]> db = new Dictionary<string, string[]>();
private string RootHash;
private const int LeafSize = 3;
private const int NodeSize = 18;
private readonly Dictionary<string, string[]> _db = new Dictionary<string, string[]>();
private string _rootHash;

/// <summary>
/// Get and set the key and valeu pairs of the tree.
/// </summary>
/// <param name="key">The key that indicates the reference.</param>
public string this[string key]
{
get => RootHash.IsEmpty() ? null : Get(db[RootHash], key.CompactEncodeString());
get => _rootHash.IsEmpty() ? null : Get(_db[_rootHash], key.CompactEncodeString());
set
{
var node = RootHash.IsEmpty() ? null : db[RootHash];
if (!RootHash.IsEmpty() && db.ContainsKey(RootHash))
var node = _rootHash.IsEmpty() ? null : _db[_rootHash];
if (!_rootHash.IsEmpty() && _db.ContainsKey(_rootHash))
{
db.Remove(RootHash);
_db.Remove(_rootHash);
}

RootHash = Set(node, key.CompactEncodeString(), key, value);
_rootHash = Set(node, key.CompactEncodeString(), key, value);
}
}

Expand All @@ -49,7 +49,7 @@ private string Get(string[] node, string path)
return null;
}

if (node.Length == LEAF_SIZE)
if (node.Length == LeafSize)
{
return node[0].Substring(1) == path ? node[node.Length - 1] : null;
}
Expand All @@ -61,7 +61,7 @@ private string Get(string[] node, string path)

int kint = path.Substring(0, 1).FromHex();
if (node[kint] == null) return null;
node = db[node[kint]];
node = _db[node[kint]];
path = path.Substring(1);
}

Expand All @@ -74,13 +74,13 @@ private string Set(string[] node, string path, string key, string value)
{
node = new[] {2 + path.Length % 2 + path, key, value};
}
else if (node.Length == LEAF_SIZE)
else if (node.Length == LeafSize)
{
var innerHash = Set(new string[NODE_SIZE], node[0].Substring(1), node[1], node[2]);
node = db[innerHash];
db.Remove(innerHash);
var innerHash = Set(new string[NodeSize], node[0].Substring(1), node[1], node[2]);
node = _db[innerHash];
_db.Remove(innerHash);
innerHash = Set(node, path, key, value);
node = db[innerHash];
node = _db[innerHash];
}
else
{
Expand All @@ -93,18 +93,18 @@ private string Set(string[] node, string path, string key, string value)
{
int kint = path.Substring(0, 1).FromHex();
var innerHash = node[kint];
var innerNode = innerHash != null ? db[innerHash] : null;
if (innerHash != null && db.ContainsKey(innerHash))
var innerNode = innerHash != null ? _db[innerHash] : null;
if (innerHash != null && _db.ContainsKey(innerHash))
{
db.Remove(innerHash);
_db.Remove(innerHash);
}

node[kint] = Set(innerNode, path.Substring(1), key, value);
}
}

var tempHash = node.Hash();
db[tempHash] = node;
_db[tempHash] = node;
return tempHash;
}

Expand All @@ -115,28 +115,28 @@ private string Set(string[] node, string path, string key, string value)
/// <returns>true is the key was present and sucessifully removed.</returns>
public bool Remove(string key)
{
if (RootHash.IsEmpty())
if (_rootHash.IsEmpty())
{
return false;
}

var removido = Remove(RootHash, key.CompactEncodeString());
db.Remove(RootHash);
var resp = removido != RootHash;
RootHash = removido;
var removido = Remove(_rootHash, key.CompactEncodeString());
_db.Remove(_rootHash);
var resp = removido != _rootHash;
_rootHash = removido;

return resp;
}

private string Remove(string nodeHash, string path)
{
var node = db[nodeHash];
var node = _db[nodeHash];
switch (node.Length)
{
case LEAF_SIZE when node[0].Substring(1) == path:
db.Remove(nodeHash);
case LeafSize when node[0].Substring(1) == path:
_db.Remove(nodeHash);
return null;
case LEAF_SIZE:
case LeafSize:
return nodeHash;
}

Expand All @@ -149,7 +149,7 @@ private string Remove(string nodeHash, string path)
var innerNodeHash = Remove(node[kint], path.Substring(1));
if (node[kint] != innerNodeHash)
{
db.Remove(node[kint]);
_db.Remove(node[kint]);
node[kint] = innerNodeHash;
if (node[node.Length - 2] == null && node.Count(x => x != null) == 1)
{
Expand All @@ -163,7 +163,7 @@ private string Remove(string nodeHash, string path)
}

var removedHash = node.Hash();
db[removedHash] = node;
_db[removedHash] = node;
return removedHash;
}

Expand All @@ -175,17 +175,17 @@ private string Remove(string nodeHash, string path)
/// </summary>
/// <returns>The height.</returns>
public int Height() =>
RootHash.IsEmpty() ? 0 : (db[RootHash].Length == LEAF_SIZE ? 1 : 1 + db[RootHash].Select(Height).Max());
_rootHash.IsEmpty() ? 0 : (_db[_rootHash].Length == LeafSize ? 1 : 1 + _db[_rootHash].Select(Height).Max());

private int Height(string nodeHash) =>
nodeHash.IsEmpty() ? 0 : (db[nodeHash].Length == LEAF_SIZE ? 1 : 1 + db[nodeHash].Select(Height).Max());
nodeHash.IsEmpty() ? 0 : (_db[nodeHash].Length == LeafSize ? 1 : 1 + _db[nodeHash].Select(Height).Max());


/// <summary>
/// Checks if the hashes correspond to their nodes.
/// </summary>
/// <returns>In the case the validation is Ok.</returns>
public bool Validade() => RootHash.IsEmpty() || Validade(RootHash, db[RootHash]);
public bool Validade() => _rootHash.IsEmpty() || Validade(_rootHash, _db[_rootHash]);

private bool Validade(string nodeHash, string[] node)
{
Expand All @@ -194,15 +194,15 @@ private bool Validade(string nodeHash, string[] node)
return false;
}

if (node.Length == LEAF_SIZE)
if (node.Length == LeafSize)
{
return true;
}

for (int i = 0; i < node.Length - LEAF_SIZE + 1; i++)
for (int i = 0; i < node.Length - LeafSize + 1; i++)
{
var subNodeHash = node[i];
if (!subNodeHash.IsEmpty() && !Validade(subNodeHash, db[subNodeHash]))
if (!subNodeHash.IsEmpty() && !Validade(subNodeHash, _db[subNodeHash]))
{
return false;
}
Expand All @@ -224,15 +224,15 @@ public override bool Equals(object obj)
}

var objMp = (MerklePatricia) obj;
var nodeHashA = RootHash;
var nodeHashB = objMp.RootHash;
return !nodeHashA.IsEmpty() && nodeHashA == nodeHashB && nodeEquals(objMp, nodeHashA);
var nodeHashA = _rootHash;
var nodeHashB = objMp._rootHash;
return !nodeHashA.IsEmpty() && nodeHashA == nodeHashB && NodeEquals(objMp, nodeHashA);
}

private bool nodeEquals(MerklePatricia mpB, string nodeHash)
private bool NodeEquals(MerklePatricia mpB, string nodeHash)
{
var nodeA = db[nodeHash];
var nodeB = mpB.db[nodeHash];
var nodeA = _db[nodeHash];
var nodeB = mpB._db[nodeHash];
if (nodeA.Length != nodeB.Length)
{
return false;
Expand All @@ -249,7 +249,7 @@ private bool nodeEquals(MerklePatricia mpB, string nodeHash)
return false;
}

if (nodeA.Length == LEAF_SIZE)
if (nodeA.Length == LeafSize)
{
return nodeA[0].IsEquals(nodeB[0], true) && nodeA[2].IsEquals(nodeB[2], true);
}
Expand All @@ -261,7 +261,7 @@ private bool nodeEquals(MerklePatricia mpB, string nodeHash)
return false;
}

if (!nodeA[i].IsEmpty() && !nodeEquals(mpB, nodeA[i]))
if (!nodeA[i].IsEmpty() && !NodeEquals(mpB, nodeA[i]))
{
return false;
}
Expand All @@ -285,23 +285,28 @@ private bool nodeEquals(MerklePatricia mpB, string nodeHash)
return !(b1 == b2);
}

public override int GetHashCode()
{
return _rootHash != null ? _rootHash.GetHashCode() : 0;
}

/// <summary>
/// Generates a JSON for the tree.
/// </summary>
/// <returns>The generated JSON tree.</returns>
public override string ToString()
{
return RootHash.IsEmpty() ? "{}" : $"{nodeToString(db[RootHash])}";
return _rootHash.IsEmpty() ? "{}" : $"{NodeToString(_db[_rootHash])}";
}

private string nodeToString(string[] node)
private string NodeToString(string[] node)
{
if (node == null)
{
return "{}";
}

if (node.Length == LEAF_SIZE)
if (node.Length == LeafSize)
{
return $"[\"{node[0]}\",\"{node[1]}\",\"{node[2]}\"]";
}
Expand All @@ -312,7 +317,7 @@ private string nodeToString(string[] node)
{
var nit = node[i];
if (nit == null) continue;
resp.Append(virgula ? "," : "").Append($"\"{i}\":{nodeToString(db[nit])}");
resp.Append(virgula ? "," : "").Append($"\"{i}\":{NodeToString(_db[nit])}");
virgula = true;
}

Expand Down
6 changes: 2 additions & 4 deletions NeoDataStructureTest/MerklePatriciaTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Collections.Generic;

namespace com.github.neoresearch.NeoDataStructureTest
{
using Xunit;
using System.Collections.Generic;
using NeoDataStructure;
using Xunit;

public class MerklePatriciaTest
{
Expand Down Expand Up @@ -195,7 +194,6 @@ public void PatriciaToString()
mp["ca123"] = "3bala12";
mp["oi123"] = "asfbala12";
System.Console.WriteLine($"a:\n {mp}");

}
}
}
6 changes: 0 additions & 6 deletions NeoDataStructureTest/MerklePatriciaToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,5 @@ public void Sha256()
Assert.Equal("87F633634CC4B02F628685651F0A29B7BFA22A0BD841F725C6772DD00A58D489", "oi".Sha256(true));
Assert.Equal("4FDACBCA234070237E8561232C1BEE38250D1B8446B7AD787A61737B4406009D", "batatinha".Sha256(true));
}

[Fact]
public void CompactEncodeString()
{
Assert.Equal("6f69", "oi".CompactEncodeString());
}
}
}
1 change: 1 addition & 0 deletions NeoDataStructureTest/NeoDataStructureTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>com.github.neoresearch.NeoDataStructureTest</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
Expand Down

0 comments on commit 0017b5d

Please sign in to comment.