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