Skip to content

Commit

Permalink
#90 New admin endpoint to get genesis hash (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
satran004 authored Jan 3, 2025
1 parent 92a0a85 commit 16b1256
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions applications/cli/api-tests/admin.http
Original file line number Diff line number Diff line change
Expand Up @@ -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)
%}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,13 @@ public String reset() {
public int getKesPeriod() throws IOException {
return clusterUtilService.getKESPeriod();
}

@GetMapping("/devnet/genesis/hash")
public ResponseEntity<String> getGenesisHash() {
var genesisHash = clusterService.getGenesisHash(DEFAULT_CLUSTER_NAME);
if (genesisHash != null)
return ResponseEntity.ok(genesisHash);
else
return ResponseEntity.notFound().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 16b1256

Please sign in to comment.