/* BLOCKCHAIN VALIDATION */
        // Validate the integrity of the state of the Blockchain
        private void Validate_Click(object sender, EventArgs e)
        {
            // CASE: Genesis Block - Check only hash as no transactions are currently present
            if (blockchain.blocks.Count == 1)
            {
                if (!Blockchain.ValidateHash(blockchain.blocks[0])) // Recompute Hash to check validity
                {
                    UpdateText("Blockchain is invalid");
                }
                else
                {
                    UpdateText("Blockchain is valid");
                }
                return;
            }

            for (int i = 1; i < blockchain.blocks.Count - 1; i++)
            {
                if (
                    blockchain.blocks[i].prevHash != blockchain.blocks[i - 1].hash || // Check hash "chain"
                    !Blockchain.ValidateHash(blockchain.blocks[i]) ||                 // Check each blocks hash
                    !Blockchain.ValidateMerkleRoot(blockchain.blocks[i])              // Check transaction integrity using Merkle Root
                    )
                {
                    UpdateText("Blockchain is invalid");
                    return;
                }
            }
            UpdateText("Blockchain is valid");
        }
Esempio n. 2
0
        private void Validate_Click(object sender, EventArgs e)
        {
            // CASE: Genesis Block - Check only the hash since there are currently no transactions.
            if (blockchain.Blocks.Count == 1)
            {
                if (!Blockchain.ValidateHash(blockchain.Blocks[0])) // Check the validity of Hash by recalculating it.
                {
                    outputToRichTextBox1("Blockchain is invalid - Hash ");
                }
                else
                {
                    outputToRichTextBox1("Blockchain is valid");
                }
                return;
            }

            Console.WriteLine(" NewBlock: " + (blockchain.Blocks.Count - 1));
            for (int i = 1; i < blockchain.Blocks.Count - 1; i++)
            {
                Console.WriteLine("Hash for block " + i);
                if (
                    blockchain.Blocks[i].prevHash != blockchain.Blocks[i - 1].hash || // Check the "chain" hash.
                    !Blockchain.ValidateHash(blockchain.Blocks[i]) ||                 // Check the hash of each Block.
                    !Blockchain.ValidateMerkleRoot(blockchain.Blocks[i])              // Merkle Root can be used to check transaction integrity.
                    )
                {
                    outputToRichTextBox1("Blockchain is invalid " + (blockchain.Blocks[i].prevHash != blockchain.Blocks[i - 1].hash).ToString() + "  " +
                                         !Blockchain.ValidateHash(blockchain.Blocks[i]) + "  " + // Check the hash of each Block.
                                         !Blockchain.ValidateMerkleRoot(blockchain.Blocks[i]) + " " + blockchain.Blocks[i].nonce);
                    return;
                }
            }
            outputToRichTextBox1("Blockchain is valid");
        }