Block structure

Let's consider a simple block whose header and data are combined to create a data structure called Block. Each Block will contain the following: an index, the previous hash, a timestamp, data, and its own hash value:

class Block(object): 
    """A class representing the block for the blockchain""" 
 
    def __init__(self, index, previous_hash, timestamp, data, hash): 
        self.index = index 
        self.previous_hash = previous_hash 
        self.timestamp = timestamp 
        self.data = data 
        self.hash = hash 

The preceding code snippet defines a Python class called Block that has all the basic attributes of a blockchain block. Usually, a block will contain both a header and a body, with the header containing metadata about the block. However, the preceding example doesn't distinguish between the header and the body. A typical blockchain application, such as Bitcoin, will have a huge set of data that could be in the form of transactions, but in the example, we will consider the data to be of a string type.

A typical block will also contain a nonce and a difficulty target in the header. This information is used in consensus algorithms, such as Proof of Work. Since our intention is just to describe a blockchain, these fields are outside the scope of this section.