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

     1  //go:build all || testnets
     2  // +build all testnets
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"bytes"
    28  	"os"
    29  	"regexp"
    30  	"testing"
    31  	"time"
    32  
    33  	"github.com/rs/zerolog"
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestIntegrationNodeForTransaction(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	client := ClientForTestnet()
    41  	operatorID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
    42  	require.NoError(t, err)
    43  	operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    44  	require.NoError(t, err)
    45  	client.SetOperator(operatorID, operatorKey)
    46  
    47  	var buf bytes.Buffer
    48  	writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
    49  	zerolog.SetGlobalLevel(zerolog.TraceLevel)
    50  
    51  	l := NewLogger("test", LoggerLevelTrace)
    52  	l.SetLevel(LoggerLevelTrace)
    53  
    54  	logger := zerolog.New(&writer)
    55  	l.logger = &logger
    56  	client.SetLogger(l)
    57  	ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
    58  	client.SetTransportSecurity(true)
    59  	client.SetLedgerID(*ledger)
    60  	client.SetMaxAttempts(3)
    61  	nodeAccountIDs := map[string]struct{}{}
    62  	for i := 0; i < 5; i++ {
    63  		_, err := NewTransferTransaction().AddHbarTransfer(operatorID, HbarFromTinybar(-1)).
    64  			AddHbarTransfer(AccountID{Shard: 0, Realm: 0, Account: 3}, HbarFromTinybar(1)).Execute(client)
    65  		require.NoError(t, err)
    66  		logOutput := buf.String()
    67  		sanitizedLogOutput := regexp.MustCompile(`\x1b\[[0-9;]*m`).ReplaceAllString(logOutput, "")
    68  		re := regexp.MustCompile(`nodeAccountID=([\d.]+)`)
    69  		matches := re.FindStringSubmatch(sanitizedLogOutput)
    70  		if len(matches) > 1 {
    71  			nodeAccountID := matches[1]
    72  			nodeAccountIDs[nodeAccountID] = struct{}{}
    73  		}
    74  		buf.Reset()
    75  	}
    76  	require.True(t, len(nodeAccountIDs) > 1, "Expected multiple different node account IDs")
    77  }
    78  
    79  func TestIntegrationNodeForQuery(t *testing.T) {
    80  	t.Parallel()
    81  
    82  	client := ClientForTestnet()
    83  	operatorID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
    84  	require.NoError(t, err)
    85  	operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
    86  	require.NoError(t, err)
    87  	client.SetOperator(operatorID, operatorKey)
    88  
    89  	var buf bytes.Buffer
    90  	writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
    91  	zerolog.SetGlobalLevel(zerolog.TraceLevel)
    92  
    93  	l := NewLogger("test", LoggerLevelTrace)
    94  	l.SetLevel(LoggerLevelTrace)
    95  
    96  	logger := zerolog.New(&writer)
    97  	l.logger = &logger
    98  	client.SetLogger(l)
    99  	ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
   100  	client.SetTransportSecurity(true)
   101  	client.SetLedgerID(*ledger)
   102  	client.SetMaxAttempts(3)
   103  	nodeAccountIDs := map[string]struct{}{}
   104  	for i := 0; i < 5; i++ {
   105  		_, err := NewAccountBalanceQuery().
   106  			SetAccountID(AccountID{Account: 3}).
   107  			Execute(client)
   108  		require.NoError(t, err)
   109  		logOutput := buf.String()
   110  		sanitizedLogOutput := regexp.MustCompile(`\x1b\[[0-9;]*m`).ReplaceAllString(logOutput, "")
   111  		re := regexp.MustCompile(`nodeAccountID=([\d.]+)`)
   112  		matches := re.FindStringSubmatch(sanitizedLogOutput)
   113  		if len(matches) > 1 {
   114  			nodeAccountID := matches[1]
   115  			nodeAccountIDs[nodeAccountID] = struct{}{}
   116  		}
   117  		buf.Reset()
   118  	}
   119  	require.True(t, len(nodeAccountIDs) > 1, "Expected multiple different node account IDs")
   120  }
   121  
   122  func TestIntegrationNodeForTransactionSourceListUnchanged(t *testing.T) {
   123  	t.Parallel()
   124  
   125  	client := ClientForTestnet()
   126  	operatorID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
   127  	require.NoError(t, err)
   128  	operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
   129  	require.NoError(t, err)
   130  	client.SetOperator(operatorID, operatorKey)
   131  
   132  	var buf bytes.Buffer
   133  	writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
   134  	zerolog.SetGlobalLevel(zerolog.TraceLevel)
   135  
   136  	l := NewLogger("test", LoggerLevelTrace)
   137  	l.SetLevel(LoggerLevelTrace)
   138  
   139  	logger := zerolog.New(&writer)
   140  	l.logger = &logger
   141  	client.SetLogger(l)
   142  	ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
   143  	client.SetTransportSecurity(true)
   144  	client.SetLedgerID(*ledger)
   145  	client.SetMaxAttempts(3)
   146  
   147  	_, err = NewAccountBalanceQuery().
   148  		SetAccountID(AccountID{Account: 3}).
   149  		Execute(client)
   150  	expectedHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
   151  	copy(expectedHealthyNodes, client.network.healthyNodes)
   152  	resultHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
   153  	_, err = NewAccountBalanceQuery().
   154  		SetAccountID(AccountID{Account: 3}).
   155  		Execute(client)
   156  	copy(resultHealthyNodes, client.network.healthyNodes)
   157  	require.Equal(t, expectedHealthyNodes, resultHealthyNodes)
   158  }
   159  
   160  func TestIntegrationNodeForQuerySourceListUnchanged(t *testing.T) {
   161  	t.Parallel()
   162  
   163  	client := ClientForTestnet()
   164  	operatorID, err := AccountIDFromString(os.Getenv("OPERATOR_ID"))
   165  	require.NoError(t, err)
   166  	operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
   167  	require.NoError(t, err)
   168  	client.SetOperator(operatorID, operatorKey)
   169  
   170  	var buf bytes.Buffer
   171  	writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
   172  	zerolog.SetGlobalLevel(zerolog.TraceLevel)
   173  
   174  	l := NewLogger("test", LoggerLevelTrace)
   175  	l.SetLevel(LoggerLevelTrace)
   176  
   177  	logger := zerolog.New(&writer)
   178  	l.logger = &logger
   179  	client.SetLogger(l)
   180  	ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
   181  	client.SetTransportSecurity(true)
   182  	client.SetLedgerID(*ledger)
   183  	client.SetMaxAttempts(3)
   184  
   185  	_, err = NewAccountBalanceQuery().
   186  		SetAccountID(AccountID{Account: 3}).
   187  		Execute(client)
   188  	expectedHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
   189  	copy(expectedHealthyNodes, client.network.healthyNodes)
   190  	resultHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
   191  	_, err = NewAccountBalanceQuery().
   192  		SetAccountID(AccountID{Account: 3}).
   193  		Execute(client)
   194  	copy(resultHealthyNodes, client.network.healthyNodes)
   195  	require.Equal(t, expectedHealthyNodes, resultHealthyNodes)
   196  }