1 of 20

Learn Blockchain by building one with Python

2 of 20

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

3 of 20

Agenda

  • Blockchain introduction
  • Understanding how blockchain works
  • Coding your own tiny blockchain with Python
  • Blockchain as an API using Flask
  • Kahoot Quiz

4 of 20

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.

5 of 20

Benefits of Blockchain

  • Trust
  • Decentralized
  • Availability

Use cases of Blockchain

  • Cryptocurrencies
  • Distributed Apps
  • Smart Contracts

6 of 20

How does a blockchain works ?

7 of 20

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.

8 of 20

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.

9 of 20

Hash functions

Hash functions takes a arbitrary length input and returns a fixed size alphanumeric string.

hash(‘mohit’) = '22b91144cd86ad7f21f30e005010d6ef3c1833cd93397952a1b8d5e4ec6e5d43’

Hash(‘rohit’) = '4320472dca0d474b698d34e00ea152eae7c4fe91290b6745c78ed67b4b0551b4'

10 of 20

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.

11 of 20

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.

12 of 20

13 of 20

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.

14 of 20

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.

15 of 20

Hands On: Coding your own tiny blockchain with Python

16 of 20

Hands On: Blockchain as an API using Flask�

17 of 20

Take home exercises

  • Try to change difficulty level in proof of work algorithm and see how long does it takes to compute nonce.�
  • Write a method to validate the chain.�
  • Reward the mining node.

You can mail me your solutions at mohitkh7@gmail.com.

18 of 20

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/)

19 of 20

Its QUIZ time!!!

20 of 20

Thanks