github.com/ledgerwatch/erigon-lib@v1.0.0/direct/mining_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.MiningClient = (*MiningClient)(nil) 30 31 type MiningClient struct { 32 server txpool_proto.MiningServer 33 } 34 35 func NewMiningClient(server txpool_proto.MiningServer) *MiningClient { 36 return &MiningClient{server: server} 37 } 38 39 func (s *MiningClient) Version(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*types.VersionReply, error) { 40 return s.server.Version(ctx, in) 41 } 42 43 // -- start OnPendingBlock 44 45 func (s *MiningClient) OnPendingBlock(ctx context.Context, in *txpool_proto.OnPendingBlockRequest, opts ...grpc.CallOption) (txpool_proto.Mining_OnPendingBlockClient, error) { 46 ch := make(chan *onPendigBlockReply, 16384) 47 streamServer := &MiningOnPendingBlockS{ch: ch, ctx: ctx} 48 go func() { 49 defer close(ch) 50 streamServer.Err(s.server.OnPendingBlock(in, streamServer)) 51 }() 52 return &MiningOnPendingBlockC{ch: ch, ctx: ctx}, nil 53 } 54 55 type onPendigBlockReply struct { 56 r *txpool_proto.OnPendingBlockReply 57 err error 58 } 59 60 type MiningOnPendingBlockS struct { 61 ch chan *onPendigBlockReply 62 ctx context.Context 63 grpc.ServerStream 64 } 65 66 func (s *MiningOnPendingBlockS) Send(m *txpool_proto.OnPendingBlockReply) error { 67 s.ch <- &onPendigBlockReply{r: m} 68 return nil 69 } 70 func (s *MiningOnPendingBlockS) Context() context.Context { return s.ctx } 71 func (s *MiningOnPendingBlockS) Err(err error) { 72 if err == nil { 73 return 74 } 75 s.ch <- &onPendigBlockReply{err: err} 76 } 77 78 type MiningOnPendingBlockC struct { 79 ch chan *onPendigBlockReply 80 ctx context.Context 81 grpc.ClientStream 82 } 83 84 func (c *MiningOnPendingBlockC) Recv() (*txpool_proto.OnPendingBlockReply, error) { 85 m, ok := <-c.ch 86 if !ok || m == nil { 87 return nil, io.EOF 88 } 89 return m.r, m.err 90 } 91 func (c *MiningOnPendingBlockC) Context() context.Context { return c.ctx } 92 93 // -- end OnPendingBlock 94 // -- start OnMinedBlock 95 96 func (s *MiningClient) OnMinedBlock(ctx context.Context, in *txpool_proto.OnMinedBlockRequest, opts ...grpc.CallOption) (txpool_proto.Mining_OnMinedBlockClient, error) { 97 ch := make(chan *onMinedBlockReply, 16384) 98 streamServer := &MiningOnMinedBlockS{ch: ch, ctx: ctx} 99 go func() { 100 defer close(ch) 101 streamServer.Err(s.server.OnMinedBlock(in, streamServer)) 102 }() 103 return &MiningOnMinedBlockC{ch: ch, ctx: ctx}, nil 104 } 105 106 type onMinedBlockReply struct { 107 r *txpool_proto.OnMinedBlockReply 108 err error 109 } 110 111 type MiningOnMinedBlockS struct { 112 ch chan *onMinedBlockReply 113 ctx context.Context 114 grpc.ServerStream 115 } 116 117 func (s *MiningOnMinedBlockS) Send(m *txpool_proto.OnMinedBlockReply) error { 118 s.ch <- &onMinedBlockReply{r: m} 119 return nil 120 } 121 func (s *MiningOnMinedBlockS) Context() context.Context { return s.ctx } 122 func (s *MiningOnMinedBlockS) Err(err error) { 123 if err == nil { 124 return 125 } 126 s.ch <- &onMinedBlockReply{err: err} 127 } 128 129 type MiningOnMinedBlockC struct { 130 ch chan *onMinedBlockReply 131 ctx context.Context 132 grpc.ClientStream 133 } 134 135 func (c *MiningOnMinedBlockC) Recv() (*txpool_proto.OnMinedBlockReply, error) { 136 m, ok := <-c.ch 137 if !ok || m == nil { 138 return nil, io.EOF 139 } 140 return m.r, m.err 141 } 142 func (c *MiningOnMinedBlockC) Context() context.Context { return c.ctx } 143 144 // -- end OnMinedBlock 145 // -- end OnPendingLogs 146 147 func (s *MiningClient) OnPendingLogs(ctx context.Context, in *txpool_proto.OnPendingLogsRequest, opts ...grpc.CallOption) (txpool_proto.Mining_OnPendingLogsClient, error) { 148 ch := make(chan *onPendingLogsReply, 16384) 149 streamServer := &MiningOnPendingLogsS{ch: ch, ctx: ctx} 150 go func() { 151 defer close(ch) 152 streamServer.Err(s.server.OnPendingLogs(in, streamServer)) 153 }() 154 return &MiningOnPendingLogsC{ch: ch, ctx: ctx}, nil 155 } 156 157 type onPendingLogsReply struct { 158 r *txpool_proto.OnPendingLogsReply 159 err error 160 } 161 type MiningOnPendingLogsS struct { 162 ch chan *onPendingLogsReply 163 ctx context.Context 164 grpc.ServerStream 165 } 166 167 func (s *MiningOnPendingLogsS) Send(m *txpool_proto.OnPendingLogsReply) error { 168 s.ch <- &onPendingLogsReply{r: m} 169 return nil 170 } 171 func (s *MiningOnPendingLogsS) Context() context.Context { return s.ctx } 172 func (s *MiningOnPendingLogsS) Err(err error) { 173 if err == nil { 174 return 175 } 176 s.ch <- &onPendingLogsReply{err: err} 177 } 178 179 type MiningOnPendingLogsC struct { 180 ch chan *onPendingLogsReply 181 ctx context.Context 182 grpc.ClientStream 183 } 184 185 func (c *MiningOnPendingLogsC) Recv() (*txpool_proto.OnPendingLogsReply, error) { 186 m, ok := <-c.ch 187 if !ok || m == nil { 188 return nil, io.EOF 189 } 190 return m.r, m.err 191 } 192 func (c *MiningOnPendingLogsC) Context() context.Context { return c.ctx } 193 194 // -- end OnPendingLogs 195 196 func (s *MiningClient) GetWork(ctx context.Context, in *txpool_proto.GetWorkRequest, opts ...grpc.CallOption) (*txpool_proto.GetWorkReply, error) { 197 return s.server.GetWork(ctx, in) 198 } 199 200 func (s *MiningClient) SubmitWork(ctx context.Context, in *txpool_proto.SubmitWorkRequest, opts ...grpc.CallOption) (*txpool_proto.SubmitWorkReply, error) { 201 return s.server.SubmitWork(ctx, in) 202 } 203 204 func (s *MiningClient) SubmitHashRate(ctx context.Context, in *txpool_proto.SubmitHashRateRequest, opts ...grpc.CallOption) (*txpool_proto.SubmitHashRateReply, error) { 205 return s.server.SubmitHashRate(ctx, in) 206 } 207 208 func (s *MiningClient) HashRate(ctx context.Context, in *txpool_proto.HashRateRequest, opts ...grpc.CallOption) (*txpool_proto.HashRateReply, error) { 209 return s.server.HashRate(ctx, in) 210 } 211 212 func (s *MiningClient) Mining(ctx context.Context, in *txpool_proto.MiningRequest, opts ...grpc.CallOption) (*txpool_proto.MiningReply, error) { 213 return s.server.Mining(ctx, in) 214 }