0
0
Blockchain / Solidityprogramming~7 mins

Proof of Work vs Proof of Stake in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Proof of Work and Proof of Stake are two ways blockchains decide who adds the next block. They help keep the network safe and fair.

When you want to secure a blockchain network from cheating or attacks.
When deciding how new blocks are added to a blockchain.
When choosing a method to reward participants who help maintain the blockchain.
When designing a system that needs to be decentralized and trustless.
When comparing energy use and speed of blockchain consensus methods.
Syntax
Blockchain / Solidity
Proof of Work (PoW):
- Miners solve a hard puzzle by trying many guesses.
- First to solve adds the block and gets a reward.

Proof of Stake (PoS):
- Validators are chosen based on how many coins they hold and lock up.
- Chosen validator adds the block and earns fees or rewards.

PoW uses computing power to secure the network.

PoS uses ownership of coins to secure the network.

Examples
This shows how miners compete by guessing many times.
Blockchain / Solidity
PoW example:
Miner tries many numbers to find a hash starting with zeros.
First miner to find it adds the block.
This shows how stake size affects chances to add blocks.
Blockchain / Solidity
PoS example:
Validator locks 100 coins.
Network randomly picks validator weighted by locked coins.
Validator adds the block.
Sample Program

This program shows a simple Proof of Work mining by finding a nonce that makes a hash start with zeros (simulated by checking the hash prefix). It also shows Proof of Stake by randomly picking a validator weighted by their stake.

Blockchain / Solidity
import random
import hashlib

class ProofOfWork:
    def mine(self, difficulty):
        nonce = 0
        target = '0' * difficulty
        while True:
            guess = f"blockdata{nonce}"
            guess_hash = hashlib.sha256(guess.encode()).hexdigest()
            if guess_hash.startswith(target):
                return nonce
            nonce += 1

class ProofOfStake:
    def __init__(self, stakes):
        self.stakes = stakes  # dict of validator: stake

    def choose_validator(self):
        total_stake = sum(self.stakes.values())
        pick = random.uniform(0, total_stake)
        current = 0
        for validator, stake in self.stakes.items():
            current += stake
            if current >= pick:
                return validator

# Simulate PoW mining
pow_system = ProofOfWork()
nonce_found = pow_system.mine(difficulty=3)  # small difficulty for demo

# Simulate PoS validator selection
stakes = {"Alice": 50, "Bob": 30, "Carol": 20}
pos_system = ProofOfStake(stakes)
chosen_validator = pos_system.choose_validator()

print(f"PoW nonce found: {nonce_found}")
print(f"PoS chosen validator: {chosen_validator}")
OutputSuccess
Important Notes

Proof of Work uses a lot of electricity because of many guesses.

Proof of Stake is faster and uses less energy but needs trust in stake holders.

Both methods aim to keep the blockchain secure and fair.

Summary

Proof of Work uses computing power to find a solution first.

Proof of Stake chooses validators based on coin ownership.

Both help blockchains agree on the next block safely.