Blockchains rely on consensus mechanisms to agree on transactions. These protocols help networks resist fraud and manipulation.
In this article, we’ll explore three popular algorithms: Proof of Work (PoW), Proof of Stake (PoS), and Delegated Proof of Stake (DPoS). By the end, you’ll understand how each solves unique problems and how they work under the hood.
Proof of Work(PoW)
Key Concepts
Miner: Node that competes to validate blocks.
Hash puzzle: A cryptographic challenge requiring trial‑and‑error.
Difficulty: Level of puzzle complexity.
Block reward: Incentive paid to the winning miner.
How It Works
- A miner gathers pending transactions into a block.
- The miner repeatedly hashes the block with a nonce until the hash meets the target.
- Once found, the miner broadcasts the block to the network.
- Other nodes verify the hash and transactions.
- Upon validation, the block is appended to the chain.
`// Simplified PoW block verification in Go
func VerifyPoW(header BlockHeader) bool {
// Compute block hash
h := header.Hash()
// Accept if hash is below target
return h.Cmp(header.Target) == -1
}`
Proof of Stake (PoS)
Problem
PoS addresses PoW’s high energy use and hardware centralization risks.
Key Concepts
Validator: Node staking funds to propose blocks.
Stake: Amount of cryptocurrency locked as collateral.
Slashing: Penalty for malicious behavior.
Finality: Assurance that a block cannot be reverted.
How It Works
- Validators lock up stake to participate.
- The protocol selects a validator based on stake weight.
- The chosen validator proposes a new block.
- Other validators vote on the proposal.
- If at least two‑thirds vote yes, the block is finalized.
// Simplified PoS voting in Go
func CastVote(v Validator, blk Block) (Vote, error) {
// Ensure block is valid
if err := ValidateBlock(blk); err != nil {
return Vote{}, err
}
// Sign block hash with validator key
sig := v.Sign(blk.Hash())
return Vote{BlkHash: blk.Hash(), PubKey: v.PubKey, Sig: sig}, nil
}
Delegated Proof of Stake (DPoS)
Problem
DPoS improves scalability and on‑chain governance.
Key Concepts
Delegate: Elected validator with block‑producing rights.
Voting power: Influence based on stake by token holders.
Round: A cycle in which each delegate produces one block.
Revocation: Removal of misbehaving delegates.
How It Works
- Stakeholders vote to elect a fixed set of delegates.
- Delegates take turns producing blocks in a round‑robin.
- Each delegate verifies the previous delegate’s block.
- Misbehaving delegates can be voted out.
- Elections repeat at set intervals.
// Round‑robin delegate selection in JavaScript
function getNextDelegate(delegates, round) {
// Cycle through elected delegates
return delegates[round % delegates.length];
}
Summary
PoW: Secure via computational puzzles; energy‑intensive.
PoS: Secures with stake; energy‑efficient.
DPoS: Secures via elected delegates; highly scalable.