Exemplo n.º 1
0
        public MerkleProofElement BuildFromBinaryTreeInternal(BinaryTreeElement currentElement, MerkleHashCalculator calculator)
        {
            if (currentElement is EmptyLeaf)
            {
                return(new ProofHashedLeaf(new byte[32]));
            }
            else if (currentElement is Leaf)
            {
                var leafElement = (Leaf)currentElement;
                var pathElem    = currentElement.PathElem;
                if (!(pathElem is null))
                {
                    if (pathElem is PathLeafElement)
                    {
                        return(new ProofValueLeaf(leafElement.Content, pathElem.Previous));
                    }
                    else
                    {
                        throw new System.Exception("The path and structure don't match. We are at a leaf, but path elem is not a leaf: " + pathElem);
                    }
                }
                else
                {
                    var hash = calculator.CalculateLeafHash(leafElement.Content);

                    return(new ProofHashedLeaf(hash));
                }
            }
Exemplo n.º 2
0
 private ArrayHeadNode <object[]> BuildFromOneLeaf(object[] array, BinaryTreeElement orgRoot, PathElement pathElem)
 {
     if (array.Length > 1)
     {
         throw new System.Exception("How come we got a leaf returned when we had " + array.Length + " elements is the args?");
     }
     else
     {
         return(new ArrayHeadNode <object[]>(orgRoot, new EmptyLeaf(), array, array.Length, pathElem));
     }
 }
Exemplo n.º 3
0
        private List <BinaryTreeElement> BuildHigherLayer(int layer, List <BinaryTreeElement> inList)
        {
            if (inList.Count == 0)
            {
                throw new System.Exception("Cannot work on empty arrays. Layer: " + layer);
            }
            else if (inList.Count == 1)
            {
                return(inList);
            }

            var returnArray             = new List <BinaryTreeElement>();
            var nrOfNodesToCreate       = inList.Count / 2;
            BinaryTreeElement leftValue = null;
            var isLeft = true;

            foreach (var element in inList)
            {
                if (isLeft)
                {
                    leftValue = element;
                    isLeft    = false;
                }
                else
                {
                    var tempNode = new Node(leftValue, element);
                    returnArray.Add(tempNode);
                    nrOfNodesToCreate--;
                    isLeft    = true;
                    leftValue = null;
                }
            }

            if (!isLeft)
            {
                returnArray.Add(leftValue);
            }

            if (nrOfNodesToCreate != 0)
            {
                System.Console.WriteLine("Why didn't we build exactly the correct amount? Layer: " + layer + " , residue: " + nrOfNodesToCreate + " , input args size: " + inList.Count + ".");
            }

            return(this.BuildHigherLayer(layer + 1, returnArray));
        }