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

     1  //go:build all || e2e
     2  // +build all e2e
     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  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestIntegrationClientCanExecuteSerializedTransactionFromAnotherClient(t *testing.T) { // nolint
    35  	t.Parallel()
    36  	env := NewIntegrationTestEnv(t)
    37  	client2 := ClientForNetwork(env.Client.GetNetwork())
    38  	client2.SetOperator(env.OperatorID, env.OperatorKey)
    39  
    40  	tx, err := NewTransferTransaction().AddHbarTransfer(env.OperatorID, HbarFromTinybar(-1)).
    41  		AddHbarTransfer(AccountID{Account: 3}, HbarFromTinybar(1)).SetNodeAccountIDs([]AccountID{{Account: 3}}).FreezeWith(env.Client)
    42  	require.NoError(t, err)
    43  	txBytes, err := tx.ToBytes()
    44  	FromBytes, err := TransactionFromBytes(txBytes)
    45  	require.NoError(t, err)
    46  	txFromBytes, ok := FromBytes.(TransferTransaction)
    47  	require.True(t, ok)
    48  	resp, err := txFromBytes.Execute(client2)
    49  	require.NoError(t, err)
    50  	reciept, err := resp.SetValidateStatus(true).GetReceipt(client2)
    51  	require.NoError(t, err)
    52  	assert.Equal(t, StatusSuccess, reciept.Status)
    53  }
    54  
    55  func TestIntegrationClientCanFailGracefullyWhenDoesNotHaveNodeOfAnotherClient(t *testing.T) { // nolint
    56  	t.Parallel()
    57  	env := NewIntegrationTestEnv(t)
    58  
    59  	// Get one of the nodes of the network from the original client
    60  	var address string
    61  	for key := range env.Client.GetNetwork() {
    62  		address = key
    63  		break
    64  	}
    65  	// Use that node to create a network for the second client but with a different node account id
    66  	var network = map[string]AccountID{
    67  		address: {Account: 99},
    68  	}
    69  
    70  	client2 := ClientForNetwork(network)
    71  	client2.SetOperator(env.OperatorID, env.OperatorKey)
    72  
    73  	// Create a transaction with a node using original client
    74  	tx, err := NewTransferTransaction().AddHbarTransfer(env.OperatorID, HbarFromTinybar(-1)).
    75  		AddHbarTransfer(AccountID{Account: 3}, HbarFromTinybar(1)).SetNodeAccountIDs([]AccountID{{Account: 3}}).FreezeWith(env.Client)
    76  	require.NoError(t, err)
    77  	txBytes, err := tx.ToBytes()
    78  	FromBytes, err := TransactionFromBytes(txBytes)
    79  	require.NoError(t, err)
    80  	txFromBytes, ok := FromBytes.(TransferTransaction)
    81  	require.True(t, ok)
    82  
    83  	// Try to execute it with the second client, which does not have the node
    84  	_, err = txFromBytes.Execute(client2)
    85  	require.Error(t, err)
    86  	require.Equal(t, err.Error(), "Invalid node AccountID was set for transaction: 0.0.3")
    87  }
    88  
    89  func DisabledTestIntegrationClientPingAllBadNetwork(t *testing.T) { // nolint
    90  	t.Parallel()
    91  	env := NewIntegrationTestEnv(t)
    92  
    93  	netwrk := _NewNetwork()
    94  	netwrk.SetNetwork(env.Client.GetNetwork())
    95  
    96  	tempClient := _NewClient(netwrk, env.Client.GetMirrorNetwork(), env.Client.GetLedgerID())
    97  	tempClient.SetOperator(env.OperatorID, env.OperatorKey)
    98  
    99  	tempClient.SetMaxNodeAttempts(1)
   100  	tempClient.SetMaxNodesPerTransaction(2)
   101  	tempClient.SetMaxAttempts(3)
   102  	net := tempClient.GetNetwork()
   103  	assert.True(t, len(net) > 1)
   104  
   105  	keys := make([]string, len(net))
   106  	val := make([]AccountID, len(net))
   107  	i := 0
   108  	for st, n := range net {
   109  		keys[i] = st
   110  		val[i] = n
   111  		i++
   112  	}
   113  
   114  	tempNet := make(map[string]AccountID, 2)
   115  	tempNet["in.process.ew:3123"] = val[0]
   116  	tempNet[keys[1]] = val[1]
   117  
   118  	err := tempClient.SetNetwork(tempNet)
   119  	require.NoError(t, err)
   120  
   121  	tempClient.PingAll()
   122  
   123  	net = tempClient.GetNetwork()
   124  	i = 0
   125  	for st, n := range net {
   126  		keys[i] = st
   127  		val[i] = n
   128  		i++
   129  	}
   130  
   131  	_, err = NewAccountBalanceQuery().
   132  		SetAccountID(val[0]).
   133  		Execute(tempClient)
   134  	require.NoError(t, err)
   135  
   136  	assert.Equal(t, 1, len(tempClient.GetNetwork()))
   137  
   138  	err = CloseIntegrationTestEnv(env, nil)
   139  	require.NoError(t, err)
   140  }