4361603 Foundation of Black Chain 226370316001
Practical: - 02
Aim: - Implement following concept to understand the
Blockchain and its implementation.
2.1 Creating a Merkle tree.
2.2 Creation of a block for Blockchain.
2.3 Block chain Implementation Programming code.
2.4 Creating ERC20 token.
2.5 Java code to implement Blockchain in Merkle Trees.
2.6 Java Code to implement Mining using Blockchain.
4361603 Foundation of Black Chain 226370316001
Practical: - 02
Aim: - Implement following concept to understand the
Blockchain and its implementation.
2.1 Creating a Merkle tree.
from typing import List
import hashlib
class Node:
def __init__(self, left, right, value: str, content, is_copied=False) -> None:
[Link]: Node = left
[Link]: Node = right
[Link] = value
[Link] = content
self.is_copied = is_copied
@staticmethod
def hash(val: str) -> str:
return hashlib.sha256([Link]('utf-8')).hexdigest()
def __str__(self):
return (str([Link]))
def copy(self):
"""
class copy function
"""
return Node([Link], [Link], [Link], [Link], True)
class MerkleTree:
4361603 Foundation of Black Chain 226370316001
def __init__(self, values: List[str]) -> None:
self.__buildTree(values)
def __buildTree(self, values: List[str]) -> None:
leaves: List[Node] = [Node(None, None, [Link](e), e)
for e in values]
if len(leaves) % 2 == 1:
[Link](leaves[-1].copy())
[Link]: Node = self.__buildTreeRec(leaves)
def __buildTreeRec(self, nodes: List[Node]) -> Node:
if len(nodes) % 2 == 1:
[Link](nodes[-1].copy())
half: int = len(nodes) // 2
if len(nodes) == 2:
return Node(nodes[0], nodes[1], [Link](nodes[0].value +
nodes[1].value), nodes[0].content+"+"+nodes[1].content)
left: Node = self.__buildTreeRec(nodes[:half])
right: Node = self.__buildTreeRec(nodes[half:])
value: str = [Link]([Link] + [Link])
content: str = f'{[Link]}+{[Link]}'
return Node(left, right, value, content)
def printTree(self) -> None:
self.__printTreeRec([Link])
4361603 Foundation of Black Chain 226370316001
def __printTreeRec(self, node: Node) -> None:
if node != None:
if [Link] != None:
print("Left: "+str([Link]))
print("Right: "+str([Link]))
else:
print("Input")
if node.is_copied:
print('(Padding)')
print("Value: "+str([Link]))
print("Content: "+str([Link]))
print("")
self.__printTreeRec([Link])
self.__printTreeRec([Link])
def getRootHash(self) -> str:
return [Link]
def mixmerkletree() -> None:
elems = ["Jai ", "Shree", "Ram"]
print("Inputs: ")
print(*elems, sep=" | ")
print("")
mtree = MerkleTree(elems)
print("Root Hash: "+[Link]()+"\n")
[Link]()
mixmerkletree()
4361603 Foundation of Black Chain 226370316001
Output: -
4361603 Foundation of Black Chain 226370316001
Practical: - 02
Aim: - Implement following concept to understand the
Blockchain and its implementation.
2.2 Creation of a block for Blockchain.
import hashlib
import time
class Block:
def __init__(self, index, data, previous_hash=''):
[Link] = index
[Link] = [Link]()
[Link] = data
self.previous_hash = previous_hash
[Link] = self.calculate_hash()
def calculate_hash(self):
return hashlib.sha256(f'{[Link]}{[Link]}{[Link]}
{self.previous_hash}'.encode()).hexdigest()
block = Block(0, "Genesis Block", "0")
print(f'Index: {[Link]}')
print(f'Timestamp: {[Link]}')
print(f'Data: {[Link]}')
print(f'Previous Hash: {block.previous_hash}')
print(f'Hash: {[Link]}')
Output: -
4361603 Foundation of Black Chain 226370316001
Practical: - 02
4361603 Foundation of Black Chain 226370316001
Aim: - Implement following concept to understand the
Blockchain and its implementation.
2.3 Block chain Implementation Programming code.
import hashlib
import time
class Block:
def __init__(self, index, previous_hash, timestamp, data, hash_value):
[Link] = index
self.previous_hash = previous_hash
[Link] = timestamp
[Link] = data
self.hash_value = hash_value
def __repr__(self):
return f"Block(index={[Link]}, timestamp={[Link]}, data={[Link]},
hash={self.hash_value}, prev_hash={self.previous_hash})"
class Blockchain:
def __init__(self):
[Link] = []
self.create_genesis_block()
def create_genesis_block(self):
genesis_block = Block(0, "0", int([Link]()), "Hello World",
self.calculate_hash(0, "0", int([Link]()), "Hello World"))
[Link](genesis_block)
def calculate_hash(self, index, previous_hash, timestamp, data):
"""
4361603 Foundation of Black Chain 226370316001
Create a SHA-256 hash of a block
"""
block_string = f"{index}{previous_hash}{timestamp}{data}"
return hashlib.sha256(block_string.encode('utf-8')).hexdigest()
def add_block(self, data):
"""
Add a new block to the chain
"""
previous_block = [Link][-1]
new_index = previous_block.index + 1
timestamp = int([Link]())
hash_value = self.calculate_hash(new_index, previous_block.hash_value,
timestamp, data)
new_block = Block(new_index, previous_block.hash_value, timestamp, data,
hash_value)
[Link](new_block)
def display_chain(self):
"""
Print the entire blockchain
"""
for block in [Link]:
print(block)
if __name__ == "__main__":
blockchain = Blockchain()
blockchain.add_block("Jai Shree Ram")
blockchain.add_block("Information Techonology")
blockchain.display_chain()
Output: -
4361603 Foundation of Black Chain 226370316001