Polkadot (NPoS) Consensus Interview Questions
Polkadot consensus algorithm interview questions covering Nominated Proof-of-Stake (NPoS) with BABE and GRANDPA.
Q1: How does Polkadot (NPoS) consensus work?
Answer:
Polkadot uses Nominated Proof-of-Stake (NPoS) with BABE + GRANDPA.
Sequence Diagram:
Overall Flow Diagram:
Individual Node Decision Diagram:
Polkadot Consensus Components:
1. BABE (Blind Assignment for Blockchain Extension):
- Block Production: Creates blocks
- VRF Selection: Verifiable Random Function selects leaders
- Slots: Discrete time intervals
- Multiple Leaders: Can have multiple blocks per slot
2. GRANDPA (GHOST-based Recursive Ancestor Deriving Prefix Agreement):
- Finality Gadget: Finalizes blocks
- Not Block-by-Block: Finalizes chains, not individual blocks
- Fast Finality: Can finalize many blocks at once
- Safety: Requires
2/3+validator stake
3. Validator Set:
- Validators: Block producers and finalizers
- Nominators: Stake to validators
- Election: Phragmén algorithm selects validators
- Rotation: Validator set changes each era
4. Era Structure:
- Era: ~24 hours
- Session: ~1 hour
- Epoch: Multiple slots
Key Properties:
- Hybrid: BABE for liveness, GRANDPA for finality
- Fast Blocks: ~6 seconds
- Fast Finality: ~12-60 seconds
- Shared Security: All parachains share security
Example:
1// BABE block production
2fn produce_block(slot: Slot, parent: BlockHash) -> Option<Block> {
3 // VRF to determine if selected
4 let vrf_output = compute_vrf(slot, validator_key);
5 let threshold = calculate_threshold(validator_stake, total_stake);
6
7 if vrf_output < threshold {
8 // Selected as leader
9 let block = Block {
10 parent_hash: parent,
11 slot: slot,
12 extrinsics: select_extrinsics(),
13 state_root: compute_state_root(),
14 };
15
16 return Some(block);
17 }
18
19 None
20}
21
22// GRANDPA finalization
23fn finalize_chain(
24 validators: &[Validator],
25 chain: &[Block]
26) -> Option<BlockHash> {
27 // Validators vote on chain
28 let votes: Vec<Vote> = validators
29 .iter()
30 .map(|v| v.vote_on_chain(chain))
31 .collect();
32
33 // Check for 2/3+ majority
34 let mut vote_counts: HashMap<BlockHash, u64> = HashMap::new();
35 for vote in votes {
36 *vote_counts.entry(vote.block_hash).or_insert(0) += vote.stake;
37 }
38
39 let total_stake: u64 = validators.iter().map(|v| v.stake).sum();
40 let threshold = (total_stake * 2) / 3 + 1;
41
42 // Find block with 2/3+ votes
43 for (block_hash, votes) in vote_counts {
44 if votes >= threshold {
45 return Some(block_hash);
46 }
47 }
48
49 None
50}
Phragmén Election:
- Optimizes validator selection
- Maximizes minimum stake
- Ensures fair distribution
Use Cases:
- Polkadot
- Kusama
- Substrate-based chains
Related Snippets
- Bitcoin (Nakamoto) Consensus Interview Questions
Bitcoin consensus algorithm interview questions covering Proof-of-Work (PoW) and … - Byzantine Fault Tolerance (BFT) Consensus Interview Questions
Byzantine Fault Tolerance (BFT) consensus algorithm interview questions covering … - Cardano Interview Questions - Easy
Easy-level Cardano interview questions covering blockchain basics, Plutus, and … - Cardano Interview Questions - Hard
Hard-level Cardano interview questions covering advanced optimization and formal … - Cardano Interview Questions - Medium
Medium-level Cardano interview questions covering advanced Plutus development … - Consensus Algorithms Comparison Interview Questions
Consensus algorithm comparison and general implementation interview questions. … - Cosmos Chain Operations Interview Questions - Easy
Easy-level Cosmos chain operation interview questions covering chain operations, … - Cosmos Chain Operations Interview Questions - Hard
Hard-level Cosmos chain operation questions covering advanced algorithms, … - Cosmos Chain Operations Interview Questions - Medium
Medium-level Cosmos chain operation questions covering advanced chain … - Cosmos SDK Interview Questions - Easy
Easy-level Cosmos SDK interview questions covering chain code, SDK basics, and … - Cosmos SDK Interview Questions - Hard
Hard-level Cosmos SDK interview questions covering advanced SDK internals, … - Cosmos SDK Interview Questions - Medium
Medium-level Cosmos SDK interview questions covering advanced module … - Ethereum Proof-of-Stake Consensus Interview Questions
Ethereum Proof-of-Stake consensus algorithm interview questions covering Casper … - Ouroboros (Cardano) Consensus Interview Questions
Ouroboros consensus algorithm interview questions covering Cardano's … - Paxos Consensus Interview Questions
Paxos consensus algorithm interview questions covering the classic distributed … - Polkadot Interview Questions - Easy
Easy-level Polkadot interview questions covering blockchain basics, Substrate, … - Polkadot Interview Questions - Hard
Hard-level Polkadot interview questions covering advanced optimization and … - Polkadot Interview Questions - Medium
Medium-level Polkadot interview questions covering advanced Substrate … - Solana Interview Questions - Easy
Easy-level Solana interview questions covering blockchain basics, programs, and … - Solana Interview Questions - Hard
Hard-level Solana interview questions covering advanced optimization, security, … - Solana Interview Questions - Medium
Medium-level Solana interview questions covering advanced program development, … - Solana Proof of History Consensus Interview Questions
Solana consensus algorithm interview questions covering Proof of History (PoH) … - Tendermint Consensus Interview Questions
Tendermint consensus algorithm interview questions covering the Byzantine Fault … - Web3 Interview Questions - Easy
Easy-level Web3 interview questions covering blockchain fundamentals, Ethereum, … - Web3 Interview Questions - Hard
Hard-level Web3 interview questions covering MEV, zero-knowledge proofs, … - Web3 Interview Questions - Medium
Medium-level Web3 interview questions covering DeFi, advanced Solidity, …