2022-07-29 08:36:20 +00:00
|
|
|
# beacon_chain
|
2024-01-03 05:58:34 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2022-07-29 08:36:20 +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-18 01:16:49 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-01-06 06:18:28 +00:00
|
|
|
import ./common
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-07-14 21:11:25 +00:00
|
|
|
const
|
|
|
|
ServiceName = "fallback_service"
|
|
|
|
|
2023-06-28 13:33:07 +00:00
|
|
|
FAIL_TIME_OFFSETS = [
|
|
|
|
TimeOffset.init(-(MAXIMUM_GOSSIP_CLOCK_DISPARITY.nanoseconds)),
|
|
|
|
TimeOffset.init(MAXIMUM_GOSSIP_CLOCK_DISPARITY.nanoseconds * 4)
|
|
|
|
]
|
|
|
|
WARN_TIME_OFFSETS = [
|
|
|
|
TimeOffset.init(-(MAXIMUM_GOSSIP_CLOCK_DISPARITY.nanoseconds div 2)),
|
|
|
|
TimeOffset.init(MAXIMUM_GOSSIP_CLOCK_DISPARITY.nanoseconds * 2),
|
|
|
|
]
|
|
|
|
NOTE_TIME_OFFSETS = [
|
|
|
|
TimeOffset.init(-(MAXIMUM_GOSSIP_CLOCK_DISPARITY.nanoseconds div 4)),
|
|
|
|
TimeOffset.init(MAXIMUM_GOSSIP_CLOCK_DISPARITY.nanoseconds),
|
|
|
|
]
|
|
|
|
|
|
|
|
declareGauge validator_client_time_offset,
|
|
|
|
"Wall clock offset(s) between validator client and beacon node(s)",
|
|
|
|
labels = ["node"]
|
|
|
|
|
2022-07-14 21:11:25 +00:00
|
|
|
logScope: service = ServiceName
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc nodesCount*(vc: ValidatorClientRef,
|
|
|
|
statuses: set[RestBeaconNodeStatus],
|
|
|
|
roles: set[BeaconNodeRole] = {}): int =
|
2022-09-29 07:57:14 +00:00
|
|
|
if len(roles) == 0:
|
2023-02-23 00:11:00 +00:00
|
|
|
vc.beaconNodes.countIt(it.status in statuses)
|
2022-09-29 07:57:14 +00:00
|
|
|
else:
|
2023-02-23 00:11:00 +00:00
|
|
|
vc.beaconNodes.countIt((it.roles * roles != {}) and (it.status in statuses))
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc filterNodes*(vc: ValidatorClientRef, statuses: set[RestBeaconNodeStatus],
|
|
|
|
roles: set[BeaconNodeRole] = {}): seq[BeaconNodeServerRef] =
|
2022-09-29 07:57:14 +00:00
|
|
|
if len(roles) == 0:
|
2023-02-23 00:11:00 +00:00
|
|
|
vc.beaconNodes.filterIt(it.status in statuses)
|
2022-09-29 07:57:14 +00:00
|
|
|
else:
|
2023-02-23 00:11:00 +00:00
|
|
|
vc.beaconNodes.filterIt((it.roles * roles != {}) and
|
|
|
|
(it.status in statuses))
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-09-15 05:34:32 +00:00
|
|
|
proc nonameNodes*(vc: ValidatorClientRef): seq[BeaconNodeServerRef] =
|
|
|
|
vc.beaconNodes.filterIt(it.status == RestBeaconNodeStatus.Noname)
|
|
|
|
|
|
|
|
proc offlineNodes*(vc: ValidatorClientRef): seq[BeaconNodeServerRef] =
|
|
|
|
vc.beaconNodes.filterIt(it.status == RestBeaconNodeStatus.Offline)
|
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc otherNodes*(vc: ValidatorClientRef): seq[BeaconNodeServerRef] =
|
|
|
|
vc.beaconNodes.filterIt(it.status != RestBeaconNodeStatus.Synced)
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc otherNodesCount*(vc: ValidatorClientRef): int =
|
|
|
|
vc.beaconNodes.countIt(it.status != RestBeaconNodeStatus.Synced)
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
proc preGenesisNodes*(vc: ValidatorClientRef): seq[BeaconNodeServerRef] =
|
|
|
|
vc.beaconNodes.filterIt(it.status notin {RestBeaconNodeStatus.Synced,
|
|
|
|
RestBeaconNodeStatus.OptSynced})
|
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc waitNodes*(vc: ValidatorClientRef, timeoutFut: Future[void],
|
|
|
|
statuses: set[RestBeaconNodeStatus],
|
|
|
|
roles: set[BeaconNodeRole], waitChanges: bool) {.async.} =
|
2022-07-13 14:43:57 +00:00
|
|
|
doAssert(not(isNil(vc.fallbackService)))
|
2023-02-23 00:11:00 +00:00
|
|
|
var iterations = 0
|
2022-07-13 14:43:57 +00:00
|
|
|
while true:
|
2023-02-23 00:11:00 +00:00
|
|
|
if not(waitChanges) or (iterations != 0):
|
|
|
|
if vc.nodesCount(statuses, roles) != 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
if vc.fallbackService.changesEvent.isSet():
|
|
|
|
vc.fallbackService.changesEvent.clear()
|
2022-07-14 21:11:25 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
if isNil(timeoutFut):
|
|
|
|
await vc.fallbackService.changesEvent.wait()
|
|
|
|
else:
|
|
|
|
let breakLoop =
|
|
|
|
block:
|
|
|
|
let waitFut = vc.fallbackService.changesEvent.wait()
|
|
|
|
try:
|
|
|
|
discard await race(waitFut, timeoutFut)
|
|
|
|
except CancelledError as exc:
|
2022-07-14 21:11:25 +00:00
|
|
|
if not(waitFut.finished()):
|
|
|
|
await waitFut.cancelAndWait()
|
2023-02-23 00:11:00 +00:00
|
|
|
raise exc
|
|
|
|
|
|
|
|
if not(waitFut.finished()):
|
|
|
|
await waitFut.cancelAndWait()
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
if breakLoop:
|
|
|
|
break
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
inc(iterations)
|
|
|
|
|
2023-09-15 05:34:32 +00:00
|
|
|
proc checkName*(
|
|
|
|
node: BeaconNodeServerRef): RestBeaconNodeStatus {.raises: [].} =
|
|
|
|
## Could return only {Invalid, Noname, Offline}
|
|
|
|
logScope: endpoint = node
|
|
|
|
let client =
|
|
|
|
block:
|
|
|
|
let res = initClient(node.uri)
|
|
|
|
if res.isErr():
|
|
|
|
return
|
|
|
|
case res.error
|
|
|
|
of CriticalHttpAddressError:
|
|
|
|
RestBeaconNodeStatus.Invalid
|
|
|
|
of RecoverableHttpAddressError:
|
|
|
|
RestBeaconNodeStatus.Noname
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
node.client = client
|
|
|
|
RestBeaconNodeStatus.Offline
|
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc checkCompatible(
|
|
|
|
vc: ValidatorClientRef,
|
|
|
|
node: BeaconNodeServerRef
|
|
|
|
): Future[RestBeaconNodeStatus] {.async.} =
|
2023-05-30 08:38:34 +00:00
|
|
|
## Could return only {Offline, Incompatible, Compatible}
|
2022-07-13 14:43:57 +00:00
|
|
|
logScope: endpoint = node
|
|
|
|
let info =
|
|
|
|
try:
|
|
|
|
debug "Requesting beacon node network configuration"
|
|
|
|
let res = await node.client.getSpecVC()
|
|
|
|
res.data.data
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Configuration request was interrupted"
|
|
|
|
raise exc
|
|
|
|
except RestError as exc:
|
2023-02-23 00:11:00 +00:00
|
|
|
if node.status != RestBeaconNodeStatus.Offline:
|
|
|
|
debug "Unable to obtain beacon node's configuration",
|
|
|
|
error_name = exc.name, error_message = exc.msg
|
|
|
|
return RestBeaconNodeStatus.Offline
|
2022-07-13 14:43:57 +00:00
|
|
|
except CatchableError as exc:
|
2023-02-23 00:11:00 +00:00
|
|
|
if node.status != RestBeaconNodeStatus.Offline:
|
|
|
|
error "Unexpected exception", error_name = exc.name,
|
|
|
|
error_message = exc.msg
|
|
|
|
return RestBeaconNodeStatus.Offline
|
2022-07-13 14:43:57 +00:00
|
|
|
|
|
|
|
let genesis =
|
|
|
|
try:
|
|
|
|
debug "Requesting beacon node genesis information"
|
|
|
|
let res = await node.client.getGenesis()
|
|
|
|
res.data.data
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Genesis request was interrupted"
|
|
|
|
raise exc
|
|
|
|
except RestError as exc:
|
2023-02-23 00:11:00 +00:00
|
|
|
if node.status != RestBeaconNodeStatus.Offline:
|
|
|
|
debug "Unable to obtain beacon node's genesis",
|
|
|
|
error_name = exc.name, error_message = exc.msg
|
|
|
|
return RestBeaconNodeStatus.Offline
|
2022-07-13 14:43:57 +00:00
|
|
|
except CatchableError as exc:
|
2023-02-23 00:11:00 +00:00
|
|
|
if node.status != RestBeaconNodeStatus.Offline:
|
|
|
|
error "Unexpected exception", error_name = exc.name,
|
|
|
|
error_message = exc.msg
|
|
|
|
return RestBeaconNodeStatus.Offline
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-03-03 20:20:01 +00:00
|
|
|
let
|
|
|
|
genesisFlag = (genesis != vc.beaconGenesis)
|
|
|
|
configFlag = not(checkConfig(info))
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-06-28 13:33:38 +00:00
|
|
|
node.config = info
|
2023-05-30 08:38:34 +00:00
|
|
|
node.genesis = Opt.some(genesis)
|
2023-08-15 17:00:27 +00:00
|
|
|
|
|
|
|
return
|
2023-02-23 00:11:00 +00:00
|
|
|
if configFlag or genesisFlag:
|
|
|
|
if node.status != RestBeaconNodeStatus.Incompatible:
|
|
|
|
warn "Beacon node has incompatible configuration",
|
|
|
|
genesis_flag = genesisFlag, config_flag = configFlag
|
|
|
|
RestBeaconNodeStatus.Incompatible
|
|
|
|
else:
|
2023-08-15 17:00:27 +00:00
|
|
|
let res = vc.updateRuntimeConfig(node, node.config)
|
|
|
|
if res.isErr():
|
|
|
|
warn "Beacon nodes report different configuration values",
|
|
|
|
reason = res.error
|
|
|
|
RestBeaconNodeStatus.Incompatible
|
|
|
|
else:
|
|
|
|
RestBeaconNodeStatus.Compatible
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc checkSync(
|
|
|
|
vc: ValidatorClientRef,
|
|
|
|
node: BeaconNodeServerRef
|
|
|
|
): Future[RestBeaconNodeStatus] {.async.} =
|
2023-05-30 08:38:34 +00:00
|
|
|
## Could return only {Offline, NotSynced, Synced, OptSynced}
|
2022-07-13 14:43:57 +00:00
|
|
|
logScope: endpoint = node
|
|
|
|
let syncInfo =
|
|
|
|
try:
|
|
|
|
debug "Requesting beacon node sync status"
|
|
|
|
let res = await node.client.getSyncingStatus()
|
|
|
|
res.data.data
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Sync status request was interrupted"
|
|
|
|
raise exc
|
|
|
|
except RestError as exc:
|
2023-02-23 00:11:00 +00:00
|
|
|
if node.status != RestBeaconNodeStatus.Offline:
|
|
|
|
debug "Unable to obtain beacon node's sync status",
|
|
|
|
error_name = exc.name, error_message = exc.msg
|
|
|
|
return RestBeaconNodeStatus.Offline
|
2022-07-13 14:43:57 +00:00
|
|
|
except CatchableError as exc:
|
2023-02-23 00:11:00 +00:00
|
|
|
if node.status != RestBeaconNodeStatus.Offline:
|
|
|
|
error "Unexpected exception", error_name = exc.name,
|
|
|
|
error_message = exc.msg
|
|
|
|
return RestBeaconNodeStatus.Offline
|
2023-05-30 08:38:34 +00:00
|
|
|
node.syncInfo = Opt.some(syncInfo)
|
2023-02-23 00:11:00 +00:00
|
|
|
let res =
|
2022-08-16 22:14:32 +00:00
|
|
|
block:
|
|
|
|
if not(syncInfo.is_syncing) or (syncInfo.sync_distance < SYNC_TOLERANCE):
|
|
|
|
if not(syncInfo.is_optimistic.get(false)):
|
2023-02-23 00:11:00 +00:00
|
|
|
RestBeaconNodeStatus.Synced
|
2022-08-16 22:14:32 +00:00
|
|
|
else:
|
2023-02-23 00:11:00 +00:00
|
|
|
RestBeaconNodeStatus.OptSynced
|
|
|
|
else:
|
2022-08-16 22:14:32 +00:00
|
|
|
RestBeaconNodeStatus.NotSynced
|
2023-02-23 00:11:00 +00:00
|
|
|
return res
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc checkOnline(
|
|
|
|
node: BeaconNodeServerRef
|
|
|
|
): Future[RestBeaconNodeStatus] {.async.} =
|
2023-05-30 08:38:34 +00:00
|
|
|
## Could return only {Offline, Online}.
|
2022-07-13 14:43:57 +00:00
|
|
|
logScope: endpoint = node
|
|
|
|
debug "Checking beacon node status"
|
|
|
|
let agent =
|
|
|
|
try:
|
|
|
|
let res = await node.client.getNodeVersion()
|
|
|
|
res.data.data
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Status request was interrupted"
|
|
|
|
raise exc
|
|
|
|
except RestError as exc:
|
2023-03-03 20:20:01 +00:00
|
|
|
debug "Unable to check beacon node's status",
|
|
|
|
error_name = exc.name, error_message = exc.msg
|
2023-02-23 00:11:00 +00:00
|
|
|
return RestBeaconNodeStatus.Offline
|
2022-07-13 14:43:57 +00:00
|
|
|
except CatchableError as exc:
|
2023-03-03 20:20:01 +00:00
|
|
|
error "Unexpected exception", error_name = exc.name,
|
|
|
|
error_message = exc.msg
|
2023-02-23 00:11:00 +00:00
|
|
|
return RestBeaconNodeStatus.Offline
|
2023-05-30 08:38:34 +00:00
|
|
|
node.ident = Opt.some(agent.version)
|
2023-02-23 00:11:00 +00:00
|
|
|
return RestBeaconNodeStatus.Online
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-05-30 08:38:34 +00:00
|
|
|
func getReason(status: RestBeaconNodeStatus): string =
|
|
|
|
case status
|
2023-09-15 05:34:32 +00:00
|
|
|
of RestBeaconNodeStatus.Invalid:
|
|
|
|
"Beacon node address invalid"
|
|
|
|
of RestBeaconNodeStatus.Noname:
|
|
|
|
"Beacon node address cannot be resolved"
|
2023-05-30 08:38:34 +00:00
|
|
|
of RestBeaconNodeStatus.Offline:
|
|
|
|
"Connection with node has been lost"
|
|
|
|
of RestBeaconNodeStatus.Online:
|
|
|
|
"Connection with node has been established"
|
|
|
|
else:
|
|
|
|
"Beacon node reports"
|
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
proc checkNode(vc: ValidatorClientRef,
|
2023-02-23 00:11:00 +00:00
|
|
|
node: BeaconNodeServerRef): Future[bool] {.async.} =
|
|
|
|
let nstatus = node.status
|
|
|
|
debug "Checking beacon node", endpoint = node, status = node.status
|
|
|
|
|
2023-09-15 05:34:32 +00:00
|
|
|
if nstatus in {RestBeaconNodeStatus.Noname}:
|
|
|
|
let
|
|
|
|
status = node.checkName()
|
|
|
|
failure = ApiNodeFailure.init(ApiFailure.NoError, "checkName",
|
|
|
|
node, status.getReason())
|
|
|
|
node.updateStatus(status, failure)
|
|
|
|
if status != RestBeaconNodeStatus.Offline:
|
|
|
|
return nstatus != status
|
|
|
|
|
2023-03-03 20:20:01 +00:00
|
|
|
if nstatus in {RestBeaconNodeStatus.Offline,
|
2023-05-30 08:38:34 +00:00
|
|
|
RestBeaconNodeStatus.UnexpectedCode,
|
|
|
|
RestBeaconNodeStatus.UnexpectedResponse,
|
2023-03-03 20:20:01 +00:00
|
|
|
RestBeaconNodeStatus.InternalError}:
|
2023-05-30 08:38:34 +00:00
|
|
|
let
|
|
|
|
status = await node.checkOnline()
|
|
|
|
failure = ApiNodeFailure.init(ApiFailure.NoError, "checkOnline",
|
|
|
|
node, status.getReason())
|
|
|
|
node.updateStatus(status, failure)
|
2023-02-23 00:11:00 +00:00
|
|
|
if status != RestBeaconNodeStatus.Online:
|
|
|
|
return nstatus != status
|
|
|
|
|
|
|
|
if nstatus in {RestBeaconNodeStatus.Offline,
|
2023-05-30 08:38:34 +00:00
|
|
|
RestBeaconNodeStatus.UnexpectedCode,
|
|
|
|
RestBeaconNodeStatus.UnexpectedResponse,
|
2023-03-03 20:20:01 +00:00
|
|
|
RestBeaconNodeStatus.InternalError,
|
2023-02-23 00:11:00 +00:00
|
|
|
RestBeaconNodeStatus.Online,
|
|
|
|
RestBeaconNodeStatus.Incompatible}:
|
2023-05-30 08:38:34 +00:00
|
|
|
let
|
|
|
|
status = await vc.checkCompatible(node)
|
|
|
|
failure = ApiNodeFailure.init(ApiFailure.NoError, "checkCompatible",
|
|
|
|
node, status.getReason())
|
|
|
|
node.updateStatus(status, failure)
|
2023-02-23 00:11:00 +00:00
|
|
|
if status != RestBeaconNodeStatus.Compatible:
|
|
|
|
return nstatus != status
|
|
|
|
|
|
|
|
if nstatus in {RestBeaconNodeStatus.Offline,
|
2023-05-30 08:38:34 +00:00
|
|
|
RestBeaconNodeStatus.UnexpectedCode,
|
|
|
|
RestBeaconNodeStatus.UnexpectedResponse,
|
2023-03-03 20:20:01 +00:00
|
|
|
RestBeaconNodeStatus.InternalError,
|
2023-02-23 00:11:00 +00:00
|
|
|
RestBeaconNodeStatus.Online,
|
|
|
|
RestBeaconNodeStatus.Incompatible,
|
|
|
|
RestBeaconNodeStatus.Compatible,
|
|
|
|
RestBeaconNodeStatus.OptSynced,
|
|
|
|
RestBeaconNodeStatus.NotSynced}:
|
2023-05-30 08:38:34 +00:00
|
|
|
let
|
|
|
|
status = await vc.checkSync(node)
|
|
|
|
failure = ApiNodeFailure.init(ApiFailure.NoError, "checkSync",
|
|
|
|
node, status.getReason())
|
|
|
|
node.updateStatus(status, failure)
|
2023-02-23 00:11:00 +00:00
|
|
|
return nstatus != status
|
2022-07-13 14:43:57 +00:00
|
|
|
|
2023-02-23 00:11:00 +00:00
|
|
|
proc checkNodes*(service: FallbackServiceRef): Future[bool] {.async.} =
|
2022-07-13 14:43:57 +00:00
|
|
|
let
|
2023-04-17 21:31:54 +00:00
|
|
|
vc = service.client
|
|
|
|
nodesToCheck =
|
|
|
|
if vc.genesisEvent.isSet():
|
|
|
|
service.client.otherNodes()
|
|
|
|
else:
|
|
|
|
service.client.preGenesisNodes()
|
2022-07-13 14:43:57 +00:00
|
|
|
pendingChecks = nodesToCheck.mapIt(service.client.checkNode(it))
|
2023-02-23 00:11:00 +00:00
|
|
|
var res = false
|
2021-07-13 11:15:07 +00:00
|
|
|
try:
|
|
|
|
await allFutures(pendingChecks)
|
2023-02-23 00:11:00 +00:00
|
|
|
for fut in pendingChecks:
|
|
|
|
if fut.completed() and fut.read():
|
|
|
|
res = true
|
2021-07-13 11:15:07 +00:00
|
|
|
except CancelledError as exc:
|
2023-09-24 07:28:09 +00:00
|
|
|
let pending = pendingChecks
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
2021-07-13 11:15:07 +00:00
|
|
|
raise exc
|
2023-02-23 00:11:00 +00:00
|
|
|
return res
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2023-06-28 13:33:07 +00:00
|
|
|
proc checkOffsetStatus(node: BeaconNodeServerRef, offset: TimeOffset) =
|
|
|
|
logScope:
|
|
|
|
node = node
|
|
|
|
|
|
|
|
node.timeOffset = Opt.some(offset)
|
|
|
|
validator_client_time_offset.set(float64(offset.milliseconds()), @[$node])
|
|
|
|
|
|
|
|
debug "Beacon node time offset", time_offset = offset
|
|
|
|
|
|
|
|
let updateStatus =
|
|
|
|
if (offset <= WARN_TIME_OFFSETS[0]) or (offset >= WARN_TIME_OFFSETS[1]):
|
|
|
|
warn "Beacon node has significant time offset",
|
|
|
|
time_offset = offset
|
|
|
|
if (offset <= FAIL_TIME_OFFSETS[0]) or (offset >= FAIL_TIME_OFFSETS[1]):
|
|
|
|
# Beacon node's clock is out of acceptable offsets, we marking this
|
|
|
|
# beacon node and remote it from the list of working nodes.
|
|
|
|
warn "Beacon node has enormous time offset",
|
|
|
|
time_offset = offset
|
|
|
|
let failure = ApiNodeFailure.init(ApiFailure.NoError,
|
|
|
|
"checkTimeOffsetStatus()", node, 200,
|
|
|
|
"Beacon node has enormous time offset")
|
|
|
|
node.updateStatus(RestBeaconNodeStatus.BrokenClock, failure)
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
elif (offset <= NOTE_TIME_OFFSETS[0]) or (offset >= NOTE_TIME_OFFSETS[1]):
|
|
|
|
info "Beacon node has notable time offset",
|
|
|
|
time_offset = offset
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
|
|
|
|
if updateStatus:
|
|
|
|
if node.status == RestBeaconNodeStatus.BrokenClock:
|
|
|
|
# Beacon node's clock has been recovered to some acceptable offset, so we
|
|
|
|
# could restore beacon node.
|
|
|
|
let failure = ApiNodeFailure.init(ApiFailure.NoError,
|
|
|
|
"checkTimeOffsetStatus()", node, 200,
|
|
|
|
"Beacon node has acceptable time offset")
|
|
|
|
node.updateStatus(RestBeaconNodeStatus.Offline, failure)
|
|
|
|
|
2024-01-25 10:52:25 +00:00
|
|
|
proc disableNimbusExtensions(node: BeaconNodeServerRef) =
|
|
|
|
node.features.incl(RestBeaconNodeFeature.NoNimbusExtensions)
|
|
|
|
if node.status == RestBeaconNodeStatus.BrokenClock:
|
|
|
|
let failure = ApiNodeFailure.init(ApiFailure.NoError,
|
|
|
|
"disableNimbusExtensions()", node, 200,
|
|
|
|
"Nimbus extensions no longer available")
|
|
|
|
node.updateStatus(RestBeaconNodeStatus.Offline, failure)
|
|
|
|
|
2023-06-28 13:33:07 +00:00
|
|
|
proc runTimeMonitor(service: FallbackServiceRef,
|
|
|
|
node: BeaconNodeServerRef) {.async.} =
|
2024-01-03 05:58:34 +00:00
|
|
|
const NimbusExtensionsLog = "Beacon node does not support Nimbus extensions"
|
2023-06-28 13:33:07 +00:00
|
|
|
let
|
|
|
|
vc = service.client
|
|
|
|
roles = AllBeaconNodeRoles
|
|
|
|
statuses = AllBeaconNodeStatuses - {RestBeaconNodeStatus.Offline}
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
node = node
|
|
|
|
|
|
|
|
if BeaconNodeRole.NoTimeCheck in node.roles:
|
|
|
|
debug "Beacon node time offset checks disabled"
|
|
|
|
return
|
|
|
|
|
|
|
|
while true:
|
|
|
|
while node.status notin statuses:
|
|
|
|
await vc.waitNodes(nil, statuses, roles, true)
|
|
|
|
|
|
|
|
if RestBeaconNodeFeature.NoNimbusExtensions in node.features:
|
|
|
|
return
|
|
|
|
|
|
|
|
let tres =
|
|
|
|
try:
|
2024-01-25 10:52:25 +00:00
|
|
|
let delay = vc.processingDelay.valueOr: ZeroDuration
|
|
|
|
await node.client.getTimeOffset(delay)
|
2023-06-28 13:33:07 +00:00
|
|
|
except RestResponseError as exc:
|
|
|
|
case exc.status
|
|
|
|
of 400:
|
|
|
|
debug "Beacon node returns invalid response",
|
|
|
|
status = $exc.status, reason = $exc.msg,
|
|
|
|
error_message = $exc.message
|
|
|
|
else:
|
2024-02-07 19:51:36 +00:00
|
|
|
notice NimbusExtensionsLog, status = $exc.status
|
2023-06-28 13:33:07 +00:00
|
|
|
# Exiting loop
|
2024-01-25 10:52:25 +00:00
|
|
|
node.disableNimbusExtensions()
|
2023-06-28 13:33:07 +00:00
|
|
|
return
|
|
|
|
except RestError as exc:
|
|
|
|
debug "Unable to obtain beacon node's time offset", reason = $exc.msg
|
|
|
|
notice NimbusExtensionsLog
|
2024-01-25 10:52:25 +00:00
|
|
|
node.disableNimbusExtensions()
|
2023-06-28 13:33:07 +00:00
|
|
|
return
|
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "An unexpected error occurred while asking for time offset",
|
|
|
|
reason = $exc.msg, error = $exc.name
|
|
|
|
notice NimbusExtensionsLog
|
2024-01-25 10:52:25 +00:00
|
|
|
node.disableNimbusExtensions()
|
2023-06-28 13:33:07 +00:00
|
|
|
return
|
|
|
|
|
2024-01-25 10:52:25 +00:00
|
|
|
checkOffsetStatus(node, TimeOffset.init(tres))
|
2023-06-28 13:33:07 +00:00
|
|
|
|
|
|
|
await service.waitForNextSlot()
|
|
|
|
|
|
|
|
proc processTimeMonitoring(service: FallbackServiceRef) {.async.} =
|
|
|
|
let
|
|
|
|
vc = service.client
|
2023-09-15 05:34:32 +00:00
|
|
|
blockNodes = vc.filterNodes(
|
|
|
|
ResolvedBeaconNodeStatuses, AllBeaconNodeRoles)
|
2023-06-28 13:33:07 +00:00
|
|
|
|
|
|
|
var pendingChecks: seq[Future[void]]
|
|
|
|
|
|
|
|
try:
|
|
|
|
for node in blockNodes:
|
|
|
|
pendingChecks.add(service.runTimeMonitor(node))
|
|
|
|
await allFutures(pendingChecks)
|
|
|
|
except CancelledError as exc:
|
2023-09-24 07:28:09 +00:00
|
|
|
let pending = pendingChecks
|
|
|
|
.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
|
|
|
|
await noCancel allFutures(pending)
|
2023-06-28 13:33:07 +00:00
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "An unexpected error occurred while running time monitoring",
|
|
|
|
reason = $exc.msg, error = $exc.name
|
|
|
|
return
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc mainLoop(service: FallbackServiceRef) {.async.} =
|
2022-07-13 14:43:57 +00:00
|
|
|
let vc = service.client
|
2021-07-13 11:15:07 +00:00
|
|
|
service.state = ServiceState.Running
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Service started"
|
|
|
|
|
2023-06-28 13:33:07 +00:00
|
|
|
let timeMonitorFut = processTimeMonitoring(service)
|
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
try:
|
|
|
|
await vc.preGenesisEvent.wait()
|
|
|
|
except CancelledError:
|
|
|
|
debug "Service interrupted"
|
2023-06-28 13:33:07 +00:00
|
|
|
if not(timeMonitorFut.finished()): await timeMonitorFut.cancelAndWait()
|
2023-04-17 21:31:54 +00:00
|
|
|
return
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "Service crashed with unexpected error", err_name = exc.name,
|
|
|
|
err_msg = exc.msg
|
|
|
|
return
|
|
|
|
|
2022-07-13 14:43:57 +00:00
|
|
|
while true:
|
|
|
|
# This loop could look much more nicer/better, when
|
|
|
|
# https://github.com/nim-lang/Nim/issues/19911 will be fixed, so it could
|
|
|
|
# become safe to combine loops, breaks and exception handlers.
|
|
|
|
let breakLoop =
|
|
|
|
try:
|
2023-02-23 00:11:00 +00:00
|
|
|
if await service.checkNodes(): service.changesEvent.fire()
|
2022-07-13 14:43:57 +00:00
|
|
|
await sleepAsync(2.seconds)
|
|
|
|
false
|
2023-12-05 11:45:47 +00:00
|
|
|
except CancelledError:
|
2022-07-13 14:43:57 +00:00
|
|
|
debug "Service interrupted"
|
2023-06-28 13:33:07 +00:00
|
|
|
if not(timeMonitorFut.finished()): await timeMonitorFut.cancelAndWait()
|
2022-07-13 14:43:57 +00:00
|
|
|
true
|
|
|
|
except CatchableError as exc:
|
2023-03-03 20:20:01 +00:00
|
|
|
error "Service crashed with unexpected error", err_name = exc.name,
|
|
|
|
err_msg = exc.msg
|
2022-07-13 14:43:57 +00:00
|
|
|
true
|
|
|
|
|
|
|
|
if breakLoop:
|
|
|
|
break
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc init*(t: typedesc[FallbackServiceRef],
|
2022-07-13 14:43:57 +00:00
|
|
|
vc: ValidatorClientRef): Future[FallbackServiceRef] {.async.} =
|
2022-07-14 21:11:25 +00:00
|
|
|
logScope: service = ServiceName
|
|
|
|
var res = FallbackServiceRef(name: ServiceName, client: vc,
|
2022-07-13 14:43:57 +00:00
|
|
|
state: ServiceState.Initialized,
|
2023-02-23 00:11:00 +00:00
|
|
|
changesEvent: newAsyncEvent())
|
2022-07-14 21:11:25 +00:00
|
|
|
debug "Initializing service"
|
2021-07-13 11:15:07 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
proc start*(service: FallbackServiceRef) =
|
|
|
|
service.lifeFut = mainLoop(service)
|