Custom Implementation of Blockchain In Rust(Part 3)

Reading Time: 2 minutes

In previous blogs(part-1 and part-2), we created a sample blockchain application. We also implemented validation and proof of work algorithm. Now we are going to upgrade our blockchain to simple cryptocurrency. We can do this by:-

  • Including the functionality of paying out rewards to miners
  • Signing the transaction

We will be covering only first part in this blog. We will cover transaction signature in next blog.

Paying Rewards to Miners:-
Anyone from anywhere can become a miner. Mining is a computer-intensive work where miners need to solve a puzzle called “Proof of Work”. Anyone who solves the puzzle first gets to add the block in the network and is rewarded for that.
We have to do few things:-

1) Add Mining reward

2) Store unmined transactions

3) A new function to mine a block for unmined transactions

Let’s start coding. Add a constant variable in the library file.

const MINING_REWARD: f32 = 100f32;

Now we will be adding two new properties in our blockchain class. One is for pending transactions and another one is for mining reward which will control how much coins miner get as reward.

pub struct Blockchain {
   blocks: Vec<Block>,
   unmined_transactions: Vec<Transaction>,
   mining_reward: f32,
}

We will replace the add_block method with mine_unmined_transactions method. This method will not only mine a new block with all the pending transactions, but it will also send a mining reward to the miner. If you notice, mine_unmined_transactions method also takes miner_address as input. Once you successfully mined a block, the system will create a new transaction to give you your mining reward.

pub fn mine_unmined_transactions(&mut self, miner_address: String) {
       let transactions = &self.unmined_transactions;
       let mut block = Block::new(transactions.to_vec());
       match self.blocks.last() {
           Some(pre_block) => block.set_pre_hash(pre_block.hash.to_owned()),
           None => block.set_pre_hash("0".to_string()), // genesis_block
       }
       block.set_hash();
       block.mine();
       self.blocks.push(block);
       self.unmined_transactions = vec![Transaction {
           sender: String::new(),
           receiver: miner_address,
           amount: self.mining_reward,
       }];
   }

Now, this method has the logic for:-

  • Create a block object
  • Set hash and pre_hash
  • Mining unmined transactions
  • Adding a block to the chain after mining
  • Create a new transaction for miner reward

In the mining method, we have included all unmined transactions in the block. But this is not the case in the real application. Because the size of a block is limited. In Bitcoin’s case, there is a block size limit of 2mb.

You can find code here rust_blockchain. Thank you for reading the blog!!

If you want to read more content like this?  Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7

Video: Learn more and enhance your knowledge and skills of Rust Language by subscribing Rust Times newsletter and receive updates bi-weekly. https://bit.ly/2Vdlld7

Template: For more such template updates, subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7

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.