Skip to content

Commit

Permalink
nixos service: add p2p topology support.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbgi authored and coot committed Nov 30, 2021
1 parent cd644f6 commit 6f22d7d
Showing 1 changed file with 115 additions and 10 deletions.
125 changes: 115 additions & 10 deletions nix/nixos/cardano-node-service.nix
Expand Up @@ -13,7 +13,14 @@ let
let baseConfig = recursiveUpdate (cfg.nodeConfig
// (mapAttrs' (era: epoch:
nameValuePair "Test${era}HardForkAtEpoch" epoch
) cfg.forceHardForks)) cfg.extraNodeConfig;
) cfg.forceHardForks)
// (optionalAttrs cfg.useNewTopology {
EnableP2P = true;
TargetNumberOfRootPeers = cfg.targetNumberOfRootPeers;
TargetNumberOfKnownPeers = cfg.targetNumberOfKnownPeers;
TargetNumberOfEstablishedPeers = cfg.targetNumberOfEstablishedPeers;
TargetNumberOfActivePeers = cfg.targetNumberOfActivePeers;
})) cfg.extraNodeConfig;
in i: let
instanceConfig = recursiveUpdate (baseConfig
// (optionalAttrs (baseConfig ? hasEKG) {
Expand All @@ -24,9 +31,34 @@ let
})) (cfg.extraNodeInstanceConfig i);
nodeConfigFile = 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);
});
newTopology = {
LocalRoots = {
groups = map (g: {
localRoots = {
inherit (g) addrs;
advertise = g.advertise or false;
};
valency = g.valency or (length g.addrs);
}) (cfg.producers ++ (cfg.instanceProducers i));
};
PublicRoots = map (g: {
publicRoots = {
inherit (g) addrs;
advertise = g.advertise or false;
};
}) (cfg.publicProducers ++ (cfg.instancePublicProducers i));
} // optionalAttrs (cfg.usePeersFromLedgerAfterSlot != null) {
useLedgerAfterSlot = cfg.usePeersFromLedgerAfterSlot;
};
oldTopology = {
Producers = concatMap (g: map (a: a // { valency = a.valency or 1; }) g.addrs) (
cfg.producers ++ (cfg.instanceProducers i) ++ cfg.publicProducers ++ (cfg.instancePublicProducers i)
);
};
topology = if cfg.topology != null then cfg.topology else toFile "topology.yaml" (toJSON (
if (cfg.useNewTopology) then newTopology
else oldTopology
));
consensusParams = {
RealPBFT = [
"${lib.optionalString (cfg.signingKey != null)
Expand Down Expand Up @@ -331,21 +363,62 @@ in {
'';
};

producers = mkOption {
publicProducers = mkOption {
type = types.listOf types.attrs;
default = [{
addr = envConfig.relaysNew;
port = envConfig.edgePort;
valency = 1;
addrs = [{
addr = envConfig.relaysNew;
port = envConfig.edgePort;
}];
advertise = false;
}];
description = ''Routes to public peers. Only used if slot < usePeersFromLedgerAfterSlot'';
};

instancePublicProducers = mkOption {
# type = types.functionTo (types.listOf types.attrs);
default = _: [];
description = ''Routes to public peers. Only used if slot < usePeersFromLedgerAfterSlot and specific to a given instance (when multiple instances are used).'';
};

producers = mkOption {
type = types.listOf types.attrs;
description = ''Static routes to peers.'';
default = [];
example = [{
addrs = [{
addr = "127.0.0.1";
port = 3001;
}];
advertise = false;
valency = 1;
}];
description = ''Static routes to local peers.'';
};

instanceProducers = mkOption {
type = types.functionTo (types.listOf types.attrs);
default = _: [];
description = ''
Static routes to peers, specific to a given instance (when multiple instances are used).
Static routes to local peers, specific to a given instance (when multiple instances are used).
'';
};

useNewTopology = mkOption {
type = types.bool;
default = cfg.nodeConfig.EnableP2P or false;
description = ''
Use new, p2p/ledger peers compatible topology.
'';
};

usePeersFromLedgerAfterSlot = mkOption {
type = types.nullOr types.int;
default = if cfg.kesKey != null then null
else envConfig.usePeersFromLedgerAfterSlot or null;
description = ''
If set, bootstraps from public roots until it reaches given slot,
then it switches to using the ledger as a source of peers. It maintains a connection to its local roots.
Default to null for block producers.
'';
};

Expand All @@ -365,6 +438,38 @@ in {
description = ''Internal representation of the config.'';
};

targetNumberOfRootPeers = mkOption {
type = types.int;
default = cfg.nodeConfig.TargetNumberOfRootPeers or 60;
description = "Limits the maximum number of root peers the node will know about";
};

targetNumberOfKnownPeers = mkOption {
type = types.int;
default = cfg.nodeConfig.TargetNumberOfKnownPeers or cfg.targetNumberOfRootPeers;
description = ''
Target number for known peers (root peers + peers known through gossip).
Default to targetNumberOfRootPeers.
'';
};

targetNumberOfEstablishedPeers = mkOption {
type = types.int;
default = cfg.nodeConfig.TargetNumberOfEstablishedPeers
or (2 * cfg.targetNumberOfKnownPeers / 3);
description = ''Number of peers the node will be connected to, but not necessarily following their chain.
Default to 2/3 of targetNumberOfKnownPeers.
'';
};

targetNumberOfActivePeers = mkOption {
type = types.int;
default = cfg.nodeConfig.TargetNumberOfActivePeers or (cfg.targetNumberOfEstablishedPeers / 2);
description = ''Number of peers your node is actively downloading headers and blocks from.
Default to half of targetNumberOfEstablishedPeers.
'';
};

extraNodeConfig = mkOption {
type = types.attrs // {
merge = loc: foldl' (res: def: recursiveUpdate res def.value) {};
Expand Down

0 comments on commit 6f22d7d

Please sign in to comment.