-
Notifications
You must be signed in to change notification settings - Fork 721
/
cardano-node-service.nix
555 lines (511 loc) · 19.5 KB
/
cardano-node-service.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
{ config
, lib
, pkgs
, ... }:
with lib; with builtins;
let
cfg = config.services.cardano-node;
inherit (cfg.cardanoNodePkgs) commonLib cardano-node cardano-node-profiled cardano-node-eventlogged cardano-node-asserted;
envConfig = cfg.environments.${cfg.environment}; systemdServiceName = "cardano-node${optionalString cfg.instanced "@"}";
runtimeDir = if cfg.runtimeDir == null then cfg.stateDir else "/run/${cfg.runtimeDir}";
mkScript = cfg: i: let
instanceConfig = cfg.nodeConfig // (optionalAttrs (cfg.nodeConfig ? hasEKG) {
hasEKG = cfg.nodeConfig.hasEKG + i;
}) // (optionalAttrs (cfg.nodeConfig ? hasPrometheus) {
hasPrometheus = map (n: if isInt n then n + i else n) cfg.nodeConfig.hasPrometheus;
});
realNodeConfigFile = if (cfg.environment == "selfnode" || cfg.environment == "shelley_selfnode") then "${cfg.stateDir}/config.yaml"
else if (cfg.nodeConfigFile != null) then cfg.nodeConfigFile
else toFile "config-${toString cfg.nodeId}-${toString i}.json" (toJSON instanceConfig);
topology = if cfg.topology != null then cfg.topology else toFile "topology.yaml" (toJSON {
Producers = cfg.producers ++ (cfg.instanceProducers i);
});
consensusParams = {
RealPBFT = [
"${lib.optionalString (cfg.signingKey != null)
"--signing-key ${cfg.signingKey}"}"
"${lib.optionalString (cfg.delegationCertificate != null)
"--delegation-certificate ${cfg.delegationCertificate}"}"
];
TPraos = [
"${lib.optionalString (cfg.vrfKey != null)
"--shelley-vrf-key ${cfg.vrfKey}"}"
"${lib.optionalString (cfg.kesKey != null)
"--shelley-kes-key ${cfg.kesKey}"}"
"${lib.optionalString (cfg.operationalCertificate != null)
"--shelley-operational-certificate ${cfg.operationalCertificate}"}"
];
Cardano = [
"${lib.optionalString (cfg.signingKey != null)
"--signing-key ${cfg.signingKey}"}"
"${lib.optionalString (cfg.delegationCertificate != null)
"--delegation-certificate ${cfg.delegationCertificate}"}"
"${lib.optionalString (cfg.vrfKey != null)
"--shelley-vrf-key ${cfg.vrfKey}"}"
"${lib.optionalString (cfg.kesKey != null)
"--shelley-kes-key ${cfg.kesKey}"}"
"${lib.optionalString (cfg.operationalCertificate != null)
"--shelley-operational-certificate ${cfg.operationalCertificate}"}"
];
};
instanceDbPath = "${cfg.databasePath}${optionalString (i > 0) "-${toString i}"}";
exec = "cardano-node run";
cmd = builtins.filter (x: x != "") [
"${cfg.package}/bin/${exec}"
"--config ${realNodeConfigFile}"
"--database-path ${instanceDbPath}"
"--topology ${topology}"
] ++ (lib.optionals (!cfg.systemdSocketActivation) ([
"--host-addr ${cfg.hostAddr}"
"--port ${toString (cfg.port + i)}"
"--socket-path ${cfg.socketPath}"
] ++ lib.optional (cfg.ipv6HostAddr != null)
"--host-ipv6-addr ${cfg.ipv6HostAddr}"
)) ++ consensusParams.${cfg.nodeConfig.Protocol} ++ cfg.extraArgs ++ cfg.rtsArgs;
in ''
choice() { i=$1; shift; eval "echo \''${$((i + 1))}"; }
echo "Starting ${exec}: ${concatStringsSep "\"\n echo \"" cmd}"
echo "..or, once again, in a single line:"
echo "${toString cmd}"
${lib.optionalString (cfg.environment == "selfnode") ''
echo "Wiping all data in ${cfg.stateDir}"
rm -rf ${cfg.stateDir}/*
GENESIS_FILE=$(${pkgs.jq}/bin/jq -r .GenesisFile < ${cfg.nodeConfigFile})
START_TIME=$(date +%s --date="30 seconds")
${pkgs.jq}/bin/jq -r --arg startTime "''${START_TIME}" '. + {startTime: $startTime|tonumber}' < $GENESIS_FILE > ${cfg.stateDir}/genesis.json
${pkgs.jq}/bin/jq -r --arg GenesisFile genesis.json '. + {GenesisFile: $GenesisFile}' < ${cfg.nodeConfigFile} > ${realNodeConfigFile}
''}
${lib.optionalString (cfg.environment == "shelley_selfnode") ''
echo "Wiping all data in ${cfg.stateDir}"
rm -rf ${cfg.stateDir}/*
GENESIS_FILE=$(${pkgs.jq}/bin/jq -r .GenesisFile < ${cfg.nodeConfigFile})
START_TIME=$(date --utc +"%Y-%m-%dT%H:%M:%SZ" --date="30 seconds")
${pkgs.jq}/bin/jq -r --arg startTime "''${START_TIME}" '. + {startTime: $startTime}' < $GENESIS_FILE > ${cfg.stateDir}/genesis.json
${pkgs.jq}/bin/jq -r --arg GenesisFile genesis.json '. + {GenesisFile: $GenesisFile}' < ${cfg.nodeConfigFile} > ${realNodeConfigFile}
''}
# If exist copy state from existing instance instead of syncing from scratch:
if [ ! -d ${instanceDbPath} ] && [ -d ${cfg.databasePath} ]; then
echo "Copying existing immutable db from ${instanceDbPath}"
mkdir -p ${instanceDbPath}
cp -a ${cfg.databasePath}/immutable ${instanceDbPath}/
cp -a ${cfg.databasePath}/protocolMagicId ${instanceDbPath}/
fi
exec ${toString cmd}'';
in {
options = {
services.cardano-node = {
enable = mkOption {
type = types.bool;
default = false;
description = ''
Enable cardano-node, a node implementing ouroboros protocols
(the blockchain protocols running cardano).
'';
};
instanced = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable systemd service instancing.
For details see https://fedoramagazine.org/systemd-template-unit-files/
'';
};
instances = mkOption {
type = types.int;
default = 1;
description = ''
Number of instance of the service to run.
'';
};
script = mkOption {
type = types.str;
default = mkScript cfg 0;
};
profiling = mkOption {
type = types.enum ["none" "time" "space" "space-module" "space-closure" "space-type" "space-retainer" "space-bio"];
default = "none";
};
eventlog = mkOption {
type = types.bool;
default = false;
};
asserts = mkOption {
type = types.bool;
default = false;
description = ''
Whether to use an executable with asserts enabled.
'';
};
cardanoNodePkgs = mkOption {
type = types.attrs;
default = import ../. {};
defaultText = "cardano-node pkgs";
description = ''
The cardano-node packages and library that should be used.
Main usage is sharing optimization:
reduce eval time when service is instantiated multiple times.
'';
};
package = mkOption {
type = types.package;
default = if (cfg.profiling != "none")
then cardano-node-profiled
else if cfg.eventlog then cardano-node-eventlogged
else if cfg.asserts then cardano-node-asserted
else cardano-node;
defaultText = "cardano-node";
description = ''
The cardano-node package that should be used
'';
};
environments = mkOption {
type = types.attrs;
default = commonLib.environments;
description = ''
environment node will connect to
'';
};
environment = mkOption {
type = types.enum (builtins.attrNames cfg.environments);
default = "testnet";
description = ''
environment node will connect to
'';
};
# TODO: remove
genesisFile = mkOption {
type = types.path;
default = envConfig.genesisFile or null;
description = ''
Genesis json file
'';
};
# TODO: remove
genesisHash = mkOption {
type = types.nullOr types.str;
default = envConfig.genesisHash;
description = ''
Hash of the genesis file
'';
};
# TODO: remove
genesisHashPath = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Path to the Hash of the genesis file
'';
};
# Byron signing/delegation
signingKey = mkOption {
type = types.nullOr (types.either types.str types.path);
default = null;
description = ''
Signing key
'';
};
delegationCertificate = mkOption {
type = types.nullOr (types.either types.str types.path);
default = null;
description = ''
Delegation certificate
'';
};
# Shelley kes/vrf keys and operation cert
kesKey = mkOption {
type = types.nullOr (types.either types.str types.path);
default = null;
description = ''
Signing key
'';
};
vrfKey = mkOption {
type = types.nullOr (types.either types.str types.path);
default = null;
description = ''
Signing key
'';
};
operationalCertificate = mkOption {
type = types.nullOr (types.either types.str types.path);
default = null;
description = ''
Operational certificate
'';
};
hostAddr = mkOption {
type = types.str;
default = "127.0.0.1";
description = ''
The host address to bind to
'';
};
ipv6HostAddr = mkOption {
type = types.nullOr types.str;
default = "::1";
description = ''
The ipv6 host address to bind to. Set to null to disable.
'';
};
stateDir = mkOption {
type = types.str;
default = "/var/lib/cardano-node";
description = ''
Directory to store blockchain data.
'';
};
runtimeDir = mkOption {
type = types.nullOr types.str;
default = "cardano-node";
description = ''
Runtime directory relative to /run
'';
};
databasePath = mkOption {
type = types.str;
default = "${cfg.stateDir}/${cfg.dbPrefix}";
description = ''Node database path.'';
};
socketPath = mkOption {
type = types.str;
default = "${runtimeDir}/node.socket";
description = ''Local communication socket path.'';
};
systemdSocketActivation = mkOption {
type = types.bool;
default = false;
description = ''Use systemd socket activation'';
};
extraServiceConfig = mkOption {
# activate type for nixos-21.03:
type = types.unspecified # types.functionTo (types.listOf types.attrs);
// {
merge = loc: foldl' (res: def: i: recursiveUpdate (res i) (def.value i)) (i: {});
};
default = i: {};
description = ''
Extra systemd service config (apply to all instances).
'';
};
extraSocketConfig = mkOption {
# activate type for nixos-21.03:
type = types.unspecified # types.functionTo (types.listOf types.attrs);
// {
merge = loc: foldl' (res: def: i: recursiveUpdate (res i) (def.value i)) (i: {});
};
default = i: {};
description = ''
Extra systemd socket config (apply to all instances).
'';
};
dbPrefix = mkOption {
type = types.str;
default = "db-${cfg.environment}";
description = ''
Prefix of database directories inside `stateDir`.
(eg. for "db", there will be db-0, etc.).
'';
};
port = mkOption {
type = types.either types.int types.str;
default = 3001;
description = ''
The port number
'';
};
shareIpv4port = mkOption {
type = types.bool;
default = cfg.systemdSocketActivation;
description = ''
Should instances on same machine share ipv4 port.
Default: true if systemd activated socket. Otherwise always false.
If false use port increments starting from `port`.
'';
};
shareIpv6port = mkOption {
type = types.bool;
default = false;
description = ''
Should instances on same machine share ipv6 port.
Only works with systemd socket.
Default: false.
If false use port increments starting from `port`.
'';
};
nodeId = mkOption {
type = types.int;
default = 0;
description = ''
The ID for this node
'';
};
producers = mkOption {
default = [{
addr = envConfig.relaysNew;
port = envConfig.edgePort;
valency = 1;
}];
type = types.listOf types.attrs;
description = ''Static routes to peers.'';
};
instanceProducers = mkOption {
# activate type for nixos-21.03:
# type = types.functionTo (types.listOf types.attrs);
default = _: [];
description = ''
Static routes to peers, specific to a given instance (when multiple instances are used).
'';
};
topology = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
Cluster topology. If not set `producers` array is used to generated topology file.
'';
};
nodeConfig = mkOption {
type = types.attrs // {
merge = loc: foldl' (res: def: recursiveUpdate res def.value) {};
};
default = envConfig.nodeConfig;
description = ''Internal representation of the config.'';
};
nodeConfigFile = mkOption {
type = types.nullOr types.str;
default = null;
description = ''Actual configuration file (shell expression).'';
};
extraArgs = mkOption {
type = types.listOf types.str;
default = [];
description = ''Extra CLI args for 'cardano-node'.'';
};
rtsArgs = mkOption {
type = types.listOf types.str;
default = [];
apply = args: if (args != [] || cfg.profilingArgs != []) then
["+RTS"] ++ cfg.profilingArgs ++ args ++ ["-RTS"]
else [];
description = ''Extra CLI args for 'cardano-node', to be surrounded by "+RTS"/"-RTS"'';
};
profilingArgs = mkOption {
type = types.listOf types.str;
default = let commonProfilingArgs = ["--machine-readable" "-tcardano-node.stats" "-pocardano-node"]
++ lib.optional (cfg.eventlog) "-l";
in if cfg.profiling == "time" then ["-P"] ++ commonProfilingArgs
else if cfg.profiling == "space" then ["-h"] ++ commonProfilingArgs
else if cfg.profiling == "space-module" then ["-hm"] ++ commonProfilingArgs
else if cfg.profiling == "space-closure" then ["-hd"] ++ commonProfilingArgs
else if cfg.profiling == "space-type" then ["-hy"] ++ commonProfilingArgs
else if cfg.profiling == "space-retainer" then ["-hr"] ++ commonProfilingArgs
else if cfg.profiling == "space-bio" then ["-hb"] ++ commonProfilingArgs
else [];
description = ''RTS profiling options'';
};
tracingVerbosity = mkOption {
type = types.str;
default = envConfig.tracingVerbosity;
description = ''verbosity level of structured representation of traced values.'';
};
protover-major = mkOption {
type = types.nullOr types.int;
default = null;
};
protover-minor = mkOption {
type = types.nullOr types.int;
default = null;
};
protover-alt = mkOption {
type = types.nullOr types.int;
default = null;
};
};
};
config = mkIf cfg.enable ( let
stateDirBase = "/var/lib/";
genInstanceConf = f: listToAttrs (if cfg.instances > 1 && !cfg.instanced
then genList (i: let n = "${systemdServiceName}-${toString i}"; in nameValuePair n (f n i)) cfg.instances
else [ (nameValuePair systemdServiceName (f systemdServiceName 0)) ]); in lib.mkMerge [
{
users.groups.cardano-node.gid = 10016;
users.users.cardano-node = {
description = "cardano-node node daemon user";
uid = 10016;
group = "cardano-node";
};
## TODO: use http://hackage.haskell.org/package/systemd for:
## 1. only declaring success after we perform meaningful init (local state recovery)
## 2. heartbeat & watchdog functionality
systemd.services = genInstanceConf (n: i: recursiveUpdate {
description = "cardano-node node ${toString i} service";
after = [ "network-online.target" ]
++ (optional cfg.systemdSocketActivation "${n}.socket")
++ (optional (cfg.instances > 1) "cardano-node.service");
requires = optional cfg.systemdSocketActivation "${n}.socket"
++ (optional (cfg.instances > 1) "cardano-node.service");
wants = [ "network-online.target" ];
wantedBy = mkIf (!cfg.instanced) [ "multi-user.target" ];
partOf = mkIf (cfg.instances > 1) ["cardano-node.service"];
script = mkScript cfg i;
serviceConfig = {
User = "cardano-node";
Group = "cardano-node";
Restart = "always";
RuntimeDirectory = if (i == 0) then runtimeDir else "${runtimeDir}-${toString i}";
WorkingDirectory = cfg.stateDir;
# This assumes /var/lib/ is a prefix of cfg.stateDir.
# This is checked as an assertion below.
StateDirectory = lib.removePrefix stateDirBase cfg.stateDir;
NonBlocking = lib.mkIf cfg.systemdSocketActivation true;
# time to sleep before restarting a service
RestartSec = 1;
KillSignal = "SIGINT";
};
} (cfg.extraServiceConfig i));
systemd.sockets = genInstanceConf (n: i: lib.mkIf cfg.systemdSocketActivation (recursiveUpdate {
description = "Socket of the ${n} service.";
wantedBy = [ "sockets.target" ];
partOf = [ "${n}.service" ];
socketConfig = {
ListenStream = [ "${cfg.hostAddr}:${toString (if cfg.shareIpv4port then cfg.port else cfg.port + i)}" ]
++ optional (cfg.ipv6HostAddr != null) "[${cfg.ipv6HostAddr}]:${toString (if cfg.shareIpv6port then cfg.port else cfg.port + i)}"
++ [(if (i == 0) then cfg.socketPath else "${runtimeDir}-${toString i}/node.socket")];
ReusePort = "yes";
SocketMode = "0660";
SocketUser = "cardano-node";
SocketGroup = "cardano-node";
FreeBind = "yes";
};
} (cfg.extraSocketConfig i)));
}
{
# oneshot service start allows to easily control all instances at once.
systemd.services.cardano-node = lib.mkIf (cfg.instances > 1) {
description = "Control all ${toString cfg.instances} at once.";
enable = true;
wants = genList (i: "cardano-node-${toString i}.service") cfg.instances;
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
User = "cardano-node";
Group = "cardano-node";
ExecStart = "${pkgs.coreutils}/bin/echo Starting ${toString cfg.instances} cardano-node instances";
WorkingDirectory = cfg.stateDir;
StateDirectory = lib.removePrefix stateDirBase cfg.stateDir;
};
};
}
{
assertions = [
{
assertion = lib.hasPrefix stateDirBase cfg.stateDir;
message = "The option services.cardano-node.stateDir should have ${stateDirBase} as a prefix!";
}
{
assertion = (cfg.kesKey == null) == (cfg.vrfKey == null) && (cfg.kesKey == null) == (cfg.operationalCertificate == null);
message = "Shelley Era: all of three [operationalCertificate kesKey vrfKey] options must be defined (or none of them).";
}
];
}
]);
}