Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/wrong nodes for transactions #828

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package hedera
*/

import (
"math/rand"
"time"
)

Expand Down Expand Up @@ -139,10 +140,18 @@ func (network *_Network) _GetNodeAccountIDsForExecute() []AccountID { //nolint

network.healthyNodesMutex.RLock()
defer network.healthyNodesMutex.RUnlock()

healthyNodes := make([]_IManagedNode, len(network.healthyNodes))
copy(healthyNodes, network.healthyNodes)

// shuffle the nodes, so that the first node is not always the same
for i := range healthyNodes {
j := rand.Intn(i + 1) // #nosec
healthyNodes[i], healthyNodes[j] = healthyNodes[j], healthyNodes[i]
}
for i := 0; i < nodesForTransaction; i++ {
nodes = append(nodes, network.healthyNodes[i].(*_Node).accountID)
nodes = append(nodes, healthyNodes[i].(*_Node).accountID)
}

return nodes
}

Expand Down
205 changes: 205 additions & 0 deletions network_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
//go:build all || testnets
// +build all testnets

package hedera

/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import (
"bytes"
"fmt"
"os"
"regexp"
"testing"
"time"

"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

func TestIntegrationNodeForTransaction(t *testing.T) {
t.Parallel()

client := ClientForTestnet()
operatorID,err:= AccountIDFromString(os.Getenv("OPERATOR_ID"))
require.NoError(t, err)
operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
require.NoError(t, err)
client.SetOperator(operatorID, operatorKey)

var buf bytes.Buffer
writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
zerolog.SetGlobalLevel(zerolog.TraceLevel)

l := NewLogger("test", LoggerLevelTrace)
l.SetLevel(LoggerLevelTrace)

logger := zerolog.New(&writer)
l.logger = &logger
client.SetLogger(l)
ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
client.SetTransportSecurity(true)
client.SetLedgerID(*ledger)
client.SetMaxAttempts(3)
nodeAccountIDs := map[string]struct{}{}
for i := 0; i < 5; i++ {
_, err := NewTransferTransaction().AddHbarTransfer(operatorID, HbarFromTinybar(-1)).
AddHbarTransfer(AccountID{Shard: 0, Realm: 0, Account: 3}, HbarFromTinybar(1)).Execute(client)
require.NoError(t, err)
logOutput := buf.String()
sanitizedLogOutput := regexp.MustCompile(`\x1b\[[0-9;]*m`).ReplaceAllString(logOutput, "")
re := regexp.MustCompile(`nodeAccountID=([\d.]+)`)
matches := re.FindStringSubmatch(sanitizedLogOutput)
if len(matches) > 1 {
nodeAccountID := matches[1]
nodeAccountIDs[nodeAccountID] = struct{}{}
} else {
fmt.Println("Node Account ID not found")
}
buf.Reset()
}
require.True(t, len(nodeAccountIDs) > 1, "Expected multiple different node account IDs")
}

func TestIntegrationNodeForQuery(t *testing.T) {
t.Parallel()

client := ClientForTestnet()
operatorID,err:= AccountIDFromString(os.Getenv("OPERATOR_ID"))
require.NoError(t, err)
operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
require.NoError(t, err)
client.SetOperator(operatorID, operatorKey)

var buf bytes.Buffer
writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
zerolog.SetGlobalLevel(zerolog.TraceLevel)

l := NewLogger("test", LoggerLevelTrace)
l.SetLevel(LoggerLevelTrace)

logger := zerolog.New(&writer)
l.logger = &logger
client.SetLogger(l)
ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
client.SetTransportSecurity(true)
client.SetLedgerID(*ledger)
client.SetMaxAttempts(3)
nodeAccountIDs := map[string]struct{}{}
for i := 0; i < 5; i++ {
_, err := NewAccountBalanceQuery().
SetAccountID(AccountID{Account: 3}).
Execute(client)
require.NoError(t, err)
logOutput := buf.String()
sanitizedLogOutput := regexp.MustCompile(`\x1b\[[0-9;]*m`).ReplaceAllString(logOutput, "")
re := regexp.MustCompile(`nodeAccountID=([\d.]+)`)
matches := re.FindStringSubmatch(sanitizedLogOutput)
if len(matches) > 1 {
nodeAccountID := matches[1]
nodeAccountIDs[nodeAccountID] = struct{}{}
} else {
fmt.Println("Node Account ID not found")
}
buf.Reset()
}
require.True(t, len(nodeAccountIDs) > 1, "Expected multiple different node account IDs")
}

func TestIntegrationNodeForTransactionSourceListUnchanged(t *testing.T) {
t.Parallel()

client := ClientForTestnet()
operatorID,err:= AccountIDFromString(os.Getenv("OPERATOR_ID"))
require.NoError(t, err)
operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
require.NoError(t, err)
client.SetOperator(operatorID, operatorKey)



var buf bytes.Buffer
writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
zerolog.SetGlobalLevel(zerolog.TraceLevel)

l := NewLogger("test", LoggerLevelTrace)
l.SetLevel(LoggerLevelTrace)

logger := zerolog.New(&writer)
l.logger = &logger
client.SetLogger(l)
ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
client.SetTransportSecurity(true)
client.SetLedgerID(*ledger)
client.SetMaxAttempts(3)

_, err = NewAccountBalanceQuery().
SetAccountID(AccountID{Account: 3}).
Execute(client)
expectedHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
copy(expectedHealthyNodes, client.network.healthyNodes)
resultHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
_, err = NewAccountBalanceQuery().
SetAccountID(AccountID{Account: 3}).
Execute(client)
copy(resultHealthyNodes, client.network.healthyNodes)
require.Equal(t, expectedHealthyNodes, resultHealthyNodes)
}

func TestIntegrationNodeForQuerySourceListUnchanged(t *testing.T) {
t.Parallel()

client := ClientForTestnet()
operatorID,err:= AccountIDFromString(os.Getenv("OPERATOR_ID"))
require.NoError(t, err)
operatorKey, err := PrivateKeyFromString(os.Getenv("OPERATOR_KEY"))
require.NoError(t, err)
client.SetOperator(operatorID, operatorKey)



var buf bytes.Buffer
writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
zerolog.SetGlobalLevel(zerolog.TraceLevel)

l := NewLogger("test", LoggerLevelTrace)
l.SetLevel(LoggerLevelTrace)

logger := zerolog.New(&writer)
l.logger = &logger
client.SetLogger(l)
ledger, _ := LedgerIDFromNetworkName(NetworkNameTestnet)
client.SetTransportSecurity(true)
client.SetLedgerID(*ledger)
client.SetMaxAttempts(3)

_, err = NewAccountBalanceQuery().
SetAccountID(AccountID{Account: 3}).
Execute(client)
expectedHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
copy(expectedHealthyNodes, client.network.healthyNodes)
resultHealthyNodes := make([]_IManagedNode, len(client.network.healthyNodes))
_, err = NewAccountBalanceQuery().
SetAccountID(AccountID{Account: 3}).
Execute(client)
copy(resultHealthyNodes, client.network.healthyNodes)
require.Equal(t, expectedHealthyNodes, resultHealthyNodes)
}