Skip to content

Latest commit

 

History

History
312 lines (244 loc) · 9.53 KB

File metadata and controls

312 lines (244 loc) · 9.53 KB

⭐ Stellar Consensus Protocol (SCP) - Complete Guide

🌟 What is Stellar Consensus Protocol?

The Stellar Consensus Protocol (SCP) is a consensus mechanism that uses Federated Byzantine Agreement (FBA). Unlike traditional BFT which requires a fixed threshold (like 2/3), SCP allows each node to define its own quorum slice - a set of nodes it trusts. This creates flexible trust relationships and makes the network more adaptable.

Key Innovation: Flexible Trust

In traditional BFT:

  • All nodes must agree on the same threshold (e.g., 2/3)
  • Network size is fixed
  • Trust relationships are symmetric

In SCP:

  • Each node defines its own quorum slice
  • Network can grow organically
  • Trust relationships can be asymmetric (Node A trusts B, but B might not trust A)

🔄 How SCP Consensus Works

Core Concepts

1. Quorum Slice

A quorum slice is a set of nodes that a particular node trusts. Each node defines its own quorum slice. For consensus to be reached, enough nodes in each quorum slice must agree.

Example:

  • Node A's quorum slice: {A, B, C}
  • Node B's quorum slice: {B, C, D}
  • Node C's quorum slice: {C, A, D}

2. Quorum

A quorum is a set of nodes that includes a quorum slice for each of its members. This ensures that every node in the quorum can reach consensus.

3. Quorum Intersection

For safety, quorums must intersect. This prevents conflicting decisions.

Consensus Process

SCP uses a two-phase process:

Phase 1: Nomination

A node nominates a block for consideration:

🔄 SCP-Node-6001 NOMINATING NEW BLOCK (SCP Nomination Phase):
   Data: "My first transaction"

Phase 2: Ballot (Voting)

Nodes vote on the nominated block. Consensus is reached when a quorum is formed:

🗳️  BALLOT PHASE:
   Voted: ACCEPT for block 00a7f3d2e1b5c8f9...
   Current votes: 2/2 (need majority of quorum slice)

Simplified Implementation

In this educational implementation, we simplify SCP:

  • Each node's quorum slice includes all connected nodes + itself
  • Consensus requires a majority (50%) of the quorum slice
  • Real SCP has more complex quorum intersection rules

🔒 Key Differences from BFT

Feature BFT SCP
Threshold Fixed 2/3 of all nodes Flexible (based on quorum slices)
Trust Symmetric (all nodes equal) Asymmetric (each node defines trust)
Network Growth Requires reconfiguration Can grow organically
Quorum Fixed set Dynamic based on slices
Safety 2/3 threshold Quorum intersection

🔄 Consensus Process in This Engine

Step 1: NOMINATE Phase

A node nominates a new block:

🔄 SCP-Node-6001 NOMINATING NEW BLOCK (SCP Nomination Phase):
   Data: "My first transaction"

Step 2: MINE Phase

The block is mined (same as BFT):

⛏️  MINING PHASE:
🔨 Mining block 1...
📊 Target: Hash must start with 00
✅ Block mined! Nonce: 23847, Hash: 00a7f3d2e1b5c8f9...

Step 3: NOMINATION Broadcast

The nomination is broadcast to peers:

📡 NOMINATION PHASE:
   Nomination broadcast to 2 peers
   Waiting for quorum slice to accept nomination...

Step 4: BALLOT Phase

Nodes vote (ballot) on the nomination:

📨 NOMINATION RECEIVED:
   From: SCP-Node-6002
   Block: 00a7f3d2e1b5c8f9...

🗳️  BALLOT PHASE:
   Voted: ACCEPT for block 00a7f3d2e1b5c8f9...
   Current votes: 1/2 (need majority of quorum slice)

Step 5: QUORUM Reached

When enough votes are collected:

🗳️  BALLOT RECEIVED:
   From: SCP-Node-6003
   Vote: ACCEPT
   Current votes: 2/2

🎉 QUORUM REACHED! (SCP Consensus):
   Block 1 has been accepted by the network!
   Final votes: 2 out of 3 quorum slice nodes

⚡ Key Features of SCP

1. Flexible Trust Relationships

  • Each node defines who it trusts
  • No need for global agreement on trust
  • Network can grow without reconfiguration

2. Asymmetric Trust

  • Node A can trust Node B, but B might not trust A
  • More realistic trust model
  • Reflects real-world relationships

3. Organic Network Growth

  • New nodes can join by being trusted by existing nodes
  • No need to reconfigure the entire network
  • Scales better than fixed-threshold BFT

4. Quorum-Based Safety

  • Safety comes from quorum intersection
  • More flexible than fixed thresholds
  • Can handle heterogeneous networks

🎓 Learning with the Terminal Application

How This Application Helps You Learn SCP

1. Visual Quorum Formation

Watch quorum slices form:

  • See which nodes each node trusts
  • Observe how quorums are built
  • Understand quorum intersection

2. Nomination and Ballot Phases

Experience the two-phase process:

  • Nomination: Proposing a block
  • Ballot: Voting on nominations
  • See how consensus emerges

3. Flexible Trust in Action

Experiment with different trust configurations:

  • Connect nodes in different patterns
  • Observe how quorum slices change
  • See how consensus adapts

4. Compare with BFT

Run the same scenario with both protocols:

  • Notice the different thresholds
  • See how SCP is more flexible
  • Understand when to use each

Practical Learning Scenarios

Scenario 1: Understanding Quorum Slices

  1. Start 3 nodes (ports 6001, 6002, 6003)
  2. Connect them: 6002 → 6001, 6003 → 6001
  3. Check status on each node
  4. Observe: Each node's quorum slice includes connected nodes
  5. Notice: Quorum slices can be different for each node

Scenario 2: Nomination and Ballot

  1. Start multiple nodes and connect them
  2. Propose a block (creates nomination)
  3. Watch other nodes receive the nomination
  4. See them create ballots (votes)
  5. Observe when quorum is reached

Scenario 3: Flexible Threshold

  1. Start 4 nodes
  2. Connect them in a star pattern (all to one central node)
  3. Propose a block
  4. Notice: Consensus requires majority of each quorum slice
  5. Compare: This is different from BFT's fixed 2/3

Scenario 4: Network Growth

  1. Start with 2 nodes
  2. Add a third node
  3. Observe how quorum slices update
  4. See how the network adapts without reconfiguration
  5. Compare: BFT would need threshold recalculation

🔍 Technical Implementation Details

Quorum Slice Management

// Simplified: quorum slice = self + all connected peers
this.quorumSlice = new Set([this.nodeId]);

// When peer connects
this.quorumSlice.add(`SCP-Node-${peerPort}`);

Nomination Handling

// Receive nomination
handleNomination(message) {
  // Validate block
  // Create ballot (vote)
  voteOnBlock(blockHash, 'ACCEPT');
}

Quorum Check

// Simplified: need majority of quorum slice
const requiredVotes = Math.ceil(this.quorumSlice.size * 0.5);
return ballot.votes.size >= requiredVotes;

Message Types

  • NOMINATION: A node nominates a block
  • BALLOT: A node votes on a nomination

📊 Real-World SCP

Stellar Network

The Stellar blockchain uses SCP:

  • Validators: Nodes that participate in consensus
  • Quorum Sets: Each validator defines its quorum set
  • Safety: Quorum intersection ensures safety
  • Liveness: Progress when honest validators can form quorums

Key Advantages in Production

  1. Decentralization: No single point of control
  2. Flexibility: Validators choose who to trust
  3. Performance: Fast consensus (3-5 seconds)
  4. Low Energy: No proof-of-work required

🎯 Comparison with BFT

When to Use BFT

  • Fixed network size
  • All nodes are known and trusted equally
  • Need simple, predictable threshold
  • Permissioned networks

When to Use SCP

  • Dynamic network growth
  • Asymmetric trust relationships
  • Need flexible trust model
  • Public or permissionless networks
  • Heterogeneous node capabilities

🧪 Exercises for Students

  1. Quorum Slice Design: Design quorum slices for a 5-node network
  2. Quorum Intersection: Verify that quorums intersect for safety
  3. Network Growth: Simulate adding nodes and observe quorum changes
  4. Trust Analysis: Compare symmetric vs asymmetric trust
  5. Consensus Time: Measure consensus time vs network size
  6. Failure Tolerance: Test how many nodes can fail in different configurations

🔬 Advanced Concepts

Quorum Intersection Theorem

For safety, any two quorums must intersect. This ensures:

  • No two quorums can make conflicting decisions
  • At least one honest node is in the intersection
  • Safety is maintained

Federated Voting

SCP uses federated voting where:

  • Nodes vote based on their quorum slices
  • Votes are weighted by trust relationships
  • Consensus emerges from quorum formation

Safety and Liveness

  • Safety: Quorum intersection prevents conflicts
  • Liveness: Progress when honest nodes can form quorums
  • Trade-off: More flexible trust = more complex safety analysis

📚 Further Reading

🎓 Key Takeaways

  1. SCP uses flexible trust - Each node defines its quorum slice
  2. Two-phase process - Nomination then Ballot
  3. Quorum-based consensus - Consensus requires quorum formation
  4. More flexible than BFT - Can handle asymmetric trust
  5. Real-world application - Used by Stellar blockchain

Ready to try SCP? Run node index.js and select option 2 for Stellar Consensus Protocol!