Skip to content

Commit

Permalink
add process-compose demo (#1503)
Browse files Browse the repository at this point in the history
Adds `nix run .#demo` to start the demo cluster locally with one command

* fixes prepare and seed scripts to work both with and without docker
  • Loading branch information
disassembler authored Jul 19, 2024
1 parent 209de1d commit b04e59d
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ hspec-results.md
/demo/devnet/
/demo/persistence/
/docs/static/haddock/

# demo
devnet
.env
logs
acks
server-output
state
10 changes: 5 additions & 5 deletions demo/prepare-devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

# Prepare a "devnet" directory holding credentials, a dummy topology and
# "up-to-date" genesis files. If the directory exists, it is wiped out.
set -e
set -eo pipefail

BASEDIR=$(realpath $(dirname $(realpath $0))/..)
TARGETDIR="devnet"
BASEDIR=${BASEDIR:-$(realpath $(dirname $(realpath $0))/..)}
TARGETDIR=${TARGETDIR:-devnet}

[ -d "$TARGETDIR" ] && { echo "Cleaning up directory $TARGETDIR" ; sudo rm -r $TARGETDIR ; }
[ -d "$TARGETDIR" ] && { echo "Cleaning up directory $TARGETDIR" ; rm -r $TARGETDIR ; }

cp -af "$BASEDIR/hydra-cluster/config/devnet/" "$TARGETDIR"
cp -af "$BASEDIR/hydra-cluster/config/credentials" "$TARGETDIR"
echo '{"Producers": []}' > "$TARGETDIR/topology.json"
sed -i.bak "s/\"startTime\": [0-9]*/\"startTime\": $(date +%s)/" "$TARGETDIR/genesis-byron.json" && \
sed -i.bak "s/\"systemStart\": \".*\"/\"systemStart\": \"$(date -u +%FT%TZ)\"/" "$TARGETDIR/genesis-shelley.json"

find $TARGETDIR -type f -exec chmod 0400 {} \;
find $TARGETDIR -type f -name '*.skey' -exec chmod 0400 {} \;
mkdir "$TARGETDIR/ipc"
echo "Prepared devnet, you can start the cluster now"

19 changes: 10 additions & 9 deletions demo/seed-devnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Seed a "devnet" by distributing Ada to hydra nodes
set -eo pipefail

SCRIPT_DIR=$(realpath $(dirname $(realpath $0)))
SCRIPT_DIR=${SCRIPT_DIR:-$(realpath $(dirname $(realpath $0)))}
NETWORK_ID=42

CCLI_CMD=
Expand All @@ -23,10 +23,12 @@ if [[ -n ${2} ]]; then
fi

DOCKER_COMPOSE_CMD=
if docker compose --version > /dev/null 2>&1; then
DOCKER_COMPOSE_CMD="docker compose"
else
DOCKER_COMPOSE_CMD="docker-compose"
if [[ ! -x ${CCLI_CMD} ]]; then
if docker compose --version > /dev/null 2>&1; then
DOCKER_COMPOSE_CMD="docker compose"
else
DOCKER_COMPOSE_CMD="docker-compose"
fi
fi

# Invoke cardano-cli in running cardano-node container or via provided cardano-cli
Expand Down Expand Up @@ -97,12 +99,11 @@ function publishReferenceScripts() {

function queryPParams() {
echo >&2 "Query Protocol parameters"
if [ "$( docker container inspect -f '{{.State.Running}}' demo-cardano-node-1 )" == "true" ];
then
docker exec demo-cardano-node-1 cardano-cli query protocol-parameters --testnet-magic ${NETWORK_ID} --socket-path ${DEVNET_DIR}/node.socket --out-file /dev/stdout \
if [[ -x ${CCLI_CMD} ]]; then
ccli query protocol-parameters --socket-path ${DEVNET_DIR}/node.socket --out-file /dev/stdout \
| jq ".txFeeFixed = 0 | .txFeePerByte = 0 | .executionUnitPrices.priceMemory = 0 | .executionUnitPrices.priceSteps = 0" > devnet/protocol-parameters.json
else
cardano-cli query protocol-parameters --testnet-magic ${NETWORK_ID} --socket-path ${DEVNET_DIR}/node.socket --out-file /dev/stdout \
docker exec demo-cardano-node-1 cardano-cli query protocol-parameters --testnet-magic ${NETWORK_ID} --socket-path ${DEVNET_DIR}/node.socket --out-file /dev/stdout \
| jq ".txFeeFixed = 0 | .txFeePerByte = 0 | .executionUnitPrices.priceMemory = 0 | .executionUnitPrices.priceSteps = 0" > devnet/protocol-parameters.json
fi
echo >&2 "Saved in protocol-parameters.json"
Expand Down
35 changes: 34 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
inputs = {
nixpkgs.follows = "haskellNix/nixpkgs";
nixpkgsLatest.url = "github:NixOS/nixpkgs/nixos-24.05";
haskellNix.url = "github:input-output-hk/haskell.nix";
iohk-nix.url = "github:input-output-hk/iohk-nix";
flake-parts.url = "github:hercules-ci/flake-parts";
process-compose-flake.url = "github:Platonic-Systems/process-compose-flake";
lint-utils = {
url = "github:homotopic/lint-utils";
inputs.nixpkgs.follows = "haskellNix/nixpkgs";
Expand All @@ -25,10 +27,15 @@
{ self
, flake-parts
, nixpkgs
# TODO remove when haskellNix updated to newer nixpkgs
, nixpkgsLatest
, cardano-node
, ...
} @ inputs:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.process-compose-flake.flakeModule
];
systems = [
"x86_64-linux"
"x86_64-darwin"
Expand All @@ -41,6 +48,9 @@

# nixpkgs enhanced with haskell.nix and crypto libs as used by iohk

pkgsLatest = import nixpkgsLatest {
inherit system;
};
pkgs = import nixpkgs {
inherit system;
overlays = [
Expand Down Expand Up @@ -114,13 +124,23 @@
})
x.components."${y}") [ "benchmarks" "exes" "sublibs" "tests" ]);
in
rec {
{
legacyPackages = hsPkgs;

packages =
hydraPackages //
(if pkgs.stdenv.isLinux then (prefixAttrs "docker-" hydraImages) else { }) //
{ spec = import ./spec { inherit pkgs; }; };
(if pkgs.stdenv.isLinux then (prefixAttrs "docker-" hydraImages) else { }) // {
spec = import ./spec {
inherit pkgs;
};
};
process-compose."demo" = import ./nix/hydra/demo.nix {
inherit system pkgs inputs self;
demoDir = ./demo;
inherit (pkgsLatest) process-compose;
inherit (pkgs) cardano-node cardano-cli;
inherit (hydraPackages) hydra-node;
};

checks = let lu = inputs.lint-utils.linters.${system}; in {
hlint = lu.hlint { src = self; hlint = pkgs.hlint; };
Expand Down
121 changes: 121 additions & 0 deletions nix/hydra/demo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# A demo using process-compose

{ system ? builtins.currentSystem
, pkgs
, inputs
, cardano-cli
, cardano-node
, hydra-node
, self
, demoDir
, process-compose
}: {
# httpServer.enable = true;
package = process-compose;
settings = {
log_location = "./logs/all.log";
log_level = "debug";
environment = {
BASEDIR = "./";
SCRIPT_DIR = "./";
CARDANO_NODE_SOCKET_PATH = "devnet/node.socket";
CARDANO_NODE_NETWORK_ID = "42";
};

processes = {
prepare-devnet = {
command = "${demoDir}/prepare-devnet.sh";
};
cardano-node = {
command = ''
${cardano-node}/bin/cardano-node run \
--config devnet/cardano-node.json \
--topology devnet/topology.json \
--database-path devnet/db \
--socket-path devnet/node.socket \
--shelley-operational-certificate devnet/opcert.cert \
--shelley-kes-key devnet/kes.skey \
--shelley-vrf-key devnet/vrf.skey
'';
ready_log_line = "NodeIsLeader";
depends_on."prepare-devnet".condition = "process_completed";
};
seed-devnet = {
command = "${demoDir}/seed-devnet.sh ${cardano-cli}/bin/cardano-cli ${hydra-node}/bin/hydra-node";
depends_on."cardano-node".condition = "process_log_ready";
};
hydra-node-alice = {
command = ''
source .env &&
${hydra-node}/bin/hydra-node \
--node-id 1 \
--port 5001 \
--api-port 4001 \
--monitoring-port 6001 \
--peer 127.0.0.1:5002 \
--peer 127.0.0.1:5003 \
--hydra-signing-key ${demoDir}/alice.sk \
--hydra-verification-key ${demoDir}/bob.vk \
--hydra-verification-key ${demoDir}/carol.vk \
--hydra-scripts-tx-id $HYDRA_SCRIPTS_TX_ID \
--cardano-signing-key devnet/credentials/alice.sk \
--cardano-verification-key devnet/credentials/bob.vk \
--cardano-verification-key devnet/credentials/carol.vk \
--ledger-protocol-parameters devnet/protocol-parameters.json \
--testnet-magic 42 \
--node-socket devnet/node.socket
'';
ready_log_line = "NodeIsLeader";
depends_on."seed-devnet".condition = "process_completed";
};
hydra-node-bob = {
command = ''
source .env &&
${hydra-node}/bin/hydra-node \
--node-id 2 \
--port 5002 \
--api-port 4002 \
--monitoring-port 6002 \
--peer 127.0.0.1:5001 \
--peer 127.0.0.1:5003 \
--hydra-signing-key ${demoDir}/bob.sk \
--hydra-verification-key ${demoDir}/alice.vk \
--hydra-verification-key ${demoDir}/carol.vk \
--hydra-scripts-tx-id $HYDRA_SCRIPTS_TX_ID \
--cardano-signing-key devnet/credentials/bob.sk \
--cardano-verification-key devnet/credentials/alice.vk \
--cardano-verification-key devnet/credentials/carol.vk \
--ledger-protocol-parameters devnet/protocol-parameters.json \
--testnet-magic 42 \
--node-socket devnet/node.socket
'';
ready_log_line = "NodeIsLeader";
depends_on."seed-devnet".condition = "process_completed";
};
hydra-node-carol = {
command = ''
source .env &&
${hydra-node}/bin/hydra-node \
--node-id 3 \
--port 5003 \
--api-port 4003 \
--monitoring-port 6003 \
--peer 127.0.0.1:5001 \
--peer 127.0.0.1:5002 \
--hydra-signing-key ${demoDir}/carol.sk \
--hydra-verification-key ${demoDir}/alice.vk \
--hydra-verification-key ${demoDir}/bob.vk \
--hydra-scripts-tx-id $HYDRA_SCRIPTS_TX_ID \
--cardano-signing-key devnet/credentials/carol.sk \
--cardano-verification-key devnet/credentials/alice.vk \
--cardano-verification-key devnet/credentials/bob.vk \
--ledger-protocol-parameters devnet/protocol-parameters.json \
--testnet-magic 42 \
--node-socket devnet/node.socket
'';
ready_log_line = "NodeIsLeader";
depends_on."seed-devnet".condition = "process_completed";
};
};
};
}

0 comments on commit b04e59d

Please sign in to comment.