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