github.com/ledgerwatch/erigon-lib@v1.0.0/downloader/downloadergrpc/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 downloadergrpc
    18  
    19  import (
    20  	"context"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"time"
    24  
    25  	"github.com/anacrolix/torrent/metainfo"
    26  	"github.com/c2h5oh/datasize"
    27  	"github.com/ledgerwatch/erigon-lib/gointerfaces"
    28  	proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader"
    29  	prototypes "github.com/ledgerwatch/erigon-lib/gointerfaces/types"
    30  	"google.golang.org/grpc"
    31  	"google.golang.org/grpc/backoff"
    32  	"google.golang.org/grpc/credentials/insecure"
    33  	"google.golang.org/grpc/keepalive"
    34  )
    35  
    36  func NewClient(ctx context.Context, downloaderAddr string) (proto_downloader.DownloaderClient, error) {
    37  	// creating grpc client connection
    38  	var dialOpts []grpc.DialOption
    39  
    40  	backoffCfg := backoff.DefaultConfig
    41  	backoffCfg.BaseDelay = 500 * time.Millisecond
    42  	backoffCfg.MaxDelay = 10 * time.Second
    43  	dialOpts = []grpc.DialOption{
    44  		grpc.WithConnectParams(grpc.ConnectParams{Backoff: backoffCfg, MinConnectTimeout: 10 * time.Minute}),
    45  		grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(16 * datasize.MB))),
    46  		grpc.WithKeepaliveParams(keepalive.ClientParameters{}),
    47  	}
    48  
    49  	dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
    50  	conn, err := grpc.DialContext(ctx, downloaderAddr, dialOpts...)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("creating client connection to sentry P2P: %w", err)
    53  	}
    54  	return proto_downloader.NewDownloaderClient(conn), nil
    55  }
    56  
    57  func InfoHashes2Proto(in []metainfo.Hash) []*prototypes.H160 {
    58  	infoHashes := make([]*prototypes.H160, len(in))
    59  	i := 0
    60  	for _, h := range in {
    61  		infoHashes[i] = gointerfaces.ConvertAddressToH160(h)
    62  		i++
    63  	}
    64  	return infoHashes
    65  }
    66  
    67  func Strings2Proto(in []string) []*prototypes.H160 {
    68  	infoHashes := make([]*prototypes.H160, len(in))
    69  	i := 0
    70  	for _, h := range in {
    71  		infoHashes[i] = String2Proto(h)
    72  		i++
    73  	}
    74  	return infoHashes
    75  }
    76  
    77  func String2Proto(in string) *prototypes.H160 {
    78  	var infoHash [20]byte
    79  	inHex, _ := hex.DecodeString(in)
    80  	copy(infoHash[:], inHex)
    81  	return gointerfaces.ConvertAddressToH160(infoHash)
    82  }