Develop a Blockchain using Python, Flask & Postman: Part 1

Aditya Kumar
The Startup
Published in
7 min readNov 3, 2019
blockchain graphic for article
Original business vector created by pikisuperstar — www.freepik.com (Edited)

Below is the link to the blockchain project repository on github incase you’re interested in jumping straight to the code.

https://github.com/adi2381/py-blockchain

The year is 2008, A person named Satoshi Nakamoto has just invented the concept of Blockchain and upon it, built a cryptocurrency named Bitcoin and published a paper for the same called Bitcoin: A Peer-to-Peer Electronic Cash System.

Fast forward to 2019, Blockchain has become a household name. You have to be literally living under the sea like SpongeBob to not know about it or not even have heard about it at least once. The potential of blockchain has been explored up to a large extent already and it’s applications have been implemented in various fields such as Digital Identity, Healthcare and many more.

The internet is brimming with video tutorials, articles & research papers for those who actually want to try building a blockchain or understand it’s concepts in a technical way. This article comprises of 2 parts serving the same purpose — Summarizing blockchain concepts quickly in 1ˢᵗ part and then diving into its technical side in 2ⁿᵈ part.

What is Blockchain?

In simple words, blockchain is a chain of blocks where each block contains some data and a code(hash) that has to match with the code of the previous block for the chain to be considered valid. It is important to note that once the data has been written to a block in the blockchain, it becomes immutable(There is no way to modify it or whatsoever).

Explaining Blockchain using Ledger:

Blockchain is just like a ledger. A ledger is a registry that records details of transactions. This ledger is distributed across the blockchain network so each node has a copy of this ledger.

This maintains the consistency and validity of the blockchain because if one copy of ledger at someone’s node is changed intentionally anyhow, but 99% of the ledger doesn’t have the same change then that node can be thought of as a malicious node and it’s copy of ledger will become invalid when cross-checked against other copies of ledgers in the network and its blockchain will be rendered corrupt.

This way your copy of the ledger and your data remain secure & valid. (note: the data inside the copies of the ledger is encrypted and cannot be viewed by anyone)

Now, what added more to the popularity of blockchain is the word Decentralization. It means, the blockchain isn’t controlled by anyone and there’s no centralized authority like banks or government that can control it. Instead, it is managed autonomously using a peer-to-peer network where each peer has equal privileges.

What about Cryptocurrency Mining?

Cryptocurrency is a virtual currency that has some digital value attached to it. This cryptocurrency can be used as a medium of exchange for various purposes such as purchasing products or subscribing to some service etc.

Miner: The individual who shares their computational resources for validating transactions

Mining: It is the process of adding an unprocessed transaction into a block after which it becomes verified and processed

So, Cryptocurrency mining means that you use your computational resources to validate a transaction on the network in order to add it to the blockchain. What you get in back for carrying out this process and sharing your computational resources is a mining reward which is usually the cryptocurrency itself.

Now the question arises, there are millions of miners in this world, so how is the winner selected or how is the reward given out?

To answer this, we will use an example of the lottery system.

  • In the lottery scheme, every user who has a lottery ticket is a miner
  • pre-decided lottery number is a puzzle
  • Your lottery ticket is your computer & your computational power is your chances of winning
  • To earn the reward, your lottery ticket should have the maximum probability of winning (your computer should have large computational power)
  • So, the more computational power you have, the greater the chances of winning
  • Now, if there are 5 users whose computational power amounts to be the same then whoever is the first to solve(match the numbers) the puzzle wins the lottery and thus, in turn, the lottery reward

So, this is how the winner is chosen in a cryptocurrency network, to win the mining reward(lottery reward), you have to solve a puzzle(predefined lottery numbers), so using your computational resources(winning chances) you try to solve(match the numbers) the puzzle. Now, whoever has the most powerful computational resources has the maximum chance to solve the puzzle. If there are 5 people with the same computational resources then the lottery becomes a contest and whoever is able to solve the puzzle first, wins it. Once the block is mined and the reward is given out, this whole process starts from scratch.

What about Security?

Blockchain has several layers of verification and validation schemes that make it secure.

First & foremost is its own attributes such as being decentralized and a distributed peer-to-peer network. Every node has a copy of the ledger and can see it. The ledger remains immutable and the data in the ledger is in an encrypted form so you can’t actually see the name of the sender (e.g. john) or recipient.

Then, there is the concept of hashing where each block in the blockchain has a hash in its data. When a new block is mined, the hash of the new block is cross-checked with the hash of the previous block. If the hash checks valid then the mined block is considered valid otherwise not and the mined block fails to be a valid block and is not included in the blockchain.

Consensus

A consensus is a widely accepted agreement. In blockchain networks such as bitcoin or ethereum cryptocurrency network, there is a consensus in place which the miners abide by for the benefit of the network. This consensus is usually put in place to make sure that the mining is difficult such that no one anyone can easily mine a block and get rewarded and to strengthen the security.

There are many types of consensus such as proof of work, proof of stake and proof of burning, etc. but I’m only going to discuss the most common ones — proof of work & proof of stake.

Proof of Work — Proof of work is the default consensus in place in a blockchain network where the miner is rewarded for the work they carry out in mining a new block using their computing resources. In simple words, under this agreement, the miners have to compete against one another using their computing resources to validate transactions on the network and mine a block and get rewarded. The downside of this consensus is the high costs of power spent in mining a new block. For e.g., if you are paying more for the electricity that runs your systems than the reward earned for mining a new block, it becomes unfeasible and losses can be incurred easily.

Proof of Stake — In proof of stake, your stake is verified. The stake here refers to the number of coins you have in your wallet & the duration for which you’ve had those coins in the same wallet. If you transfer the coins from one wallet to another wallet, the duration attribute of the stake gets reset. Under this consensus, the miner who holds a significant stake for e.g. has a large number of coins and have stored them in their wallet for more than 2 years has a larger stake as compared to someone who has a small number of coins and has had them in their wallet for only 6 months. So, the miner with the greater stake in the network is given a higher chance to validate and mine the block and in turn win the reward. This type of agreement helps in building a dedicated and decentralized network of miners who are loyal to a blockchain network.

What about Storing Coins?

When it comes to storing coins securing, there are 2 options.

  • Virtual Wallets: These wallets contain a public key, private key, coins & details of transactions. The public key is your public identifier and the key used when sending cryptocurrency across the network. The private key is your digital signature which is used to decrypt a message encrypted using a public key. This public and private key ecosystem is implemented using the RSA technique which is an encryption-decryption algorithm.
  • Physical wallets: Ledger Nano S is the most popular physical wallet. These wallets store your critical information offline so it is secured from online attacks. Also, virtual wallets sometimes support certain cryptocurrencies only, for e.g. A wallet named XYZ might only support bitcoin & ethereum. However, when it comes to physical wallets such as Ledger Nano S, they have huge support for storing almost any cryptocurrency out there and that too in large amounts.

To Conclude

  • We discussed blockchain, its distributed and decentralized properties.
  • We discussed cryptocurrency mining and explained it using the lottery example
  • We discussed Blockchain Security & Consensus
  • Finally, we discussed storing of cryptocurrency using wallets

Link for Second Part

--

--