Skip to content

Commit

Permalink
Optimize MerkleTree (3x) (neo-project#1203)
Browse files Browse the repository at this point in the history
* Optimize MerkleTree

* Update MerkleTree.cs

* Update MerkleTree.cs
  • Loading branch information
shargon authored and Tommo-L committed Jun 22, 2020
1 parent e2fb181 commit e43bc73
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions neo/Cryptography/MerkleTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Neo.Cryptography
{
public class MerkleTree
{
private MerkleTreeNode root;
private readonly MerkleTreeNode root;

public int Depth { get; private set; }

Expand All @@ -25,6 +26,8 @@ private static MerkleTreeNode Build(MerkleTreeNode[] leaves)
{
if (leaves.Length == 0) throw new ArgumentException();
if (leaves.Length == 1) return leaves[0];

var buffer = new byte[64];
MerkleTreeNode[] parents = new MerkleTreeNode[(leaves.Length + 1) / 2];
for (int i = 0; i < parents.Length; i++)
{
Expand All @@ -40,11 +43,20 @@ private static MerkleTreeNode Build(MerkleTreeNode[] leaves)
parents[i].RightChild = leaves[i * 2 + 1];
leaves[i * 2 + 1].Parent = parents[i];
}
parents[i].Hash = new UInt256(Crypto.Default.Hash256(parents[i].LeftChild.Hash.ToArray().Concat(parents[i].RightChild.Hash.ToArray()).ToArray()));
parents[i].Hash = Concat(buffer, parents[i].LeftChild.Hash, parents[i].RightChild.Hash);
}
return Build(parents); //TailCall
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static UInt256 Concat(byte[] buffer, UInt256 hash1, UInt256 hash2)
{
Buffer.BlockCopy(hash1.ToArray(), 0, buffer, 0, 32);
Buffer.BlockCopy(hash2.ToArray(), 0, buffer, 32, 32);

return new UInt256(Crypto.Default.Hash256(buffer));
}

public static UInt256 ComputeRoot(IReadOnlyList<UInt256> hashes)
{
if (hashes.Count == 0) throw new ArgumentException();
Expand Down

0 comments on commit e43bc73

Please sign in to comment.