Python Blockchain Block Class
Python Blockchain Block Class
A block consists of a variable number of transactions. For simplicity, we will assume that a block consists of a fixed number of transactions, in this case three. Since the block needs to store a list of these three transactions, we will declare an instance variable called verified_transactions as shown below.
self.verified_transactions = []
We named this variable verified_transactions to indicate that only valid transactions that have been verified will be added to the block. Each block also contains the hash of the previous block, making the blockchain immutable.
To store the previous hash value, we declare an instance variable as shown below.
self.previous_block_hash = ""
Finally, we declare a variable called Nonce to store the nonce generated by the miner during the mining process.
self.Nonce = ""
The complete definition of the Block class is as follows
class Block:
def __init__(self):
self.verified_transactions = []
self.previous_block_hash = ""
self.Nonce = ""
Since each block requires the hash of the previous block, we declare a global variable called last_block_hash as shown below
last_block_hash = ""
Now let’s create our first block in the blockchain.