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

fix: check if node ids actually map to nodes #523

Merged
merged 2 commits into from
Jun 22, 2021
Merged
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
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()) {
Sean-Tedrow-LB marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalStateException("Request node account IDs were not set before executing");
}
Sean-Tedrow-LB marked this conversation as resolved.
Show resolved Hide resolved

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);
Sean-Tedrow-LB marked this conversation as resolved.
Show resolved Hide resolved
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