Defines gRPC services for peer-to-peer network communication in Sorcha. Use when: Creating service-to-service communication, implementing P2P protocols, defining proto contracts, configuring gRPC...
Sorcha uses gRPC for high-performance service-to-service communication, particularly for validator consensus, peer discovery, and wallet signing operations. The codebase follows .NET gRPC patterns with proto-first contract design.
// src/Services/Sorcha.*.Service/Protos/service_name.proto
syntax = "proto3";
package sorcha.servicename.v1;
option csharp_namespace = "Sorcha.ServiceName.Grpc.V1";
import "google/protobuf/empty.proto";
service MyService {
rpc GetStatus(google.protobuf.Empty) returns (StatusResponse);
rpc ProcessItem(ItemRequest) returns (ItemResponse);
}
// GrpcServices/MyGrpcService.cs
public class MyGrpcService : Protos.MyService.MyServiceBase
{
private readonly ILogger<MyGrpcService> _logger;
public override async Task<StatusResponse> GetStatus(
Empty request, ServerCallContext context)
{
context.CancellationToken.ThrowIfCancellationRequested();
return new StatusResponse { IsHealthy = true };
}
}
// Program.cs
builder.Services.AddGrpc(options => options.EnableDetailedErrors = true);
var app = builder.Build();
app.MapGrpcService<MyGrpcService>();
| Concept | Usage | Example |
|---|---|---|
| Proto-first | Define contracts in .proto files |
Protos/wallet_service.proto |
| Service base | Inherit generated base class | : WalletService.WalletServiceBase |
| ServerCallContext | Access metadata, cancellation, deadlines | context.CancellationToken |
| GrpcChannel | Reusable client connection | GrpcChannel.ForAddress(endpoint) |
| Streaming | Server/client/bidirectional streams | returns (stream Entry) |
Services/Sorcha.*.Service/
โโโ Protos/ # Proto contract files
โ โโโ service.proto
โโโ GrpcServices/ # Service implementations
โ โโโ MyGrpcService.cs
โโโ Program.cs # gRPC registration
When: Creating long-lived connections between services
builder.Services.AddSingleton(sp =>
{
var config = sp.GetRequiredService<ServiceConfiguration>();
return GrpcChannel.ForAddress(config.Endpoint, new GrpcChannelOptions
{
HttpHandler = new SocketsHttpHandler
{
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(5),
KeepAlivePingDelay = TimeSpan.FromSeconds(60),
KeepAlivePingTimeout = TimeSpan.FromSeconds(30),
EnableMultipleHttp2Connections = true
}
});
});
When: Separating gRPC (HTTP/2) from REST/health endpoints
builder.WebHost.ConfigureKestrel(options =>
{
// REST + health checks
options.ListenAnyIP(8080, o => o.Protocols = HttpProtocols.Http1AndHttp2);
// gRPC only
options.ListenAnyIP(5000, o => o.Protocols = HttpProtocols.Http2);
});
Fetch latest gRPC documentation with Context7.
How to use Context7:
mcp__context7__resolve-library-id to search for "grpc dotnet"/websites/) when availablemcp__context7__query-docs using the resolved library IDRecommended Queries: