Skip to content

Commit

Permalink
Merge pull request #54 from bloxbean/fix_50
Browse files Browse the repository at this point in the history
Private network with custom options
  • Loading branch information
satran004 committed Jun 29, 2024
2 parents 4ae62cf + d9174cc commit b8bc952
Show file tree
Hide file tree
Showing 102 changed files with 2,613 additions and 509 deletions.
1 change: 1 addition & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ zip:
RUN echo "tag=${tag}" >> /app/yaci-devkit-${tag}/config/version
RUN echo "revision=${EARTHLY_GIT_SHORT_HASH}" >> /app/yaci-devkit-${tag}/config/version
COPY config/env /app/yaci-devkit-${tag}/config/
COPY config/node.properties /app/yaci-devkit-${tag}/config/

COPY bin/devkit.sh /app/yaci-devkit-${tag}/bin/

Expand Down
1 change: 1 addition & 0 deletions applications/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This CLI uses [Yaci](https://github.com/bloxbean/yaci-core), a mini protocol imp

- To run


```
$> java -jar yaci-cli-<version>.jar
```
Expand Down
2 changes: 2 additions & 0 deletions applications/cli/config/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spring.config.import=optional:file:./config/node.properties

server.port=10000
#topup_addresses=addr_test1qqxnp3khzm7kcj9t23hskehat7428ghsenk0pfew4rqy5v9frnmht7uwrl073q4jvq20z82kh4rksyns540azhndqexqpvhgqr:20000,\
# addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82:10000
Expand Down
11 changes: 11 additions & 0 deletions applications/cli/config/node.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#protocolMagic=42
#
#disableFaucet=true

#initialAddresses[0].address=addr_test1qzx9hu8j4ah3auytk0mwcupd69hpc52t0cw39a65ndrah86djs784u92a3m5w475w3w35tyd6v3qumkze80j8a6h5tuqq5xe8y
#initialAddresses[0].balance=450000000
#initialAddresses[0].staked=true
#
#initialAddresses[1].address=addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82
#initialAddresses[1].balance=250000000
#initialAddresses[1].staked=false
2 changes: 2 additions & 0 deletions applications/cli/docker/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spring.config.import=optional:file:/app/config/node.properties

cardano.cli.path=/app/cardano-bin
yaci.store.folder=/app/store
ogmios.folder=/app/ogmios
Expand Down
2 changes: 1 addition & 1 deletion applications/cli/docker/store-application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ server.port=8080

store.cardano.host=localhost
store.cardano.port=3001
store.cardano.protocol-magic=42
#store.cardano.protocol-magic=42

store.cardano.n2c-node-socket-path=/clusters/nodes/default/node/node.sock
store.cardano.submit-api-url=http://localhost:8090/api/submit/tx
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.bloxbean.cardano.yacicli;

import com.bloxbean.cardano.yacicli.commands.localcluster.ClusterConfig;
import com.bloxbean.cardano.yacicli.commands.localcluster.config.GenesisConfig;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.shell.jline.NonInteractiveShellRunner;
import org.springframework.stereotype.Component;

Expand All @@ -20,6 +22,7 @@
import java.util.stream.Collectors;

@SpringBootApplication
@EnableConfigurationProperties(GenesisConfig.class)
@Slf4j
public class YaciCliApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import static com.bloxbean.cardano.yacicli.commands.localcluster.ClusterConfig.CLUSTER_NAME;

@Component
@RequiredArgsConstructor
public class YaciCliPromptProvider implements PromptProvider {
Expand All @@ -22,7 +24,7 @@ public AttributedString getPrompt() {
return new AttributedString("yaci-cli:>",
AttributedStyle.DEFAULT.foreground(AttributedStyle.GREEN).bold());
} else {
String clusterName = CommandContext.INSTANCE.getProperty(ClusterCommands.CLUSTER_NAME);
String clusterName = CommandContext.INSTANCE.getProperty(CLUSTER_NAME);
if (clusterName == null)
clusterName = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public class Groups {
public static final String NODE_CMD_GROUP = "Node Commands";
public static final String YACI_STORE_CMD_GROUP = "Yaci Store Commands";
public static final String OGMIOS_CMD_GROUP = "Ogmios Commands";

public static final String TXN_CMD_GROUP = "Transaction Commands";
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.bloxbean.cardano.yacicli.commands.localcluster;

import com.bloxbean.cardano.client.api.model.Utxo;
import com.bloxbean.cardano.client.plutus.spec.PlutusData;
import com.bloxbean.cardano.client.plutus.spec.serializers.PlutusDataJsonConverter;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
import com.bloxbean.cardano.yaci.core.protocol.localstate.api.Era;
import com.bloxbean.cardano.yaci.core.util.HexUtil;
import com.bloxbean.cardano.yacicli.commands.common.Groups;
import com.bloxbean.cardano.yacicli.commands.common.RootLogService;
import com.bloxbean.cardano.yacicli.commands.localcluster.config.GenesisConfig;
import com.bloxbean.cardano.yacicli.commands.localcluster.events.ClusterDeleted;
import com.bloxbean.cardano.yacicli.commands.localcluster.events.ClusterStarted;
import com.bloxbean.cardano.yacicli.commands.localcluster.events.ClusterStopped;
import com.bloxbean.cardano.yacicli.commands.localcluster.events.FirstRunDone;
import com.bloxbean.cardano.yacicli.commands.localcluster.profiles.GenesisProfile;
import com.bloxbean.cardano.yacicli.commands.localcluster.service.AccountService;
import com.bloxbean.cardano.yacicli.commands.localcluster.service.ClusterUtilService;
import com.bloxbean.cardano.yacicli.common.AnsiColors;
Expand All @@ -29,23 +28,23 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static com.bloxbean.cardano.yacicli.commands.localcluster.ClusterConfig.CLUSTER_NAME;
import static com.bloxbean.cardano.yacicli.util.ConsoleWriter.*;

@ShellComponent
@ShellCommandGroup(Groups.NODE_CMD_GROUP)
@RequiredArgsConstructor
@Slf4j
public class ClusterCommands {
public static final String CLUSTER_NAME = "cluster_name";
private final ClusterService localClusterService;
private final RootLogService rootLogService;
private final ClusterUtilService clusterUtilService;
private final AccountService accountService;
private final ShellHelper shellHelper;
private final ApplicationEventPublisher publisher;
private final ClusterPortInfoHelper clusterUrlPrinter;
private final GenesisConfig genesisConfig;

@ShellMethod(value = "List devnet nodes. Use `list-nodes`. Deprecated: `list-clusters`", key = {"list-nodes", "list-clusters"})
public void listLocalClusters() {
Expand Down Expand Up @@ -86,7 +85,9 @@ public void createCluster(@ShellOption(value = {"-n", "--name"}, defaultValue =
@ShellOption(value = {"-e", "--epoch-length"}, help = "No of slots in an epoch", defaultValue = "500") int epochLength,
@ShellOption(value = {"-o", "--overwrite"}, defaultValue = "false", help = "Overwrite existing node directory. default: false") boolean overwrite,
@ShellOption(value = {"--start"}, defaultValue = "false", help = "Automatically start the node after create. default: false") boolean start,
@ShellOption(value = {"--era"}, defaultValue = "babbage", help = "Era (babbage, conway)") String era
@ShellOption(value = {"--era"}, defaultValue = "babbage", help = "Era (babbage, conway)") String era,
@ShellOption(value = {"--genesis-profile",}, defaultValue = ShellOption.NULL, help = "Use a pre-defined genesis profile (Options: zero_fee)") GenesisProfile genesisProfile,
@ShellOption(value = {"--generate-new-keys"}, defaultValue = "false", help = "Generate new genesis keys, pool keys instead of default keys") boolean generateNewKeys
) {

try {
Expand Down Expand Up @@ -118,7 +119,7 @@ else if (era.equalsIgnoreCase("conway"))
return;
}

long protocolMagic = 42; //always 42 for now.
long protocolMagic = genesisConfig.getProtocolMagic();

//stop any cluster if running
localClusterService.stopCluster(msg -> writeLn(msg));
Expand All @@ -135,9 +136,10 @@ else if (era.equalsIgnoreCase("conway"))
.masterNode(true)
.isBlockProducer(true)
.era(nodeEra)
.genesisProfile(genesisProfile)
.build();

boolean success = localClusterService.createNodeClusterFolder(clusterName, clusterInfo, overwrite, (msg) -> writeLn(msg));
boolean success = localClusterService.createNodeClusterFolder(clusterName, clusterInfo, overwrite, generateNewKeys, (msg) -> writeLn(msg));

if (success) {
printClusterInfo(clusterName);
Expand Down Expand Up @@ -298,57 +300,6 @@ public void showFaucetAddresses() {
});
}

@ShellMethod(value = "Topup account", key = "topup")
@ShellMethodAvailability("localClusterCmdAvailability")
public void topUp(@ShellOption(value = {"-a", "--address"}, help = "Receiver address") String address,
@ShellOption(value = {"-v", "--value"}, help = "Ada value") double adaValue) {
String clusterName = CommandContext.INSTANCE.getProperty(CLUSTER_NAME);
Era era = CommandContext.INSTANCE.getEra();

boolean topupStatus = accountService.topup(clusterName, era, address, adaValue, msg -> writeLn(msg));
if (!topupStatus)
return;

boolean status = clusterUtilService.waitForNextBlocks(1, msg -> writeLn(msg));

if (status) {
writeLn(info("Available utxos") + "\n");
getUtxos(address, false);
}
}

@ShellMethod(value = "Get utxos at an address", key = "utxos")
@ShellMethodAvailability("localClusterCmdAvailability")
public void getUtxos(@ShellOption(value = {"-a", "--address"}, help = "Address") String address,
@ShellOption(value = {"--pretty-print-inline-datum"}, defaultValue = "false") boolean prettyPrintInlineDatum) {
String clusterName = CommandContext.INSTANCE.getProperty(CLUSTER_NAME);
Era era = CommandContext.INSTANCE.getEra();

List<Utxo> utxos = accountService.getUtxos(clusterName, era, address, msg -> writeLn(msg));

AtomicInteger index = new AtomicInteger(0);
utxos.forEach(utxo -> {
writeLn(index.incrementAndGet() + ". " + utxo.getTxHash() + "#" + utxo.getOutputIndex() + " : " + utxo.getAmount());
if (utxo.getInlineDatum() != null) {
if (prettyPrintInlineDatum) {
try {
writeLn("InlineDatum : \n" +
PlutusDataJsonConverter.toJson(PlutusData.deserialize(HexUtil.decodeHexString(utxo.getInlineDatum()))));
} catch (Exception e) {
throw new RuntimeException(e);
}
} else
writeLn("InlineDatum: " + utxo.getInlineDatum());
}
if (utxo.getDataHash() != null)
writeLn("DatumHash: " + utxo.getDataHash());

if (utxo.getReferenceScriptHash() != null)
writeLn("ReferenceScriptHash: " + utxo.getReferenceScriptHash());
writeLn("--------------------------------------------------------------------------------------");
});
}

@ShellMethod(value = "Get tip/current block number", key = "tip")
@ShellMethodAvailability("localClusterCmdAvailability")
public void getTip() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class ClusterConfig {
public static final String CLUSTER_NAME = "cluster_name";
public final static String CLUSTER_INFO_FILE = "cluster-info.json";
public final static String NODE_FOLDER_PREFIX = "node";
public final static String NODE_RELAY_SCRIPT = "node-relay";
Expand All @@ -25,6 +26,9 @@ public class ClusterConfig {
@Value("${pool.keys.home:#{null}}")
private String poolKeysHome;

@Value("${genesis.keys.home:#{null}}")
private String genesisKeysHome;

@Value("${cardano.cli.path:#{null}}")
private String cardanoCliPath;

Expand Down Expand Up @@ -65,6 +69,13 @@ public String getPoolKeysHome() {
return poolKeysHome;
}

public String getGenesisKeysHome() {
if (genesisKeysHome == null || !StringUtils.hasLength(genesisKeysHome.trim()))
return Path.of(YACI_CLI_HOME, "genesis-keys").toAbsolutePath().toString();
else
return genesisKeysHome;
}

public String getOgmiosHome() {
if (ogmiosFolder == null || !StringUtils.hasLength(ogmiosFolder.trim()))
return Path.of(YACI_CLI_HOME, "bin", "ogmios").toAbsolutePath().toString();
Expand All @@ -78,4 +89,16 @@ public String getKupoHome() {
else
return Path.of(kupoFolder).toAbsolutePath().toString();
}

public Path getClusterFolder(String clusterName) {
return Path.of(getClusterHome(), clusterName);
}

public Path getPoolKeysFolder(String clusterName) {
return Path.of(getPoolKeysHome(), clusterName);
}

public Path getGenesisKeysFolder(String clusterName) {
return Path.of(getGenesisKeysHome(), clusterName);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bloxbean.cardano.yacicli.commands.localcluster;

import com.bloxbean.cardano.yaci.core.protocol.localstate.api.Era;
import com.bloxbean.cardano.yacicli.commands.localcluster.profiles.GenesisProfile;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -24,6 +25,7 @@ public class ClusterInfo {
private boolean isBlockProducer;
private String adminNodeUrl; //Only for peer nodes
private Era era;
private GenesisProfile genesisProfile;

@Builder.Default
private int ogmiosPort = 1337;
Expand Down
Loading

0 comments on commit b8bc952

Please sign in to comment.