Ejemplo n.º 1
0
        public void SetRightNode(MerkleNode node)
        {
            MerkleTree.Contract(() => node.Hash != null, "Node hash must be initialized.");
            RightNode        = node;
            RightNode.Parent = this;

            // Can't compute hash if the left node isn't set yet.
            if (LeftNode != null)
            {
                ComputeHash();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verifies the hash for this node against the computed hash for our child nodes.
        /// If we don't have any children, the return is always true because we have nothing to verify against.
        /// </summary>
        public bool VerifyHash()
        {
            if (LeftNode == null && RightNode == null)
            {
                return(true);
            }

            if (RightNode == null)
            {
                return(Hash.Equals(LeftNode.Hash));
            }

            MerkleTree.Contract(() => LeftNode != null, "Left branch must be a node if right branch is a node.");
            MerkleHash leftRightHash = MerkleHash.Create(LeftNode.Hash, RightNode.Hash);

            return(Hash.Equals(leftRightHash));
        }
Ejemplo n.º 3
0
        public byte[] AddLeaf(string tran)
        {
            int numberOfTree = NumOfLeafs / 4;

            byte[] hash = null;
            NumOfLeafs++;
            counter++;
            if (Trees[numberOfTree] == null)
            {
                Trees[numberOfTree] = new MerkleTree();
            }
            Trees[numberOfTree].AppendLeaf(new MerkleNode(MerkleHash.Create(tran)));
            if (counter == 4)
            {
                hash    = Trees[numberOfTree].BuildTree().Value;
                counter = 0;
            }
            return(hash);
        }
Ejemplo n.º 4
0
 public void SetHash(byte[] hash)
 {
     MerkleTree.Contract(() => hash.Length == Constants.HASH_LENGTH, "Unexpected hash length.");
     Value = hash;
 }
Ejemplo n.º 5
0
 public override bool Equals(object obj)
 {
     MerkleTree.Contract(() => obj is MerkleHash, "rvalue is not a MerkleHash");
     return(Equals((MerkleHash)obj));
 }