Skip to content

Commit

Permalink
fix: check if node ids actually map to nodes (#523)
Browse files Browse the repository at this point in the history
* fix: check if node ids actually map to nodes

Signed-off-by: Sean-Tedrow-LB <sean.tedrow@launchbadge.com>

* style: made requested style changes

Signed-off-by: Sean-Tedrow-LB <sean.tedrow@launchbadge.com>
  • Loading branch information
Sean-Tedrow-LB authored Jun 22, 2021
1 parent a12d3c4 commit a1f068d
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Executable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,6 +23,7 @@ abstract class Executable<SdkRequestT, ProtoRequestT, ResponseT, O> implements W
protected int maxAttempts = 10;
protected int nextNodeIndex = 0;
protected List<AccountId> nodeAccountIds = Collections.emptyList();
protected List<Node> nodes = new ArrayList<>();

Executable() {
}
Expand Down Expand Up @@ -82,15 +85,34 @@ public SdkRequestT setNodeAccountIds(List<AccountId> nodeAccountIds) {

@FunctionalExecutable
public CompletableFuture<O> 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<O> executeAsync(Client client, int attempt, @Nullable Throwable lastException) {
if (attempt > maxAttempts) {
return CompletableFuture.<O>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);
Expand Down Expand Up @@ -193,14 +215,6 @@ void advanceRequest() {
*/
abstract MethodDescriptor<ProtoRequestT, ResponseT> 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();

Expand Down

0 comments on commit a1f068d

Please sign in to comment.