Upgrade Linked List in Blockchain by python
Let's upgrade the linked list to the blockchain. In order to accomplish this, we add a new property to the Block class called the parent's hash.
Take a look at the following code block , If we change the history of BlockA, then the hash value of BlockC will also change; otherwise, it won't. That's the allure of blockchain technology.
import hashlib
import json
class Block:
id = None
history = None
parent_id = None
parent_hash = None
blockA = Block()
blockA.id = 1
blockA.history = 'He likes Dora'
blockB = Block()
blockB.id = 2
blockB.history = 'She likes Ryder'
blockB.parent_id = blockA.id
blockB.parent_hash = hashlib.sha256(json.dumps(blockA.__dict__).encode('utf-8')).hexdigest()
blockC = Block()
blockC.id = 3
blockC.history = 'She likes Ryder'
blockC.parent_id = blockB.id
blockC.parent_hash = hashlib.sha256(json.dumps(blockB.__dict__).encode('utf-8')).hexdigest()
print("Original parent hash in block C")
print(blockC.parent_hash)
print()
print("No Change in the history on block A:")
print()
blockA.history = 'He likes Dora'
blockB.parent_hash = hashlib.sha256(json.dumps(blockA.__dict__).encode('utf-8')).hexdigest()
blockC.parent_hash = hashlib.sha256(json.dumps(blockB.__dict__).encode('utf-8')).hexdigest()
print()
print("Change History on block A")
print()
blockA.history = 'He doesn\'t likes Ryder'
blockB.parent_hash = hashlib.sha256(json.dumps(blockA.__dict__).encode('utf-8')).hexdigest()
blockC.parent_hash = hashlib.sha256(json.dumps(blockB.__dict__).encode('utf-8')).hexdigest()
print("Ahh! , New parent hash in block C which is different from original one")
print(blockC.parent_hash)
Run the above script
PS D:\KnowledgeHunt\blockchain\ch1> python .\upgradelinklist.py
Original parent hash in block C
79be004af591f9724f09d47ce0fc9bdb7565f748e56f81447a694a3cfe0393ee
No Change in the history on block A:
79be004af591f9724f09d47ce0fc9bdb7565f748e56f81447a694a3cfe0393ee
Change History on block A
Ahh! , New parent hash in block C which is different from original one
1a11773a77eb6e3555d1936e6b9bc91687fe29336ebe8c41f47114f3fceda9bb
Comments
Post a Comment