Creating a blockchain with your own hands in Python

Creating a blockchain with your own hands in Python

03.02.2023
Author: Robert Strickland
Subscribe

Creating a blockchain with your own hands in Python

Creating a blockchain with your own hands in Python
Blockchain is a decentralized database. Everyone knows a similar definition of blockchain, but in this article, we want to make a guide on how to create a blockchain with your own hands. This will give you a better understanding of the technology itself and maybe some practice in programming.

  • THE BLOCKCHAIN WITH YOUR OWN HANDS: THE STRUCTURE OF THE BLOCKCHAIN

So, we will write the blockchain with our own hands in Python. To begin with it is necessary to determine with what parameters the data block itself is specified. As a rule, the block structure consists of the following components:

  • Timestamp.
  • The data itself.
  • Index (optional).

For greater certainty in our own blockchain, we will use both the timestamp and the index. In addition, the data itself in the block is not fed in its original form, it is converted into a hash using the encryption function. The previous block is also hashed. We get that the last hash block will include:

  • Timestamp.
  • Index.
  • Data.
  • The hash of the previous block.

In the form of a code it will look like this:

 blockchain

  • BLOCKCHAIN WITH YOUR OWN HANDS: THE FIRST BLOCK

Everything has to start somewhere. So the blockchain must have the first block. But a problem appears - the block must contain the hash of the previous block, and where to get it if we are talking about the first cell of the chain? It has to be entered manually, so that's what we'll do. The first block will have index 0, arbitrary data and arbitrary hash of the previous block.

 blockchain2

  • BLOCKCHAIN WITH OUR OWN HANDS: GENERATING NEW BLOCKS

At this stage, we already have the first block, but for the normal functioning of our simple blockchain, we need to set the algorithm of building the next blocks. It will be as follows:

  • The function takes the previous block as a parameter.
  • Form data of a new block from received parameters.
  • Return the new block with valid data.

Thus by hashing information about the old block in the new block we will increase the chain's security, its verifiability and integrity. If this is not done, it will be easier to spoof data "from the past" in the blockchain, falsifying the present.

 blockchain3

  • BLOCKCHAIN WITH YOUR OWN HANDS: BLOCKCHAIN GENERATION

So, now we have ready block structure, first block and function that defines the algorithm of block generation. The smallest thing is left - to formalize the generation of the chain itself. The first block will be the beginning of the chain, and then it will be filled with new blocks. Since the so-called blockchain of our own hands is only a training sample, we will limit the generation of blocks to 20 pieces. The chain will be filled with a simple for-cycle:

 blockchain4

Now we run our blockchain file and look at the result.

 blockchain5

Of course, our blockchain is too far from the bitcoin chain, but it will do well as a sample for those who just begin to learn this technology. To make this chain work, we will have to introduce a lot of additional functions, e.g. to limit the number of blocks added per unit of time, to track the state of the chain on multiple computers, etc.


 

Other instructions

Cold and hot cryptocurrency wallets. What is the difference and how to create them?
Securing Information: Shamir's Secret Sharing Scheme and Bitcoin
"Reputation Institute. What will happen to blockchain in 30 years
What is Worldcoin? Why the creator of ChatGPT needs a catalog of all the inhabitants of the planet
Measure blockchain. How important is the TVL indicator
What is Lightning Network and how the project solves bitcoin's main problem
Trustpilot