github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/abci/server/grpc_server.go (about)

     1  package server
     2  
     3  import (
     4  	"net"
     5  
     6  	"google.golang.org/grpc"
     7  
     8  	"github.com/tendermint/tendermint/abci/types"
     9  	tmnet "github.com/tendermint/tendermint/libs/net"
    10  	"github.com/tendermint/tendermint/libs/service"
    11  )
    12  
    13  type GRPCServer struct {
    14  	service.BaseService
    15  
    16  	proto    string
    17  	addr     string
    18  	listener net.Listener
    19  	server   *grpc.Server
    20  
    21  	app types.ABCIApplicationServer
    22  }
    23  
    24  // NewGRPCServer returns a new gRPC ABCI server
    25  func NewGRPCServer(protoAddr string, app types.ABCIApplicationServer) service.Service {
    26  	proto, addr := tmnet.ProtocolAndAddress(protoAddr)
    27  	s := &GRPCServer{
    28  		proto:    proto,
    29  		addr:     addr,
    30  		listener: nil,
    31  		app:      app,
    32  	}
    33  	s.BaseService = *service.NewBaseService(nil, "ABCIServer", s)
    34  	return s
    35  }
    36  
    37  // OnStart starts the gRPC service.
    38  func (s *GRPCServer) OnStart() error {
    39  	ln, err := net.Listen(s.proto, s.addr)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	s.listener = ln
    45  	s.server = grpc.NewServer()
    46  	types.RegisterABCIApplicationServer(s.server, s.app)
    47  
    48  	s.Logger.Info("Listening", "proto", s.proto, "addr", s.addr)
    49  	go s.server.Serve(s.listener)
    50  
    51  	return nil
    52  }
    53  
    54  // OnStop stops the gRPC server.
    55  func (s *GRPCServer) OnStop() {
    56  	s.server.Stop()
    57  }