Custom Implementation of Blockchain In Rust(Part 2)

Table of contents
Reading Time: 2 minutes

Hello Everyone, in the previous blog, we talked about blockchain, what is block and its content. we also implemented a sample Blockchain application using Rust.
In this blog, we will continue working with our sample blockchain application and talk about blockchain validation and its mining process.

Blockchain Validation

Why do we need blockchain validation? Because we have to ensure that no one can tamper any data in any of the blocks in the blockchain. We will be considering two things for blockchain verification:

1) Recalculate the hash of block and compare it with original hash.
2) Compare previous block hash with registered previous hash.

Let’s add a method in blockchain.rs

pub fn is_valid_chain(&self) -> bool {
        let blocks = &self.blocks;

        for (i, block) in blocks.iter().enumerate() {
            if block.hash
                != calculate_hash(
                    &block.pre_hash,
                    &block.transaction,
                    &block.timestamp,
                    &block.nonce,
                )
            {
                return false;
            }
            if i > 0 && block.pre_hash != blocks[i - 1].hash {
                return false;
            }
        }

        return true;
    }

Blockchain Mining
Till now we can create blocks very quickly. All we have to do is just create a transaction, compute the hash and add in the blockchain. People can create millions of blocks in seconds. But that’s not what we want because they can spam our blockchain or attack on blockchain. There is also a security issue. You can change the content of block and recalculate hashes of all blocks. You can end up with a valid blockchain. It’s difficult but still possible. This is not we want.

To solve this security issue, blockchain has something called, proof of work. With this process, you have to prove that you have put a lot of computing power into making a block. This process is called mining. so what it does, it alters first leading characters of hash to zeros. That is called the difficulty level. It is the number of zeros, we want as the leading character in the hash. 
so we will add new value nonce in block with initial value 0.  While computing hash, we will keep increasing its value by 1 until hash’s leading characters match with the difficulty level.

Let’s see mine method

    pub fn mine(&mut self) {
        let target = get_difficult_string();

        while &self.hash[..DIFFICULT_LEVEL as usize] != target {
            self.nonce += 1;
            self.hash = calculate_hash(
                &self.pre_hash,
                &self.transaction,
                &self.timestamp,
                &self.nonce,
            )
        }

        println!("Block Mined");
    }

You can find code here rust_blockchain.

Thank you for reading the blog!!

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading