Skip to content

Commit ddadcde

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

File tree

3 files changed

+48
-71
lines changed

3 files changed

+48
-71
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: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -528,98 +528,76 @@ void testSubsequentTransactionWithNewNodeAccountIdSucceeds() throws Exception {
528528
@DisplayName(
529529
"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")
530530
void testSdkUpdatesNetworkConfigurationOnInvalidNodeAccount() throws Exception {
531-
// Set the network
532531
var network = new HashMap<String, AccountId>();
533532
network.put("localhost:50211", new AccountId(0, 0, 3));
534533
network.put("localhost:51211", new AccountId(0, 0, 4));
535534

536535
try (var client =
537536
Client.forNetwork(network).setTransportSecurity(false).setMirrorNetwork(List.of("localhost:5600"))) {
538537

539-
// Set the operator to be account 0.0.2
540538
var originalOperatorKey = PrivateKey.fromString(
541539
"302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137");
542540
client.setOperator(new AccountId(0, 0, 2), originalOperatorKey);
543541

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;
542+
var newNodeAccountID = createAccount(client, originalOperatorKey.getPublicKey());
557543
assertThat(newNodeAccountID).isNotNull();
558544

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);
545+
updateNodeAccountId(client, 0, newNodeAccountID, null);
568546

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

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);
579-
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
549+
// Trigger INVALID_NODE_ACCOUNT error and retry
550+
executeAccountCreate(client, List.of(new AccountId(0, 0, 3), new AccountId(0, 0, 4)));
592551

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);
552+
// Verify subsequent transaction with new node account ID
553+
executeAccountCreate(client, List.of(newNodeAccountID));
603554

604555
// Verify the network configuration now includes the new account ID
605-
// This confirms the SDK successfully updated its network from the address book
606556
var finalNetwork = client.getNetwork();
607557
var hasNewAccountId =
608558
finalNetwork.values().stream().anyMatch(accountId -> accountId.equals(newNodeAccountID));
609559
assertThat(hasNewAccountId)
610560
.as("Client network should contain the new node account ID after address book update")
611561
.isTrue();
612562

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);
563+
// Cleanup
564+
updateNodeAccountId(client, 0, new AccountId(0, 0, 3), List.of(newNodeAccountID));
565+
}
566+
}
620567

621-
receipt = resp.setValidateStatus(true).getReceipt(client);
622-
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
568+
private AccountId createAccount(Client client, Key key) throws Exception {
569+
var resp = new AccountCreateTransaction()
570+
.setKey(key)
571+
.setInitialBalance(Hbar.from(1))
572+
.execute(client);
573+
return resp.setValidateStatus(true).getReceipt(client).accountId;
574+
}
575+
576+
private void updateNodeAccountId(Client client, long nodeId, AccountId newAccountId, List<AccountId> nodeAccountIds)
577+
throws Exception {
578+
var transaction = new NodeUpdateTransaction()
579+
.setNodeId(nodeId)
580+
.setDescription("testUpdated")
581+
.setAccountId(newAccountId);
582+
583+
if (nodeAccountIds != null) {
584+
transaction.setNodeAccountIds(nodeAccountIds);
623585
}
586+
587+
var resp = transaction.execute(client);
588+
var receipt = resp.setValidateStatus(true).getReceipt(client);
589+
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
590+
}
591+
592+
private void executeAccountCreate(Client client, List<AccountId> nodeAccountIds) throws Exception {
593+
var newAccountKey = PrivateKey.generateED25519();
594+
var resp = new AccountCreateTransaction()
595+
.setKey(newAccountKey.getPublicKey())
596+
.setNodeAccountIds(nodeAccountIds)
597+
.execute(client);
598+
599+
assertThat(resp).isNotNull();
600+
var receipt = resp.setValidateStatus(true).getReceipt(client);
601+
assertThat(receipt.status).isEqualTo(Status.SUCCESS);
624602
}
625603
}

0 commit comments

Comments
 (0)