-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/worker/consensusrpc: Add public consensus RPC services worker
A public consensus services worker enables any full consensus node to expose light client services to other nodes that may need them (e.g., they are needed to support light clients). The worker can be enabled using --worker.consensusrpc.enabled and is disabled by default. Enabling the public consensus services worker exposes the light consensus client interface over publicly accessible gRPC.
- Loading branch information
Showing
10 changed files
with
173 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
go/worker/consensusrpc: Add public consensus RPC services worker | ||
|
||
A public consensus services worker enables any full consensus node to expose | ||
light client services to other nodes that may need them (e.g., they are needed | ||
to support light clients). | ||
|
||
The worker can be enabled using `--worker.consensusrpc.enabled` and is | ||
disabled by default. Enabling the public consensus services worker exposes | ||
the light consensus client interface over publicly accessible gRPC. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go/common/node: Add RoleConsensusRPC role bit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Package consensus implements publicly accessible consensus services. | ||
package consensus | ||
|
||
import ( | ||
"fmt" | ||
|
||
flag "github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/oasislabs/oasis-core/go/common/logging" | ||
"github.com/oasislabs/oasis-core/go/common/node" | ||
consensus "github.com/oasislabs/oasis-core/go/consensus/api" | ||
workerCommon "github.com/oasislabs/oasis-core/go/worker/common" | ||
"github.com/oasislabs/oasis-core/go/worker/registration" | ||
) | ||
|
||
const ( | ||
// CfgWorkerEnabled enables the consensus RPC services worker. | ||
CfgWorkerEnabled = "worker.consensusrpc.enabled" | ||
) | ||
|
||
// Flags has the configuration flags. | ||
var Flags = flag.NewFlagSet("", flag.ContinueOnError) | ||
|
||
// Worker is a worker providing publicly accessible consensus services. | ||
// | ||
// Currently this only exposes the consensus light client service. | ||
type Worker struct { | ||
enabled bool | ||
|
||
commonWorker *workerCommon.Worker | ||
|
||
quitCh chan struct{} | ||
|
||
logger *logging.Logger | ||
} | ||
|
||
// Name returns the service name. | ||
func (w *Worker) Name() string { | ||
return "public consensus RPC services worker" | ||
} | ||
|
||
// Enabled returns if worker is enabled. | ||
func (w *Worker) Enabled() bool { | ||
return w.enabled | ||
} | ||
|
||
// Start starts the worker. | ||
func (w *Worker) Start() error { | ||
if w.enabled { | ||
w.logger.Info("starting public consensus RPC services worker") | ||
} | ||
return nil | ||
} | ||
|
||
// Stop halts the service. | ||
func (w *Worker) Stop() { | ||
close(w.quitCh) | ||
} | ||
|
||
// Quit returns a channel that will be closed when the service terminates. | ||
func (w *Worker) Quit() <-chan struct{} { | ||
return w.quitCh | ||
} | ||
|
||
// Cleanup performs the service specific post-termination cleanup. | ||
func (w *Worker) Cleanup() { | ||
} | ||
|
||
// New creates a new public consensus services worker. | ||
func New(commonWorker *workerCommon.Worker, registration *registration.Worker) (*Worker, error) { | ||
w := &Worker{ | ||
enabled: Enabled(), | ||
commonWorker: commonWorker, | ||
quitCh: make(chan struct{}), | ||
logger: logging.GetLogger("worker/consensusrpc"), | ||
} | ||
|
||
if w.enabled { | ||
// Register the consensus light client service. | ||
consensus.RegisterLightService(commonWorker.Grpc.Server(), commonWorker.Consensus) | ||
|
||
// Publish our role to ease discovery for clients. | ||
rp, err := registration.NewRoleProvider(node.RoleConsensusRPC) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create role provider: %w", err) | ||
} | ||
|
||
// The consensus RPC service is available immediately. | ||
rp.SetAvailable(func(*node.Node) error { return nil }) | ||
} | ||
|
||
return w, nil | ||
} | ||
|
||
// Enabled reads our enabled flag from viper. | ||
func Enabled() bool { | ||
return viper.GetBool(CfgWorkerEnabled) | ||
} | ||
|
||
func init() { | ||
Flags.Bool(CfgWorkerEnabled, false, "Enable public consensus RPC services worker") | ||
|
||
_ = viper.BindPFlags(Flags) | ||
} |