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: Panic when node account id is not in network list #805

Merged
merged 1 commit into from
Sep 19, 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
55 changes: 55 additions & 0 deletions client_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,61 @@ import (
"github.com/stretchr/testify/require"
)

func TestIntegrationClientCanExecuteSerializedTransactionFromAnotherClient(t *testing.T) { // nolint
t.Parallel()
env := NewIntegrationTestEnv(t)
client2 := ClientForNetwork(env.Client.GetNetwork())
client2.SetOperator(env.OperatorID, env.OperatorKey)

tx, err := NewTransferTransaction().AddHbarTransfer(env.OperatorID, HbarFromTinybar(-1)).
AddHbarTransfer(AccountID{Account: 3}, HbarFromTinybar(1)).SetNodeAccountIDs([]AccountID{{Account: 3}}).FreezeWith(env.Client)
require.NoError(t, err)
txBytes, err := tx.ToBytes()
FromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)
txFromBytes, ok := FromBytes.(TransferTransaction)
require.True(t, ok)
resp, err := txFromBytes.Execute(client2)
require.NoError(t, err)
reciept, err := resp.SetValidateStatus(true).GetReceipt(client2)
require.NoError(t, err)
assert.Equal(t, StatusSuccess, reciept.Status)
}

func TestIntegrationClientCanFailGracefullyWhenDoesNotHaveNodeOfAnotherClient(t *testing.T) { // nolint
t.Parallel()
env := NewIntegrationTestEnv(t)

// Get one of the nodes of the network from the original client
var address string
for key := range env.Client.GetNetwork() {
address = key
break
}
// Use that node to create a network for the second client but with a different node account id
var network = map[string]AccountID{
address: {Account: 99},
}

client2 := ClientForNetwork(network)
client2.SetOperator(env.OperatorID, env.OperatorKey)

// Create a transaction with a node using original client
tx, err := NewTransferTransaction().AddHbarTransfer(env.OperatorID, HbarFromTinybar(-1)).
AddHbarTransfer(AccountID{Account: 3}, HbarFromTinybar(1)).SetNodeAccountIDs([]AccountID{{Account: 3}}).FreezeWith(env.Client)
require.NoError(t, err)
txBytes, err := tx.ToBytes()
FromBytes, err := TransactionFromBytes(txBytes)
require.NoError(t, err)
txFromBytes, ok := FromBytes.(TransferTransaction)
require.True(t, ok)

// Try to execute it with the second client, which does not have the node
_, err = txFromBytes.Execute(client2)
require.Error(t, err)
require.Equal(t, err.Error(), "Invalid node AccountID was set for transaction: 0.0.3")
}

func DisabledTestIntegrationClientPingAllBadNetwork(t *testing.T) { // nolint
t.Parallel()
env := NewIntegrationTestEnv(t)
Expand Down
3 changes: 3 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func (network *_Network) _IncreaseBackoff(node *_Node) {

func (network *_Network) _GetNodeForAccountID(id AccountID) (*_Node, bool) {
node, ok := network.network[id.String()]
if !ok || node == nil {
rokn marked this conversation as resolved.
Show resolved Hide resolved
return nil, false
}
return node[0].(*_Node), ok
}

Expand Down