mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package node
|
|
|
|
import (
|
|
"net"
|
|
"sync"
|
|
|
|
executionv1a1 "github.com/ethereum/go-ethereum/grpc/gen/astria/execution/v1alpha1"
|
|
executionv1a2 "github.com/ethereum/go-ethereum/grpc/gen/astria/execution/v1alpha2"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// GRPCServerHandler is the gRPC server handler.
|
|
// It gives us a way to attach the gRPC server to the node so it can be stopped on shutdown.
|
|
type GRPCServerHandler struct {
|
|
mu sync.Mutex
|
|
|
|
endpoint string
|
|
server *grpc.Server
|
|
executionServiceServerV1a1 *executionv1a1.ExecutionServiceServer
|
|
executionServiceServerV1a2 *executionv1a2.ExecutionServiceServer
|
|
}
|
|
|
|
// NewServer creates a new gRPC server.
|
|
// It registers the execution service server.
|
|
// It registers the gRPC server with the node so it can be stopped on shutdown.
|
|
func NewGRPCServerHandler(node *Node, execServiceV1a1 executionv1a1.ExecutionServiceServer, execServiceV1a2 executionv1a2.ExecutionServiceServer, cfg *Config) error {
|
|
server := grpc.NewServer()
|
|
|
|
log.Info("gRPC server enabled", "endpoint", cfg.GRPCEndpoint())
|
|
|
|
serverHandler := &GRPCServerHandler{
|
|
endpoint: cfg.GRPCEndpoint(),
|
|
server: server,
|
|
executionServiceServerV1a1: &execServiceV1a1,
|
|
executionServiceServerV1a2: &execServiceV1a2,
|
|
}
|
|
|
|
executionv1a1.RegisterExecutionServiceServer(server, execServiceV1a1)
|
|
executionv1a2.RegisterExecutionServiceServer(server, execServiceV1a2)
|
|
|
|
node.RegisterGRPCServer(serverHandler)
|
|
return nil
|
|
}
|
|
|
|
// Start starts the gRPC server if it is enabled.
|
|
func (handler *GRPCServerHandler) Start() error {
|
|
handler.mu.Lock()
|
|
defer handler.mu.Unlock()
|
|
|
|
if handler.endpoint == "" {
|
|
return nil
|
|
}
|
|
|
|
// Start the gRPC server
|
|
lis, err := net.Listen("tcp", handler.endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
go handler.server.Serve(lis)
|
|
log.Info("gRPC server started", "endpoint", handler.endpoint)
|
|
return nil
|
|
}
|
|
|
|
// Stop stops the gRPC server.
|
|
func (handler *GRPCServerHandler) Stop() error {
|
|
handler.mu.Lock()
|
|
defer handler.mu.Unlock()
|
|
|
|
handler.server.Stop()
|
|
log.Info("gRPC server stopped", "endpoint", handler.endpoint)
|
|
return nil
|
|
}
|