Skip to content

A sample implementation of the Practical Byzantine Fault Tolerance (PBFT) consensus algorithm in Rust. πŸ¦€

License

Notifications You must be signed in to change notification settings

0xjeffro/pbft-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

pbft-rust πŸ¦€

This project provides a Rust-based implementation of the Practical Byzantine Fault Tolerance (PBFT) consensus algorithm, inspired by the original paper by Miguel Castro and Barbara Liskov, β€œPractical Byzantine Fault Tolerance”.

The implementation simulates a distributed environment with multiple threads representing both clients and consensus nodes (servers). Clients initiate transaction requests to the consensus nodes, which then reach agreement using the PBFT algorithm and return the result to the client.

Key assumptions in this implementation:

  • The client listens on port 9000 by default.
  • Consensus nodes listen on ports starting from 8000 and increment by one for each additional node.
  • Faulty nodes are modeled by not responding to any requests during the consensus process, simulating a node failure.

Project Structure:

src/
β”œβ”€β”€ main.rs              # Main entry point
β”œβ”€β”€ lib.rs               # Library module
β”œβ”€β”€ consensus/           # Consensus-related code
β”‚   β”œβ”€β”€ message.rs       # Message structures
β”‚   β”œβ”€β”€ pbft.rs          # Stage definitions
β”œβ”€β”€ network/             # Networking code
β”‚   β”œβ”€β”€ client.rs        # Client logic
β”‚   β”œβ”€β”€ node.rs          # Consensus node logic
β”‚   β”œβ”€β”€ server.rs        # Server-related code
β”œβ”€β”€ β”œβ”€β”€ utils.rs         # Utility functions

Getting Started

1. Clone the repository.

git clone https://github.com/0xjeffro/pbft-rust.git
cd pbft-rust

2. Run the following command to start the server.

cargo run -- -n <num_nodes> -f <num_faulty_nodes>

Replace <num_nodes> with the total number of nodes in the network and <num_faulty_nodes> with the number of faulty nodes.

3. Send requests to the client. In a separate terminal, use the following command to send a request:

curl -H "Content-Type: application/json" -X POST -d '{"client_id":0, "operation":"BTC to da moon!", "time_stamp":1726496460,"sequence_id":8}' http://localhost:9000/req

Replace client_id, operation, time_stamp, and sequence_id with the appropriate values as needed for your request.

Log Output

During execution, logs are output to the console. To make it easier to understand the state and behavior of the nodes, we use emojis to represent different node types and stages of the consensus process:

  • πŸ’»: Represents a Client
  • πŸ˜ƒ: Represents a healthy node
  • 😈: Represents a faulty node
  • 🌟: Indicates transition to the PrePrepare stage
  • 🌟🌟: Indicates transition to the Prepare stage
  • 🌟🌟🌟: Indicates transition to the Commit stage
  • βœ…: Indicates the client has received f+1 identical replies, and consensus has been reached

Examples

Successful Consensus

cargo run -- -n 7 -f 2
curl -H "Content-Type: application/json" -X POST -d '{"client_id":0, "operation":"BTC to da moon!", "time_stamp":1726496460,"sequence_id":8}' http://localhost:9000/req

img.png

Failed to Reach Consensus

cargo run -- -n 4 -f 2
curl -H "Content-Type: application/json" -X POST -d '{"client_id":0, "operation":"BTC to da moon!", "time_stamp":1726496460,"sequence_id":8}' http://localhost:9000/req

img_1.png

About

A sample implementation of the Practical Byzantine Fault Tolerance (PBFT) consensus algorithm in Rust. πŸ¦€

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages