github.com/DFWallet/tendermint-cosmos@v0.0.2/privval/signer_server.go (about)

     1  package privval
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/DFWallet/tendermint-cosmos/libs/service"
     7  	tmsync "github.com/DFWallet/tendermint-cosmos/libs/sync"
     8  	privvalproto "github.com/DFWallet/tendermint-cosmos/proto/tendermint/privval"
     9  	"github.com/DFWallet/tendermint-cosmos/types"
    10  )
    11  
    12  // ValidationRequestHandlerFunc handles different remoteSigner requests
    13  type ValidationRequestHandlerFunc func(
    14  	privVal types.PrivValidator,
    15  	requestMessage privvalproto.Message,
    16  	chainID string) (privvalproto.Message, error)
    17  
    18  type SignerServer struct {
    19  	service.BaseService
    20  
    21  	endpoint *SignerDialerEndpoint
    22  	chainID  string
    23  	privVal  types.PrivValidator
    24  
    25  	handlerMtx               tmsync.Mutex
    26  	validationRequestHandler ValidationRequestHandlerFunc
    27  }
    28  
    29  func NewSignerServer(endpoint *SignerDialerEndpoint, chainID string, privVal types.PrivValidator) *SignerServer {
    30  	ss := &SignerServer{
    31  		endpoint:                 endpoint,
    32  		chainID:                  chainID,
    33  		privVal:                  privVal,
    34  		validationRequestHandler: DefaultValidationRequestHandler,
    35  	}
    36  
    37  	ss.BaseService = *service.NewBaseService(endpoint.Logger, "SignerServer", ss)
    38  
    39  	return ss
    40  }
    41  
    42  // OnStart implements service.Service.
    43  func (ss *SignerServer) OnStart() error {
    44  	go ss.serviceLoop()
    45  	return nil
    46  }
    47  
    48  // OnStop implements service.Service.
    49  func (ss *SignerServer) OnStop() {
    50  	ss.endpoint.Logger.Debug("SignerServer: OnStop calling Close")
    51  	_ = ss.endpoint.Close()
    52  }
    53  
    54  // SetRequestHandler override the default function that is used to service requests
    55  func (ss *SignerServer) SetRequestHandler(validationRequestHandler ValidationRequestHandlerFunc) {
    56  	ss.handlerMtx.Lock()
    57  	defer ss.handlerMtx.Unlock()
    58  	ss.validationRequestHandler = validationRequestHandler
    59  }
    60  
    61  func (ss *SignerServer) servicePendingRequest() {
    62  	if !ss.IsRunning() {
    63  		return // Ignore error from closing.
    64  	}
    65  
    66  	req, err := ss.endpoint.ReadMessage()
    67  	if err != nil {
    68  		if err != io.EOF {
    69  			ss.Logger.Error("SignerServer: HandleMessage", "err", err)
    70  		}
    71  		return
    72  	}
    73  
    74  	var res privvalproto.Message
    75  	{
    76  		// limit the scope of the lock
    77  		ss.handlerMtx.Lock()
    78  		defer ss.handlerMtx.Unlock()
    79  		res, err = ss.validationRequestHandler(ss.privVal, req, ss.chainID)
    80  		if err != nil {
    81  			// only log the error; we'll reply with an error in res
    82  			ss.Logger.Error("SignerServer: handleMessage", "err", err)
    83  		}
    84  	}
    85  
    86  	err = ss.endpoint.WriteMessage(res)
    87  	if err != nil {
    88  		ss.Logger.Error("SignerServer: writeMessage", "err", err)
    89  	}
    90  }
    91  
    92  func (ss *SignerServer) serviceLoop() {
    93  	for {
    94  		select {
    95  		default:
    96  			err := ss.endpoint.ensureConnection()
    97  			if err != nil {
    98  				return
    99  			}
   100  			ss.servicePendingRequest()
   101  
   102  		case <-ss.Quit():
   103  			return
   104  		}
   105  	}
   106  }