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

     1  //go:build all || unit
     2  // +build all unit
     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  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	"google.golang.org/grpc/codes"
    32  	"google.golang.org/grpc/status"
    33  
    34  	"github.com/stretchr/testify/assert"
    35  
    36  	"github.com/stretchr/testify/require"
    37  )
    38  
    39  func TestUnitNodeCreateTransactionValidate(t *testing.T) {
    40  	t.Parallel()
    41  
    42  	client, err := _NewMockClient()
    43  	client.SetLedgerID(*NewLedgerIDTestnet())
    44  	require.NoError(t, err)
    45  	client.SetAutoValidateChecksums(true)
    46  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    47  	require.NoError(t, err)
    48  
    49  	tx := NewNodeCreateTransaction().
    50  		SetAccountID(accountID)
    51  
    52  	err = tx.validateNetworkOnIDs(client)
    53  	require.NoError(t, err)
    54  }
    55  
    56  func TestUnitNodeCreateTransactionValidateWrong(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	client, err := _NewMockClient()
    60  	client.SetLedgerID(*NewLedgerIDTestnet())
    61  	require.NoError(t, err)
    62  	client.SetAutoValidateChecksums(true)
    63  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    64  	require.NoError(t, err)
    65  
    66  	tx := NewNodeCreateTransaction().
    67  		SetAccountID(accountID)
    68  
    69  	err = tx.validateNetworkOnIDs(client)
    70  	assert.Error(t, err)
    71  	if err != nil {
    72  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    73  	}
    74  }
    75  
    76  func endpoints(offsets ...uint) []Endpoint {
    77  	endpoints := make([]Endpoint, 0)
    78  
    79  	for _, offset := range offsets {
    80  		endpoints = append(endpoints, Endpoint{
    81  			address: []byte{byte(offset), byte(offset), byte(offset), byte(offset)},
    82  		})
    83  	}
    84  
    85  	return endpoints
    86  }
    87  
    88  func TestUnitNodeCreateTransactionMock(t *testing.T) {
    89  	t.Parallel()
    90  
    91  	responses := [][]interface{}{{
    92  		status.New(codes.Unavailable, "node is UNAVAILABLE").Err(),
    93  		status.New(codes.Internal, "Received RST_STREAM with code 0").Err(),
    94  		&services.TransactionResponse{
    95  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_BUSY,
    96  		},
    97  		&services.TransactionResponse{
    98  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
    99  		},
   100  		&services.Response{
   101  			Response: &services.Response_TransactionGetReceipt{
   102  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
   103  					Header: &services.ResponseHeader{
   104  						Cost:         0,
   105  						ResponseType: services.ResponseType_COST_ANSWER,
   106  					},
   107  				},
   108  			},
   109  		},
   110  		&services.Response{
   111  			Response: &services.Response_TransactionGetReceipt{
   112  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
   113  					Header: &services.ResponseHeader{
   114  						Cost:         0,
   115  						ResponseType: services.ResponseType_ANSWER_ONLY,
   116  					},
   117  					Receipt: &services.TransactionReceipt{
   118  						Status: services.ResponseCodeEnum_RECEIPT_NOT_FOUND,
   119  					},
   120  				},
   121  			},
   122  		},
   123  		&services.Response{
   124  			Response: &services.Response_TransactionGetReceipt{
   125  				TransactionGetReceipt: &services.TransactionGetReceiptResponse{
   126  					Header: &services.ResponseHeader{
   127  						Cost:         0,
   128  						ResponseType: services.ResponseType_ANSWER_ONLY,
   129  					},
   130  					Receipt: &services.TransactionReceipt{
   131  						Status: services.ResponseCodeEnum_SUCCESS,
   132  						AccountID: &services.AccountID{Account: &services.AccountID_AccountNum{
   133  							AccountNum: 234,
   134  						}},
   135  					},
   136  				},
   137  			},
   138  		},
   139  	}}
   140  
   141  	client, server := NewMockClientAndServer(responses)
   142  	defer server.Close()
   143  
   144  	newKey, err := PrivateKeyGenerateEd25519()
   145  	require.NoError(t, err)
   146  
   147  	tran := TransactionIDGenerate(AccountID{Account: 3})
   148  
   149  	resp, err := NewNodeCreateTransaction().
   150  		SetNodeAccountIDs([]AccountID{{Account: 3}, {Account: 4}}).
   151  		SetAdminKey(newKey).
   152  		SetDescription("test").
   153  		SetGossipEndpoints(endpoints(0, 1, 2)).
   154  		SetServiceEndpoints(endpoints(3, 4, 5)).
   155  		SetGossipCaCertificate([]byte{111}).
   156  		SetGrpcCertificateHash([]byte{222}).
   157  		SetTransactionID(tran).
   158  		Execute(client)
   159  	require.NoError(t, err)
   160  
   161  	receipt, err := resp.GetReceipt(client)
   162  	require.NoError(t, err)
   163  	require.Equal(t, receipt.AccountID, &AccountID{Account: 234})
   164  }
   165  
   166  func TestUnitNodeCreateTransactionGet(t *testing.T) {
   167  	t.Parallel()
   168  
   169  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   170  
   171  	key, err := PrivateKeyGenerateEd25519()
   172  
   173  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   174  
   175  	transaction, err := NewNodeCreateTransaction().
   176  		SetTransactionID(transactionID).
   177  		SetNodeAccountIDs(nodeAccountID).
   178  		SetAdminKey(key).
   179  		SetTransactionMemo("").
   180  		SetDescription("test").
   181  		SetGossipEndpoints(endpoints(0, 1, 2)).
   182  		SetServiceEndpoints(endpoints(3, 4, 5)).
   183  		SetGossipCaCertificate([]byte{111}).
   184  		SetGrpcCertificateHash([]byte{222}).
   185  		SetTransactionValidDuration(60 * time.Second).
   186  		Freeze()
   187  	require.NoError(t, err)
   188  
   189  	transaction.GetTransactionID()
   190  	transaction.GetNodeAccountIDs()
   191  
   192  	_, err = transaction.GetTransactionHash()
   193  	require.NoError(t, err)
   194  
   195  	transaction.GetMaxTransactionFee()
   196  	transaction.GetTransactionMemo()
   197  	transaction.GetRegenerateTransactionID()
   198  	_, err = transaction.GetSignatures()
   199  	require.NoError(t, err)
   200  	transaction.GetRegenerateTransactionID()
   201  	transaction.GetMaxTransactionFee()
   202  	transaction.GetRegenerateTransactionID()
   203  	transaction.GetAccountID()
   204  	transaction.GetDescription()
   205  	transaction.GetGossipEndpoints()
   206  	transaction.GetServiceEndpoints()
   207  	transaction.GetGossipCaCertificate()
   208  	transaction.GetGrpcCertificateHash()
   209  	transaction.GetAdminKey()
   210  }
   211  
   212  func TestUnitNodeCreateTransactionSetNothing(t *testing.T) {
   213  	t.Parallel()
   214  
   215  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   216  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   217  
   218  	transaction, err := NewNodeCreateTransaction().
   219  		SetTransactionID(transactionID).
   220  		SetNodeAccountIDs(nodeAccountID).
   221  		Freeze()
   222  	require.NoError(t, err)
   223  
   224  	transaction.GetTransactionID()
   225  	transaction.GetNodeAccountIDs()
   226  
   227  	_, err = transaction.GetTransactionHash()
   228  	require.NoError(t, err)
   229  
   230  	transaction.GetMaxTransactionFee()
   231  	transaction.GetTransactionMemo()
   232  	transaction.GetRegenerateTransactionID()
   233  	_, err = transaction.GetSignatures()
   234  	require.NoError(t, err)
   235  	transaction.GetRegenerateTransactionID()
   236  	transaction.GetMaxTransactionFee()
   237  	transaction.GetRegenerateTransactionID()
   238  	transaction.GetRegenerateTransactionID()
   239  	transaction.GetMaxTransactionFee()
   240  	transaction.GetRegenerateTransactionID()
   241  	transaction.GetAccountID()
   242  	transaction.GetDescription()
   243  	transaction.GetGossipEndpoints()
   244  	transaction.GetServiceEndpoints()
   245  	transaction.GetGossipCaCertificate()
   246  	transaction.GetGrpcCertificateHash()
   247  	transaction.GetAdminKey()
   248  }
   249  
   250  func TestUnitNodeCreateTransactionProtoCheck(t *testing.T) {
   251  	t.Parallel()
   252  
   253  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   254  	stackedAccountID := AccountID{Account: 5}
   255  
   256  	key, err := PrivateKeyGenerateEd25519()
   257  	require.NoError(t, err)
   258  	gossipEndpoints := endpoints(1, 2, 3)
   259  	serviceEndpoints := endpoints(3, 4, 5)
   260  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   261  
   262  	transaction, err := NewNodeCreateTransaction().
   263  		SetTransactionID(transactionID).
   264  		SetNodeAccountIDs(nodeAccountID).
   265  		SetAccountID(stackedAccountID).
   266  		SetAdminKey(key).
   267  		SetTransactionMemo("").
   268  		SetDescription("test").
   269  		SetGossipEndpoints(gossipEndpoints).
   270  		SetServiceEndpoints(serviceEndpoints).
   271  		SetGossipCaCertificate([]byte{111}).
   272  		SetGrpcCertificateHash([]byte{222}).
   273  		SetTransactionValidDuration(60 * time.Second).
   274  		Freeze()
   275  	require.NoError(t, err)
   276  
   277  	transaction.GetTransactionID()
   278  	transaction.GetNodeAccountIDs()
   279  
   280  	proto := transaction.build().GetNodeCreate()
   281  	require.Equal(t, proto.AccountId.String(), stackedAccountID._ToProtobuf().String())
   282  	require.Equal(t, proto.Description, "test")
   283  	require.Equal(t, proto.GossipEndpoint[0], gossipEndpoints[0]._ToProtobuf())
   284  	require.Equal(t, proto.ServiceEndpoint[0], serviceEndpoints[0]._ToProtobuf())
   285  	require.Equal(t, proto.GossipCaCertificate, []byte{111})
   286  	require.Equal(t, proto.GrpcCertificateHash, []byte{222})
   287  	require.Equal(t, proto.AdminKey, key._ToProtoKey())
   288  }
   289  
   290  func TestUnitNodeCreateTransactionCoverage(t *testing.T) {
   291  	t.Parallel()
   292  
   293  	checksum := "dmqui"
   294  	account := AccountID{Account: 3, checksum: &checksum}
   295  	nodeAccountID := []AccountID{{Account: 10}}
   296  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   297  
   298  	key, err := PrivateKeyGenerateEd25519()
   299  	require.NoError(t, err)
   300  
   301  	client, err := _NewMockClient()
   302  	client.SetLedgerID(*NewLedgerIDTestnet())
   303  	require.NoError(t, err)
   304  	client.SetAutoValidateChecksums(true)
   305  
   306  	trx, err := NewNodeCreateTransaction().
   307  		SetTransactionID(transactionID).
   308  		SetNodeAccountIDs(nodeAccountID).
   309  		SetAdminKey(key).
   310  		SetAccountID(account).
   311  		SetMaxTransactionFee(NewHbar(3)).
   312  		SetMaxRetry(3).
   313  		SetMaxBackoff(time.Second * 30).
   314  		SetMinBackoff(time.Second * 10).
   315  		SetTransactionMemo("no").
   316  		SetTransactionValidDuration(time.Second * 30).
   317  		SetRegenerateTransactionID(false).
   318  		Freeze()
   319  	require.NoError(t, err)
   320  
   321  	trx.validateNetworkOnIDs(client)
   322  	_, err = trx.Schedule()
   323  	require.NoError(t, err)
   324  	trx.GetTransactionID()
   325  	trx.GetNodeAccountIDs()
   326  	trx.GetMaxRetry()
   327  	trx.GetMaxTransactionFee()
   328  	trx.GetMaxBackoff()
   329  	trx.GetMinBackoff()
   330  	trx.GetRegenerateTransactionID()
   331  	byt, err := trx.ToBytes()
   332  	require.NoError(t, err)
   333  	txFromBytes, err := TransactionFromBytes(byt)
   334  	require.NoError(t, err)
   335  	sig, err := key.SignTransaction(&trx.Transaction)
   336  	require.NoError(t, err)
   337  
   338  	_, err = trx.GetTransactionHash()
   339  	require.NoError(t, err)
   340  	trx.GetMaxTransactionFee()
   341  	trx.GetTransactionMemo()
   342  	trx.GetRegenerateTransactionID()
   343  	trx.GetAccountID()
   344  	trx.GetDescription()
   345  	trx.GetGossipEndpoints()
   346  	trx.GetServiceEndpoints()
   347  	trx.GetGossipCaCertificate()
   348  	trx.GetGrpcCertificateHash()
   349  	trx.GetAdminKey()
   350  	_, err = trx.GetSignatures()
   351  	require.NoError(t, err)
   352  	trx.getName()
   353  	switch b := txFromBytes.(type) {
   354  	case NodeCreateTransaction:
   355  		b.AddSignature(key.PublicKey(), sig)
   356  	}
   357  }