avatarEsteban Thilliez

Summary

This context provides a tutorial on building a basic blockchain using Python, covering the concepts of blockchain, hashes, and Proof of Work.

Abstract

The context begins by explaining the basics of blockchain, including its definition as a sequential chain of records called blocks, its immutability, and its use in storing data. It then introduces the concept of hashes, which are used to chain blocks together and prove that data is the same without revealing it. The tutorial proceeds to demonstrate how to build a blockchain in Python using classes for the blockchain, blocks, and transactions. It also covers the implementation of Proof of Work, a mechanism used to create blocks and make the blockchain less susceptible to attacks. The tutorial concludes with a simple main function to test the blockchain.

Bullet points

  • A blockchain is a sequential chain of records called blocks, which is immutable and can be used to store any data.
  • Blockchains use hashes to chain blocks together and prove that data is the same without revealing it.
  • The tutorial demonstrates how to build a blockchain in Python using classes for the blockchain, blocks, and transactions.
  • The tutorial covers the implementation of Proof of Work, a mechanism used to create blocks and make the blockchain less susceptible to attacks.
  • The tutorial concludes with a simple main function to test the blockchain.

Learn Blockchains by Building One in Python — 1. Blockchain Basics

Photo by GuerrillaBuzz Crypto PR on Unsplash

Do you like Python and are you interested in learning how a blockchain works? Well, I’ll do my best to teach you the basic blockchain concepts and how to implement them in Python.

Basics

A blockchain is a sequential chain of records called Blocks. It is immutable and can be used to store any data you like. Most of the time, you know blockchains because you know cryptocurrency, in this case, they are used to store transactions.

But they can also be used in a company to store confidential data or to transfer invoices between two companies, for example.

To be chained, blockchains use hashes. Hashes come from hash functions, which allow you to prove that something is the same as something else without revealing these things.

For example, let’s say I own 2 gold ingots. I want to send one to a friend, but I don’t want the post office to know I’m sending a gold ingot. I put it in a package and I weight it. When my friend will receive it, he can weigh it too, and thanks to the weight, he will know that in the package there is a gold ingot (else he can just open it). The weight is the hash.

Now you know some base concepts, let’s dive into the code. If you want, I’ve made a GitHub repo for this series, so that you can check the code there: Simple Blockchain Series.

Blockchain Class

How to represent a blockchain in Python? Basically, it’s just a chained list. The list will be used to store blocks.

We’ll build a blockchain to store transactions, so we also need another list to store transactions.

Then, we’ll also need some methods to add transactions or blocks and hash blocks. We’ll also add a property to get the last block from our blockchain.

Block Class

To represent blocks, we will use another class. But what does a block contain?

A block as an index, a timestamp, a list of transactions, a proof (we’ll see that later), and the hash of the previous block so that it forms our blockchain.

We’ll also need a method to turn a block to JSON, and another method to turn a block’s transactions to JSON. These things will allow our block to be hashed because by default we can’t hash such a Python object.

Transaction Class

A transaction is easy to represent. It consists of a recipient, a sender, and an amount.

Mix it Up

Now, we can complete our Blockchain class.

The code is pretty self-explanatory, except for the hash function. To generate a hash, we’ll encode our block and convert it into a hexadecimal string containing only digits.

We also need to create an initial block, called the “genesis block”.

Now, we need to implement Proof of Work.

What is Proof of Work

Proof of Work is used to produce costly data, but easy to verify once it is produced.

Applied to blockchains, it is an algorithm used in blockchains to create blocks. Without Proof of Work, anyone could create blocks easily and alter the blockchain, and that’s not what we want. This makes the blockchain much less susceptible to attacks.

Let’s take an example to understand. We want to find a number that meets this condition: the hash of this number multiplied by another number must end in 0.

hash(x*y) = ....0

To find x and y, we have to make several calculations, which can be costly. But once x and y are found, we can prove easily that they are good just by passing them to hash .

Proof of Work follows the same principle, but the solution is harder to find because the condition is more complex.

The Bitcoin Proof of Work algorithm is called the Hashcash. Miners use computational power to find the solution, and when a miner has found the solution, a new block is created, and there is another number to find. Then, the miner is rewarded with a coin. The network can finally verify that the block is not a fake block and attach it to the blockchain.

Implement Proof of Work

We’ll implement a simple algorithm. Here is our condition:

Find a number that when hashed with the previous block’s solution, gives a hash with 3 leading zeros

We can increase/reduce the number of zeros to increase/reduce the difficulty of finding the solution.

Let’s first implement a function to resolve our Proof of Work condition.

Now we can implement our condition. It will work this way:

  1. We encode our last proof and our new proof.
  2. We convert it to a hex-digit hash.
  3. If this hash includes 3 leading zeros, we return True , else False .

And that’s all for Proof of Work!

Test our Blockchain

Now, we can implement a simple main to test our blockchain. For now, we can create new blocks without calculating the proof because we have not yet implemented the validation of the chain, but later it won’t be possible.

For the example, we will do as if we were calculating the right proof.

As you can see, it works. If you want to test the concept of Proof of Work, you can try to increase the difficulty from 3 to anything you want and see how it changes the execution time of the script.

Final Note

That’s all for this first part. We’ll see in the next story how you can turn your blockchain into an API to interact with it and mine blocks or make transactions using HTTP requests.

Be sure to follow me if you don’t want to miss the other stories from this series!

You can find the other stories of this series here:

To explore more of my Python stories, click here! You can also access all my content by checking this page.

If you liked the story, don’t forget to clap, comment, and maybe follow me if you want to explore more of my content :)

You can also subscribe to me via email to be notified every time I publish a new story, just click here!

If you’re not subscribed to medium yet and wish to support me or get access to all my stories, you can use my link:

Blockchain
Cryptocurrency
Python
Bitcoin
Learning To Code
Recommended from ReadMedium