github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/mirror_node.go (about)

     1  package hedera
     2  
     3  /*-
     4   *
     5   * Hedera Go SDK
     6   *
     7   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
     8   *
     9   * Licensed under the Apache License, Version 2.0 (the "License");
    10   * you may not use this file except in compliance with the License.
    11   * You may obtain a copy of the License at
    12   *
    13   *      http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing, software
    16   * distributed under the License is distributed on an "AS IS" BASIS,
    17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    18   * See the License for the specific language governing permissions and
    19   * limitations under the License.
    20   *
    21   */
    22  
    23  import (
    24  	"crypto/tls"
    25  	"time"
    26  
    27  	"google.golang.org/grpc/credentials/insecure"
    28  
    29  	"github.com/hashgraph/hedera-protobufs-go/mirror"
    30  	"github.com/pkg/errors"
    31  	"google.golang.org/grpc/credentials"
    32  	"google.golang.org/grpc/keepalive"
    33  
    34  	"google.golang.org/grpc"
    35  )
    36  
    37  type _MirrorNode struct {
    38  	*_ManagedNode
    39  	consensusServiceClient *mirror.ConsensusServiceClient
    40  	networkServiceClient   *mirror.NetworkServiceClient
    41  	client                 *grpc.ClientConn
    42  }
    43  
    44  func (node *_MirrorNode) _SetVerifyCertificate(_ bool) {
    45  }
    46  
    47  func (node *_MirrorNode) _GetVerifyCertificate() bool {
    48  	return false
    49  }
    50  
    51  func _NewMirrorNode(address string) (node *_MirrorNode, err error) {
    52  	node = &_MirrorNode{}
    53  	node._ManagedNode, err = _NewManagedNode(address, 250*time.Millisecond)
    54  	return node, err
    55  }
    56  
    57  func (node *_MirrorNode) _GetKey() string {
    58  	return node.address._String()
    59  }
    60  
    61  func (node *_MirrorNode) _SetMinBackoff(waitTime time.Duration) {
    62  	node._ManagedNode._SetMinBackoff(waitTime)
    63  }
    64  
    65  func (node *_MirrorNode) _GetMinBackoff() time.Duration {
    66  	return node._ManagedNode._GetMinBackoff()
    67  }
    68  
    69  func (node *_MirrorNode) _SetMaxBackoff(waitTime time.Duration) {
    70  	node._ManagedNode._SetMaxBackoff(waitTime)
    71  }
    72  
    73  func (node *_MirrorNode) _GetMaxBackoff() time.Duration {
    74  	return node._ManagedNode._GetMaxBackoff()
    75  }
    76  
    77  func (node *_MirrorNode) _InUse() {
    78  	node._ManagedNode._InUse()
    79  }
    80  
    81  func (node *_MirrorNode) _IsHealthy() bool {
    82  	return node._ManagedNode._IsHealthy()
    83  }
    84  
    85  func (node *_MirrorNode) _IncreaseBackoff() {
    86  	node._ManagedNode._IncreaseBackoff()
    87  }
    88  
    89  func (node *_MirrorNode) _DecreaseBackoff() {
    90  	node._ManagedNode._DecreaseBackoff()
    91  }
    92  
    93  func (node *_MirrorNode) _Wait() time.Duration {
    94  	return node._ManagedNode._Wait()
    95  }
    96  
    97  func (node *_MirrorNode) _GetUseCount() int64 {
    98  	return node._ManagedNode._GetUseCount()
    99  }
   100  
   101  func (node *_MirrorNode) _GetLastUsed() time.Time {
   102  	return node._ManagedNode._GetLastUsed()
   103  }
   104  
   105  func (node *_MirrorNode) _GetManagedNode() *_ManagedNode {
   106  	return node._ManagedNode
   107  }
   108  
   109  func (node *_MirrorNode) _GetAttempts() int64 {
   110  	return node._ManagedNode._GetAttempts()
   111  }
   112  
   113  func (node *_MirrorNode) _GetAddress() string {
   114  	return node._ManagedNode._GetAddress()
   115  }
   116  
   117  func (node *_MirrorNode) _GetReadmitTime() *time.Time {
   118  	return node._ManagedNode._GetReadmitTime()
   119  }
   120  
   121  func (node *_MirrorNode) _GetConsensusServiceClient() (*mirror.ConsensusServiceClient, error) {
   122  	if node.consensusServiceClient != nil {
   123  		return node.consensusServiceClient, nil
   124  	} else if node.client != nil {
   125  		channel := mirror.NewConsensusServiceClient(node.client)
   126  		node.consensusServiceClient = &channel
   127  		return node.consensusServiceClient, nil
   128  	}
   129  
   130  	var kacp = keepalive.ClientParameters{
   131  		Time:                10 * time.Second,
   132  		Timeout:             time.Second,
   133  		PermitWithoutStream: true,
   134  	}
   135  
   136  	var security grpc.DialOption
   137  
   138  	if node._ManagedNode.address._IsTransportSecurity() {
   139  		security = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})) // nolint
   140  	} else {
   141  		security = grpc.WithTransportCredentials(insecure.NewCredentials()) //nolint
   142  	}
   143  
   144  	conn, err := grpc.NewClient(node._ManagedNode.address._String(), security, grpc.WithKeepaliveParams(kacp))
   145  	if err != nil {
   146  		return nil, errors.Wrapf(err, "error connecting to mirror at %s", node._ManagedNode.address._String())
   147  	}
   148  
   149  	channel := mirror.NewConsensusServiceClient(conn)
   150  	node.consensusServiceClient = &channel
   151  	node.client = conn
   152  
   153  	return node.consensusServiceClient, nil
   154  }
   155  
   156  func (node *_MirrorNode) _GetNetworkServiceClient() (*mirror.NetworkServiceClient, error) {
   157  	if node.networkServiceClient != nil {
   158  		return node.networkServiceClient, nil
   159  	} else if node.client != nil {
   160  		channel := mirror.NewNetworkServiceClient(node.client)
   161  		node.networkServiceClient = &channel
   162  		return node.networkServiceClient, nil
   163  	}
   164  
   165  	var kacp = keepalive.ClientParameters{
   166  		Time:                time.Minute,
   167  		Timeout:             20 * time.Second,
   168  		PermitWithoutStream: true,
   169  	}
   170  
   171  	var security grpc.DialOption
   172  
   173  	if node._ManagedNode.address._IsTransportSecurity() {
   174  		security = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})) // nolint
   175  	} else {
   176  		security = grpc.WithTransportCredentials(insecure.NewCredentials()) //nolint
   177  	}
   178  
   179  	conn, err := grpc.NewClient(node._ManagedNode.address._String(), security, grpc.WithKeepaliveParams(kacp))
   180  	if err != nil {
   181  		return nil, errors.Wrapf(err, "error connecting to mirror at %s", node._ManagedNode.address._String())
   182  	}
   183  
   184  	channel := mirror.NewNetworkServiceClient(conn)
   185  	node.networkServiceClient = &channel
   186  	node.client = conn
   187  
   188  	return node.networkServiceClient, nil
   189  }
   190  
   191  func (node *_MirrorNode) _ToSecure() _IManagedNode {
   192  	managed := _ManagedNode{
   193  		address:            node.address._ToSecure(),
   194  		currentBackoff:     node.currentBackoff,
   195  		lastUsed:           node.lastUsed,
   196  		readmitTime:        node.readmitTime,
   197  		useCount:           node.useCount,
   198  		minBackoff:         node.minBackoff,
   199  		badGrpcStatusCount: node.badGrpcStatusCount,
   200  	}
   201  
   202  	return &_MirrorNode{
   203  		_ManagedNode:           &managed,
   204  		consensusServiceClient: node.consensusServiceClient,
   205  		client:                 node.client,
   206  	}
   207  }
   208  
   209  func (node *_MirrorNode) _ToInsecure() _IManagedNode {
   210  	managed := _ManagedNode{
   211  		address:            node.address._ToInsecure(),
   212  		currentBackoff:     node.currentBackoff,
   213  		lastUsed:           node.lastUsed,
   214  		readmitTime:        node.readmitTime,
   215  		useCount:           node.useCount,
   216  		minBackoff:         node.minBackoff,
   217  		badGrpcStatusCount: node.badGrpcStatusCount,
   218  	}
   219  
   220  	return &_MirrorNode{
   221  		_ManagedNode:           &managed,
   222  		consensusServiceClient: node.consensusServiceClient,
   223  		client:                 node.client,
   224  	}
   225  }
   226  
   227  func (node *_MirrorNode) _Close() error {
   228  	if node.consensusServiceClient != nil {
   229  		return node.client.Close()
   230  	}
   231  
   232  	return nil
   233  }