Пример #1
0
 /// <summary>
 /// Recursively calculate every nodes.
 /// </summary>
 /// <param name="proofList"></param>
 private void CalculateMerkle(List <MerkleNode> proofList)
 {
     if (proofList.Count == 1)
     {
         MerkleRoot = proofList[0].ProofHashValue;
         return;
     }
     else
     {
         List <MerkleNode> newProofList = new List <MerkleNode>();
         for (int i = 0; i < proofList.Count; i += 2)
         {
             MerkleNode newProof;
             if (i + 1 >= proofList.Count)
             {
                 newProof = NewProofBySelf(proofList[i]);
             }
             else
             {
                 newProof = CombineTwoProofs(proofList[i], proofList[i + 1]);
             }
             newProofList.Add(newProof);
             MerkleProofDict.Add(newProof.Code, newProof);
         }
         CalculateMerkle(newProofList);
     }
 }
Пример #2
0
 /// <summary>
 /// Modify node.
 /// </summary>
 /// <param name="code"></param>
 /// <param name="newValue"></param>
 public void ModifyChild(int code, string newValue)
 {
     if (MerkleProofDict.ContainsKey(code))
     {
         ModifyChild(MerkleProofDict[code], newValue);
     }
 }
Пример #3
0
        /// <summary>
        /// Add a node.
        /// </summary>
        /// <param name="newChildValue"></param>
        public void AddChildValue(string newChildValue)
        {
            MerkleNode proof = new MerkleNode()
            {
                ProofHashValue = newChildValue.GetMerkleHash(),
                Code           = currentCode++,
                ChildrenList   = new List <int>()
            };

            MerkleProofDict.Add(proof.Code, proof);
            AddMerkleProof(lastChildProof, proof);
            lastChildProof = proof;
        }
Пример #4
0
        /// <summary>
        /// Add Merkle Proof.
        /// </summary>
        /// <param name="previProof"></param>
        /// <param name="proof"></param>
        public void AddMerkleProof(MerkleNode previProof, MerkleNode proof)
        {
            if (previProof.BrotherNode > 0)
            {
                MerkleNode newProof = NewProofBySelf(proof);
                MerkleProofDict.Add(newProof.Code, newProof);
                AddMerkleProof(MerkleProofDict[previProof.ParentProofCode], newProof);
            }
            else
            {
                if (previProof.ParentProofCode == 0)
                {
                    MerkleNode newProof = CombineTwoProofs(previProof, proof);
                    MerkleProofDict.Add(newProof.Code, newProof);
                    MerkleRoot = newProof.ProofHashValue;
                    return;
                }
                else
                {
                    previProof.AddBrotherNode(new Node()
                    {
                        Side = 1, HashValue = proof.ProofHashValue
                    }, MerkleProofDict);
                    proof.AddBrotherNode(new Node()
                    {
                        Side = 2, HashValue = previProof.ProofHashValue
                    }, MerkleProofDict);
                    proof.ParentProofCode = previProof.ParentProofCode;
                    MerkleProofDict[previProof.ParentProofCode].ChildrenList.Add(proof.Code);

                    string newStr = previProof.ProofHashValue + proof.ProofHashValue;
                    newStr = newStr.GetMerkleHash();
                    ModifyChild(MerkleProofDict[previProof.ParentProofCode], newStr);
                }
            }
        }