github.com/ledgerwatch/erigon-lib@v1.0.0/direct/eth_backend_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 "github.com/ledgerwatch/erigon-lib/gointerfaces/remote" 24 "github.com/ledgerwatch/erigon-lib/gointerfaces/types" 25 "google.golang.org/grpc" 26 "google.golang.org/protobuf/types/known/emptypb" 27 ) 28 29 type EthBackendClientDirect struct { 30 server remote.ETHBACKENDServer 31 } 32 33 func NewEthBackendClientDirect(server remote.ETHBACKENDServer) *EthBackendClientDirect { 34 return &EthBackendClientDirect{server: server} 35 } 36 37 func (s *EthBackendClientDirect) Etherbase(ctx context.Context, in *remote.EtherbaseRequest, opts ...grpc.CallOption) (*remote.EtherbaseReply, error) { 38 return s.server.Etherbase(ctx, in) 39 } 40 41 func (s *EthBackendClientDirect) NetVersion(ctx context.Context, in *remote.NetVersionRequest, opts ...grpc.CallOption) (*remote.NetVersionReply, error) { 42 return s.server.NetVersion(ctx, in) 43 } 44 45 func (s *EthBackendClientDirect) NetPeerCount(ctx context.Context, in *remote.NetPeerCountRequest, opts ...grpc.CallOption) (*remote.NetPeerCountReply, error) { 46 return s.server.NetPeerCount(ctx, in) 47 } 48 49 func (s *EthBackendClientDirect) Version(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*types.VersionReply, error) { 50 return s.server.Version(ctx, in) 51 } 52 53 func (s *EthBackendClientDirect) ProtocolVersion(ctx context.Context, in *remote.ProtocolVersionRequest, opts ...grpc.CallOption) (*remote.ProtocolVersionReply, error) { 54 return s.server.ProtocolVersion(ctx, in) 55 } 56 57 func (s *EthBackendClientDirect) ClientVersion(ctx context.Context, in *remote.ClientVersionRequest, opts ...grpc.CallOption) (*remote.ClientVersionReply, error) { 58 return s.server.ClientVersion(ctx, in) 59 } 60 61 // -- start Subscribe 62 63 func (s *EthBackendClientDirect) Subscribe(ctx context.Context, in *remote.SubscribeRequest, opts ...grpc.CallOption) (remote.ETHBACKEND_SubscribeClient, error) { 64 ch := make(chan *subscribeReply, 16384) 65 streamServer := &SubscribeStreamS{ch: ch, ctx: ctx} 66 go func() { 67 defer close(ch) 68 streamServer.Err(s.server.Subscribe(in, streamServer)) 69 }() 70 return &SubscribeStreamC{ch: ch, ctx: ctx}, nil 71 } 72 73 type subscribeReply struct { 74 r *remote.SubscribeReply 75 err error 76 } 77 type SubscribeStreamS struct { 78 ch chan *subscribeReply 79 ctx context.Context 80 grpc.ServerStream 81 } 82 83 func (s *SubscribeStreamS) Send(m *remote.SubscribeReply) error { 84 s.ch <- &subscribeReply{r: m} 85 return nil 86 } 87 func (s *SubscribeStreamS) Context() context.Context { return s.ctx } 88 func (s *SubscribeStreamS) Err(err error) { 89 if err == nil { 90 return 91 } 92 s.ch <- &subscribeReply{err: err} 93 } 94 95 type SubscribeStreamC struct { 96 ch chan *subscribeReply 97 ctx context.Context 98 grpc.ClientStream 99 } 100 101 func (c *SubscribeStreamC) Recv() (*remote.SubscribeReply, error) { 102 m, ok := <-c.ch 103 if !ok || m == nil { 104 return nil, io.EOF 105 } 106 return m.r, m.err 107 } 108 func (c *SubscribeStreamC) Context() context.Context { return c.ctx } 109 110 // -- end Subscribe 111 112 // -- SubscribeLogs 113 114 func (s *EthBackendClientDirect) SubscribeLogs(ctx context.Context, opts ...grpc.CallOption) (remote.ETHBACKEND_SubscribeLogsClient, error) { 115 subscribeLogsRequestChan := make(chan *subscribeLogsRequest, 16384) 116 subscribeLogsReplyChan := make(chan *subscribeLogsReply, 16384) 117 srv := &SubscribeLogsStreamS{ 118 chSend: subscribeLogsReplyChan, 119 chRecv: subscribeLogsRequestChan, 120 ctx: ctx, 121 } 122 go func() { 123 defer close(subscribeLogsRequestChan) 124 defer close(subscribeLogsReplyChan) 125 srv.Err(s.server.SubscribeLogs(srv)) 126 }() 127 cli := &SubscribeLogsStreamC{ 128 chSend: subscribeLogsRequestChan, 129 chRecv: subscribeLogsReplyChan, 130 ctx: ctx, 131 } 132 return cli, nil 133 } 134 135 type SubscribeLogsStreamS struct { 136 chSend chan *subscribeLogsReply 137 chRecv chan *subscribeLogsRequest 138 ctx context.Context 139 grpc.ServerStream 140 } 141 142 type subscribeLogsReply struct { 143 r *remote.SubscribeLogsReply 144 err error 145 } 146 147 type subscribeLogsRequest struct { 148 r *remote.LogsFilterRequest 149 err error 150 } 151 152 func (s *SubscribeLogsStreamS) Send(m *remote.SubscribeLogsReply) error { 153 s.chSend <- &subscribeLogsReply{r: m} 154 return nil 155 } 156 157 func (s *SubscribeLogsStreamS) Recv() (*remote.LogsFilterRequest, error) { 158 m, ok := <-s.chRecv 159 if !ok || m == nil { 160 return nil, io.EOF 161 } 162 return m.r, m.err 163 } 164 165 func (s *SubscribeLogsStreamS) Err(err error) { 166 if err == nil { 167 return 168 } 169 s.chSend <- &subscribeLogsReply{err: err} 170 } 171 172 type SubscribeLogsStreamC struct { 173 chSend chan *subscribeLogsRequest 174 chRecv chan *subscribeLogsReply 175 ctx context.Context 176 grpc.ClientStream 177 } 178 179 func (c *SubscribeLogsStreamC) Send(m *remote.LogsFilterRequest) error { 180 c.chSend <- &subscribeLogsRequest{r: m} 181 return nil 182 } 183 184 func (c *SubscribeLogsStreamC) Recv() (*remote.SubscribeLogsReply, error) { 185 m, ok := <-c.chRecv 186 if !ok || m == nil { 187 return nil, io.EOF 188 } 189 return m.r, m.err 190 } 191 192 // -- end SubscribeLogs 193 194 func (s *EthBackendClientDirect) Block(ctx context.Context, in *remote.BlockRequest, opts ...grpc.CallOption) (*remote.BlockReply, error) { 195 return s.server.Block(ctx, in) 196 } 197 198 func (s *EthBackendClientDirect) TxnLookup(ctx context.Context, in *remote.TxnLookupRequest, opts ...grpc.CallOption) (*remote.TxnLookupReply, error) { 199 return s.server.TxnLookup(ctx, in) 200 } 201 202 func (s *EthBackendClientDirect) NodeInfo(ctx context.Context, in *remote.NodesInfoRequest, opts ...grpc.CallOption) (*remote.NodesInfoReply, error) { 203 return s.server.NodeInfo(ctx, in) 204 } 205 206 func (s *EthBackendClientDirect) Peers(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*remote.PeersReply, error) { 207 return s.server.Peers(ctx, in) 208 } 209 210 func (s *EthBackendClientDirect) AddPeer(ctx context.Context, in *remote.AddPeerRequest, opts ...grpc.CallOption) (*remote.AddPeerReply, error) { 211 return s.server.AddPeer(ctx, in) 212 } 213 214 func (s *EthBackendClientDirect) PendingBlock(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*remote.PendingBlockReply, error) { 215 return s.server.PendingBlock(ctx, in) 216 } 217 218 func (s *EthBackendClientDirect) BorEvent(ctx context.Context, in *remote.BorEventRequest, opts ...grpc.CallOption) (*remote.BorEventReply, error) { 219 return s.server.BorEvent(ctx, in) 220 }