// 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";
        }
Example #2
0
        //Checks validity of blockchain using merkle root and checking hashes align
        private void validateChain_Click(object sender, EventArgs e)
        {
            if (blockchain.Blocks.Count == 1)
            {
                if (!blockchain.validateMerkleRoot(blockchain.Blocks[0]))
                {
                    richTextBox1.Text = "Blockchain is invalid";
                }
                else
                {
                    richTextBox1.Text = "Blockchain is valid";
                }
                return;
            }
            bool valid = true;

            for (int i = 1; i < blockchain.Blocks.Count - 1; i++)
            {
                if (blockchain.Blocks[i].prevHash != blockchain.Blocks[i - 1].hash || !blockchain.validateMerkleRoot(blockchain.Blocks[i]))
                {
                    richTextBox1.Text = "Blockchain is invalid";
                    return;
                }
            }
            if (valid)
            {
                richTextBox1.Text = "Blockchain is valid";
            }
            else
            {
                richTextBox1.Text = "Blockchain is invalid";
            }
        }
        private void validateChain_Click(object sender, EventArgs e)
        {
            // Contiguity checks
            if (blockchain.Blocks.Count == 1)
            {
                if (!blockchain.validateMerkleRoot(blockchain.Blocks[0]))
                {
                    richTextBox1.Text = "Blockchain is invalid.";
                }
                else
                {
                    richTextBox1.Text = "Blockchain is valid.";
                }

                return;
            }

            for (int i = 1; i < blockchain.Blocks.Count - 1; i++)
            {
                // Compare hash to previous hash for all blocks in the chain and check cransactions via merkle root.
                if (blockchain.Blocks[i].prevHash != blockchain.Blocks[i - 1].hash || !blockchain.validateMerkleRoot(blockchain.Blocks[i]))
                {
                    richTextBox1.Text = "Blockchain is invalid.";
                    return;
                }
            }

            richTextBox1.Text = "Blockchain is valid.";
        }