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

feat: support ports that are not well known for use with port forwarding #1930

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
20 changes: 17 additions & 3 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/BaseNodeAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BaseNodeAddress {
@Nullable
private final String address;
private final int port;
private final boolean secure;

/**
* Constructor.
Expand All @@ -49,9 +50,22 @@ class BaseNodeAddress {
* @param port the port part
*/
public BaseNodeAddress(@Nullable String name, @Nullable String address, int port) {
this(name, address, port, false);
}

/**
* Constructor.
*
* @param name the name part
* @param address the address part
* @param port the port part
* @param secure secure transport
*/
public BaseNodeAddress(@Nullable String name, @Nullable String address, int port, boolean secure) {
this.name = name;
this.address = address;
this.port = port;
this.secure = secure;
}

/**
Expand Down Expand Up @@ -118,7 +132,7 @@ public boolean isInProcess() {
* @return are we secure
*/
public boolean isTransportSecurity() {
return port == PORT_NODE_TLS || port == PORT_MIRROR_TLS;
return port == PORT_NODE_TLS || port == PORT_MIRROR_TLS || secure;
}

/**
Expand All @@ -128,7 +142,7 @@ public boolean isTransportSecurity() {
*/
public BaseNodeAddress toInsecure() {
var newPort = (this.port == PORT_NODE_TLS) ? PORT_NODE_PLAIN : this.port;
return new BaseNodeAddress(name, address, newPort);
return new BaseNodeAddress(name, address, newPort, false);
}

/**
Expand All @@ -138,7 +152,7 @@ public BaseNodeAddress toInsecure() {
*/
public BaseNodeAddress toSecure() {
var newPort = (this.port == PORT_NODE_PLAIN) ? PORT_NODE_TLS : this.port;
return new BaseNodeAddress(name, address, newPort);
return new BaseNodeAddress(name, address, newPort, true);
}

@Override
Expand Down
8 changes: 1 addition & 7 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/
package com.hedera.hashgraph.sdk;

import static com.hedera.hashgraph.sdk.BaseNodeAddress.PORT_NODE_PLAIN;
import static com.hedera.hashgraph.sdk.BaseNodeAddress.PORT_NODE_TLS;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.Gson;
Expand Down Expand Up @@ -469,10 +466,7 @@ void untrackSubscription(SubscriptionHandle subscriptionHandle) {
*/
public synchronized Client setNetworkFromAddressBook(NodeAddressBook addressBook, boolean updateAddressBook)
throws InterruptedException, TimeoutException {
network.setNetwork(Network.addressBookToNetwork(
addressBook.nodeAddresses,
isTransportSecurity() ? PORT_NODE_TLS : PORT_NODE_PLAIN
));
network.setNetwork(Network.addressBookToNetwork(addressBook.nodeAddresses));
if (updateAddressBook) {
network.setAddressBook(addressBook);
}
Expand Down
23 changes: 7 additions & 16 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
import com.google.common.io.ByteStreams;
import com.google.errorprone.annotations.Var;
import com.google.protobuf.ByteString;

import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -35,6 +31,9 @@
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

/**
* Internal utility class.
Expand Down Expand Up @@ -83,9 +82,7 @@ static Network forNetwork(ExecutorService executor, Map<String, AccountId> netwo
static Network forMainnet(ExecutorService executor) {
var addressBook = getAddressBookForLedger(LedgerId.MAINNET);
HashMap<String, AccountId> network = addressBookToNetwork(
Objects.requireNonNull(addressBook).values(),
BaseNodeAddress.PORT_NODE_PLAIN
);
Objects.requireNonNull(addressBook).values());
return new Network(executor, network).setLedgerIdInternal(LedgerId.MAINNET, addressBook);
}

Expand All @@ -98,9 +95,7 @@ static Network forMainnet(ExecutorService executor) {
static Network forTestnet(ExecutorService executor) {
var addressBook = getAddressBookForLedger(LedgerId.TESTNET);
HashMap<String, AccountId> network = addressBookToNetwork(
Objects.requireNonNull(addressBook).values(),
BaseNodeAddress.PORT_NODE_PLAIN
);
Objects.requireNonNull(addressBook).values());
return new Network(executor, network).setLedgerIdInternal(LedgerId.TESTNET, addressBook);
}

Expand All @@ -113,9 +108,7 @@ static Network forTestnet(ExecutorService executor) {
static Network forPreviewnet(ExecutorService executor) {
var addressBook = getAddressBookForLedger(LedgerId.PREVIEWNET);
HashMap<String, AccountId> network = addressBookToNetwork(
Objects.requireNonNull(addressBook).values(),
BaseNodeAddress.PORT_NODE_PLAIN
);
Objects.requireNonNull(addressBook).values());
return new Network(executor, network).setLedgerIdInternal(LedgerId.PREVIEWNET, addressBook);
}

Expand Down Expand Up @@ -205,13 +198,11 @@ private static Map<AccountId, NodeAddress> getAddressBookForLedger(@Nullable Led
readAddressBookResource("addressbook/" + ledgerId + ".pb");
}

static HashMap<String, AccountId> addressBookToNetwork(Collection<NodeAddress> addressBook, int desiredPort) {
static HashMap<String, AccountId> addressBookToNetwork(Collection<NodeAddress> addressBook) {
var network = new HashMap<String, AccountId>();
for (var nodeAddress : addressBook) {
for (var endpoint : nodeAddress.addresses) {
if (endpoint.port == desiredPort) {
network.put(endpoint.toString(), nodeAddress.accountId);
}
}
}
return network;
Expand Down
Loading