Now let’s think of a way to represent a block in Python. We could create a bigger dictionary and store our data inside this dictionary. But since blocks can be represented as objects, let’s create a Block Class which we can easily use to create new blocks.
Recall that a Block contains the following properties:
- Previous Hash
In this exercise, we will be creating the default constructor for the Block class in our Mini-Blockchain.
Block in the blockchain has a timestamp associated with it. In order to dynamically generate a timestamp, we must import a Python module that returns the current date and time.
datetime module from the
Now let’s work on creating our
Block. We will be passing
previous_hash to the default constructor each time a
Block is created.
__init__() method inside the Block class by initializing the following instance variables:
nonce(with a default value of
__init__() method, create a
timestamp instance variable that stores the current date and time.