Subject Syllabus
05101258 - Block Chain Technology- I
Course: BCA Semester: 4
Objective
This implementation will include the creation of a
Block class, a Blockchain class,
and the process of adding blocks to the blockchain
with hashing and verifying.
Printed on : 16-11-2024 02:31 PM Page 1 of 2
Subject Syllabus
05101258 - Block Chain Technology- I
import hashlib
import time
# Define the Block class
class Block:
def __init__(self, index, previous_hash, timestamp, data, hash_value):
self.index = index
self.previous_hash = previous_hash
self.timestamp = timestamp
self.data = data
self.hash_value = hash_value
def calculate_hash(self):
# Combine all block attributes to create a string, then hash it
block_string = f"{self.index}{self.previous_hash}{self.timestamp}
{self.data}"
return hashlib.sha256(block_string.encode('utf-8')).hexdigest()
# Define the Blockchain class
class Blockchain:
def __init__(self):
self.chain = []
self.create_genesis_block()
def create_genesis_block(self):
# Manually create the first block (Genesis Block)
genesis_block = Block(0, "0", time.time(), "Genesis Block", "")
genesis_block.hash_value = genesis_block.calculate_hash()
self.chain.append(genesis_block)
Printed on : 16-11-2024 02:31 PM Page 2 of 2
Subject Syllabus
05101258 - Block Chain Technology- I
def add_block(self, data):
# Get the last block in the chain
last_block = self.chain[-1]
# Create the new block based on the last block's hash
new_block = Block(len(self.chain), last_block.hash_value, time.time(),
data, "")
new_block.hash_value = new_block.calculate_hash()
# Add the new block to the blockchain
self.chain.append(new_block)
def is_chain_valid(self):
# Verify the integrity of the blockchain
for i in range(1, len(self.chain)):
current_block = self.chain[i]
previous_block = self.chain[i - 1]
# Check if the hash is correct
if current_block.hash_value != current_block.calculate_hash():
print(f"Invalid hash at block {current_block.index}")
return False
# Check if the previous hash matches
if current_block.previous_hash != previous_block.hash_value:
print(f"Invalid previous hash at block {current_block.index}")
return False
Printed on : 16-11-2024 02:31 PM Page 3 of 2
Subject Syllabus
05101258 - Block Chain Technology- I
return True
def print_chain(self):
for block in self.chain:
print(f"Block #{block.index}")
print(f"Timestamp: {block.timestamp}")
print(f"Data: {block.data}")
print(f"Hash: {block.hash_value}")
print(f"Previous Hash: {block.previous_hash}")
print("-" * 30)
Example usage
if __name__ == "__main__":
blockchain = Blockchain()
# Add some blocks with data
blockchain.add_block("Transaction 1: Alice sends 10 BTC to Bob")
blockchain.add_block("Transaction 2: Bob sends 5 BTC to
Charlie")
# Print the blockchain
blockchain.print_chain()
# Validate the blockchain
if blockchain.is_chain_valid():
print("Blockchain is valid.")
else:
print("Blockchain is invalid.")
Printed on : 16-11-2024 02:31 PM Page 4 of 2
Subject Syllabus
05101258 - Block Chain Technology- I
OUTPUT
Printed on : 16-11-2024 02:31 PM Page 5 of 2
Subject Syllabus
05101258 - Block Chain Technology- I
Learning outcome
•Exploring the potential impacts of
quantum computing on blockchain
security.
•Blockchain and AI Integration:
Learning about the nmental and energy
consumption issues related to blockchain,
especially in proof-of-work systems.
Printed on : 16-11-2024 02:31 PM Page 6 of 2