Python blockchain creates the initial Block
Python Blockchain: Creating the Initial Block
Let’s assume that the originator of TPCoins initially issued 500 TPCoins to a known client, Dinesh. To do this, he first created a Dinesh instance – Dinesh.
Dinesh = Client()
Then we create a genesis transaction, sending 500 TPCoins to Dinesh’s public address.
t0 = Transaction (
"Genesis",
Dinesh.identity,
500.0
)
Now, we create an instance of the Block class and call it block0.
block0 = Block()
We initialize the previous_block_hash and Nonce instance variables to None since this is the first transaction stored in our blockchain.
block0.previous_block_hash = None
Nonce = None
Next, we append the aforementioned t0 transaction to the verified_transactions list maintained within the block.
block0.verified_transactions.append (t0)
At this point, the block is fully initialized and ready to be added to our blockchain. We will create the blockchain for this purpose. Before we add a block to the blockchain, we hash the block and store its value in the global variable we declared earlier called last_block_hash. This value will be used by the next miner in their block.
We use the following two lines of code to hash the block and store the digest value.
digest = hash (block0)
last_block_hash = digest
Finally, we create a blockchain, as we’ll see in the next chapter.