0% found this document useful (0 votes)
5 views3 pages

Blockchain Lab Manual

The lab manual aims to simulate and compare Proof of Work (PoW) and Proof of Stake (PoS) consensus mechanisms using Python, focusing on their applications in securing IoT. PoW is energy-intensive and less suitable for IoT, while PoS is energy-efficient and preferred for secure IoT applications. The manual includes Python code for both mechanisms and concludes that PoS is more effective for scalable IoT solutions.

Uploaded by

Sundari YBT
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)
5 views3 pages

Blockchain Lab Manual

The lab manual aims to simulate and compare Proof of Work (PoW) and Proof of Stake (PoS) consensus mechanisms using Python, focusing on their applications in securing IoT. PoW is energy-intensive and less suitable for IoT, while PoS is energy-efficient and preferred for secure IoT applications. The manual includes Python code for both mechanisms and concludes that PoS is more effective for scalable IoT solutions.

Uploaded by

Sundari YBT
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
You are on page 1/ 3

Lab Manual: Blockchain Consensus Mechanisms for IoT

Aim:

To simulate and compare the working of Proof of Work (PoW) and Proof of Stake (PoS) consensus

mechanisms using Python in Anaconda, and understand their applications in blockchain for IoT

security.

Apparatus / Software Requirements:

- Anaconda Navigator / Jupyter Notebook

- Python 3.x

- Python libraries: hashlib, random, time

Theory:

Blockchain is a decentralized ledger technology used in cryptocurrencies and IoT for secure and

tamper-proof transactions.

Proof of Work (PoW): A consensus mechanism where nodes (miners) solve complex mathematical

problems to validate transactions. Used in Bitcoin. It is energy-intensive.

Proof of Stake (PoS): Validators are chosen based on the number of coins they hold and are willing

to stake. It's energy-efficient and suitable for IoT environments.

IoT Relevance: Blockchain ensures secure communication between IoT devices. PoS is more

suitable for IoT due to less power consumption.

Python Code:
import hashlib
import time
import random

def proof_of_work(difficulty):
prefix = '0' * difficulty
nonce = 0
start_time = time.time()
while True:
data = "IoT Data Block" + str(nonce)
hash_result = hashlib.sha256(data.encode()).hexdigest()
if hash_result.startswith(prefix):
break
nonce += 1
end_time = time.time()
print(f"Nonce: {nonce}, Hash: {hash_result}, Time: {end_time - start_time:.4f} sec")

def proof_of_stake(validators):
total_stake = sum(validators.values())
pick = random.uniform(0, total_stake)
current = 0
for validator, stake in validators.items():
current += stake
if current > pick:
print(f"Selected: {validator} with Stake: {stake}")
break

print("Blockchain Consensus Mechanisms for IoT")


proof_of_work(difficulty=4)
validators = {'Node_A': 10, 'Node_B': 30, 'Node_C': 60}
proof_of_stake(validators)

Expected Output:

** Blockchain Consensus Mechanisms for IoT **

--- Proof of Work Simulation ---

Block mined successfully!

Nonce: [number]

Hash: [value]

Time taken: [seconds]

--- Proof of Stake Simulation ---

Validator selected: Node_C (or any based on stake)

Conclusion:

PoW consumes more time and power, making it less suitable for IoT.

PoS selects validators based on stake, ensuring energy efficiency.


PoS is preferred for secure and scalable IoT applications.

Viva Questions:

1. What is the role of consensus algorithms in blockchain?

2. Explain the difference between Proof of Work and Proof of Stake.

3. Why is PoS more suitable for IoT applications?

4. What is a nonce in PoW?

5. How does the hash function work in PoW?

6. What are validators in PoS?

7. Can blockchain be integrated with IoT? How?

8. What is the drawback of using PoW in low-power devices?

You might also like