github.com/ledgerwatch/erigon-lib@v1.0.0/direct/txpool_client.go (about)

     1  /*
     2     Copyright 2021 Erigon contributors
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package direct
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  
    23  	txpool_proto "github.com/ledgerwatch/erigon-lib/gointerfaces/txpool"
    24  	"github.com/ledgerwatch/erigon-lib/gointerfaces/types"
    25  	"google.golang.org/grpc"
    26  	"google.golang.org/protobuf/types/known/emptypb"
    27  )
    28  
    29  var _ txpool_proto.TxpoolClient = (*TxPoolClient)(nil)
    30  
    31  type TxPoolClient struct {
    32  	server txpool_proto.TxpoolServer
    33  }
    34  
    35  func NewTxPoolClient(server txpool_proto.TxpoolServer) *TxPoolClient {
    36  	return &TxPoolClient{server}
    37  }
    38  
    39  func (s *TxPoolClient) Version(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*types.VersionReply, error) {
    40  	return s.server.Version(ctx, in)
    41  }
    42  
    43  func (s *TxPoolClient) FindUnknown(ctx context.Context, in *txpool_proto.TxHashes, opts ...grpc.CallOption) (*txpool_proto.TxHashes, error) {
    44  	return s.server.FindUnknown(ctx, in)
    45  }
    46  
    47  func (s *TxPoolClient) Add(ctx context.Context, in *txpool_proto.AddRequest, opts ...grpc.CallOption) (*txpool_proto.AddReply, error) {
    48  	return s.server.Add(ctx, in)
    49  }
    50  
    51  func (s *TxPoolClient) Transactions(ctx context.Context, in *txpool_proto.TransactionsRequest, opts ...grpc.CallOption) (*txpool_proto.TransactionsReply, error) {
    52  	return s.server.Transactions(ctx, in)
    53  }
    54  
    55  func (s *TxPoolClient) All(ctx context.Context, in *txpool_proto.AllRequest, opts ...grpc.CallOption) (*txpool_proto.AllReply, error) {
    56  	return s.server.All(ctx, in)
    57  }
    58  
    59  func (s *TxPoolClient) Pending(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*txpool_proto.PendingReply, error) {
    60  	return s.server.Pending(ctx, in)
    61  }
    62  
    63  // -- start OnAdd
    64  
    65  func (s *TxPoolClient) OnAdd(ctx context.Context, in *txpool_proto.OnAddRequest, opts ...grpc.CallOption) (txpool_proto.Txpool_OnAddClient, error) {
    66  	ch := make(chan *onAddReply, 16384)
    67  	streamServer := &TxPoolOnAddS{ch: ch, ctx: ctx}
    68  	go func() {
    69  		defer close(ch)
    70  		streamServer.Err(s.server.OnAdd(in, streamServer))
    71  	}()
    72  	return &TxPoolOnAddC{ch: ch, ctx: ctx}, nil
    73  }
    74  
    75  type onAddReply struct {
    76  	r   *txpool_proto.OnAddReply
    77  	err error
    78  }
    79  
    80  type TxPoolOnAddS struct {
    81  	ch  chan *onAddReply
    82  	ctx context.Context
    83  	grpc.ServerStream
    84  }
    85  
    86  func (s *TxPoolOnAddS) Send(m *txpool_proto.OnAddReply) error {
    87  	s.ch <- &onAddReply{r: m}
    88  	return nil
    89  }
    90  func (s *TxPoolOnAddS) Context() context.Context { return s.ctx }
    91  func (s *TxPoolOnAddS) Err(err error) {
    92  	if err == nil {
    93  		return
    94  	}
    95  	s.ch <- &onAddReply{err: err}
    96  }
    97  
    98  type TxPoolOnAddC struct {
    99  	ch  chan *onAddReply
   100  	ctx context.Context
   101  	grpc.ClientStream
   102  }
   103  
   104  func (c *TxPoolOnAddC) Recv() (*txpool_proto.OnAddReply, error) {
   105  	m, ok := <-c.ch
   106  	if !ok || m == nil {
   107  		return nil, io.EOF
   108  	}
   109  	return m.r, m.err
   110  }
   111  func (c *TxPoolOnAddC) Context() context.Context { return c.ctx }
   112  
   113  // -- end OnAdd
   114  
   115  func (s *TxPoolClient) Status(ctx context.Context, in *txpool_proto.StatusRequest, opts ...grpc.CallOption) (*txpool_proto.StatusReply, error) {
   116  	return s.server.Status(ctx, in)
   117  }
   118  
   119  func (s *TxPoolClient) Nonce(ctx context.Context, in *txpool_proto.NonceRequest, opts ...grpc.CallOption) (*txpool_proto.NonceReply, error) {
   120  	return s.server.Nonce(ctx, in)
   121  }