예제 #1
0
        // function to check that sure the blockchain is valid
        // It checks that the hash chain is consistent throughout
        // the blockchain.
        private void validate_Click(object sender, EventArgs e)
        {
            if (blockchain.Blocks.Count == 1)
            {
                if (blockchain.validateMerkleRoot(blockchain.Blocks[0]))
                {
                    richTextBox1.Text = "Valid Blockchain";
                }
                else
                {
                    richTextBox1.Text = "Invalid Blockchain: Merkle Root";
                }

                return;
            }
            else
            {
                for (int i = 1; i < blockchain.Blocks.Count - 1; i++)
                {
                    if (blockchain.Blocks[i].prevHash != blockchain.Blocks[i - 1].hash || // hash line check
                        !blockchain.validateMerkleRoot(blockchain.Blocks[i]) ||           // merkle root check
                        !blockchain.validateHash(blockchain.Blocks[i]))                   //hash check
                    {
                        richTextBox1.Text = "Invalid Blockchain";
                        return;
                    }
                }
            }
            richTextBox1.Text = "Valid Blockchain";
        }