Learn Blockchain by building one with Python
About Me
Hi all, myself Mohit Khandelwal. I am a python enthusiast currently working as lead consultant at Genpact.
Linkedin: https://www.linkedin.com/in/mohitkh7/
Github: https://github.com/mohitkh7
Mail: mohitkh7@gmail.com
Agenda
What is a blockchain ?
Blockchain is a immutable & distributed digital ledger. Each transaction on the ledger is stored in a block. The information contained in a block is linked to information in a previous block and over time forms a chain. Hence the word blockchain.
Benefits of Blockchain
Use cases of Blockchain
How does a blockchain works ?
Transaction |
X paid Rs. 2 to Y |
Y paid Rs. 5 to Z |
Z paid Rs. 3 to X |
We have stored few transactions over here.
Transaction |
X paid Rs. 2 to Y |
Y paid Rs. 50 to Z |
Z paid Rs. 3 to X |
The second record has been modified. There is no mechanism to identify or stop modification.
Hash functions
Hash functions takes a arbitrary length input and returns a fixed size alphanumeric string.
hash(‘mohit’) = '22b91144cd86ad7f21f30e005010d6ef3c1833cd93397952a1b8d5e4ec6e5d43’
Hash(‘rohit’) = '4320472dca0d474b698d34e00ea152eae7c4fe91290b6745c78ed67b4b0551b4'
Prev Hash | Transaction | Hash |
0 | X paid Rs. 2 to Y | 0X2Y |
X2Y | Y paid Rs. 5 to Z | X2YY5Z |
Y5Z | Z paid Rs. 3 to X | Y5ZZ3X |
We are computing hash of every record. Than we are chaining next record with previous hash.
Prev Hash | Transaction | Hash |
0 | X paid Rs. 2 to Y | 0X2Y |
X2Y | Y paid Rs. 50 to Z | X2YY50Z |
Y5Z | Z paid Rs. 3 to X | Y5ZZ3X |
Even though second record is modified. By mismatch of hash and previous hash we can identify data has been forged.
Prev Hash | Transaction | Hash |
0 | X paid Rs. 2 to Y | 0X2Y |
X2Y | Y paid Rs. 50 to Z | X2YY50Z |
Y50Z | Z paid Rs. 3 to X | Y50ZZ3X |
The second row has been modified. Furthermore attacker has changed previous hash and recomputed the hash of all subsequent records and modified them too.
Prev Hash | Transaction | Nonce | Hash |
0 | X paid Rs. 2 to Y | # | @0X2Y |
X2Y | Y paid Rs. 5 to Z | $ | @X2YY5Z |
Y5Z | Z paid Rs. 3 to X | * | @Y5ZZ3X |
Since recomputing a hash of block is easy task. We introduced a nonce which is difficult and time consuming to compute but easy to verify. Now even a smartest attacker cannot modify any record.
Hands On: Coding your own tiny blockchain with Python
Hands On: Blockchain as an API using Flask�
Take home exercises
You can mail me your solutions at mohitkh7@gmail.com.
Extending to distributed ledger
Today what we have built is an immutable ledger. You can extend it to be distributed as well. Doing so you will explore other concepts on blockchain like asymmetrical cryptography, conflict resolution etc.
Refer to blog “A practical introduction to blockchain with Python” by Adil Moujahid (http://adilmoujahid.com/posts/2018/03/intro-blockchain-bitcoin-python/)
Its QUIZ time!!!
Thanks