Skip to content

Commit

Permalink
cardano-cluster-service: a flock of cardano-node@NODE-ID services
Browse files Browse the repository at this point in the history
  • Loading branch information
deepfire committed Sep 12, 2019
1 parent 8768c29 commit 4764ea5
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
156 changes: 156 additions & 0 deletions nix/nixos/cardano-cluster-service.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{ config
, lib
, pkgs
, ... }:

with lib; with builtins;
let
cfg = config.services.cardano-cluster;
ncfg = config.services.cardano-node;
node-ids = range 0 (cfg.node-count - 1);
cardano-node = ncfg.package;

# mkFullyConnectedLocalClusterTopology
# :: Address String -> Port String -> Int -> Int -> Topology FilePath
mkFullyConnectedLocalClusterTopology =
{ hostAddr
, port
, node-count
, valency ? 1
}:
let
addr = hostAddr;
ports = map (x: port + x) (range 0 (node-count - 1));
mkPeer = port: { inherit addr port valency; };
mkNodeTopo = nodeId: port: {
inherit nodeId;
nodeAddress = { inherit addr port; };
producers = map mkPeer (remove port ports);
};
in toFile "topology.yaml" (toJSON (imap mkNodeTopo ports));

## Note how some values are literal strings, and some integral.
## This is an important detail.
defaultGenesisProtocolParams = {
heavyDelThd = "300000000000";
maxBlockSize = "2000000";
maxHeaderSize = "2000000";
maxProposalSize = "700";
maxTxSize = "4096";
mpcThd = "20000000000000";
scriptVersion = 0;
slotDuration = "20000";
softforkRule = {
initThd = "900000000000000";
minThd = "600000000000000";
thdDecrement = "50000000000000";
};
txFeePolicy = {
multiplier = "43946000000";
summand = "155381000000000";
};
unlockStakeEpoch = "18446744073709551615";
updateImplicit = "10000";
updateProposalThd = "100000000000000";
updateVoteThd = "1000000000000";
};

defaultGenesisArgs = {
protocol_params_file = toFile "genesis-protocol-params.json" (toJSON defaultGenesisProtocolParams);
k = 2160;
protocol_magic = 314159265;
n_poors = 128;
n_delegates = 3;
total_balance = 8000000000000000;
delegate_share = 900000000000000;
avvm_entries = 128;
avvm_entry_balance = 10000000000000;
secret_seed = 271828182;
};

# We need this to have a balance of:
# 1. moderate amount of rebuilds
# 2. small chain length for quick validation
# genesisUpdatePeriod
# :: Seconds Int
genesisUpdatePeriod = 300;

# mkFixedGenesisOfDate
# :: Date String -> Topology FilePath
mkFixedGenesisOfDate = start_time: args:
pkgs.runCommand "genesis-of-${start_time}" {} ''
args=(
--genesis-output-dir "''${out}"
--start-time ${start_time}
--avvm-entry-balance ${toString args.avvm_entry_balance}
--avvm-entry-count ${toString args.avvm_entries}
--delegate-share ${toString args.delegate_share}
--k ${toString args.k}
--n-delegate-addresses ${toString args.n_delegates}
--n-poor-addresses ${toString args.n_poors}
--protocol-magic ${toString args.protocol_magic}
--protocol-parameters-file "${args.protocol_params_file}"
--secret-seed ${toString args.secret_seed}
--total-balance ${toString args.total_balance}
--use-hd-addresses
)
${cardano-node}/bin/cardano-cli --real-pbft genesis "''${args[@]}"
'';

## This value will change every given amount of seconds.
periodicNewsTimestamp = period:
toString ((builtins.currentTime / period) * period);
in
let
genesisDir = mkFixedGenesisOfDate (periodicNewsTimestamp genesisUpdatePeriod) defaultGenesisArgs;
genesisFile = "${genesisDir}/genesis.json";
topology = mkFullyConnectedLocalClusterTopology
{ inherit (cfg) node-count;
inherit (ncfg) hostAddr port;
};
in {
options = with types; {

services.cardano-cluster = {
enable = mkOption {
type = bool;
default = false;
description = ''
Enable cardano-node, a node implementing ouroboros protocols
(the blockchain protocols running cardano).
'';
};
node-count = mkOption {
type = int;
default = 3;
description = ''
Number of nodes in cluster.
'';
};
};
};

config = mkIf cfg.enable {
services.cardano-node = {
enable = true;
instanced = true;
inherit topology genesisFile;
};
systemd.services."cardano-node@" = {
scriptArgs = "%i ${genesisDir}";
};
systemd.services.cardano-cluster =
let node-services = map (id: "cardano-node@${toString id}.service") node-ids;
in {
description = "Cluster of cardano nodes.";
requiredBy = [ "multi-user.target" ];
enable = true;
after = node-services;
bindsTo = node-services;
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.coreutils}/bin/echo Starting cluster of ${toString cfg.node-count} nodes, topology ${topology}";
};
};
};
}
1 change: 1 addition & 0 deletions nix/nixos/module-list.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
./cardano-node-options.nix
./cardano-node-service.nix
./cardano-cluster-service.nix
]

0 comments on commit 4764ea5

Please sign in to comment.