nimbus-eth1/nimbus_unified/nimbus_unified.nim

165 lines
5.1 KiB
Nim
Raw Normal View History

# nimbus_unified
# Copyright (c) 2024 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.
import
std/[atomics, os],
chronicles,
consensus/consensus_wrapper,
execution/execution_wrapper
## Constants
const cNimbusMaxTasks* = 5
const cNimbusTaskTimeoutMs* = 5000
## Exceptions
type NimbusTasksError* = object of CatchableError
## Task and associated task information
type NimbusTask* = ref object
name*: string
timeoutMs*: uint32
threadHandler*: Thread[TaskParameters]
## Task manager
type NimbusTasks* = ref object
taskList*: array[cNimbusMaxTasks, NimbusTask]
2024-10-19 02:01:07 +01:00
## log
logScope:
topics = "Task manager"
# ------------------------------------------------------------------------------
# Private and helper functions
# ------------------------------------------------------------------------------
## Execution Layer handler
proc executionLayerHandler(parameters: TaskParameters) {.thread.} =
2024-10-19 02:01:07 +01:00
info "Started task:", task = parameters.name
while true:
executionWrapper(parameters)
if isShutDownRequired.load() == true:
break
2024-10-19 02:01:07 +01:00
info "\tExiting task;", task = parameters.name
## Consensus Layer handler
proc consensusLayerHandler(parameters: TaskParameters) {.thread.} =
2024-10-19 02:01:07 +01:00
info "Started task:", task = parameters.name
consensusWrapper(parameters)
2024-10-19 02:01:07 +01:00
info "\tExiting task:", task = parameters.name
## Waits for tasks to finish (joinThreads)
proc joinTasks(tasks: var NimbusTasks) =
for i in 0 .. cNimbusMaxTasks - 1:
if not tasks.taskList[i].isNil:
joinThread(tasks.taskList[i].threadHandler)
2024-10-19 02:01:07 +01:00
info "\tAll tasks finished"
# ----
# ------------------------------------------------------------------------------
# Public functions
# ------------------------------------------------------------------------------
## adds a new task to nimbus Tasks.
## Note that thread handler passed by argument needs to have the signature: proc foobar(NimbusParameters)
proc addNewTask*(
tasks: var NimbusTasks,
name: string,
timeout: uint32,
taskHandler: proc(config: TaskParameters) {.thread.},
parameters: var TaskParameters,
) =
#search next available worker
var currentIndex = -1
for i in 0 .. cNimbusMaxTasks - 1:
if tasks.taskList[i].isNil:
tasks.taskList[i] = NimbusTask.new
tasks.taskList[i].name = name
tasks.taskList[i].timeoutMs = timeout
currentIndex = i
parameters.name = name
break
if currentIndex < 0:
raise newException(NimbusTasksError, "No free slots on Nimbus Tasks")
createThread(tasks.taskList[currentIndex].threadHandler, taskHandler, parameters)
2024-10-19 02:01:07 +01:00
info "Created task:", task = tasks.taskList[currentIndex].name
## Task monitoring
proc monitor*(tasksList: var NimbusTasks, config: NimbusConfig) =
2024-10-19 02:01:07 +01:00
info "monitoring tasks"
while true:
2024-10-19 02:44:53 +01:00
info "checking tasks ... "
# -check an atomic (to be created when needed) if it s required to shutdown
# this will atomic flag solves:
# - non responding thread
# - thread that required shutdown
sleep(cNimbusTaskTimeoutMs)
## create running workers
proc startTasks*(tasksList: var NimbusTasks, configs: NimbusConfig) =
# TODO: extract configs for each task from NimbusConfig
# or extract them somewhere else and passs them here
var
paramsExecution: TaskParameters =
TaskParameters(configs: "task configs extracted from NimbusConfig go here")
paramsConsensus: TaskParameters =
TaskParameters(configs: "task configs extracted from NimbusConfig go here")
tasksList.addNewTask(
"Execution Layer", cNimbusTaskTimeoutMs, executionLayerHandler, paramsExecution
)
tasksList.addNewTask(
"Consensus Layer", cNimbusTaskTimeoutMs, consensusLayerHandler, paramsConsensus
)
2024-10-18 19:14:35 +01:00
2024-10-19 02:44:53 +01:00
# ------
when isMainModule:
2024-10-19 02:01:07 +01:00
info "Starting Nimbus"
2024-10-18 19:14:35 +01:00
## TODO
## - make banner and config
## - file limits
## - check if we have permissions to create data folder if needed
## - setup logging
# TODO - read configuration
# TODO - implement config reader for all components
let nimbusConfigs = NimbusConfig()
var tasksList: NimbusTasks = NimbusTasks.new
2024-10-19 02:44:53 +01:00
## this code snippet requires a conf.nim file (eg: beacon_lc_bridge_conf.nim)
2024-10-18 19:14:35 +01:00
# var config = makeBannerAndConfig("Nimbus client ", NimbusConfig)
# setupLogging(config.logLevel, config.logStdout, config.logFile)
tasksList.startTasks(nimbusConfigs)
2024-10-18 19:14:35 +01:00
## Graceful shutdown by handling of Ctrl+C signal
proc controlCHandler() {.noconv.} =
when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057
try:
setupForeignThreadGc()
except NimbusTasksError as exc:
raiseAssert exc.msg # shouldn't happen
2024-10-19 02:01:07 +01:00
notice "\nCtrl+C pressed. Shutting down working tasks"
2024-10-18 19:14:35 +01:00
isShutDownRequired.store(true)
tasksList.joinTasks()
2024-10-19 02:01:07 +01:00
notice "Shutting down now"
2024-10-18 19:14:35 +01:00
quit(0)
setControlCHook(controlCHandler)
2024-10-19 02:44:53 +01:00
#start monitoring
tasksList.monitor(nimbusConfigs)