diff --git a/applications/cli/api-tests/admin.http b/applications/cli/api-tests/admin.http index 64c495c..82f7c4c 100644 --- a/applications/cli/api-tests/admin.http +++ b/applications/cli/api-tests/admin.http @@ -211,3 +211,11 @@ GET {{admin_url}}/local-cluster/api/epochs/latest client.assert(response.body.epoch>= 0, "Lastest epoch not found. Actual value: " + response.body.epoch) %} +### +### Get genesis hash +GET {{admin_url}}/local-cluster/api/admin/devnet/genesis/hash + +> {% + client.assert(response.status === 200, "Response status is not 200"); + client.assert(response.body.length > 10, "genesis hash not found " + response.body) +%} diff --git a/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/ClusterService.java b/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/ClusterService.java index a6e9191..9bd64fd 100644 --- a/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/ClusterService.java +++ b/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/ClusterService.java @@ -11,6 +11,7 @@ import com.bloxbean.cardano.yacicli.localcluster.profiles.GenesisProfile; import com.bloxbean.cardano.yacicli.commands.tail.BlockStreamerService; import com.bloxbean.cardano.yacicli.output.OutputFormatter; +import com.bloxbean.cardano.yacicli.util.ProcessUtil; import com.bloxbean.cardano.yacicli.util.TemplateEngine; import com.bloxbean.cardano.yacicli.util.AdvancedTemplateEngine; import com.bloxbean.cardano.yacicli.util.ZipUtil; @@ -78,6 +79,9 @@ public class ClusterService { @Autowired private ApplicationConfig applicationConfig; + @Autowired + private ProcessUtil processUtil; + public ClusterService(ClusterConfig config, ClusterStartService clusterStartService, BlockStreamerService blockStreamerService) { this.clusterConfig = config; this.clusterStartService = clusterStartService; @@ -504,6 +508,45 @@ public void ltail(String clusterName, boolean showMint, boolean showInputs, bool } } + public String getGenesisHash(String clusterName) { + try { + Path binFolder = Path.of(clusterConfig.getCLIBinFolder()); + if (!Files.exists(binFolder)) + Files.createDirectories(binFolder); + + String cardanoCLI = OSUtil.getOperatingSystem() == OSUtil.OS.WINDOWS ? "cardano-cli.exe" : "cardano-cli"; + Path cardanoCliPath = binFolder.resolve(cardanoCLI); //TODO -- Handle for windows + if (!Files.exists(cardanoCliPath)) { + writeLn(error("cardano-cli not found")); + return null; + } + + var clusterPath = clusterConfig.getClusterFolder(clusterName); + var byronGenesisPath = clusterPath.resolve("node").resolve("genesis").resolve("byron-genesis.json"); + var byronGenesisPathStr = byronGenesisPath.toFile().getAbsolutePath(); + + ProcessBuilder builder = new ProcessBuilder(); + if (OSUtil.getOperatingSystem() == OSUtil.OS.WINDOWS) { + builder.command("cmd.exe", + cardanoCliPath.toFile().getAbsolutePath(), + "byron", "genesis", "print-genesis-hash", "--genesis-json", + byronGenesisPathStr); + } else { + builder.command(cardanoCliPath.toFile().getAbsolutePath(), + "byron", "genesis", "print-genesis-hash", "--genesis-json", + byronGenesisPathStr); + } + + var genesisHash = processUtil.executeAndReturnOutput(builder); + return genesisHash; + + } catch (Exception e) { + e.printStackTrace(); + writeLn(error("Error getting genesis hash", e)); + return null; + } + } + public boolean isFirstRunt(String clusterName) { return clusterStartService.checkIfFirstRun(getClusterFolder(clusterName)); } diff --git a/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/api/ClusterAdminController.java b/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/api/ClusterAdminController.java index dcc30fb..6d28512 100644 --- a/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/api/ClusterAdminController.java +++ b/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/localcluster/api/ClusterAdminController.java @@ -161,4 +161,13 @@ public String reset() { public int getKesPeriod() throws IOException { return clusterUtilService.getKESPeriod(); } + + @GetMapping("/devnet/genesis/hash") + public ResponseEntity getGenesisHash() { + var genesisHash = clusterService.getGenesisHash(DEFAULT_CLUSTER_NAME); + if (genesisHash != null) + return ResponseEntity.ok(genesisHash); + else + return ResponseEntity.notFound().build(); + } } diff --git a/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/util/ProcessUtil.java b/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/util/ProcessUtil.java index 376971b..5652336 100644 --- a/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/util/ProcessUtil.java +++ b/applications/cli/src/main/java/com/bloxbean/cardano/yacicli/util/ProcessUtil.java @@ -80,4 +80,29 @@ public Process startLongRunningProcess(String processName, ProcessBuilder builde return process; } + public String executeAndReturnOutput(ProcessBuilder processBuilder) { + try { + StringBuilder sb = new StringBuilder(); + Process process = processBuilder.start(); + + ProcessStream processStream = + new ProcessStream(process.getInputStream(), line -> { + if (line != null && !line.isEmpty()) + sb.append(line).append(System.lineSeparator()); + }); + + Future inputFuture = executorHelper.getExecutor().submit(processStream); + inputFuture.get(); + + // Wait for process to complete + process.waitFor(); + + return sb.toString(); + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + }