2020-05-22 17:04:52 +00:00
|
|
|
# beacon_chain
|
2021-03-23 06:57:10 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2020-05-22 17:04:52 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2021-03-26 06:52:01 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-05-27 17:06:28 +00:00
|
|
|
# Common routines for a BeaconNode and a ValidatorClient
|
2020-05-22 17:04:52 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
2021-11-02 17:06:36 +00:00
|
|
|
std/[os, tables, strutils, terminal, typetraits],
|
2020-05-22 17:04:52 +00:00
|
|
|
|
|
|
|
# Nimble packages
|
2020-07-15 13:15:55 +00:00
|
|
|
chronos, confutils/defs,
|
2020-10-27 09:00:57 +00:00
|
|
|
chronicles, chronicles/helpers as chroniclesHelpers, chronicles/topics_registry,
|
2020-08-27 18:23:41 +00:00
|
|
|
stew/io2,
|
2020-05-22 17:04:52 +00:00
|
|
|
|
|
|
|
# Local modules
|
2021-08-12 13:08:20 +00:00
|
|
|
./spec/[helpers],
|
|
|
|
./spec/datatypes/base,
|
2021-11-02 17:06:36 +00:00
|
|
|
"."/[beacon_clock, conf, filepath, beacon_node_status]
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2021-03-16 08:06:45 +00:00
|
|
|
when defined(posix):
|
|
|
|
import termios
|
|
|
|
|
2021-10-19 14:09:26 +00:00
|
|
|
export beacon_node_status
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
type
|
|
|
|
SlotStartProc*[T] = proc(node: T, wallTime: BeaconTime,
|
|
|
|
lastSlot: Slot): Future[void] {.gcsafe,
|
|
|
|
raises: [Defect].}
|
|
|
|
|
2021-11-02 17:06:36 +00:00
|
|
|
# silly chronicles, colors is a compile-time property
|
|
|
|
proc stripAnsi(v: string): string =
|
|
|
|
var
|
|
|
|
res = newStringOfCap(v.len)
|
|
|
|
i: int
|
|
|
|
|
|
|
|
while i < v.len:
|
|
|
|
let c = v[i]
|
|
|
|
if c == '\x1b':
|
|
|
|
var
|
|
|
|
x = i + 1
|
|
|
|
found = false
|
|
|
|
|
|
|
|
while x < v.len: # look for [..m
|
|
|
|
let c2 = v[x]
|
|
|
|
if x == i + 1:
|
|
|
|
if c2 != '[':
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if c2 in {'0'..'9'} + {';'}:
|
|
|
|
discard # keep looking
|
|
|
|
elif c2 == 'm':
|
|
|
|
i = x + 1
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
inc x
|
|
|
|
|
|
|
|
if found: # skip adding c
|
|
|
|
continue
|
|
|
|
res.add c
|
|
|
|
inc i
|
|
|
|
|
|
|
|
res
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2021-03-26 06:52:01 +00:00
|
|
|
proc updateLogLevel*(logLevel: string) {.raises: [Defect, ValueError].} =
|
2020-11-12 10:46:02 +00:00
|
|
|
# Updates log levels (without clearing old ones)
|
|
|
|
let directives = logLevel.split(";")
|
|
|
|
try:
|
|
|
|
setLogLevel(parseEnum[LogLevel](directives[0]))
|
|
|
|
except ValueError:
|
|
|
|
raise (ref ValueError)(msg: "Please specify one of TRACE, DEBUG, INFO, NOTICE, WARN, ERROR or FATAL")
|
|
|
|
|
|
|
|
if directives.len > 1:
|
|
|
|
for topicName, settings in parseTopicDirectives(directives[1..^1]):
|
|
|
|
if not setTopicState(topicName, settings.state, settings.logLevel):
|
|
|
|
warn "Unrecognized logging topic", topic = topicName
|
|
|
|
|
2021-11-02 17:06:36 +00:00
|
|
|
proc setupLogging*(
|
|
|
|
logLevel: string, stdoutKind: StdoutLogKind, logFile: Option[OutFile]) =
|
|
|
|
# In the cfg file for nimbus, we create two formats: textlines and json.
|
|
|
|
# Here, we either write those logs to an output, or not, depending on the
|
|
|
|
# given configuration.
|
|
|
|
# Arguably, if we don't use a format, chronicles should not create it.
|
|
|
|
|
|
|
|
when defaultChroniclesStream.outputs.type.arity != 2:
|
|
|
|
warn "Logging configuration options not enabled in the current build"
|
|
|
|
else:
|
|
|
|
# Naive approach where chronicles will form a string and we will discard
|
|
|
|
# it, even if it could have skipped the formatting phase
|
|
|
|
|
|
|
|
proc noOutput(logLevel: LogLevel, msg: LogOutputStr) = discard
|
|
|
|
proc writeAndFlush(f: File, msg: LogOutputStr) =
|
|
|
|
try:
|
|
|
|
f.write(msg)
|
|
|
|
f.flushFile()
|
|
|
|
except IOError as err:
|
|
|
|
logLoggingFailure(cstring(msg), err)
|
|
|
|
|
|
|
|
proc stdoutFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
writeAndFlush(stdout, msg)
|
|
|
|
|
|
|
|
proc noColorsFlush(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
writeAndFlush(stdout, stripAnsi(msg))
|
|
|
|
|
|
|
|
let fileWriter =
|
|
|
|
if logFile.isSome():
|
2020-07-15 13:15:55 +00:00
|
|
|
let
|
|
|
|
logFile = logFile.get.string
|
|
|
|
logFileDir = splitFile(logFile).dir
|
2021-11-02 17:06:36 +00:00
|
|
|
lres = secureCreatePath(logFileDir)
|
|
|
|
if lres.isOk():
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
f = open(logFile, fmAppend)
|
|
|
|
x = proc(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
writeAndFlush(f, msg) # will close when program terminates
|
|
|
|
x
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "Failed to create log file", logFile, msg = exc.msg
|
|
|
|
noOutput
|
|
|
|
else:
|
2020-08-27 18:23:41 +00:00
|
|
|
error "Failed to create directory for log file",
|
|
|
|
path = logFileDir, err = ioErrorMsg(lres.error)
|
2021-11-02 17:06:36 +00:00
|
|
|
noOutput
|
2020-07-15 13:15:55 +00:00
|
|
|
else:
|
2021-11-02 17:06:36 +00:00
|
|
|
noOutput
|
|
|
|
|
|
|
|
defaultChroniclesStream.outputs[1].writer = fileWriter
|
|
|
|
|
|
|
|
let tmp =
|
|
|
|
if stdoutKind == StdoutLogKind.Auto:
|
|
|
|
if logFile.isSome():
|
|
|
|
# No stdout logging by default, when file logging is enabled
|
|
|
|
StdoutLogKind.None
|
|
|
|
elif isatty(stdout):
|
|
|
|
# On a TTY, let's be fancy
|
|
|
|
StdoutLogKind.Colors
|
|
|
|
else:
|
|
|
|
# When there's no TTY, we output no colors because this matches what
|
|
|
|
# released binaries were doing before auto-detection was around and
|
|
|
|
# looks decent in systemd-captured journals.
|
|
|
|
StdoutLogKind.NoColors
|
|
|
|
else:
|
|
|
|
stdoutKind
|
|
|
|
|
|
|
|
case tmp
|
|
|
|
of StdoutLogKind.Auto: raiseAssert "checked above"
|
|
|
|
of StdoutLogKind.Colors:
|
|
|
|
defaultChroniclesStream.outputs[0].writer = stdoutFlush
|
|
|
|
of StdoutLogKind.NoColors:
|
|
|
|
defaultChroniclesStream.outputs[0].writer = noColorsFlush
|
|
|
|
of StdoutLogKind.Json:
|
|
|
|
defaultChroniclesStream.outputs[0].writer = noOutput
|
|
|
|
|
|
|
|
let prevWriter = defaultChroniclesStream.outputs[1].writer
|
|
|
|
defaultChroniclesStream.outputs[1].writer =
|
|
|
|
proc(logLevel: LogLevel, msg: LogOutputStr) =
|
|
|
|
stdoutFlush(logLevel, msg)
|
|
|
|
prevWriter(logLevel, msg)
|
|
|
|
of StdoutLogKind.None:
|
|
|
|
defaultChroniclesStream.outputs[0].writer = noOutput
|
2020-07-15 13:15:55 +00:00
|
|
|
|
2020-05-22 17:04:52 +00:00
|
|
|
try:
|
2020-11-12 10:46:02 +00:00
|
|
|
updateLogLevel(logLevel)
|
2020-05-22 17:04:52 +00:00
|
|
|
except ValueError as err:
|
2021-03-26 06:52:01 +00:00
|
|
|
try:
|
|
|
|
stderr.write "Invalid value for --log-level. " & err.msg
|
2021-05-28 12:51:15 +00:00
|
|
|
except IOError:
|
2021-03-26 06:52:01 +00:00
|
|
|
echo "Invalid value for --log-level. " & err.msg
|
2020-05-22 17:04:52 +00:00
|
|
|
quit 1
|
|
|
|
|
2020-05-27 17:06:28 +00:00
|
|
|
template makeBannerAndConfig*(clientId: string, ConfType: type): untyped =
|
2020-06-30 12:23:52 +00:00
|
|
|
let
|
|
|
|
version = clientId & "\p" & copyrights & "\p\p" &
|
|
|
|
"eth2 specification v" & SPEC_VERSION & "\p\p" &
|
|
|
|
nimBanner
|
|
|
|
# TODO for some reason, copyrights are printed when doing `--help`
|
2020-08-04 13:00:55 +00:00
|
|
|
{.push warning[ProveInit]: off.}
|
|
|
|
let config = ConfType.load(
|
2020-06-30 12:23:52 +00:00
|
|
|
version = version,
|
|
|
|
copyrightBanner = clientId) # but a short version string makes more sense...
|
2020-08-04 13:00:55 +00:00
|
|
|
{.pop.}
|
|
|
|
config
|
2020-06-05 09:57:40 +00:00
|
|
|
|
2021-03-05 13:12:00 +00:00
|
|
|
# TODO not sure if this belongs here but it doesn't belong in `beacon_clock.nim` either
|
2020-06-05 09:57:40 +00:00
|
|
|
proc sleepToSlotOffset*(clock: BeaconClock, extra: chronos.Duration,
|
|
|
|
slot: Slot, msg: static string): Future[bool] {.async.} =
|
|
|
|
let
|
|
|
|
fromNow = clock.fromNow(slot.toBeaconTime(extra))
|
|
|
|
|
|
|
|
if fromNow.inFuture:
|
|
|
|
trace msg,
|
|
|
|
slot = shortLog(slot),
|
2020-07-16 13:16:51 +00:00
|
|
|
fromNow = shortLog(fromNow.offset)
|
2020-06-05 09:57:40 +00:00
|
|
|
|
|
|
|
await sleepAsync(fromNow.offset)
|
|
|
|
return true
|
|
|
|
return false
|
2020-09-01 13:38:34 +00:00
|
|
|
|
|
|
|
proc checkIfShouldStopAtEpoch*(scheduledSlot: Slot, stopAtEpoch: uint64) =
|
|
|
|
# Offset backwards slightly to allow this epoch's finalization check to occur
|
|
|
|
if scheduledSlot > 3 and stopAtEpoch > 0'u64 and
|
|
|
|
(scheduledSlot - 3).compute_epoch_at_slot() >= stopAtEpoch:
|
|
|
|
info "Stopping at pre-chosen epoch",
|
|
|
|
chosenEpoch = stopAtEpoch,
|
|
|
|
epoch = scheduledSlot.compute_epoch_at_slot(),
|
|
|
|
slot = scheduledSlot
|
|
|
|
|
|
|
|
# Brute-force, but ensure it's reliable enough to run in CI.
|
|
|
|
quit(0)
|
2021-03-16 08:06:45 +00:00
|
|
|
|
|
|
|
proc resetStdin*() =
|
|
|
|
when defined(posix):
|
|
|
|
# restore echoing, in case it was disabled by a password prompt
|
|
|
|
let fd = stdin.getFileHandle()
|
|
|
|
var attrs: Termios
|
|
|
|
discard fd.tcGetAttr(attrs.addr)
|
|
|
|
attrs.c_lflag = attrs.c_lflag or Cflag(ECHO)
|
|
|
|
discard fd.tcSetAttr(TCSANOW, attrs.addr)
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc runSlotLoop*[T](node: T, startTime: BeaconTime,
|
|
|
|
slotProc: SlotStartProc[T]) {.async.} =
|
|
|
|
var
|
|
|
|
curSlot = startTime.slotOrZero()
|
|
|
|
nextSlot = curSlot + 1 # No earlier than GENESIS_SLOT + 1
|
|
|
|
timeToNextSlot = nextSlot.toBeaconTime() - startTime
|
|
|
|
|
|
|
|
info "Scheduling first slot action",
|
|
|
|
startTime = shortLog(startTime),
|
|
|
|
nextSlot = shortLog(nextSlot),
|
|
|
|
timeToNextSlot = shortLog(timeToNextSlot)
|
|
|
|
|
|
|
|
while true:
|
|
|
|
# Start by waiting for the time when the slot starts. Sleeping relinquishes
|
|
|
|
# control to other tasks which may or may not finish within the alotted
|
|
|
|
# time, so below, we need to be wary that the ship might have sailed
|
|
|
|
# already.
|
|
|
|
await sleepAsync(timeToNextSlot)
|
|
|
|
|
|
|
|
let
|
|
|
|
wallTime = node.beaconClock.now()
|
|
|
|
wallSlot = wallTime.slotOrZero() # Always > GENESIS!
|
|
|
|
|
|
|
|
if wallSlot < nextSlot:
|
|
|
|
# While we were sleeping, the system clock changed and time moved
|
|
|
|
# backwards!
|
|
|
|
if wallSlot + 1 < nextSlot:
|
|
|
|
# This is a critical condition where it's hard to reason about what
|
|
|
|
# to do next - we'll call the attention of the user here by shutting
|
|
|
|
# down.
|
|
|
|
fatal "System time adjusted backwards significantly - clock may be inaccurate - shutting down",
|
|
|
|
nextSlot = shortLog(nextSlot),
|
|
|
|
wallSlot = shortLog(wallSlot)
|
|
|
|
bnStatus = BeaconNodeStatus.Stopping
|
|
|
|
return
|
|
|
|
|
|
|
|
# Time moved back by a single slot - this could be a minor adjustment,
|
|
|
|
# for example when NTP does its thing after not working for a while
|
|
|
|
warn "System time adjusted backwards, rescheduling slot actions",
|
|
|
|
wallTime = shortLog(wallTime),
|
|
|
|
nextSlot = shortLog(nextSlot),
|
|
|
|
wallSlot = shortLog(wallSlot)
|
|
|
|
|
|
|
|
# cur & next slot remain the same
|
|
|
|
timeToNextSlot = nextSlot.toBeaconTime() - wallTime
|
|
|
|
continue
|
|
|
|
|
|
|
|
if wallSlot > nextSlot + SLOTS_PER_EPOCH:
|
|
|
|
# Time moved forwards by more than an epoch - either the clock was reset
|
|
|
|
# or we've been stuck in processing for a long time - either way, we will
|
|
|
|
# skip ahead so that we only process the events of the last
|
|
|
|
# SLOTS_PER_EPOCH slots
|
|
|
|
warn "Time moved forwards by more than an epoch, skipping ahead",
|
|
|
|
curSlot = shortLog(curSlot),
|
|
|
|
nextSlot = shortLog(nextSlot),
|
|
|
|
wallSlot = shortLog(wallSlot)
|
|
|
|
|
|
|
|
curSlot = wallSlot - SLOTS_PER_EPOCH
|
|
|
|
|
|
|
|
elif wallSlot > nextSlot:
|
|
|
|
notice "Missed expected slot start, catching up",
|
|
|
|
delay = shortLog(wallTime - nextSlot.toBeaconTime()),
|
|
|
|
curSlot = shortLog(curSlot),
|
|
|
|
nextSlot = shortLog(curSlot)
|
|
|
|
|
|
|
|
await slotProc(node, wallTime, curSlot)
|
|
|
|
|
|
|
|
curSlot = wallSlot
|
|
|
|
nextSlot = wallSlot + 1
|
|
|
|
timeToNextSlot = saturate(node.beaconClock.fromNow(nextSlot))
|