github.com/ledgerwatch/erigon-lib@v1.0.0/direct/state_diff_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  	"google.golang.org/grpc"
    25  )
    26  
    27  type StateDiffClient interface {
    28  	StateChanges(ctx context.Context, in *remote.StateChangeRequest, opts ...grpc.CallOption) (remote.KV_StateChangesClient, error)
    29  	Snapshots(ctx context.Context, in *remote.SnapshotsRequest, opts ...grpc.CallOption) (*remote.SnapshotsReply, error)
    30  }
    31  
    32  var _ StateDiffClient = (*StateDiffClientDirect)(nil) // compile-time interface check
    33  
    34  // SentryClientDirect implements SentryClient interface by connecting the instance of the client directly with the corresponding
    35  // instance of SentryServer
    36  type StateDiffClientDirect struct {
    37  	server remote.KVServer
    38  }
    39  
    40  func NewStateDiffClientDirect(server remote.KVServer) *StateDiffClientDirect {
    41  	return &StateDiffClientDirect{server: server}
    42  }
    43  
    44  func (c *StateDiffClientDirect) Snapshots(ctx context.Context, in *remote.SnapshotsRequest, opts ...grpc.CallOption) (*remote.SnapshotsReply, error) {
    45  	return c.server.Snapshots(ctx, in)
    46  }
    47  
    48  // -- start StateChanges
    49  
    50  func (c *StateDiffClientDirect) StateChanges(ctx context.Context, in *remote.StateChangeRequest, opts ...grpc.CallOption) (remote.KV_StateChangesClient, error) {
    51  	ch := make(chan *stateDiffReply, 16384)
    52  	streamServer := &StateDiffStreamS{ch: ch, ctx: ctx}
    53  	go func() {
    54  		defer close(ch)
    55  		streamServer.Err(c.server.StateChanges(in, streamServer))
    56  	}()
    57  	return &StateDiffStreamC{ch: ch, ctx: ctx}, nil
    58  }
    59  
    60  type stateDiffReply struct {
    61  	r   *remote.StateChangeBatch
    62  	err error
    63  }
    64  
    65  type StateDiffStreamC struct {
    66  	ch  chan *stateDiffReply
    67  	ctx context.Context
    68  	grpc.ClientStream
    69  }
    70  
    71  func (c *StateDiffStreamC) Recv() (*remote.StateChangeBatch, error) {
    72  	m, ok := <-c.ch
    73  	if !ok || m == nil {
    74  		return nil, io.EOF
    75  	}
    76  	return m.r, m.err
    77  }
    78  func (c *StateDiffStreamC) Context() context.Context { return c.ctx }
    79  
    80  // StateDiffStreamS implements proto_sentry.Sentry_ReceiveMessagesServer
    81  type StateDiffStreamS struct {
    82  	ch  chan *stateDiffReply
    83  	ctx context.Context
    84  	grpc.ServerStream
    85  }
    86  
    87  func (s *StateDiffStreamS) Send(m *remote.StateChangeBatch) error {
    88  	s.ch <- &stateDiffReply{r: m}
    89  	return nil
    90  }
    91  func (s *StateDiffStreamS) Context() context.Context { return s.ctx }
    92  func (s *StateDiffStreamS) Err(err error) {
    93  	if err == nil {
    94  		return
    95  	}
    96  	s.ch <- &stateDiffReply{err: err}
    97  }
    98  
    99  // -- end StateChanges