2020-05-22 17:04:52 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2018-2024 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.
|
2024-02-26 08:48:07 +00:00
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
import
|
|
|
|
stew/io2, presto, metrics, metrics/chronos_httpserver,
|
|
|
|
./rpc/rest_key_management_api,
|
|
|
|
./validator_client/[
|
2023-04-17 21:31:54 +00:00
|
|
|
common, fallback_service, duties_service, fork_service, block_service,
|
2022-08-19 10:30:07 +00:00
|
|
|
doppelganger_service, attestation_service, sync_committee_service]
|
2022-07-29 08:36:20 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
const
|
|
|
|
PREGENESIS_EPOCHS_COUNT = 1
|
|
|
|
|
2021-12-21 14:24:23 +00:00
|
|
|
proc initGenesis(vc: ValidatorClientRef): Future[RestGenesis] {.async.} =
|
2021-07-13 11:15:07 +00:00
|
|
|
info "Initializing genesis", nodes_count = len(vc.beaconNodes)
|
|
|
|
var nodes = vc.beaconNodes
|
2020-06-19 09:21:17 +00:00
|
|
|
while true:
|
2022-07-21 16:54:07 +00:00
|
|
|
var pendingRequests: seq[Future[RestResponse[GetGenesisResponse]]]
|
2023-09-15 05:34:32 +00:00
|
|
|
let offlineNodes = vc.offlineNodes()
|
|
|
|
if len(offlineNodes) == 0:
|
|
|
|
let sleepDuration = 2.seconds
|
|
|
|
info "Could not resolve beacon nodes, repeating",
|
|
|
|
sleep_time = sleepDuration
|
|
|
|
await sleepAsync(sleepDuration)
|
|
|
|
for node in vc.nonameNodes():
|
|
|
|
let status = checkName(node)
|
|
|
|
node.updateStatus(status, ApiNodeFailure())
|
|
|
|
if status == RestBeaconNodeStatus.Noname:
|
|
|
|
warn "Cannot initialize beacon node", node = node, status = status
|
|
|
|
continue
|
|
|
|
|
|
|
|
for node in offlineNodes:
|
|
|
|
debug "Requesting genesis information", node = node
|
2022-07-21 16:54:07 +00:00
|
|
|
pendingRequests.add(node.client.getGenesis())
|
2020-07-08 10:11:22 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
try:
|
2022-07-21 16:54:07 +00:00
|
|
|
await allFutures(pendingRequests)
|
2021-07-13 11:15:07 +00:00
|
|
|
except CancelledError as exc:
|
2022-07-21 16:54:07 +00:00
|
|
|
var pending: seq[Future[void]]
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Genesis information request was interrupted"
|
2022-07-21 16:54:07 +00:00
|
|
|
for future in pendingRequests:
|
|
|
|
if not(future.finished()):
|
|
|
|
pending.add(future.cancelAndWait())
|
|
|
|
await allFutures(pending)
|
2021-07-13 11:15:07 +00:00
|
|
|
raise exc
|
|
|
|
|
|
|
|
let (errorNodes, genesisList) =
|
|
|
|
block:
|
2021-08-03 15:17:11 +00:00
|
|
|
var gres: seq[RestGenesis]
|
2021-07-13 11:15:07 +00:00
|
|
|
var bres: seq[BeaconNodeServerRef]
|
2022-07-21 16:54:07 +00:00
|
|
|
for i in 0 ..< len(pendingRequests):
|
|
|
|
let fut = pendingRequests[i]
|
2023-06-01 08:04:30 +00:00
|
|
|
if fut.completed():
|
2021-07-13 11:15:07 +00:00
|
|
|
let resp = fut.read()
|
|
|
|
if resp.status == 200:
|
|
|
|
debug "Received genesis information", endpoint = nodes[i],
|
|
|
|
genesis_time = resp.data.data.genesis_time,
|
|
|
|
genesis_fork_version = resp.data.data.genesis_fork_version,
|
|
|
|
genesis_root = resp.data.data.genesis_validators_root
|
|
|
|
gres.add(resp.data.data)
|
|
|
|
else:
|
2021-09-01 16:08:24 +00:00
|
|
|
debug "Received unsuccessful response code", endpoint = nodes[i],
|
2021-07-13 11:15:07 +00:00
|
|
|
response_code = resp.status
|
|
|
|
bres.add(nodes[i])
|
|
|
|
elif fut.failed():
|
|
|
|
let error = fut.readError()
|
|
|
|
debug "Could not obtain genesis information from beacon node",
|
|
|
|
endpoint = nodes[i], error_name = error.name,
|
|
|
|
error_msg = error.msg
|
|
|
|
bres.add(nodes[i])
|
|
|
|
else:
|
|
|
|
debug "Interrupted while requesting information from beacon node",
|
|
|
|
endpoint = nodes[i]
|
|
|
|
bres.add(nodes[i])
|
|
|
|
(bres, gres)
|
|
|
|
|
|
|
|
if len(genesisList) == 0:
|
|
|
|
let sleepDuration = 2.seconds
|
|
|
|
info "Could not obtain network genesis information from nodes, repeating",
|
|
|
|
sleep_time = sleepDuration
|
|
|
|
await sleepAsync(sleepDuration)
|
|
|
|
nodes = errorNodes
|
|
|
|
else:
|
|
|
|
# Boyer-Moore majority vote algorithm
|
2021-08-03 15:17:11 +00:00
|
|
|
var melem: RestGenesis
|
2021-07-13 11:15:07 +00:00
|
|
|
var counter = 0
|
|
|
|
for item in genesisList:
|
|
|
|
if counter == 0:
|
|
|
|
melem = item
|
|
|
|
inc(counter)
|
|
|
|
else:
|
|
|
|
if melem == item:
|
|
|
|
inc(counter)
|
|
|
|
else:
|
|
|
|
dec(counter)
|
|
|
|
return melem
|
|
|
|
|
2023-10-13 12:42:00 +00:00
|
|
|
proc addValidatorsFromWeb3Signer(vc: ValidatorClientRef, web3signerUrl: Web3SignerUrl) {.async.} =
|
2023-09-06 19:04:10 +00:00
|
|
|
let res = await queryValidatorsSource(web3signerUrl)
|
2023-09-04 19:14:58 +00:00
|
|
|
if res.isOk():
|
|
|
|
let dynamicKeystores = res.get()
|
|
|
|
for keystore in dynamicKeystores:
|
|
|
|
vc.addValidator(keystore)
|
2023-09-06 19:04:10 +00:00
|
|
|
|
|
|
|
proc initValidators(vc: ValidatorClientRef): Future[bool] {.async.} =
|
|
|
|
info "Loading validators", validatorsDir = vc.config.validatorsDir()
|
|
|
|
for keystore in listLoadableKeystores(vc.config, vc.keystoreCache):
|
|
|
|
vc.addValidator(keystore)
|
|
|
|
|
|
|
|
let web3signerValidatorsFuts = mapIt(
|
2023-10-13 12:42:00 +00:00
|
|
|
vc.config.web3SignerUrls,
|
2023-09-06 19:04:10 +00:00
|
|
|
vc.addValidatorsFromWeb3Signer(it))
|
|
|
|
|
|
|
|
# We use `allFutures` because all failures are already reported as
|
|
|
|
# user-visible warnings in `queryValidatorsSource`.
|
|
|
|
# We don't consider them fatal because the Web3Signer may be experiencing
|
|
|
|
# a temporary hiccup that will be resolved later.
|
|
|
|
await allFutures(web3signerValidatorsFuts)
|
|
|
|
|
2023-09-04 19:14:58 +00:00
|
|
|
true
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-12-21 14:24:23 +00:00
|
|
|
proc initClock(vc: ValidatorClientRef): Future[BeaconClock] {.async.} =
|
2021-07-13 11:15:07 +00:00
|
|
|
# This procedure performs initialization of BeaconClock using current genesis
|
|
|
|
# information. It also performs waiting for genesis.
|
2023-04-17 21:31:54 +00:00
|
|
|
let
|
2024-01-06 14:26:56 +00:00
|
|
|
res = BeaconClock.init(vc.beaconGenesis.genesis_time).valueOr:
|
|
|
|
raise (ref ValidatorClientError)(
|
|
|
|
msg: "Invalid genesis time: " & $vc.beaconGenesis.genesis_time)
|
2023-04-17 21:31:54 +00:00
|
|
|
currentTime = res.now()
|
|
|
|
currentSlot = currentTime.slotOrZero()
|
|
|
|
currentEpoch = currentSlot.epoch()
|
|
|
|
genesisTime = res.fromNow(Slot(0))
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
if genesisTime.inFuture:
|
2023-04-17 21:31:54 +00:00
|
|
|
info "Initializing beacon clock",
|
|
|
|
genesis_time = vc.beaconGenesis.genesis_time,
|
|
|
|
current_slot = "<n/a>", current_epoch = "<n/a>",
|
|
|
|
time_to_genesis = genesisTime.offset
|
|
|
|
else:
|
|
|
|
info "Initializing beacon clock",
|
|
|
|
genesis_time = vc.beaconGenesis.genesis_time,
|
|
|
|
current_slot = currentSlot, current_epoch = currentEpoch
|
2021-07-13 11:15:07 +00:00
|
|
|
return res
|
|
|
|
|
2022-07-29 08:36:20 +00:00
|
|
|
proc initMetrics(vc: ValidatorClientRef): Future[bool] {.async.} =
|
|
|
|
if vc.config.metricsEnabled:
|
|
|
|
let
|
|
|
|
metricsAddress = vc.config.metricsAddress
|
|
|
|
metricsPort = vc.config.metricsPort
|
|
|
|
url = "http://" & $metricsAddress & ":" & $metricsPort & "/metrics"
|
|
|
|
info "Starting metrics HTTP server", url = url
|
|
|
|
let server =
|
|
|
|
block:
|
|
|
|
let res = MetricsHttpServerRef.new($metricsAddress, metricsPort)
|
|
|
|
if res.isErr():
|
|
|
|
error "Could not start metrics HTTP server", url = url,
|
|
|
|
error_msg = res.error()
|
|
|
|
return false
|
|
|
|
res.get()
|
2023-05-30 08:38:34 +00:00
|
|
|
vc.metricsServer = Opt.some(server)
|
2022-07-29 08:36:20 +00:00
|
|
|
try:
|
|
|
|
await server.start()
|
|
|
|
except MetricsError as exc:
|
|
|
|
error "Could not start metrics HTTP server", url = url,
|
|
|
|
error_msg = exc.msg, error_name = exc.name
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc shutdownMetrics(vc: ValidatorClientRef) {.async.} =
|
|
|
|
if vc.config.metricsEnabled:
|
|
|
|
if vc.metricsServer.isSome():
|
|
|
|
info "Shutting down metrics HTTP server"
|
|
|
|
await vc.metricsServer.get().close()
|
|
|
|
|
|
|
|
proc shutdownSlashingProtection(vc: ValidatorClientRef) =
|
|
|
|
info "Closing slashing protection", path = vc.config.validatorsDir()
|
2022-08-19 10:30:07 +00:00
|
|
|
vc.attachedValidators[].slashingProtection.close()
|
2022-07-29 08:36:20 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
proc runVCSlotLoop(vc: ValidatorClientRef) {.async.} =
|
|
|
|
var
|
|
|
|
startTime = vc.beaconClock.now()
|
|
|
|
curSlot = startTime.slotOrZero()
|
|
|
|
nextSlot = curSlot + 1 # No earlier than GENESIS_SLOT + 1
|
|
|
|
timeToNextSlot = nextSlot.start_beacon_time() - startTime
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
info "Scheduling first slot action",
|
|
|
|
start_time = shortLog(startTime),
|
|
|
|
current_slot = shortLog(curSlot),
|
|
|
|
next_slot = shortLog(nextSlot),
|
|
|
|
time_to_next_slot = shortLog(timeToNextSlot)
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
var currentSlot = Opt.some(curSlot)
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
while true:
|
|
|
|
currentSlot = await vc.checkedWaitForNextSlot(currentSlot, ZeroTimeDiff,
|
|
|
|
true)
|
|
|
|
if currentSlot.isNone():
|
|
|
|
## Fatal log line should be printed by checkedWaitForNextSlot().
|
|
|
|
return
|
2020-05-22 17:04:52 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
let
|
2023-04-17 21:31:54 +00:00
|
|
|
wallTime = vc.beaconClock.now()
|
|
|
|
wallSlot = currentSlot.get()
|
|
|
|
delay = wallTime - wallSlot.start_beacon_time()
|
|
|
|
|
|
|
|
if checkIfShouldStopAtEpoch(wallSlot, vc.config.stopAtEpoch):
|
|
|
|
return
|
|
|
|
|
2023-06-28 13:33:07 +00:00
|
|
|
vc.processingDelay = Opt.some(nanoseconds(delay.nanoseconds))
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
if len(vc.beaconNodes) > 1:
|
|
|
|
let
|
|
|
|
counts = vc.getNodeCounts()
|
|
|
|
# Good nodes are nodes which can be used for ALL the requests.
|
|
|
|
goodNodes = counts.data[int(RestBeaconNodeStatus.Synced)]
|
|
|
|
# Viable nodes are nodes which can be used only SOME of the requests.
|
|
|
|
viableNodes = counts.data[int(RestBeaconNodeStatus.OptSynced)] +
|
|
|
|
counts.data[int(RestBeaconNodeStatus.NotSynced)] +
|
|
|
|
counts.data[int(RestBeaconNodeStatus.Compatible)]
|
|
|
|
# Bad nodes are nodes which can't be used at all.
|
|
|
|
badNodes = counts.data[int(RestBeaconNodeStatus.Offline)] +
|
|
|
|
counts.data[int(RestBeaconNodeStatus.Online)] +
|
|
|
|
counts.data[int(RestBeaconNodeStatus.Incompatible)]
|
|
|
|
info "Slot start",
|
|
|
|
slot = shortLog(wallSlot),
|
|
|
|
epoch = shortLog(wallSlot.epoch()),
|
|
|
|
attestationIn = vc.getDurationToNextAttestation(wallSlot),
|
|
|
|
blockIn = vc.getDurationToNextBlock(wallSlot),
|
|
|
|
validators = vc.attachedValidators[].count(),
|
|
|
|
good_nodes = goodNodes, viable_nodes = viableNodes,
|
2023-06-28 13:33:07 +00:00
|
|
|
bad_nodes = badNodes,
|
|
|
|
delay = shortLog(delay)
|
2023-04-17 21:31:54 +00:00
|
|
|
else:
|
|
|
|
info "Slot start",
|
|
|
|
slot = shortLog(wallSlot),
|
|
|
|
epoch = shortLog(wallSlot.epoch()),
|
|
|
|
attestationIn = vc.getDurationToNextAttestation(wallSlot),
|
|
|
|
blockIn = vc.getDurationToNextBlock(wallSlot),
|
|
|
|
validators = vc.attachedValidators[].count(),
|
|
|
|
node_status = $vc.beaconNodes[0].status,
|
|
|
|
delay = shortLog(delay)
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc new*(T: type ValidatorClientRef,
|
|
|
|
config: ValidatorClientConf,
|
|
|
|
rng: ref HmacDrbgContext): ValidatorClientRef =
|
|
|
|
let beaconNodes =
|
|
|
|
block:
|
|
|
|
var servers: seq[BeaconNodeServerRef]
|
2022-09-29 07:57:14 +00:00
|
|
|
for index, url in config.beaconNodes.pairs():
|
|
|
|
let res = BeaconNodeServerRef.init(url, index)
|
2022-08-19 10:30:07 +00:00
|
|
|
if res.isErr():
|
2022-09-29 07:57:14 +00:00
|
|
|
warn "Unable to initialize remote beacon node",
|
|
|
|
url = $url, error = res.error()
|
2022-08-19 10:30:07 +00:00
|
|
|
else:
|
2023-09-15 05:34:32 +00:00
|
|
|
if res.get().status != RestBeaconNodeStatus.Noname:
|
|
|
|
debug "Beacon node was initialized", node = res.get()
|
2023-04-03 16:25:16 +00:00
|
|
|
servers.add(res.get())
|
2022-09-29 07:57:14 +00:00
|
|
|
let missingRoles = getMissingRoles(servers)
|
|
|
|
if len(missingRoles) != 0:
|
2023-04-03 16:25:16 +00:00
|
|
|
if len(servers) == 0:
|
|
|
|
fatal "Not enough beacon nodes available",
|
|
|
|
nodes_count = len(servers)
|
|
|
|
quit 1
|
|
|
|
else:
|
|
|
|
fatal "Beacon nodes do not cover all required roles",
|
|
|
|
missing_roles = $missingRoles, nodes_count = len(servers)
|
|
|
|
quit 1
|
2022-08-19 10:30:07 +00:00
|
|
|
servers
|
|
|
|
|
|
|
|
when declared(waitSignal):
|
|
|
|
ValidatorClientRef(
|
|
|
|
rng: rng,
|
|
|
|
config: config,
|
|
|
|
beaconNodes: beaconNodes,
|
|
|
|
graffitiBytes: config.graffiti.get(defaultGraffitiBytes()),
|
2023-04-17 21:31:54 +00:00
|
|
|
preGenesisEvent: newAsyncEvent(),
|
|
|
|
genesisEvent: newAsyncEvent(),
|
2022-08-19 10:30:07 +00:00
|
|
|
nodesAvailable: newAsyncEvent(),
|
|
|
|
forksAvailable: newAsyncEvent(),
|
2022-12-09 16:05:55 +00:00
|
|
|
doppelExit: newAsyncEvent(),
|
2022-10-21 14:53:30 +00:00
|
|
|
indicesAvailable: newAsyncEvent(),
|
|
|
|
dynamicFeeRecipientsStore: newClone(DynamicFeeRecipientsStore.init()),
|
2022-08-19 10:30:07 +00:00
|
|
|
sigintHandleFut: waitSignal(SIGINT),
|
2023-02-16 17:25:48 +00:00
|
|
|
sigtermHandleFut: waitSignal(SIGTERM),
|
|
|
|
keystoreCache: KeystoreCacheRef.init()
|
2022-08-19 10:30:07 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
ValidatorClientRef(
|
|
|
|
rng: rng,
|
|
|
|
config: config,
|
|
|
|
beaconNodes: beaconNodes,
|
|
|
|
graffitiBytes: config.graffiti.get(defaultGraffitiBytes()),
|
2023-04-17 21:31:54 +00:00
|
|
|
preGenesisEvent: newAsyncEvent(),
|
|
|
|
genesisEvent: newAsyncEvent(),
|
2022-08-19 10:30:07 +00:00
|
|
|
nodesAvailable: newAsyncEvent(),
|
|
|
|
forksAvailable: newAsyncEvent(),
|
2022-10-21 14:53:30 +00:00
|
|
|
indicesAvailable: newAsyncEvent(),
|
2022-12-09 16:05:55 +00:00
|
|
|
doppelExit: newAsyncEvent(),
|
2022-10-21 14:53:30 +00:00
|
|
|
dynamicFeeRecipientsStore: newClone(DynamicFeeRecipientsStore.init()),
|
2022-08-19 10:30:07 +00:00
|
|
|
sigintHandleFut: newFuture[void]("sigint_placeholder"),
|
2023-02-16 17:25:48 +00:00
|
|
|
sigtermHandleFut: newFuture[void]("sigterm_placeholder"),
|
|
|
|
keystoreCache: KeystoreCacheRef.init()
|
2022-08-19 10:30:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc asyncInit(vc: ValidatorClientRef): Future[ValidatorClientRef] {.async.} =
|
|
|
|
notice "Launching validator client", version = fullVersionStr,
|
|
|
|
cmdParams = commandLineParams(),
|
|
|
|
config = vc.config,
|
|
|
|
beacon_nodes_count = len(vc.beaconNodes)
|
|
|
|
|
2023-04-03 16:25:16 +00:00
|
|
|
for node in vc.beaconNodes:
|
2023-09-15 05:34:32 +00:00
|
|
|
if node.status == RestBeaconNodeStatus.Offline:
|
|
|
|
notice "Beacon node initialized", node = node
|
|
|
|
else:
|
|
|
|
notice "Cannot initialize beacon node", node = node, status = node.status
|
2023-04-03 16:25:16 +00:00
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
vc.beaconGenesis = await vc.initGenesis()
|
|
|
|
info "Genesis information", genesis_time = vc.beaconGenesis.genesis_time,
|
2023-04-17 21:31:54 +00:00
|
|
|
genesis_fork_version = vc.beaconGenesis.genesis_fork_version,
|
|
|
|
genesis_root = vc.beaconGenesis.genesis_validators_root
|
2022-07-13 14:43:57 +00:00
|
|
|
|
|
|
|
vc.beaconClock = await vc.initClock()
|
|
|
|
|
2022-07-29 08:36:20 +00:00
|
|
|
if not(await initMetrics(vc)):
|
|
|
|
raise newException(ValidatorClientError,
|
|
|
|
"Could not initialize metrics server")
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
info "Initializing slashing protection", path = vc.config.validatorsDir()
|
|
|
|
|
|
|
|
let
|
|
|
|
slashingProtectionDB =
|
|
|
|
SlashingProtectionDB.init(
|
|
|
|
vc.beaconGenesis.genesis_validators_root,
|
|
|
|
vc.config.validatorsDir(), "slashing_protection")
|
2022-12-09 16:05:55 +00:00
|
|
|
validatorPool = newClone(ValidatorPool.init(
|
|
|
|
slashingProtectionDB, vc.config.doppelgangerDetection))
|
2022-08-19 10:30:07 +00:00
|
|
|
|
|
|
|
vc.attachedValidators = validatorPool
|
|
|
|
|
|
|
|
if not(await initValidators(vc)):
|
2022-07-29 08:36:20 +00:00
|
|
|
await vc.shutdownMetrics()
|
2022-08-19 10:30:07 +00:00
|
|
|
raise newException(ValidatorClientError,
|
|
|
|
"Could not initialize local validators")
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
let
|
|
|
|
keymanagerInitResult = initKeymanagerServer(vc.config, nil)
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2024-02-26 08:48:07 +00:00
|
|
|
func getCapellaForkVersion(): Opt[Version] =
|
|
|
|
if vc.runtimeConfig.forkConfig.isSome():
|
|
|
|
vc.runtimeConfig.forkConfig.get().capellaVersion
|
|
|
|
else:
|
|
|
|
Opt.none(Version)
|
|
|
|
|
|
|
|
func getDenebForkEpoch(): Opt[Epoch] =
|
|
|
|
if vc.runtimeConfig.forkConfig.isSome():
|
|
|
|
Opt.some(vc.runtimeConfig.forkConfig.get().denebEpoch)
|
|
|
|
else:
|
|
|
|
Opt.none(Epoch)
|
|
|
|
|
2023-06-14 06:46:01 +00:00
|
|
|
proc getForkForEpoch(epoch: Epoch): Opt[Fork] =
|
|
|
|
if len(vc.forks) > 0:
|
|
|
|
Opt.some(vc.forkAtEpoch(epoch))
|
|
|
|
else:
|
|
|
|
Opt.none(Fork)
|
|
|
|
|
|
|
|
proc getGenesisRoot(): Eth2Digest =
|
|
|
|
vc.beaconGenesis.genesis_validators_root
|
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
try:
|
|
|
|
vc.fallbackService = await FallbackServiceRef.init(vc)
|
|
|
|
vc.forkService = await ForkServiceRef.init(vc)
|
|
|
|
vc.dutiesService = await DutiesServiceRef.init(vc)
|
2022-07-21 16:54:07 +00:00
|
|
|
vc.doppelgangerService = await DoppelgangerServiceRef.init(vc)
|
2022-07-13 14:43:57 +00:00
|
|
|
vc.attestationService = await AttestationServiceRef.init(vc)
|
2023-04-17 21:31:54 +00:00
|
|
|
vc.blockService = await BlockServiceRef.init(vc)
|
2022-07-13 14:43:57 +00:00
|
|
|
vc.syncCommitteeService = await SyncCommitteeServiceRef.init(vc)
|
2022-08-19 10:30:07 +00:00
|
|
|
vc.keymanagerServer = keymanagerInitResult.server
|
2023-09-21 18:07:02 +00:00
|
|
|
if not(isNil(vc.keymanagerServer)):
|
2022-08-19 10:30:07 +00:00
|
|
|
vc.keymanagerHost = newClone KeymanagerHost.init(
|
|
|
|
validatorPool,
|
2023-09-21 18:07:02 +00:00
|
|
|
vc.keystoreCache,
|
2022-08-19 10:30:07 +00:00
|
|
|
vc.rng,
|
|
|
|
keymanagerInitResult.token,
|
|
|
|
vc.config.validatorsDir,
|
|
|
|
vc.config.secretsDir,
|
|
|
|
vc.config.defaultFeeRecipient,
|
2023-02-15 15:10:31 +00:00
|
|
|
vc.config.suggestedGasLimit,
|
2023-06-25 12:00:17 +00:00
|
|
|
Opt.none(string),
|
2023-02-07 14:53:36 +00:00
|
|
|
nil,
|
2023-06-14 06:46:01 +00:00
|
|
|
vc.beaconClock.getBeaconTimeFn,
|
2024-02-26 08:48:07 +00:00
|
|
|
getCapellaForkVersion,
|
|
|
|
getDenebForkEpoch,
|
2023-06-14 06:46:01 +00:00
|
|
|
getForkForEpoch,
|
|
|
|
getGenesisRoot
|
|
|
|
)
|
2022-08-19 10:30:07 +00:00
|
|
|
|
2022-07-29 08:36:20 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
warn "Unexpected error encountered while initializing",
|
|
|
|
error_name = exc.name, error_msg = exc.msg
|
|
|
|
await vc.shutdownMetrics()
|
|
|
|
vc.shutdownSlashingProtection()
|
2022-07-13 14:43:57 +00:00
|
|
|
except CancelledError:
|
|
|
|
debug "Initialization process interrupted"
|
2022-07-29 08:36:20 +00:00
|
|
|
await vc.shutdownMetrics()
|
|
|
|
vc.shutdownSlashingProtection()
|
|
|
|
return
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
return vc
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
proc runPreGenesisWaitingLoop(vc: ValidatorClientRef) {.async.} =
|
|
|
|
var breakLoop = false
|
|
|
|
while not(breakLoop):
|
|
|
|
let
|
|
|
|
genesisTime = vc.beaconClock.fromNow(Slot(0))
|
|
|
|
currentEpoch = vc.beaconClock.now().toSlot().slot.epoch()
|
|
|
|
|
|
|
|
if not(genesisTime.inFuture) or currentEpoch < PREGENESIS_EPOCHS_COUNT:
|
|
|
|
break
|
|
|
|
|
|
|
|
notice "Waiting for genesis",
|
|
|
|
genesis_time = vc.beaconGenesis.genesis_time,
|
|
|
|
time_to_genesis = genesisTime.offset
|
|
|
|
|
|
|
|
breakLoop =
|
|
|
|
try:
|
|
|
|
await sleepAsync(vc.beaconClock.durationToNextSlot())
|
|
|
|
false
|
2023-09-15 10:39:26 +00:00
|
|
|
except CancelledError as exc:
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Pre-genesis waiting loop was interrupted"
|
2023-09-15 10:39:26 +00:00
|
|
|
raise exc
|
2023-04-17 21:31:54 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
error "Pre-genesis waiting loop failed with unexpected error",
|
|
|
|
err_name = $exc.name, err_msg = $exc.msg
|
|
|
|
true
|
2023-09-15 10:39:26 +00:00
|
|
|
|
|
|
|
if not(breakLoop):
|
|
|
|
vc.preGenesisEvent.fire()
|
2023-04-17 21:31:54 +00:00
|
|
|
|
|
|
|
proc runGenesisWaitingLoop(vc: ValidatorClientRef) {.async.} =
|
|
|
|
var breakLoop = false
|
|
|
|
while not(breakLoop):
|
|
|
|
let genesisTime = vc.beaconClock.fromNow(Slot(0))
|
|
|
|
|
|
|
|
if not(genesisTime.inFuture):
|
|
|
|
break
|
|
|
|
|
|
|
|
notice "Waiting for genesis",
|
|
|
|
genesis_time = vc.beaconGenesis.genesis_time,
|
|
|
|
time_to_genesis = genesisTime.offset
|
|
|
|
|
|
|
|
breakLoop =
|
|
|
|
try:
|
|
|
|
await sleepAsync(vc.beaconClock.durationToNextSlot())
|
|
|
|
false
|
2023-09-15 10:39:26 +00:00
|
|
|
except CancelledError as exc:
|
2023-04-17 21:31:54 +00:00
|
|
|
debug "Genesis waiting loop was interrupted"
|
2023-09-15 10:39:26 +00:00
|
|
|
raise exc
|
2023-04-17 21:31:54 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
error "Genesis waiting loop failed with unexpected error",
|
|
|
|
err_name = $exc.name, err_msg = $exc.msg
|
|
|
|
true
|
2023-09-15 10:39:26 +00:00
|
|
|
|
|
|
|
if not(breakLoop):
|
|
|
|
vc.genesisEvent.fire()
|
2023-04-17 21:31:54 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc asyncRun*(vc: ValidatorClientRef) {.async.} =
|
2021-07-13 11:15:07 +00:00
|
|
|
vc.fallbackService.start()
|
|
|
|
vc.forkService.start()
|
|
|
|
vc.dutiesService.start()
|
2022-07-21 16:54:07 +00:00
|
|
|
vc.doppelgangerService.start()
|
2021-07-13 11:15:07 +00:00
|
|
|
vc.attestationService.start()
|
2023-04-17 21:31:54 +00:00
|
|
|
vc.blockService.start()
|
2022-05-10 10:03:40 +00:00
|
|
|
vc.syncCommitteeService.start()
|
2020-05-27 17:06:28 +00:00
|
|
|
|
2023-09-21 18:07:02 +00:00
|
|
|
if not(isNil(vc.keymanagerServer)):
|
|
|
|
doAssert not(isNil(vc.keymanagerHost))
|
2022-08-19 10:30:07 +00:00
|
|
|
vc.keymanagerServer.router.installKeymanagerHandlers(vc.keymanagerHost[])
|
|
|
|
vc.keymanagerServer.start()
|
|
|
|
|
2023-03-02 15:55:45 +00:00
|
|
|
let doppelEventFut = vc.doppelExit.wait()
|
2022-07-13 14:43:57 +00:00
|
|
|
try:
|
2023-04-17 21:31:54 +00:00
|
|
|
# Waiting for `GENESIS - PREGENESIS_EPOCHS_COUNT` loop.
|
|
|
|
await vc.runPreGenesisWaitingLoop()
|
|
|
|
# Waiting for `GENESIS` loop.
|
|
|
|
await vc.runGenesisWaitingLoop()
|
|
|
|
# Main processing loop.
|
|
|
|
vc.runSlotLoopFut = vc.runVCSlotLoop()
|
2023-02-16 17:25:48 +00:00
|
|
|
vc.runKeystoreCachePruningLoopFut =
|
2023-05-10 10:20:55 +00:00
|
|
|
runKeystoreCachePruningLoop(vc.keystoreCache)
|
2022-12-09 16:05:55 +00:00
|
|
|
discard await race(vc.runSlotLoopFut, doppelEventFut)
|
2022-07-14 21:11:25 +00:00
|
|
|
if not(vc.runSlotLoopFut.finished()):
|
|
|
|
notice "Received shutdown event, exiting"
|
2022-07-13 14:43:57 +00:00
|
|
|
except CancelledError:
|
|
|
|
debug "Main loop interrupted"
|
|
|
|
except CatchableError as exc:
|
|
|
|
debug "Main loop failed with an error", err_name = $exc.name,
|
|
|
|
err_msg = $exc.msg
|
|
|
|
|
2022-07-29 08:36:20 +00:00
|
|
|
await vc.shutdownMetrics()
|
|
|
|
vc.shutdownSlashingProtection()
|
2022-12-09 16:05:55 +00:00
|
|
|
|
2023-03-02 15:55:45 +00:00
|
|
|
if doppelEventFut.completed():
|
2022-12-09 16:05:55 +00:00
|
|
|
# Critically, database has been shut down - the rest doesn't matter, we need
|
|
|
|
# to stop as soon as possible
|
|
|
|
quitDoppelganger()
|
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Stopping main processing loop"
|
|
|
|
var pending: seq[Future[void]]
|
2023-09-15 10:39:26 +00:00
|
|
|
if not(isNil(vc.runSlotLoopFut)) and not(vc.runSlotLoopFut.finished()):
|
2022-07-13 14:43:57 +00:00
|
|
|
pending.add(vc.runSlotLoopFut.cancelAndWait())
|
2023-09-15 10:39:26 +00:00
|
|
|
if not(isNil(vc.runKeystoreCachePruningLoopFut)) and
|
|
|
|
not(vc.runKeystoreCachePruningLoopFut.finished()):
|
2023-02-16 17:25:48 +00:00
|
|
|
pending.add(vc.runKeystoreCachePruningLoopFut.cancelAndWait())
|
2022-12-09 16:05:55 +00:00
|
|
|
if not(doppelEventFut.finished()):
|
|
|
|
pending.add(doppelEventFut.cancelAndWait())
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Stopping running services"
|
|
|
|
pending.add(vc.fallbackService.stop())
|
|
|
|
pending.add(vc.forkService.stop())
|
|
|
|
pending.add(vc.dutiesService.stop())
|
2022-07-21 16:54:07 +00:00
|
|
|
pending.add(vc.doppelgangerService.stop())
|
2022-07-13 14:43:57 +00:00
|
|
|
pending.add(vc.attestationService.stop())
|
2023-04-17 21:31:54 +00:00
|
|
|
pending.add(vc.blockService.stop())
|
2022-07-13 14:43:57 +00:00
|
|
|
pending.add(vc.syncCommitteeService.stop())
|
2022-08-19 10:30:07 +00:00
|
|
|
if not isNil(vc.keymanagerServer):
|
|
|
|
pending.add(vc.keymanagerServer.stop())
|
2022-07-13 14:43:57 +00:00
|
|
|
await allFutures(pending)
|
|
|
|
|
|
|
|
template runWithSignals(vc: ValidatorClientRef, body: untyped): bool =
|
|
|
|
let future = body
|
|
|
|
discard await race(future, vc.sigintHandleFut, vc.sigtermHandleFut)
|
|
|
|
if future.finished():
|
|
|
|
if future.failed() or future.cancelled():
|
|
|
|
let exc = future.readError()
|
2024-01-06 14:26:56 +00:00
|
|
|
error "Validator client initialization failed", err_name = $exc.name,
|
2022-07-13 14:43:57 +00:00
|
|
|
err_msg = $exc.msg
|
|
|
|
var pending: seq[Future[void]]
|
|
|
|
if not(vc.sigintHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(vc.sigintHandleFut))
|
|
|
|
if not(vc.sigtermHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(vc.sigtermHandleFut))
|
|
|
|
await allFutures(pending)
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
let signal = if vc.sigintHandleFut.finished(): "SIGINT" else: "SIGTERM"
|
|
|
|
info "Got interrupt, trying to shutdown gracefully", signal = signal
|
|
|
|
var pending = @[cancelAndWait(future)]
|
|
|
|
if not(vc.sigintHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(vc.sigintHandleFut))
|
|
|
|
if not(vc.sigtermHandleFut.finished()):
|
|
|
|
pending.add(cancelAndWait(vc.sigtermHandleFut))
|
|
|
|
await allFutures(pending)
|
|
|
|
false
|
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
proc runValidatorClient*(config: ValidatorClientConf,
|
|
|
|
rng: ref HmacDrbgContext) {.async.} =
|
|
|
|
let vc = ValidatorClientRef.new(config, rng)
|
|
|
|
if not vc.runWithSignals(asyncInit vc):
|
2022-07-13 14:43:57 +00:00
|
|
|
return
|
2022-08-19 10:30:07 +00:00
|
|
|
if not vc.runWithSignals(asyncRun vc):
|
2022-07-13 14:43:57 +00:00
|
|
|
return
|
2021-03-26 06:52:01 +00:00
|
|
|
|
2020-05-22 17:04:52 +00:00
|
|
|
programMain:
|
2022-08-19 10:30:07 +00:00
|
|
|
let
|
|
|
|
config = makeBannerAndConfig("Nimbus validator client " & fullVersionStr,
|
|
|
|
ValidatorClientConf)
|
2022-02-27 11:02:45 +00:00
|
|
|
|
2022-08-19 10:30:07 +00:00
|
|
|
# Single RNG instance for the application - will be seeded on construction
|
|
|
|
# and avoid using system resources (such as urandom) after that
|
2023-06-19 22:43:50 +00:00
|
|
|
rng = HmacDrbgContext.new()
|
2022-02-27 11:02:45 +00:00
|
|
|
|
2023-09-21 14:08:13 +00:00
|
|
|
setupFileLimits()
|
2022-08-19 10:30:07 +00:00
|
|
|
setupLogging(config.logLevel, config.logStdout, config.logFile)
|
|
|
|
waitFor runValidatorClient(config, rng)
|