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

chore: remove IllegalStateException when specific node id is not present in address book #2056

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
11 changes: 7 additions & 4 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ void setNodesFromNodeAccountIds(Client client) {
// failure the system can retry with different proxy on each attempt
if (nodeAccountIds.size() == 1) {
var nodeProxies = client.network.getNodeProxies(nodeAccountIds.get(0));
if (nodeProxies == null || nodeProxies.size() == 0) {
if (nodeProxies == null || nodeProxies.isEmpty()) {
throw new IllegalStateException("Account ID did not map to valid node in the client's network");
}

Expand All @@ -615,15 +615,18 @@ void setNodesFromNodeAccountIds(Client client) {
for (var accountId : nodeAccountIds) {
@Nullable
var nodeProxies = client.network.getNodeProxies(accountId);
if (nodeProxies == null || nodeProxies.size() == 0) {
throw new IllegalStateException(
"Some node account IDs did not map to valid nodes in the client's network");
if (nodeProxies == null || nodeProxies.isEmpty()) {
continue;
}

var node = nodeProxies.get(random.nextInt(nodeProxies.size()));

nodes.add(Objects.requireNonNull(node));
}
if (nodes.isEmpty()) {
throw new IllegalStateException(
"All node account IDs did not map to valid nodes in the client's network");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.hedera.hashgraph.sdk.test.integration;

import com.hedera.hashgraph.sdk.*;
import java.util.Collections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.time.Duration;
Expand All @@ -32,14 +33,47 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class ClientIntegrationTest {

@Test
@DisplayName("fails when all the manually set nodes are not matching the address book")
void failsWhenNoNodesAreMatching() throws Exception {
var client = Client.forTestnet()
.setTransportSecurity(true);

var nodes = new ArrayList<AccountId>();
nodes.add(new AccountId(1000));
nodes.add(new AccountId(1001));
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> new AccountBalanceQuery()
.setNodeAccountIds(nodes)
.setAccountId(new AccountId(7))
.execute(client)).withMessageContaining("All node account IDs did not map to valid nodes in the client's network");
client.close();
}

@Test
@DisplayName("can skip invalid nodes")
void canSkipNodes() throws Exception {
var client = Client.forTestnet()
.setTransportSecurity(true);

var nodes = new ArrayList<>(client.getNetwork().values().stream().toList());
nodes.add(new AccountId(1000));
new AccountBalanceQuery()
.setNodeAccountIds(nodes)
.setAccountId(new AccountId(7))
.execute(client);

client.close();
}

@Test
@DisplayName("setNetwork() functions correctly")
void testReplaceNodes() throws Exception {
Map<String, AccountId> network = new HashMap<>();
network.put("0.testnet.hedera.com:50211", new AccountId(3));
network.put("1.testnet.hedera.com:50211", new AccountId(4));

try(var testEnv = new IntegrationTestEnv(1)){
try (var testEnv = new IntegrationTestEnv(1)) {

testEnv.client
.setMaxQueryPayment(new Hbar(2))
Expand Down Expand Up @@ -88,7 +122,7 @@ void transactionIdNetworkIsVerified() {
@Test
@DisplayName("`setMaxNodesPerTransaction()`")
void testMaxNodesPerTransaction() throws Exception {
try(var testEnv = new IntegrationTestEnv(1)){
try (var testEnv = new IntegrationTestEnv(1)) {

testEnv.client.setMaxNodesPerTransaction(1);

Expand All @@ -104,7 +138,7 @@ void testMaxNodesPerTransaction() throws Exception {

@Test
void ping() throws Exception {
try(var testEnv = new IntegrationTestEnv(1)){
try (var testEnv = new IntegrationTestEnv(1)) {
var network = testEnv.client.getNetwork();
var nodes = new ArrayList<>(network.values());

Expand All @@ -119,7 +153,7 @@ void ping() throws Exception {

@Test
void pingAll() throws Exception {
try(var testEnv = new IntegrationTestEnv()){
try (var testEnv = new IntegrationTestEnv()) {

testEnv.client.setMaxNodeAttempts(1);
testEnv.client.pingAll();
Expand All @@ -140,7 +174,7 @@ void pingAll() throws Exception {

@Test
void pingAllBadNetwork() throws Exception {
try(var testEnv = new IntegrationTestEnv(3)){
try (var testEnv = new IntegrationTestEnv(3)) {

// Skip if using local node.
// Note: this check should be removed once the local node is supporting multiple nodes.
Expand All @@ -161,7 +195,6 @@ void pingAllBadNetwork() throws Exception {

testEnv.client.setNetwork(network);


assertThatExceptionOfType(MaxAttemptsExceededException.class).isThrownBy(() -> {
testEnv.client.pingAll();
}).withMessageContaining("exceeded maximum attempts");
Expand All @@ -177,12 +210,12 @@ void pingAllBadNetwork() throws Exception {

assertThat(testEnv.client.getNetwork().values().size()).isEqualTo(1);
}

}

@Test
void pingAsync() throws Exception {
try(var testEnv = new IntegrationTestEnv(1)){
try (var testEnv = new IntegrationTestEnv(1)) {
var network = testEnv.client.getNetwork();
var nodes = new ArrayList<>(network.values());

Expand All @@ -197,7 +230,7 @@ void pingAsync() throws Exception {

@Test
void pingAllAsync() throws Exception {
try(var testEnv = new IntegrationTestEnv()){
try (var testEnv = new IntegrationTestEnv()) {

testEnv.client.setMaxNodeAttempts(1);
testEnv.client.pingAllAsync().get();
Expand All @@ -212,7 +245,7 @@ void pingAllAsync() throws Exception {
new AccountBalanceQuery()
.setAccountId(node)
.execute(testEnv.client);

}
}
}
Loading