0% found this document useful (0 votes)
10 views4 pages

80 Assignment2 BT

The document outlines an assignment focused on implementing a blockchain system that verifies its integrity through various methods such as hashing and Proof-of-Work. It includes the design of a Block class, the implementation of SHA-256 hashing, and a verification function to detect tampering. The conclusion emphasizes the effectiveness of the verification function in maintaining the reliability and security of the blockchain.

Uploaded by

adeebkarbhari04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

80 Assignment2 BT

The document outlines an assignment focused on implementing a blockchain system that verifies its integrity through various methods such as hashing and Proof-of-Work. It includes the design of a Block class, the implementation of SHA-256 hashing, and a verification function to detect tampering. The conclusion emphasizes the effectiveness of the verification function in maintaining the reliability and security of the blockchain.

Uploaded by

adeebkarbhari04
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT 02

Name: Adeeb Karbhari

Roll no: 80

PRN: 22420328

[Link]
Implementation of Block Verification Function to Validate Blockchain Integrity

[Link]
To design and implement a blockchain system and verify its integrity using hashing,
linkage checking, Proof-of-Work, and timestamp validation.

[Link]
• To create a Block class with necessary attributes
• To implement SHA-256 hashing for block generation
• To verify the integrity of the blockchain
• To detect tampering in data and linkage
• To validate Proof-of-Work and timestamps

[Link]
Blockchain is a distributed ledger where data is stored in blocks connected through
cryptographic hashes. Each block contains index, timestamp, data, previous hash, and
its own hash.

The integrity of the blockchain is maintained by:

• Hash verification: Recomputed hash must match stored hash

• Link verification: Previous hash must match the hash of the previous block

• Proof-of-Work (PoW): Ensures blocks follow difficulty rules

• Timestamp validation: Prevents invalid future blocks

If any block is modified, the hash changes and the chain becomes invalid.
[Link] Code
import hashlib

import time

class Block:

def __init__(self, index, data, previous_hash, timestamp=None, nonce=0):

[Link] = index

[Link] = timestamp if timestamp else [Link]()

[Link] = data

self.previous_hash = previous_hash

[Link] = nonce

[Link] = self.compute_hash()

def compute_hash(self):

content = f"{[Link]}{[Link]}{[Link]}{self.previous_hash}{[Link]}"

return hashlib.sha256([Link]()).hexdigest()

class Blockchain:

def __init__(self, difficulty=2):

[Link] = difficulty

[Link] = [self.create_genesis_block()]

def create_genesis_block(self):

genesis = Block(0, "Genesis Block", "0", timestamp=[Link]())

[Link] = genesis.compute_hash()

return genesis
def proof_of_work(self, block):

prefix = '0' * [Link]

while not [Link](prefix):

[Link] += 1

[Link] = block.compute_hash()

return [Link]

def add_block(self, data):

prev_block = [Link][-1]

new_block = Block(len([Link]), data, prev_block.hash)

self.proof_of_work(new_block)

[Link](new_block)

def verify_chain(self):

if len([Link]) == 0:

return False, "Blockchain is empty"

genesis = [Link][0]

if [Link] != genesis.compute_hash():

return False, "Genesis block hash mismatch"

if len([Link]) == 1:

return True, "Single block chain is valid"

for i in range(1, len([Link])):

current = [Link][i]

previous = [Link][i - 1]
if [Link] != current.compute_hash():

return False, f"Block {i}: Hash mismatch"

if current.previous_hash != [Link]:

return False, f"Block {i}: Previous hash mismatch"

if not [Link]('0' * [Link]):

return False, f"Block {i}: Invalid Proof-of-Work"

if [Link] > [Link]():

return False, f"Block {i}: Invalid timestamp"

return True, "Blockchain is valid"

[Link]

[Link]:
The blockchain verification function successfully validates the integrity of the chain by
checking hash consistency, block linkage, Proof-of-Work, and timestamps. Any
tampering in data or structure is detected, ensuring the reliability and security of the
blockchain system.

You might also like