nimbus-eth2/beacon_chain/validator_client/fallback_service.nim

58 lines
1.8 KiB
Nim
Raw Permalink Normal View History

New validator client using REST API. (#2651) * Initial commit. * Exporting getConfig(). * Add beacon node checking procedures. * Post rebase fixes. * Use runSlotLoop() from nimbus_beacon_node. Fallback implementation. Fixes for ETH2 REST serialization. * Add beacon_clock.durationToNextSlot(). Move type declarations from beacon_rest_api to json_rest_serialization. Fix seq[ValidatorIndex] serialization. Refactor ValidatorPool and add some utility procedures. Create separate version of validator_client. * Post-rebase fixes. Remove CookedPubKey from validator_pool.nim. * Now we should be able to produce attestations and aggregate and proofs. But its not working yet. * Debugging attestation sending. * Add durationToNextAttestation. Optimize some debug logs. Fix aggregation_bits encoding. Bump chronos/presto. * Its alive. * Fixes for launch_local_testnet script. Bump chronos. * Switch client API to not use `/api` prefix. * Post-rebase adjustments. * Fix endpoint for publishBlock(). * Add CONFIG_NAME. Add more checks to ensure that beacon_node is compatible. * Add beacon committee subscription support to validator_client. * Fix stacktrace should be an array of strings. Fix committee subscriptions should not be `data` keyed. * Log duration to next block proposal. * Fix beacon_node_status import. * Use jsonMsgResponse() instead of jsonError(). * Fix graffityBytes usage. Remove unnecessary `await`. Adjust creation of SignedBlock instance. Remove legacy files. * Rework durationToNextSlot() and durationToNextEpoch() to use `fromNow`. * Fix race condition for block proposal and attestations for same slot. Fix local_testnet script to properly kill tasks on Windows. Bump chronos and nim-http-tools, to allow connections to infura.io (basic auth). * Catch services errors. Improve performance of local_testnet.sh script on Windows. Fix race condition when attestation producing. * Post-rebase fixes. * Bump chronos and presto. * Calculate block publishing delay. Fix pkill in one more place. * Add error handling and timeouts to firstSuccess() template. Add onceToAll() template. Add checkNodes() procedure. Refactor firstSuccess() template. Add error checking to api.nim calls. * Deprecated usage onceToAll() for better stability. Address comment and send attestations asap. * Avoid unnecessary loop when calculating minimal duration.
2021-07-13 11:15:07 +00:00
import common, api
logScope: service = "fallback_service"
proc checkNodes*(service: FallbackServiceRef) {.async.} =
let nodesToCheck =
block:
var res: seq[BeaconNodeServerRef]
for item in service.client.beaconNodes:
if item.status != RestBeaconNodeStatus.Online:
res.add(item)
res
let pendingChecks =
block:
var res: seq[Future[void]]
for item in nodesToCheck:
res.add(service.client.checkNode(item))
res
try:
await allFutures(pendingChecks)
except CancelledError as exc:
var pendingCancel: seq[Future[void]]
for fut in pendingChecks:
if not(fut.finished()):
pendingCancel.add(fut.cancelAndWait())
await allFutures(pendingCancel)
raise exc
proc mainLoop(service: FallbackServiceRef) {.async.} =
service.state = ServiceState.Running
try:
while true:
await service.checkNodes()
# Calculating time we need to sleep until
# `time(next_slot) - SLOT_LOOKAHEAD`
let waitTime =
block:
let nextTime = service.client.beaconClock.durationToNextSlot()
if nextTime < SLOT_LOOKAHEAD:
nextTime + seconds(int64(SECONDS_PER_SLOT))
else:
nextTime - SLOT_LOOKAHEAD
await sleepAsync(waitTime)
except CatchableError as exc:
warn "Service crashed with unexpected error", err_name = exc.name,
err_msg = exc.msg
proc init*(t: typedesc[FallbackServiceRef],
vc: ValidatorClientRef): Future[FallbackServiceRef] {.async.} =
debug "Initializing service"
var res = FallbackServiceRef(client: vc, state: ServiceState.Initialized)
# Perform initial nodes check.
await res.checkNodes()
return res
proc start*(service: FallbackServiceRef) =
service.lifeFut = mainLoop(service)