Beispiel #1
0
        public bool isChainValid()
        {
            for (int i = 1; i < this.chain.Count; i++)
            {
                Block currentBlock  = this.chain[i];
                Block previousBlock = this.chain[i - 1];

                //Compare the two hashes for verification
                if (currentBlock.hash.Length == currentBlock.calculateHash().Length)
                {
                    for (int ii = 0; ii < currentBlock.hash.Length; ii++)
                    {
                        //return false if a value doesn't match
                        if (currentBlock.hash[ii] != currentBlock.calculateHash()[ii])
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    return(false);
                }

                //Compare the two hashes for verification
                if (currentBlock.previousHash.Length == previousBlock.hash.Length)
                {
                    for (int ii = 0; ii < currentBlock.previousHash.Length; ii++)
                    {
                        //return false if a value doesn't match
                        if (currentBlock.previousHash[ii] != previousBlock.hash[ii])
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #2
0
 public void addBlock(Block newBlock)
 {
     newBlock.previousHash = this.getLatestBlock().hash;
     newBlock.hash         = newBlock.calculateHash();
     this.chain.Add(newBlock);
 }