diff --git a/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java b/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java index 09a219a63..be3dac01d 100644 --- a/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java +++ b/sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java @@ -11,7 +11,9 @@ import javax.annotation.Nullable; import java.util.Collections; +import java.util.Objects; import java.util.List; +import java.util.ArrayList; import static com.hedera.hashgraph.sdk.FutureConverter.toCompletableFuture; @@ -21,6 +23,7 @@ abstract class Executable implements W protected int maxAttempts = 10; protected int nextNodeIndex = 0; protected List nodeAccountIds = Collections.emptyList(); + protected List nodes = new ArrayList<>(); Executable() { } @@ -82,7 +85,26 @@ public SdkRequestT setNodeAccountIds(List nodeAccountIds) { @FunctionalExecutable public CompletableFuture executeAsync(Client client) { - return onExecuteAsync(client).thenCompose((v) -> executeAsync(client, 1, null)); + return onExecuteAsync(client).thenCompose((v) -> { + if(nodeAccountIds.isEmpty()) { + throw new IllegalStateException("Request node account IDs were not set before executing"); + } + + setNodesFromNodeAccountIds(client); + + return executeAsync(client, 1, null); + }); + } + + private void setNodesFromNodeAccountIds(Client client) throws IllegalStateException { + for(var accountId : nodeAccountIds) { + @Nullable + var node = client.network.networkNodes.get(accountId); + if(node == null) { + throw new IllegalStateException("Some node account IDs did not map to valid nodes in the client's network"); + } + nodes.add(Objects.requireNonNull(node)); + } } private CompletableFuture executeAsync(Client client, int attempt, @Nullable Throwable lastException) { @@ -90,7 +112,7 @@ private CompletableFuture executeAsync(Client client, int attempt, @Nullable return CompletableFuture.failedFuture(new Exception("Failed to get gRPC response within maximum retry count", lastException)); } - var node = client.network.networkNodes.get(getNodeAccountId()); + var node = nodes.get(nextNodeIndex); node.inUse(); logger.trace("Sending request #{} to node {}: {}", attempt, node.accountId, this); @@ -193,14 +215,6 @@ void advanceRequest() { */ abstract MethodDescriptor getMethodDescriptor(); - final AccountId getNodeAccountId() { - if (!nodeAccountIds.isEmpty()) { - return nodeAccountIds.get(nextNodeIndex); - } else { - throw new IllegalStateException("Request node account IDs were not set before executing"); - } - } - @Nullable abstract TransactionId getTransactionId();