github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/merkletree/client.go (about)

     1  package merkletree
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/0xPolygon/supernets2-node/log"
     8  	"github.com/0xPolygon/supernets2-node/merkletree/pb"
     9  	"google.golang.org/grpc"
    10  	"google.golang.org/grpc/credentials/insecure"
    11  )
    12  
    13  // NewMTDBServiceClient creates a new MTDB client.
    14  func NewMTDBServiceClient(ctx context.Context, c Config) (pb.StateDBServiceClient, *grpc.ClientConn, context.CancelFunc) {
    15  	opts := []grpc.DialOption{
    16  		grpc.WithTransportCredentials(insecure.NewCredentials()),
    17  		grpc.WithBlock(),
    18  	}
    19  	const maxWaitSeconds = 120
    20  	ctx, cancel := context.WithTimeout(ctx, maxWaitSeconds*time.Second)
    21  
    22  	log.Infof("trying to connect to merkletree: %v", c.URI)
    23  	mtDBConn, err := grpc.DialContext(ctx, c.URI, opts...)
    24  	if err != nil {
    25  		log.Fatalf("fail to dial: %v", err)
    26  	}
    27  	log.Infof("connected to merkletree")
    28  
    29  	mtDBClient := pb.NewStateDBServiceClient(mtDBConn)
    30  	return mtDBClient, mtDBConn, cancel
    31  }