How to Create Your Own Cryptocurrency Blockchain in Python

Building the Chain

The whole concept of a blockchain is based on the fact that the blocks are “chained” to each other. Now, we’ll create a blockchain class that will play the critical role of managing the entire chain.

It will keep the transactions data and include other helper methods for completing various roles, such as adding new blocks.

Let’s talk about the helper methods.

Adding the Constructor Method

Here is the code:

class BlockChain(object):

    def __init__(self):

        self.chain = []

        self.current_data = []

        self.nodes = set()

        self.build_genesis()

The __init__() constructor method is what instantiates the blockchain.

Here are the roles of its attributes:

  • self.chain — this variable stores all the blocks.
  • self.current_data — this variable stores information about the transactions in the block.
  • self.build_genesis() — this method is used to create the initial block in the chain.
Prev3 of 6Next

Leave a Reply

Your email address will not be published. Required fields are marked *