A simple gRPC-based Bitcoin payment service implementation in Rust, demonstrating client-server communication using Protocol Buffers and Tonic.
This project implements a basic Bitcoin payment service using gRPC. It consists of:
- A gRPC server that handles payment requests
- A gRPC client that sends payment requests to the server
- Protocol Buffers definitions for the service and message types
grpc-impl/
├── proto/
│ └── payments.proto # Protocol Buffers definition
├── src/
│ ├── server.rs # gRPC server implementation
│ └── client.rs # gRPC client implementation
├── build.rs # Build script for compiling .proto files
├── Cargo.toml # Project dependencies
└── README.md # This file
Before running this project, ensure you have the following installed:
-
Rust (latest stable version)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
Protocol Buffers Compiler (
protoc)# macOS brew install protobuf # Ubuntu/Debian sudo apt install protobuf-compiler # Arch Linux sudo pacman -S protobuf
The project uses the following key dependencies:
- tonic (0.14.2) - gRPC framework for Rust
- tonic-prost (0.14.2) - Prost integration for Tonic
- prost (0.14.1) - Protocol Buffers implementation
- tokio (1.48.0) - Async runtime
- tonic-build (0.14.2) - Build-time code generation
- tonic-prost-build (0.14.2) - Build-time Prost integration
The proto/payments.proto file defines:
- Service:
Bitcoinwith aSendPaymentRPC method - Request Message:
BTCPaymentRequestcontaining sender address, receiver address, and amount - Response Message:
BTCPaymentResponsecontaining success status and message
service Bitcoin {
rpc SendPayment (BTCPaymentRequest) returns (BTCPaymentResponse);
}
message BTCPaymentRequest {
string from_addr = 1;
string to_addr = 2;
uint32 amount = 3;
}
message BTCPaymentResponse {
bool successful = 1;
string message = 2;
}The build.rs file compiles the .proto file at build time, generating Rust code for the service and message types:
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_prost_build::compile_protos("proto/payments.proto")?;
Ok(())
}Tonic generates the Rust code automatically at compile time
The src/server.rs implements the gRPC server:
- Includes generated code using
tonic::include_proto!("payments") - Implements the
Bitcointrait forBitcoinService - Handles
SendPaymentrequests and returns formatted responses - Listens on
[::1]:50051(localhost IPv6)
The src/client.rs implements the gRPC client:
- Connects to the server at
http://[::1]:50051 - Creates a payment request with sample data
- Sends the request and prints the response
To build the project:
cargo buildThis will:
- Run the build script to compile
.protofiles - Generate Rust code from Protocol Buffers
- Compile the server and client binaries
In one terminal, run:
cargo run --bin payments-serverThe server will start listening on [::1]:50051.
In another terminal, run:
cargo run --bin payments-clientYou should see output like:
Response: Response {
metadata: MetadataMap { headers: {"content-type": "application/grpc", ...} },
message: BtcPaymentResponse {
successful: true,
message: "Payment from 123456 to 654321 for 100 satoshis"
},
extensions: {}
}
-
Build Time:
build.rsruns and compilespayments.proto- Generated Rust code is placed in the build output directory
- Code is included via
tonic::include_proto!("payments")
-
Server Startup:
- Server creates a
BitcoinServiceinstance - Registers the service with Tonic's server builder
- Starts listening for incoming gRPC requests
- Server creates a
-
Client Request:
- Client connects to the server
- Creates a
BtcPaymentRequestwith payment details - Sends the request via the
send_paymentRPC method - Receives and prints the
BtcPaymentResponse
-
Server Processing:
- Server receives the request
- Extracts payment details
- Creates a response with a success message
- Returns the response to the client
Solution: Install Protocol Buffers compiler:
brew install protobuf # macOSSolution: Ensure tonic-prost is in your dependencies:
cargo add tonic-prostSolution: Make sure the server is running before starting the client.
To modify the service:
- Change the proto definition: Edit
proto/payments.proto - Update server logic: Modify
src/server.rsto implement new behavior - Update client requests: Modify
src/client.rsto send different data - Rebuild: Run
cargo buildto regenerate code from updated.protofiles
This is a learning project and is provided as-is for educational purposes.