Skip to content

Commit cda1929

Browse files
committed
refactor NodeUpdateTransactionIntegrationTest to fix Codacy warning
Signed-off-by: emiliyank <e.kadiyski@gmail.com>
1 parent 073a3b3 commit cda1929

File tree

3 files changed

+62
-152
lines changed

3 files changed

+62
-152
lines changed

sdk/src/main/java/com/hedera/hashgraph/sdk/Client.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,19 +1416,16 @@ public synchronized Client updateNetworkFromAddressBook() {
14161416
var fileId = FileId.getAddressBookFileIdFor(this.shard, this.realm);
14171417

14181418
logger.debug("Fetching address book from file {}", fileId);
1419-
System.out.println("Fetching address book from file " + fileId);
14201419

14211420
// Execute synchronously - no async complexity
14221421
var addressBook = new AddressBookQuery().setFileId(fileId).execute(this); // ← Synchronous!
14231422

14241423
logger.debug("Received address book with {} nodes", addressBook.nodeAddresses.size());
1425-
System.out.println("address book size: " + addressBook.nodeAddresses.size());
14261424

14271425
// Update the network
14281426
this.setNetworkFromAddressBook(addressBook);
14291427

14301428
logger.info("Address book update completed successfully");
1431-
System.out.println("Address book update completed successfully");
14321429

14331430
} catch (TimeoutException e) {
14341431
logger.warn("Failed to fetch address book: {}", e.getMessage());

sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,11 @@ private void executeAsyncInternal(
792792
if (status == com.hedera.hashgraph.sdk.Status.INVALID_NODE_ACCOUNT) {
793793
if (logger.isEnabledForLevel(LogLevel.TRACE)) {
794794
logger.trace(
795-
"Received INVALID_NODE_ACCOUNT; updating address book and marking node {} as unhealthy, attempt #{}",
796-
grpcRequest.getNode().getAccountId(),
797-
attempt);
795+
"Received INVALID_NODE_ACCOUNT; updating address book and marking node {} as unhealthy, attempt #{}",
796+
grpcRequest
797+
.getNode()
798+
.getAccountId(),
799+
attempt);
798800
}
799801
// Schedule async address book update
800802
client.updateNetworkFromAddressBook();

sdk/src/testIntegration/java/com/hedera/hashgraph/sdk/test/integration/NodeUpdateTransactionIntegrationTest.java

Lines changed: 57 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,7 @@ void shouldSucceedWhenUpdatingNodeAccountIdWithProperSignatures() throws Excepti
8888
// Given: A node with an existing account ID (0.0.3)
8989
// First, create a new account that will be used as the new node account ID
9090
var newAccountKey = PrivateKey.generateED25519();
91-
var newAccountCreateTransaction = new AccountCreateTransaction()
92-
.setKey(newAccountKey.getPublicKey())
93-
.setInitialBalance(Hbar.from(10))
94-
.setMaxTransactionFee(Hbar.from(1));
95-
96-
var newAccountResponse = newAccountCreateTransaction.execute(client);
97-
var newAccountReceipt = newAccountResponse.getReceipt(client);
98-
var newAccountId = newAccountReceipt.accountId;
91+
var newAccountId = createAccount(client, newAccountKey.getPublicKey(), Hbar.from(10));
9992

10093
// Create a node admin key
10194
var nodeAdminKey = PrivateKey.generateED25519();
@@ -174,17 +167,11 @@ void testNodeUpdateTransactionCanChangeNodeAccountUpdateAddressbookAndRetry() th
174167
client.setOperator(new AccountId(0, 0, 2), originalOperatorKey);
175168

176169
// Create the account that will be the node account id
177-
var resp = new AccountCreateTransaction()
178-
.setKey(originalOperatorKey.getPublicKey())
179-
.setInitialBalance(Hbar.from(1))
180-
.execute(client);
181-
182-
var receipt = resp.setValidateStatus(true).getReceipt(client);
183-
var newNodeAccountID = receipt.accountId;
170+
var newNodeAccountID = createAccount(client, originalOperatorKey.getPublicKey(), Hbar.from(1));
184171
assertThat(newNodeAccountID).isNotNull();
185172

186173
// Update node account id (0.0.3 -> newNodeAccountID)
187-
resp = new NodeUpdateTransaction()
174+
var resp = new NodeUpdateTransaction()
188175
.setNodeId(0)
189176
.setDescription("testUpdated")
190177
.setAccountId(newNodeAccountID)
@@ -193,14 +180,12 @@ void testNodeUpdateTransactionCanChangeNodeAccountUpdateAddressbookAndRetry() th
193180
System.out.println("Transaction node: " + resp.nodeId);
194181
System.out.println("Receipt query nodes: " + resp.getReceiptQuery().getNodeAccountIds());
195182
System.out.println("Client network: " + client.getNetwork());
196-
receipt = resp.setValidateStatus(true).getReceipt(client);
183+
var receipt = resp.setValidateStatus(true).getReceipt(client);
197184
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
198185

199186
// Wait for mirror node to import data
200187
Thread.sleep(10000);
201188

202-
var newAccountKey = PrivateKey.generateED25519();
203-
204189
// Submit to node 3 and node 4
205190
// Node 3 will fail with INVALID_NODE_ACCOUNT (because it now uses newNodeAccountID)
206191
// The SDK should automatically:
@@ -209,23 +194,10 @@ void testNodeUpdateTransactionCanChangeNodeAccountUpdateAddressbookAndRetry() th
209194
// 3. Update the address book asynchronously
210195
// 4. Mark node 3 as unhealthy
211196
// 5. Retry with node 4 which should succeed
212-
resp = new AccountCreateTransaction()
213-
.setKey(newAccountKey.getPublicKey())
214-
.setNodeAccountIds(List.of(new AccountId(0, 0, 3), new AccountId(0, 0, 4)))
215-
.execute(client);
216-
217-
// If we reach here without exception, the SDK successfully handled the error and retried
218-
assertThat(resp).isNotNull();
197+
executeAccountCreate(client, List.of(new AccountId(0, 0, 3), new AccountId(0, 0, 4)));
219198

220199
// This transaction should succeed using the updated node account ID
221-
resp = new AccountCreateTransaction()
222-
.setKey(newAccountKey.getPublicKey())
223-
.setNodeAccountIds(List.of(newNodeAccountID))
224-
.execute(client);
225-
226-
assertThat(resp).isNotNull();
227-
receipt = resp.setValidateStatus(true).getReceipt(client);
228-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
200+
executeAccountCreate(client, List.of(newNodeAccountID));
229201

230202
// Revert the node account id (newNodeAccountID -> 0.0.3)
231203
resp = new NodeUpdateTransaction()
@@ -259,12 +231,7 @@ void testNodeUpdateTransactionFailsWithInvalidSignatureWhenMissingNodeAdminSigna
259231

260232
// Given: Create a new operator account without node admin privileges
261233
var newOperatorKey = PrivateKey.generateED25519();
262-
var newAccountResponse = new AccountCreateTransaction()
263-
.setKey(newOperatorKey.getPublicKey())
264-
.setInitialBalance(Hbar.from(2))
265-
.execute(client);
266-
267-
var newOperatorAccountId = newAccountResponse.getReceipt(client).accountId;
234+
var newOperatorAccountId = createAccount(client, newOperatorKey.getPublicKey(), Hbar.from(2));
268235

269236
// Switch to the new operator (who doesn't have node admin privileges)
270237
client.setOperator(newOperatorAccountId, newOperatorKey);
@@ -310,12 +277,7 @@ void testNodeUpdateTransactionFailsWithInvalidSignatureWhenMissingAccountIdSigna
310277

311278
// Given: Create a new account that will be used as the new node account ID
312279
var newAccountKey = PrivateKey.generateED25519();
313-
var newAccountResponse = new AccountCreateTransaction()
314-
.setKey(newAccountKey.getPublicKey())
315-
.setInitialBalance(Hbar.from(2))
316-
.execute(client);
317-
318-
var newAccountId = newAccountResponse.getReceipt(client).accountId;
280+
var newAccountId = createAccount(client, newAccountKey.getPublicKey(), Hbar.from(2));
319281

320282
// When: A NodeUpdateTransaction is submitted with node admin signature
321283
// but WITHOUT the new account ID's signature
@@ -404,12 +366,7 @@ void testNodeUpdateTransactionFailsWithAccountDeletedForDeletedAccount() throws
404366

405367
// Given: Create a new account that will be deleted
406368
var newAccountKey = PrivateKey.generateED25519();
407-
var newAccountResponse = new AccountCreateTransaction()
408-
.setKey(newAccountKey.getPublicKey())
409-
.setInitialBalance(Hbar.from(2))
410-
.execute(client);
411-
412-
var newAccountId = newAccountResponse.getReceipt(client).accountId;
369+
var newAccountId = createAccount(client, newAccountKey.getPublicKey(), Hbar.from(2));
413370

414371
// Delete the account (transfer balance to operator account)
415372
var deleteResponse = new AccountDeleteTransaction()
@@ -463,23 +420,17 @@ void testSubsequentTransactionWithNewNodeAccountIdSucceeds() throws Exception {
463420
client.setOperator(new AccountId(0, 0, 2), originalOperatorKey);
464421

465422
// Given: Create a new account that will be the node account id
466-
var resp = new AccountCreateTransaction()
467-
.setKey(originalOperatorKey.getPublicKey())
468-
.setInitialBalance(Hbar.from(1))
469-
.execute(client);
470-
471-
var receipt = resp.setValidateStatus(true).getReceipt(client);
472-
var newNodeAccountID = receipt.accountId;
423+
var newNodeAccountID = createAccount(client, originalOperatorKey.getPublicKey(), Hbar.from(1));
473424
assertThat(newNodeAccountID).isNotNull();
474425

475426
// Update node 0's account id (0.0.3 -> newNodeAccountID)
476-
resp = new NodeUpdateTransaction()
427+
var resp = new NodeUpdateTransaction()
477428
.setNodeId(0)
478429
.setDescription("testUpdated")
479430
.setAccountId(newNodeAccountID)
480431
.execute(client);
481432

482-
receipt = resp.setValidateStatus(true).getReceipt(client);
433+
var receipt = resp.setValidateStatus(true).getReceipt(client);
483434
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
484435

485436
// Wait for mirror node to import data
@@ -488,28 +439,10 @@ void testSubsequentTransactionWithNewNodeAccountIdSucceeds() throws Exception {
488439
// Given: Successfully handled transaction with outdated node account ID
489440
// This transaction targets old node account ID (0.0.3) and new node account ID (0.0.4)
490441
// Node 0.0.3 will fail with INVALID_NODE_ACCOUNT and SDK will retry with 0.0.4
491-
var newAccountKey = PrivateKey.generateED25519();
492-
resp = new AccountCreateTransaction()
493-
.setKey(newAccountKey.getPublicKey())
494-
.setNodeAccountIds(List.of(new AccountId(0, 0, 3), new AccountId(0, 0, 4)))
495-
.execute(client);
496-
497-
// Verify the SDK handled the error and completed the transaction
498-
assertThat(resp).isNotNull();
499-
receipt = resp.setValidateStatus(true).getReceipt(client);
500-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
442+
executeAccountCreate(client, List.of(new AccountId(0, 0, 3), new AccountId(0, 0, 4)));
501443

502444
// When: Subsequent transaction targets the NEW node account ID directly
503-
var anotherAccountKey = PrivateKey.generateED25519();
504-
resp = new AccountCreateTransaction()
505-
.setKey(anotherAccountKey.getPublicKey())
506-
.setNodeAccountIds(List.of(newNodeAccountID))
507-
.execute(client);
508-
509-
// Then: The transaction should succeed without any errors
510-
assertThat(resp).isNotNull();
511-
receipt = resp.setValidateStatus(true).getReceipt(client);
512-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
445+
executeAccountCreate(client, List.of(newNodeAccountID));
513446

514447
// Cleanup: Revert the node account id (newNodeAccountID -> 0.0.3)
515448
resp = new NodeUpdateTransaction()
@@ -528,98 +461,76 @@ void testSubsequentTransactionWithNewNodeAccountIdSucceeds() throws Exception {
528461
@DisplayName(
529462
"Given an SDK receives INVALID_NODE_ACCOUNT for a node, when updating its network configuration, then the SDK updates its network with the latest node account IDs for subsequent transactions")
530463
void testSdkUpdatesNetworkConfigurationOnInvalidNodeAccount() throws Exception {
531-
// Set the network
532464
var network = new HashMap<String, AccountId>();
533465
network.put("localhost:50211", new AccountId(0, 0, 3));
534466
network.put("localhost:51211", new AccountId(0, 0, 4));
535467

536468
try (var client =
537469
Client.forNetwork(network).setTransportSecurity(false).setMirrorNetwork(List.of("localhost:5600"))) {
538470

539-
// Set the operator to be account 0.0.2
540471
var originalOperatorKey = PrivateKey.fromString(
541472
"302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137");
542473
client.setOperator(new AccountId(0, 0, 2), originalOperatorKey);
543474

544-
// Given: Verify initial network configuration
545-
var initialNetwork = client.getNetwork();
546-
assertThat(initialNetwork).containsEntry("localhost:50211", new AccountId(0, 0, 3));
547-
assertThat(initialNetwork).containsEntry("localhost:51211", new AccountId(0, 0, 4));
548-
549-
// Create a new account that will be the node account id
550-
var resp = new AccountCreateTransaction()
551-
.setKey(originalOperatorKey.getPublicKey())
552-
.setInitialBalance(Hbar.from(1))
553-
.execute(client);
554-
555-
var receipt = resp.setValidateStatus(true).getReceipt(client);
556-
var newNodeAccountID = receipt.accountId;
475+
var newNodeAccountID = createAccount(client, originalOperatorKey.getPublicKey(), Hbar.from(1));
557476
assertThat(newNodeAccountID).isNotNull();
558477

559-
// Update node 0's account id (0.0.3 -> newNodeAccountID)
560-
resp = new NodeUpdateTransaction()
561-
.setNodeId(0)
562-
.setDescription("testUpdated")
563-
.setAccountId(newNodeAccountID)
564-
.execute(client);
565-
566-
receipt = resp.setValidateStatus(true).getReceipt(client);
567-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
478+
updateNodeAccountId(client, 0, newNodeAccountID, null);
568479

569-
// Wait for mirror node to import data
570480
Thread.sleep(10000);
571481

572-
// When: Submit transaction with outdated node account ID (0.0.3)
573-
// This will trigger INVALID_NODE_ACCOUNT error and the SDK should update its network
574-
var newAccountKey = PrivateKey.generateED25519();
575-
resp = new AccountCreateTransaction()
576-
.setKey(newAccountKey.getPublicKey())
577-
.setNodeAccountIds(List.of(new AccountId(0, 0, 3), new AccountId(0, 0, 4)))
578-
.execute(client);
482+
// Trigger INVALID_NODE_ACCOUNT error and retry
483+
executeAccountCreate(client, List.of(new AccountId(0, 0, 3), new AccountId(0, 0, 4)));
579484

580-
// Verify the transaction succeeded (SDK retried with another node)
581-
assertThat(resp).isNotNull();
582-
receipt = resp.setValidateStatus(true).getReceipt(client);
583-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
584-
585-
// Then: Verify the SDK has updated its network configuration
586-
// The network should now contain the new node account ID
587-
var updatedNetwork = client.getNetwork();
588-
589-
// The network map should have been updated with the new account ID
590-
// Note: The address book update happens asynchronously, so we verify
591-
// that subsequent transactions can successfully target the new account ID
592-
593-
// Verify subsequent transaction with the new node account ID succeeds
594-
var anotherAccountKey = PrivateKey.generateED25519();
595-
resp = new AccountCreateTransaction()
596-
.setKey(anotherAccountKey.getPublicKey())
597-
.setNodeAccountIds(List.of(newNodeAccountID))
598-
.execute(client);
599-
600-
assertThat(resp).isNotNull();
601-
receipt = resp.setValidateStatus(true).getReceipt(client);
602-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
485+
// Verify subsequent transaction with new node account ID
486+
executeAccountCreate(client, List.of(newNodeAccountID));
603487

604488
// Verify the network configuration now includes the new account ID
605-
// This confirms the SDK successfully updated its network from the address book
606489
var finalNetwork = client.getNetwork();
607490
var hasNewAccountId =
608491
finalNetwork.values().stream().anyMatch(accountId -> accountId.equals(newNodeAccountID));
609492
assertThat(hasNewAccountId)
610493
.as("Client network should contain the new node account ID after address book update")
611494
.isTrue();
612495

613-
// Cleanup: Revert the node account id (newNodeAccountID -> 0.0.3)
614-
resp = new NodeUpdateTransaction()
615-
.setNodeId(0)
616-
.setNodeAccountIds(List.of(newNodeAccountID))
617-
.setDescription("testUpdated")
618-
.setAccountId(new AccountId(0, 0, 3))
619-
.execute(client);
496+
// Cleanup
497+
updateNodeAccountId(client, 0, new AccountId(0, 0, 3), List.of(newNodeAccountID));
498+
}
499+
}
620500

621-
receipt = resp.setValidateStatus(true).getReceipt(client);
622-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
501+
private AccountId createAccount(Client client, Key key, Hbar initialBalance) throws Exception {
502+
var resp = new AccountCreateTransaction()
503+
.setKey(key)
504+
.setInitialBalance(initialBalance)
505+
.execute(client);
506+
return resp.setValidateStatus(true).getReceipt(client).accountId;
507+
}
508+
509+
private void updateNodeAccountId(Client client, long nodeId, AccountId newAccountId, List<AccountId> nodeAccountIds)
510+
throws Exception {
511+
var transaction = new NodeUpdateTransaction()
512+
.setNodeId(nodeId)
513+
.setDescription("testUpdated")
514+
.setAccountId(newAccountId);
515+
516+
if (nodeAccountIds != null) {
517+
transaction.setNodeAccountIds(nodeAccountIds);
623518
}
519+
520+
var resp = transaction.execute(client);
521+
var receipt = resp.setValidateStatus(true).getReceipt(client);
522+
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
523+
}
524+
525+
private void executeAccountCreate(Client client, List<AccountId> nodeAccountIds) throws Exception {
526+
var newAccountKey = PrivateKey.generateED25519();
527+
var resp = new AccountCreateTransaction()
528+
.setKey(newAccountKey.getPublicKey())
529+
.setNodeAccountIds(nodeAccountIds)
530+
.execute(client);
531+
532+
assertThat(resp).isNotNull();
533+
var receipt = resp.setValidateStatus(true).getReceipt(client);
534+
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
624535
}
625536
}

0 commit comments

Comments
 (0)