How to create YOUR own Cryptocurrency!!!

Anantha Perumal
Coinmonks

--

Part - 3:How blockchain identifies when a hacker changes the data in a block?

pic source here

Welcome back guys🤗🤗.. hope everyone is doing good and read my previous blog if anyone missed please read it here so that you can enjoy reading this one.

Now, what's special in this blog🤔🤔? We are gonna witness how secure the blockchain is when a hacker tries to change a data in a block😯!! in addition to that, we are gonna simplify our previous code... Let’s simplify our previous code first which means we are gonna do some couple of changes😨 where the functionality remains the same. trust me they are really small changes😜

Replacing dictionary with blocks while adding blocks to the blockchain :

I have changed add() method in a blockchain class as below since chaining

add() method in BlockChain class

up of blocks (class) is a good method of coding rather than by dictionary due to this change we have to change the mine() method as below

mine() method in blockchain

here I have changed self.chain[-1].hash() because self.chain[-1] is a block itself(since we are adding a block) so I called hash() of class Block to get hash of that block

I have changed the main method as below to get better visual on output

main() method

Now you can see the output by just navigating to that directory in your command prompt and entering python <filename>.py as below

output

okay… that's all our code changes😊… hence on seeing the output we can witness that the functionality remains the same.

How blockchain identifies when someone changes the data in a block🤔

The question is cool right because everyone knows blockchain is secure🔒 and data cannot be changed but how blockchain achieves that is not known by many. Let’s discuss and verify that😊

I am gonna introduce ‘isValid()’ method in BlockChain class which looks like

isValid() method in blockchain

This method has a logic that block N’s previous hash should be equal to block N-1’s hash which is freshly calculated while checking and the hash should match the difficulty this is how the blockchain is made secure at a simpler level.

Let’s check whether our blockchain is valid or not by calling isValid() function. Now the main function look’s like

main method

This is how the output will be if our blockchain is secure then ‘BlockChain is valid’ will be printed else it will be ‘Is valid: false’.

output showing blockchain is valid

Let us try to change the data in the third block and try to verify whether blockchain shows that it is not valid

The main method to change data in the third block
output showing blockchain is not valid with block number

you can try by changing any other blocks data. For those who want source code, you can access the updated code here.

Thank you so much for spending your valuable time in my blog. Please support me by doing claps at the rate of 1–50 by the way you feel it worth. If you have any doubts regarding this blog or suggestion to improve it please do comment below and share it to those who want to know about blockchain.

Stay tuned for next part. you can find next part link here. I will get you things as simple as you could understand😉😉.

Have a good day😊😊

Also, Read

--

--