Skip to content

Commit

Permalink
ADR-101: implement gRPC PruningService (cometbft#1154)
Browse files Browse the repository at this point in the history
* proto: Add proto files for the PruningService

* proto: generate *.pb.go files for PruningService

* config: add grpc.privileged incl. pruning_service

As described in ADR-101, add the node configuration section named
grpc.privileged to control the privileged server socket,
containing a pruning_service section for the pruning service.

* config: add pruning service config to the template

* grpc: package for privileged server

The privileged server optionally instantiated with the pruning service.

* node: add setup for the privileged gRPC server

* rpc: enable PruningService in test helper config

Also stop the makeAddrs function from growing grotesquely repetitive
and replace it with makeAddr returning a single, supposedly random,
local address string.

* grpc: privileged client with PruningService

Add client-side support for the privileged connection that features
an optionally enabled pruning service.

* e2e tests for PruningService

* config: refer to [storage.pruning] section in config.toml

Replace potentially confusing text in the comments on the pruning service configuration.

Co-authored-by: Thane Thomson <connect@thanethomson.com>

* grpc: tracing with error logs for PruningService

---------

Co-authored-by: Andy Nogueira <me@andynogueira.dev>
Co-authored-by: Thane Thomson <connect@thanethomson.com>
  • Loading branch information
3 people authored Aug 10, 2023
1 parent 04dfaf5 commit 69843be
Show file tree
Hide file tree
Showing 17 changed files with 2,241 additions and 55 deletions.
23 changes: 23 additions & 0 deletions .changelog/unreleased/features/1097-pruning-service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
- `[proto]` Add definitions and generated code for [ADR-101] `PruningService`
in the `tendermint.services.pruning.v1` proto package
([\#1097](https://github.com/cometbft/cometbft/issues/1097)).
- `[rpc/grpc]` Add privileged gRPC server and client facilities, in
`server/privileged` and `client/privileged` packages respectively, to
enable a separate API server within the node which serves trusted clients
without authentication and should never be exposed to public internet
([\#1097](https://github.com/cometbft/cometbft/issues/1097)).
- `[rpc/grpc]` Add a pruning service adding on the privileged gRPC
server API to give an [ADR-101] data companion control over block data
retained by the node. The `WithPruningService` option method in
`server/privileged` is provided to configure the pruning service
([\#1097](https://github.com/cometbft/cometbft/issues/1097)).
- `[rpc/grpc]` Add `PruningServiceClient` interface
for the gRPC client in `client/privileged` along with a configuration option
to enable it
([\#1097](https://github.com/cometbft/cometbft/issues/1097)).
- `[config]` Add `[grpc.privileged]` section to configure the privileged
gRPC server for the node, and `[grpc.privileged.pruning_service]` section
to control the pruning service
([\#1097](https://github.com/cometbft/cometbft/issues/1097)).

[ADR-101]: https://github.com/cometbft/cometbft/pull/82
51 changes: 51 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ type GRPCConfig struct {
// The gRPC block results service provides the block results for a given height
// If no height is provided, the block results of the latest height are returned
BlockResultsService *GRPCBlockResultsServiceConfig `mapstructure:"block_results_service"`

// The "privileged" section provides configuration for the gRPC server
// dedicated to privileged clients.
Privileged *GRPCPrivilegedConfig `mapstructure:"privileged"`
}

func DefaultGRPCConfig() *GRPCConfig {
Expand All @@ -540,6 +544,7 @@ func DefaultGRPCConfig() *GRPCConfig {
VersionService: DefaultGRPCVersionServiceConfig(),
BlockService: DefaultGRPCBlockServiceConfig(),
BlockResultsService: DefaultGRPCBlockResultsServiceConfig(),
Privileged: DefaultGRPCPrivilegedConfig(),
}
}

Expand All @@ -549,6 +554,7 @@ func TestGRPCConfig() *GRPCConfig {
VersionService: TestGRPCVersionServiceConfig(),
BlockService: TestGRPCBlockServiceConfig(),
BlockResultsService: DefaultGRPCBlockResultsServiceConfig(),
Privileged: TestGRPCPrivilegedConfig(),
}
}

Expand Down Expand Up @@ -607,6 +613,51 @@ func TestGRPCBlockServiceConfig() *GRPCBlockServiceConfig {
}
}

//-----------------------------------------------------------------------------
// GRPCPrivilegedConfig

// GRPCPrivilegedConfig defines the configuration for the CometBFT gRPC server
// exposing privileged endpoints.
type GRPCPrivilegedConfig struct {
// TCP or Unix socket address for the gRPC server for privileged clients
// to listen on. If empty, the privileged gRPC server will be disabled.
ListenAddress string `mapstructure:"laddr"`

// The gRPC pruning service provides control over the depth of block
// storage information that the node
PruningService *GRPCPruningServiceConfig `mapstructure:"pruning_service"`
}

func DefaultGRPCPrivilegedConfig() *GRPCPrivilegedConfig {
return &GRPCPrivilegedConfig{
ListenAddress: "",
PruningService: DefaultGRPCPruningServiceConfig(),
}
}

func TestGRPCPrivilegedConfig() *GRPCPrivilegedConfig {
return &GRPCPrivilegedConfig{
ListenAddress: "tcp://127.0.0.1:36671",
PruningService: TestGRPCPruningServiceConfig(),
}
}

type GRPCPruningServiceConfig struct {
Enabled bool `mapstructure:"enabled"`
}

func DefaultGRPCPruningServiceConfig() *GRPCPruningServiceConfig {
return &GRPCPruningServiceConfig{
Enabled: false,
}
}

func TestGRPCPruningServiceConfig() *GRPCPruningServiceConfig {
return &GRPCPruningServiceConfig{
Enabled: true,
}
}

//-----------------------------------------------------------------------------
// P2PConfig

Expand Down
21 changes: 21 additions & 0 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,27 @@ enabled = {{ .GRPC.BlockService.Enabled }}
[grpc.block_results_service]
enabled = {{ .GRPC.BlockResultsService.Enabled }}
#
# Configuration for privileged gRPC endpoints, which should **never** be exposed
# to the public internet.
#
[grpc.privileged]
# The host/port on which to expose privileged gRPC endpoints.
laddr = "{{ .GRPC.Privileged.ListenAddress }}"
#
# Configuration specifically for the gRPC pruning service, which is considered a
# privileged service.
#
[grpc.privileged.pruning_service]
# Only controls whether the pruning service is accessible via the gRPC API - not
# whether a previously set pruning service retain height is honored by the
# node. See the [storage.pruning] section for control over pruning.
#
# Disabled by default.
enabled = {{ .GRPC.Privileged.PruningService.Enabled }}
#######################################################
### P2P Configuration Options ###
#######################################################
Expand Down
20 changes: 20 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cometbft/cometbft/proxy"
rpccore "github.com/cometbft/cometbft/rpc/core"
grpcserver "github.com/cometbft/cometbft/rpc/grpc/server"
grpcprivserver "github.com/cometbft/cometbft/rpc/grpc/server/privileged"
rpcserver "github.com/cometbft/cometbft/rpc/jsonrpc/server"
sm "github.com/cometbft/cometbft/state"
"github.com/cometbft/cometbft/state/indexer"
Expand Down Expand Up @@ -672,6 +673,25 @@ func (n *Node) startRPC() ([]net.Listener, error) {
listeners = append(listeners, listener)
}

if n.config.GRPC.Privileged.ListenAddress != "" {
listener, err := grpcserver.Listen(n.config.GRPC.Privileged.ListenAddress)
if err != nil {
return nil, err
}
opts := []grpcprivserver.Option{
grpcprivserver.WithLogger(n.Logger),
}
if n.config.GRPC.Privileged.PruningService.Enabled {
opts = append(opts, grpcprivserver.WithPruningService(n.pruner, n.Logger))
}
go func() {
if err := grpcprivserver.Serve(listener, opts...); err != nil {
n.Logger.Error("Error starting privileged gRPC server", "err", err)
}
}()
listeners = append(listeners, listener)
}

return listeners, nil
}

Expand Down
Loading

0 comments on commit 69843be

Please sign in to comment.