Exp-2 200
Exp-2 200
Objective:
Implementation of PoS and PoW
CO Outcome
Describe the basic concepts of Blockchain and Distributed Ledger
CO1
Technology.
Apply cryptographic hash required for Blockchain.
CO2
Abstract:
Proof of Work:
Proof of Work (PoW) is a consensus mechanism used in blockchain technology to
secure and validate transactions. It's a method that ensures that a certain amount of
computational effort is expended to add a new block to the blockchain. The concept
was popularized by Bitcoin and serves as the foundation for its security and
decentralized nature.
In a PoW system, miners compete to solve complex mathematical puzzles. The first
miner to find a solution is allowed to add a new block of transactions to the blockchain
and is rewarded with newly minted cryptocurrency and transaction fees.
The key principles of Proof of Work include:
1. Mining Competition: Miners compete against each other to solve a difficult
cryptographic puzzle. The puzzle requires significant computational power and
effort to solve.
2. Consensus Mechanism: When a miner successfully solves the puzzle, they
broadcast their solution to the network. Other participants quickly verify the
solution, and if it's correct, the new block is added to the blockchain.
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 1
K. J. Somaiya College of Engineering, Mumbai-77
Proof of Stake:
Proof of Stake (PoS) is a consensus mechanism used in blockchain technology to
validate and add new transactions to the blockchain. Unlike Proof of Work (PoW),
which relies on computational power and competition, PoS selects validators based on
the amount of cryptocurrency they hold and are willing to "stake" as collateral.
In a PoS system, validators are chosen to create new blocks based on their stake in the
network. Validators are required to lock up a certain amount of cryptocurrency as
collateral, demonstrating their commitment to the network's security and integrity. The
higher the stake, the higher the chances of being selected to validate transactions and
create new blocks.
The key principles of Proof of Stake include:
1. Staking: Validators lock up a certain amount of cryptocurrency as a stake in a
smart contract. This stake serves as collateral and indicates their commitment to
the network's proper functioning.
2. Validator Selection: Validators are chosen to create new blocks based on a
combination of factors, such as the size of their stake and a randomization
process. This avoids the energy-intensive competition seen in PoW.
3. Validation and Reward: Validators validate transactions and create new blocks.
In return, they receive transaction fees and newly minted cryptocurrency as
rewards. Validators have a financial incentive to act honestly, as their stakes are
at risk.
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 2
K. J. Somaiya College of Engineering, Mumbai-77
class Block:
def __init__(self, data, prev_hash):
self.data = data
self.prev_hash = prev_hash
self.nonce = 0
self.hash = self.calculate_hash()
def calculate_hash(self):
sha = hashlib.sha256()
sha.update(
str(self.data).encode('utf-8') +
str(self.prev_hash).encode('utf-8') +
str(self.nonce).encode('utf-8')
)
return sha.hexdigest()
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 3
K. J. Somaiya College of Engineering, Mumbai-77
PoS:
import hashlib
import random
class Block:
def __init__(self, data, prev_hash):
self.data = data
self.prev_hash = prev_hash
self.validator = None
self.hash = self.calculate_hash()
def calculate_hash(self):
sha = hashlib.sha256()
sha.update(
str(self.data).encode('utf-8') +
str(self.prev_hash).encode('utf-8') +
str(self.validator).encode('utf-8')
)
return sha.hexdigest()
class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]
def create_genesis_block(self):
return Block("Genesis Block", "0")
blockchain = Blockchain()
genesis_block = blockchain.create_genesis_block()
blockchain.add_block(genesis_block)
blockchain.add_block(block1)
blockchain.add_block(block2)
blockchain.add_block(block3)
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 4
K. J. Somaiya College of Engineering, Mumbai-77
Screenshots:
PoW:
PoS:
Proof of Work:
Define a Block class with attributes: data, prev_hash, nonce, and hash.
Implement a calculate_hash method that computes the hash of the block's
data and previous hash.
Implement a mine_block method that simulates the PoW process by finding
a valid hash with a specified number of leading zeros.
Define a Blockchain class with a list of blocks and methods for creating the
genesis block and adding blocks to the chain.
Create a genesis_block and add it to the blockchain.
Create additional blocks (block1, block2, block3), link them with the
previous block's hash, and simulate mining them using the mine_block
method.
Print the hashes of the blocks.
Proof of Stake:
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 5
K. J. Somaiya College of Engineering, Mumbai-77
Define a Blockchain class with a list of blocks and methods for creating the
genesis block and adding validators and blocks to the chain.
Create a genesis_block and add it to the blockchain.
Add validators to the blockchain.
Create additional blocks (block1, block2, block3), link them with the
previous block's hash, and set a validator for each block.
Print the data, validator, and hash of each block.
Proof of Work:
The program simulates a basic blockchain using the PoW consensus mechanism.
The Block class represents a block with its attributes. The calculate_hash method
generates a hash based on block data, previous hash, and nonce. The mine_block
method simulates mining by finding a hash that matches the specified difficulty
level.
Proof of Stake:
The program demonstrates a basic blockchain using the PoS consensus mechanism.
The Block class represents a block with its attributes. The calculate_hash method
generates a hash based on block data, previous hash, and validator.
The Blockchain class maintains a chain of blocks and a list of validators. The
create_genesis_block method initializes the blockchain with a genesis block. The
add_block method adds new blocks to the chain, linking them with the previous
block's hash and assigning a validator.
By breaking down the logic into classes and methods, the code becomes modular
and easier to understand. This approach promotes code reusability and
maintainability, making it convenient to extend the functionality for more complex
implementations.
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 6
K. J. Somaiya College of Engineering, Mumbai-77
Conclusion:
Implemented both Proof of Work (PoW) and Proof of Stake (PoS) using python.
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 7