Python Blockchain - Block Class


Advertisements

A block consists of a varying number of transactions. For simplicity, in our case we will assume that the block consists of a fixed number of transactions, which is three in this case. As the block needs to store the list of these three transactions, we will declare an instance variable called verified_transactions as follows −

self.verified_transactions = []

We have named this variable as verified_transactions to indicate that only the verified valid transactions will be added to the block. Each block also holds the hash value of the previous block, so that the chain of blocks becomes immutable.

To store the previous hash, we declare an instance variable as follows −

self.previous_block_hash = ""

Finally, we declare one more variable called Nonce for storing the nonce created by the miner during the mining process.

self.Nonce = ""

The full definition of the Block class is given below −

class Block:
   def __init__(self):
      self.verified_transactions = []
      self.previous_block_hash = ""
      self.Nonce = ""

As each block needs the value of the previous block‚Äôs hash we declare a global variable called last_block_hash as follows −

last_block_hash = ""

Now let us create our first block in the blockchain.

Advertisements