2022-07-29 08:36:20 +00:00
|
|
|
# beacon_chain
|
2024-01-06 06:18:28 +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.
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
import std/algorithm
|
2021-07-13 11:15:07 +00:00
|
|
|
import chronicles
|
2024-01-06 06:18:28 +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
|
|
|
|
2023-08-25 09:29:07 +00:00
|
|
|
proc validateForkSchedule(forks: openArray[Fork]): bool {.raises: [].} =
|
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] {.
|
2023-08-25 09:29:07 +00:00
|
|
|
raises: [].} =
|
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.} =
|
2023-04-17 21:31:54 +00:00
|
|
|
let forks =
|
|
|
|
try:
|
|
|
|
await vc.getForkSchedule(ApiStrategyKind.Best)
|
|
|
|
except ValidatorApiError as exc:
|
|
|
|
warn "Unable to retrieve fork schedule",
|
|
|
|
reason = exc.getFailureReason(), err_msg = exc.msg
|
|
|
|
return
|
|
|
|
except CancelledError as exc:
|
|
|
|
debug "Fork retrieval process was interrupted"
|
|
|
|
raise exc
|
|
|
|
except CatchableError as exc:
|
|
|
|
error "Unexpected error occured while getting fork information",
|
|
|
|
err_name = exc.name, err_msg = exc.msg
|
|
|
|
return
|
|
|
|
|
|
|
|
let sortedForks =
|
|
|
|
block:
|
|
|
|
let res = sortForks(forks)
|
|
|
|
if res.isErr():
|
|
|
|
warn "Invalid fork schedule received", reason = res.error()
|
2021-08-23 10:41:48 +00:00
|
|
|
return
|
2023-04-17 21:31:54 +00:00
|
|
|
res.get()
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2023-04-17 21:31:54 +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 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
|
|
|
|
2023-04-17 21:31:54 +00:00
|
|
|
try:
|
|
|
|
await vc.preGenesisEvent.wait()
|
|
|
|
except CancelledError:
|
|
|
|
debug "Service interrupted"
|
|
|
|
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:
|
|
|
|
await vc.pollForFork()
|
2023-06-28 13:33:07 +00:00
|
|
|
await service.waitForNextEpoch(TIME_DELAY_FROM_SLOT)
|
2022-07-13 14:43:57 +00:00
|
|
|
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
|
|
|
return res
|
|
|
|
|
|
|
|
proc start*(service: ForkServiceRef) =
|
|
|
|
service.lifeFut = mainLoop(service)
|