Skip to content

Commit

Permalink
Merge branch 'main' into make-tests-run-concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
0xivanov authored Nov 12, 2024
2 parents 2d533d0 + a6369b9 commit 14b5009
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ jobs:
java-version: 17

- name: Setup Android SDK
uses: android-actions/setup-android@00854ea68c109d98c75d956347303bf7c45b0277 # v3.2.1
uses: android-actions/setup-android@9fc6c4e9069bf8d3d10b2204b1fb8f6ef7065407 # v3.2.2

- name: Setup Gradle
uses: gradle/actions/setup-gradle@d156388eb19639ec20ade50009f3d199ce1e2808 # v4.1.0
Expand Down
176 changes: 117 additions & 59 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.hedera.hashgraph.sdk.logger.LogLevel;
import com.hedera.hashgraph.sdk.logger.Logger;
Expand Down Expand Up @@ -299,7 +301,7 @@ public static Client forPreviewnet() {
* @throws Exception if the config is incorrect
*/
public static Client fromConfig(String json) throws Exception {
return fromConfig(new StringReader(json));
return Config.fromString(json).toClient();
}

/**
Expand All @@ -310,66 +312,16 @@ public static Client fromConfig(String json) throws Exception {
* @throws Exception if the config is incorrect
*/
public static Client fromConfig(Reader json) throws Exception {
Config config = new Gson().fromJson(json, Config.class);
Client client;

if (config.network == null) {
throw new Exception("Network is not set in provided json object");
} else if (config.network.isJsonObject()) {
var networks = config.network.getAsJsonObject();
Map<String, AccountId> nodes = new HashMap<>(networks.size());
for (Map.Entry<String, JsonElement> entry : networks.entrySet()) {
nodes.put(entry.getValue().toString().replace("\"", ""),
AccountId.fromString(entry.getKey().replace("\"", "")));
}
client = Client.forNetwork(nodes);
if (config.networkName != null) {
var networkNameString = config.networkName.getAsString();
try {
client.setNetworkName(NetworkName.fromString(networkNameString));
} catch (Exception ignored) {
throw new IllegalArgumentException("networkName in config was \"" + networkNameString
+ "\", expected either \"mainnet\", \"testnet\" or \"previewnet\"");
}
}
} else {
String networks = config.network.getAsString();
client = switch (networks) {
case MAINNET -> Client.forMainnet();
case TESTNET -> Client.forTestnet();
case PREVIEWNET -> Client.forPreviewnet();
default -> throw new JsonParseException("Illegal argument for network.");
};
}

if (config.operator != null) {
AccountId operatorAccount = AccountId.fromString(config.operator.accountId);
PrivateKey privateKey = PrivateKey.fromString(config.operator.privateKey);

client.setOperator(operatorAccount, privateKey);
}
return Config.fromJson(json).toClient();
}

//already set in previous set network if?
if (config.mirrorNetwork != null) {
if (config.mirrorNetwork.isJsonArray()) {
var mirrors = config.mirrorNetwork.getAsJsonArray();
List<String> listMirrors = new ArrayList<>(mirrors.size());
for (var i = 0; i < mirrors.size(); i++) {
listMirrors.add(mirrors.get(i).getAsString().replace("\"", ""));
}
client.setMirrorNetwork(listMirrors);
} else {
String mirror = config.mirrorNetwork.getAsString();
switch (mirror) {
case MAINNET -> client.mirrorNetwork = MirrorNetwork.forMainnet(client.executor);
case TESTNET -> client.mirrorNetwork = MirrorNetwork.forTestnet(client.executor);
case PREVIEWNET -> client.mirrorNetwork = MirrorNetwork.forPreviewnet(client.executor);
default -> throw new JsonParseException("Illegal argument for mirrorNetwork.");
}
}
private static Map<String, AccountId> getNetworkNodes(JsonObject networks) {
Map<String, AccountId> nodes = new HashMap<>(networks.size());
for (Map.Entry<String, JsonElement> entry : networks.entrySet()) {
nodes.put(entry.getValue().toString().replace("\"", ""),
AccountId.fromString(entry.getKey().replace("\"", "")));
}

return client;
return nodes;
}

/**
Expand Down Expand Up @@ -1447,5 +1399,111 @@ private static class ConfigOperator {
@Nullable
private String privateKey;
}

private static Config fromString(String json) {
return new Gson().fromJson((Reader) new StringReader(json), Config.class);
}

private static Config fromJson(Reader json) {
return new Gson().fromJson(json, Config.class);
}

private Client toClient() throws Exception, InterruptedException {
Client client = initializeWithNetwork();
setOperatorOn(client);
setMirrorNetworkOn(client);
return client;
}

private Client initializeWithNetwork() throws Exception {
if (network == null) {
throw new Exception("Network is not set in provided json object");
}

Client client;
if (network.isJsonObject()) {
client = clientFromNetworkJson();
} else {;
client = clientFromNetworkString();
}
return client;
}

private Client clientFromNetworkJson() {
Client client;
var networkJson = network.getAsJsonObject();
Map<String, AccountId> nodes = Client.getNetworkNodes(networkJson);
client = Client.forNetwork(nodes);
setNetworkNameOn(client);
return client;
}

private void setNetworkNameOn(Client client) {
if (networkName != null) {
var networkNameString = networkName.getAsString();
try {
client.setNetworkName(NetworkName.fromString(networkNameString));
} catch (Exception ignored) {
throw new IllegalArgumentException("networkName in config was \"" + networkNameString
+ "\", expected either \"mainnet\", \"testnet\" or \"previewnet\"");
}
}
}

private Client clientFromNetworkString() {
return switch (network.getAsString()) {
case MAINNET -> Client.forMainnet();
case TESTNET -> Client.forTestnet();
case PREVIEWNET -> Client.forPreviewnet();
default -> throw new JsonParseException("Illegal argument for network.");
};
}

private void setMirrorNetworkOn(Client client) throws InterruptedException {
if (mirrorNetwork != null) {
setMirrorNetwork(client);
}
}

private void setMirrorNetwork(Client client) throws InterruptedException {
if (mirrorNetwork.isJsonArray()) {
setMirrorNetworksFromJsonArray(client);
} else {
setMirrorNetworkFromString(client);
}
}

private void setMirrorNetworkFromString(Client client) {
String mirror = mirrorNetwork.getAsString();
switch (mirror) {
case Client.MAINNET -> client.mirrorNetwork = MirrorNetwork.forMainnet(client.executor);
case Client.TESTNET -> client.mirrorNetwork = MirrorNetwork.forTestnet(client.executor);
case Client.PREVIEWNET -> client.mirrorNetwork = MirrorNetwork.forPreviewnet(client.executor);
default -> throw new JsonParseException("Illegal argument for mirrorNetwork.");
}
}

private void setMirrorNetworksFromJsonArray(Client client) throws InterruptedException {
var mirrors = mirrorNetwork.getAsJsonArray();
List<String> listMirrors = getListMirrors(mirrors);
client.setMirrorNetwork(listMirrors);
}

private List<String> getListMirrors(JsonArray mirrors) {
List<String> listMirrors = new ArrayList<>(mirrors.size());
for (var i = 0; i < mirrors.size(); i++) {
listMirrors.add(mirrors.get(i).getAsString().replace("\"", ""));
}
return listMirrors;
}

private void setOperatorOn(Client client) {
if (operator != null) {
AccountId operatorAccount = AccountId.fromString(operator.accountId);
PrivateKey privateKey = PrivateKey.fromString(operator.privateKey);

client.setOperator(operatorAccount, privateKey);
}
}
}
}

0 comments on commit 14b5009

Please sign in to comment.