Skip to content

Commit

Permalink
Add support for TOML config files (--config-file) (#3442)
Browse files Browse the repository at this point in the history
  • Loading branch information
zah authored Mar 5, 2022
1 parent e4b7dbf commit cdeae90
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,8 @@
url = https://github.com/status-im/nim-zlib.git
ignore = untracked
branch = master
[submodule "vendor/nim-toml-serialization"]
path = vendor/nim-toml-serialization
url = https://github.com/status-im/nim-toml-serialization.git
ignore = untracked
branch = master
81 changes: 78 additions & 3 deletions beacon_chain/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import
metrics,

chronicles, chronicles/options as chroniclesOptions,
confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet,
confutils, confutils/defs, confutils/std/net,
confutils/toml/defs as confTomlDefs,
confutils/toml/std/net as confTomlNet,
confutils/toml/std/uri as confTomlUri,
serialization/errors, stew/shims/net as stewNet,
stew/[io2, byteutils], unicodedb/properties, normalize,
eth/common/eth_types as commonEthTypes, eth/net/nat,
eth/p2p/discoveryv5/enr,
json_serialization, web3/[ethtypes, confutils_defs],

./spec/[keystore, network],
./spec/[keystore, network, crypto],
./spec/datatypes/base,
./networking/network_metadata,
./validators/slashing_protection_common,
Expand All @@ -28,7 +32,8 @@ export
uri, nat, enr,
defaultEth2TcpPort, enabledLogLevel, ValidIpAddress,
defs, parseCmdArg, completeCmdArg, network_metadata,
network, BlockHashOrNumber
network, BlockHashOrNumber,
confTomlDefs, confTomlNet, confTomlUri

declareGauge network_name, "network name", ["name"]

Expand Down Expand Up @@ -93,6 +98,10 @@ type
# migrate = "Export and remove specified validators from Nimbus."

BeaconNodeConf* = object
configFile* {.
desc: "Loads the configuration from a TOML file"
name: "config-file" }: Option[InputFile]

logLevel* {.
desc: "Sets the log level for process and topics (e.g. \"DEBUG; TRACE:discv5,libp2p; REQUIRED:none; DISABLED:none\")"
defaultValue: "INFO"
Expand Down Expand Up @@ -674,6 +683,10 @@ type
name: "backfill"}: bool

ValidatorClientConf* = object
configFile* {.
desc: "Loads the configuration from a TOML file"
name: "config-file" }: Option[InputFile]

logLevel* {.
desc: "Sets the log level"
defaultValue: "INFO"
Expand Down Expand Up @@ -751,6 +764,10 @@ type
name: "beacon-node" }: seq[string]

SigningNodeConf* = object
configFile* {.
desc: "Loads the configuration from a TOML file"
name: "config-file" }: Option[InputFile]

logLevel* {.
desc: "Sets the log level"
defaultValue: "INFO"
Expand Down Expand Up @@ -993,6 +1010,64 @@ template writeValue*(writer: var JsonWriter,
value: TypedInputFile|InputFile|InputDir|OutPath|OutDir|OutFile) =
writer.writeValue(string value)

template raiseUnexpectedValue(r: var TomlReader, msg: string) =
# TODO: We need to implement `raiseUnexpectedValue` for TOML,
# so the correct line and column information can be included
# in error messages:
raise newException(SerializationError, msg)

proc readValue*(r: var TomlReader, value: var Epoch)
{.raises: [Defect, SerializationError, IOError].} =
value = Epoch r.parseInt(uint64)

proc readValue*(r: var TomlReader, value: var GraffitiBytes)
{.raises: [Defect, SerializationError, IOError].} =
try:
value = GraffitiBytes.init(r.readValue(string))
except ValueError as err:
r.raiseUnexpectedValue("A printable string or 0x-prefixed hex-encoded raw bytes expected")

proc readValue*(r: var TomlReader, val: var NatConfig)
{.raises: [Defect, IOError, SerializationError].} =
val = try: parseCmdArg(NatConfig, TaintedString r.readValue(string))
except CatchableError as err:
raise newException(SerializationError, err.msg)

proc readValue*(r: var TomlReader, a: var Eth2Digest)
{.raises: [Defect, IOError, SerializationError].} =
try:
a = fromHex(type(a), r.readValue(string))
except ValueError:
r.raiseUnexpectedValue("Hex string expected")

proc readValue*(reader: var TomlReader, value: var ValidatorPubKey)
{.raises: [Defect, IOError, SerializationError].} =
let keyAsString = try:
reader.readValue(string)
except CatchableError:
raiseUnexpectedValue(reader, "A hex-encoded string expected")

let key = ValidatorPubKey.fromHex(keyAsString)
if key.isOk:
value = key.get
else:
# TODO: Can we provide better diagnostic?
raiseUnexpectedValue(reader, "Valid hex-encoded public key expected")

proc readValue*(r: var TomlReader, a: var PubKey0x)
{.raises: [Defect, IOError, SerializationError].} =
try:
a = parseCmdArg(PubKey0x, TaintedString r.readValue(string))
except CatchableError:
r.raiseUnexpectedValue("a 0x-prefixed hex-encoded string expected")

proc readValue*(r: var TomlReader, a: var WalletName)
{.raises: [Defect, IOError, SerializationError].} =
try:
a = parseCmdArg(WalletName, TaintedString r.readValue(string))
except CatchableError:
r.raiseUnexpectedValue("string expected")

proc loadEth2Network*(config: BeaconNodeConf): Eth2NetworkMetadata {.raises: [Defect, IOError].} =
network_name.set(2, labelValues = [config.eth2Network.get(otherwise = "mainnet")])
when not defined(gnosisChainBinary):
Expand Down
14 changes: 10 additions & 4 deletions beacon_chain/nimbus_binary_common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import
std/[os, tables, strutils, terminal, typetraits],

# Nimble packages
chronos, confutils,
chronos, confutils, toml_serialization,
chronicles, chronicles/helpers as chroniclesHelpers, chronicles/topics_registry,
stew/io2,

Expand All @@ -26,7 +26,8 @@ import
when defined(posix):
import termios

export beacon_clock, beacon_node_status, conf, confutils
export
confutils, toml_serialization, beacon_clock, beacon_node_status, conf

type
SlotStartProc*[T] = proc(node: T, wallTime: BeaconTime,
Expand Down Expand Up @@ -180,11 +181,16 @@ template makeBannerAndConfig*(clientId: string, ConfType: type): untyped =
version = clientId & "\p" & copyrights & "\p\p" &
"eth2 specification v" & SPEC_VERSION & "\p\p" &
nimBanner

# TODO for some reason, copyrights are printed when doing `--help`
{.push warning[ProveInit]: off.}
let config = ConfType.load(
version = version,
copyrightBanner = clientId) # but a short version string makes more sense...
version = version, # but a short version string makes more sense...
copyrightBanner = clientId,
secondarySources = proc (config: ConfType, sources: auto) =
if config.configFile.isSome:
sources.addConfigFile(Toml, config.configFile.get)
)
{.pop.}
config

Expand Down
24 changes: 15 additions & 9 deletions scripts/launch_local_testnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,20 @@ for NUM_NODE in $(seq 0 $(( NUM_NODES - 1 ))); do
fi
done

CLI_CONF_FILE="$CONTAINER_DATA_DIR/config.toml"

cat > "$CLI_CONF_FILE" <<END_CLI_CONFIG
non-interactive = true
nat = "extip:127.0.0.1"
network = "${CONTAINER_DATA_DIR}"
log-level = "${LOG_LEVEL}"
log-format = "json"
rest = true
rest-address = "127.0.0.1"
metrics = true
metrics-address = "127.0.0.1"
END_CLI_CONFIG

for NUM_NODE in $(seq 0 $(( NUM_NODES - 1 ))); do
NODE_DATA_DIR="${DATA_DIR}/node${NUM_NODE}"
CONTAINER_NODE_DATA_DIR="${CONTAINER_DATA_DIR}/node${NUM_NODE}"
Expand Down Expand Up @@ -471,23 +485,15 @@ for NUM_NODE in $(seq 0 $(( NUM_NODES - 1 ))); do
fi

$BEACON_NODE_COMMAND \
--non-interactive \
--nat:extip:127.0.0.1 \
--network="${CONTAINER_DATA_DIR}" \
--log-level="${LOG_LEVEL}" \
--log-format=json \
--config-file="$CLI_CONF_FILE" \
--tcp-port=$(( BASE_PORT + NUM_NODE )) \
--udp-port=$(( BASE_PORT + NUM_NODE )) \
--max-peers=$(( NUM_NODES - 1 )) \
--data-dir="${CONTAINER_NODE_DATA_DIR}" \
${BOOTSTRAP_ARG} \
${WEB3_ARG} \
${STOP_AT_EPOCH_FLAG} \
--rest \
--rest-address="127.0.0.1" \
--rest-port="$(( BASE_REST_PORT + NUM_NODE ))" \
--metrics \
--metrics-address="127.0.0.1" \
--metrics-port="$(( BASE_METRICS_PORT + NUM_NODE ))" \
${EXTRA_ARGS} \
> "${DATA_DIR}/log${NUM_NODE}.txt" 2>&1 &
Expand Down
1 change: 1 addition & 0 deletions vendor/nim-toml-serialization
Submodule nim-toml-serialization added at f4fb6c

0 comments on commit cdeae90

Please sign in to comment.