2022-07-29 08:36:20 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2021-2022 Status Research & Development GmbH
|
|
|
|
# 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-08-23 10:41:48 +00:00
|
|
|
import std/algorithm
|
2021-07-13 11:15:07 +00:00
|
|
|
import chronicles
|
2021-08-23 10:41:48 +00:00
|
|
|
import common, api
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-07-14 21:11:25 +00:00
|
|
|
const
|
|
|
|
ServiceName = "fork_service"
|
|
|
|
|
|
|
|
logScope: service = ServiceName
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
proc validateForkSchedule(forks: openArray[Fork]): bool {.raises: [Defect].} =
|
2021-08-23 10:41:48 +00:00
|
|
|
# Check if `forks` list is linked list.
|
|
|
|
var current_version = forks[0].current_version
|
2022-05-10 10:03:40 +00:00
|
|
|
for index, item in forks:
|
2021-08-23 10:41:48 +00:00
|
|
|
if index > 0:
|
|
|
|
if item.previous_version != current_version:
|
|
|
|
return false
|
|
|
|
else:
|
|
|
|
if item.previous_version != item.current_version:
|
|
|
|
return false
|
|
|
|
current_version = item.current_version
|
|
|
|
true
|
|
|
|
|
2022-04-08 16:22:49 +00:00
|
|
|
proc sortForks(forks: openArray[Fork]): Result[seq[Fork], cstring] {.
|
2022-02-16 11:31:23 +00:00
|
|
|
raises: [Defect].} =
|
2021-08-23 10:41:48 +00:00
|
|
|
proc cmp(x, y: Fork): int {.closure.} =
|
|
|
|
if uint64(x.epoch) == uint64(y.epoch): return 0
|
|
|
|
if uint64(x.epoch) < uint64(y.epoch): return -1
|
|
|
|
return 1
|
|
|
|
|
2022-02-16 11:31:23 +00:00
|
|
|
if len(forks) == 0:
|
2021-08-23 10:41:48 +00:00
|
|
|
return err("Empty fork schedule")
|
2022-02-16 11:31:23 +00:00
|
|
|
|
|
|
|
let sortedForks = sorted(forks, cmp)
|
2021-08-23 10:41:48 +00:00
|
|
|
if not(validateForkSchedule(sortedForks)):
|
|
|
|
return err("Invalid fork schedule")
|
2022-02-16 11:31:23 +00:00
|
|
|
ok(sortedForks)
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc pollForFork(vc: ValidatorClientRef) {.async.} =
|
2021-08-23 10:41:48 +00:00
|
|
|
let sres = vc.getCurrentSlot()
|
|
|
|
if sres.isSome():
|
|
|
|
let
|
|
|
|
currentSlot = sres.get()
|
|
|
|
currentEpoch = currentSlot.epoch()
|
|
|
|
|
|
|
|
let forks =
|
|
|
|
try:
|
2022-09-29 07:57:14 +00:00
|
|
|
await vc.getForkSchedule(ApiStrategyKind.Best)
|
2021-08-23 10:41:48 +00:00
|
|
|
except ValidatorApiError as exc:
|
2023-02-23 00:11:00 +00:00
|
|
|
error "Unable to retrieve fork schedule",
|
|
|
|
reason = exc.getFailureReason(), err_msg = exc.msg
|
2021-08-23 10:41:48 +00:00
|
|
|
return
|
2022-07-14 21:11:25 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Fork retrieval process was interrupted"
|
|
|
|
raise exc
|
2021-08-23 10:41:48 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
error "Unexpected error occured while getting fork information",
|
|
|
|
err_name = exc.name, err_msg = exc.msg
|
|
|
|
return
|
|
|
|
|
2022-02-16 11:31:23 +00:00
|
|
|
let sortedForks =
|
2021-08-23 10:41:48 +00:00
|
|
|
block:
|
2022-02-16 11:31:23 +00:00
|
|
|
let res = sortForks(forks)
|
2021-08-23 10:41:48 +00:00
|
|
|
if res.isErr():
|
|
|
|
error "Invalid fork schedule received", reason = res.error()
|
|
|
|
return
|
|
|
|
res.get()
|
|
|
|
|
2022-02-16 11:31:23 +00:00
|
|
|
if (len(vc.forks) == 0) or (vc.forks != sortedForks):
|
|
|
|
vc.forks = sortedForks
|
|
|
|
notice "Fork schedule updated", fork_schedule = sortedForks
|
|
|
|
vc.forksAvailable.fire()
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc waitForNextEpoch(service: ForkServiceRef) {.async.} =
|
|
|
|
let vc = service.client
|
|
|
|
let sleepTime = vc.beaconClock.durationToNextEpoch() + TIME_DELAY_FROM_SLOT
|
|
|
|
debug "Sleeping until next epoch", sleep_time = sleepTime
|
|
|
|
await sleepAsync(sleepTime)
|
|
|
|
|
|
|
|
proc mainLoop(service: ForkServiceRef) {.async.} =
|
|
|
|
let vc = service.client
|
2022-07-13 14:43:57 +00:00
|
|
|
service.state = ServiceState.Running
|
2021-07-13 11:15:07 +00:00
|
|
|
debug "Service started"
|
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:
|
|
|
|
await vc.pollForFork()
|
|
|
|
await service.waitForNextEpoch()
|
|
|
|
false
|
|
|
|
except CancelledError:
|
|
|
|
debug "Service interrupted"
|
|
|
|
true
|
|
|
|
except CatchableError as exc:
|
|
|
|
warn "Service crashed with unexpected error", err_name = exc.name,
|
|
|
|
err_msg = exc.msg
|
|
|
|
true
|
|
|
|
|
|
|
|
if breakLoop:
|
|
|
|
break
|
2021-07-13 11:15:07 +00:00
|
|
|
|
|
|
|
proc init*(t: typedesc[ForkServiceRef],
|
|
|
|
vc: ValidatorClientRef): Future[ForkServiceRef] {.async.} =
|
2022-07-14 21:11:25 +00:00
|
|
|
logScope: service = ServiceName
|
|
|
|
let res = ForkServiceRef(name: ServiceName,
|
2022-07-13 14:43:57 +00:00
|
|
|
client: vc, state: ServiceState.Initialized)
|
2022-07-14 21:11:25 +00:00
|
|
|
debug "Initializing service"
|
2021-07-13 11:15:07 +00:00
|
|
|
await vc.pollForFork()
|
|
|
|
return res
|
|
|
|
|
|
|
|
proc start*(service: ForkServiceRef) =
|
|
|
|
service.lifeFut = mainLoop(service)
|