github.com/status-im/status-go@v1.1.0/services/rpcfilters/service.go (about)

     1  package rpcfilters
     2  
     3  import (
     4  	"github.com/ethereum/go-ethereum/node"
     5  	"github.com/ethereum/go-ethereum/p2p"
     6  	"github.com/ethereum/go-ethereum/rpc"
     7  )
     8  
     9  // Make sure that Service implements node.Lifecycle interface.
    10  var _ node.Lifecycle = (*Service)(nil)
    11  
    12  // Service represents out own implementation of personal sign operations.
    13  type Service struct {
    14  	latestBlockChangedEvent        *latestBlockChangedEvent
    15  	transactionSentToUpstreamEvent *transactionSentToUpstreamEvent
    16  	rpc                            rpcProvider
    17  
    18  	quit chan struct{}
    19  }
    20  
    21  // New returns a new Service.
    22  func New(rpc rpcProvider) *Service {
    23  	provider := &latestBlockProviderRPC{rpc}
    24  	latestBlockChangedEvent := newLatestBlockChangedEvent(provider)
    25  	transactionSentToUpstreamEvent := newTransactionSentToUpstreamEvent()
    26  	return &Service{
    27  		latestBlockChangedEvent:        latestBlockChangedEvent,
    28  		transactionSentToUpstreamEvent: transactionSentToUpstreamEvent,
    29  
    30  		rpc: rpc,
    31  	}
    32  }
    33  
    34  // Protocols returns a new protocols list. In this case, there are none.
    35  func (s *Service) Protocols() []p2p.Protocol {
    36  	return []p2p.Protocol{}
    37  }
    38  
    39  // APIs returns a list of new APIs.
    40  func (s *Service) APIs() []rpc.API {
    41  	return []rpc.API{
    42  		{
    43  			Namespace: "eth",
    44  			Version:   "1.0",
    45  			Service:   NewPublicAPI(s),
    46  			Public:    true,
    47  		},
    48  	}
    49  }
    50  
    51  // Start is run when a service is started.
    52  func (s *Service) Start() error {
    53  	s.quit = make(chan struct{})
    54  	err := s.transactionSentToUpstreamEvent.Start()
    55  	if err != nil {
    56  		return err
    57  	}
    58  	return s.latestBlockChangedEvent.Start()
    59  }
    60  
    61  // Stop is run when a service is stopped.
    62  func (s *Service) Stop() error {
    63  	close(s.quit)
    64  	s.transactionSentToUpstreamEvent.Stop()
    65  	s.latestBlockChangedEvent.Stop()
    66  	return nil
    67  }
    68  
    69  func (s *Service) TransactionSentToUpstreamEvent() ChainEvent {
    70  	return s.transactionSentToUpstreamEvent
    71  }
    72  
    73  func (s *Service) TriggerTransactionSentToUpstreamEvent(txInfo *PendingTxInfo) {
    74  	s.transactionSentToUpstreamEvent.Trigger(txInfo)
    75  }