Skip to content

Commit

Permalink
- swap sequence with table data type in order to
Browse files Browse the repository at this point in the history
show the idea of service layers creating and managing
client configurations life time.
- Improved unit tests and some unit tests skeleton for upcoming
changes.
- minor corrections in project
  • Loading branch information
pmmiranda committed Feb 28, 2025
1 parent fec20cf commit e3eaaef
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 77 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ nimbus: | build deps

all_tests_nimbus: | build deps
echo -e $(BUILD_MSG) "build/$@" && \
$(ENV_SCRIPT) nim c -r $(NIM_PARAMS) --threads:on -d:chronicles_log_level=ERROR -o:build/$@ "nimbus/tests/$@.nim"
$(ENV_SCRIPT) nim c -r $(NIM_PARAMS) -d:testing --threads:on -d:chronicles_log_level=ERROR -o:build/$@ "nimbus/tests/$@.nim"

# Note about building Nimbus as a library:
#
Expand Down
8 changes: 3 additions & 5 deletions nimbus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ tbd
- mac os, windows, and linux

]$ make nimbus
## colaborate
We welcome contributions to Nimbus! Please adhere to the following guidelines:

- Use the [Status Nim style guide](https://status-im.github.io/nim-style-guide/) to maintain code consistency.
- Format your code using the [Nim Pretty Printer (nph)](https://github.com/nim-lang/nimpretty) to ensure consistency across the codebase. Run it as part of your pull request process.
## collaborate
- Use [Status Nim style guide](https://status-im.github.io/nim-style-guide/) to maintain code consistency.
- Format your code using the [Nim Pretty Printer (nph)](https://github.com/arnetheduck/nph) to ensure consistency across the codebase. Run it as part of your pull request process.
## License

Licensed and distributed under either of
Expand Down
63 changes: 42 additions & 21 deletions nimbus/common/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,53 @@

{.push raises: [].}

import results
export results

#Parses specific data from a given channel if given in following binary format:
# (array size Uint) | [ (element size Uint) (element data)]
proc parseChannelData*(p: pointer): Result[seq[string], string] =
import results, ../conf, chronicles

## Serialize table string elements
proc serializeTableElem*(offset: var uint, elem: string) =
if offset <= 0:
fatal "memory offset can't be zero"
quit(QuitFailure)

#element size
let optLen = uint(elem.len)
copyMem(cast[pointer](offset), addr optLen, sizeof(uint))
offset += uint(sizeof(uint))

#element data
copyMem(cast[pointer](offset), unsafeAddr elem[0], elem.len)
offset += uint(elem.len)

## Deserialize table string elements
proc deserializeTableElem*(offset: var uint): string =
#element size
var strLen: uint
copyMem(addr strLen, cast[pointer](offset), sizeof(uint))
offset += uint(sizeof(uint))

#element
var strData = newString(strLen)
copyMem(addr strData[0], cast[pointer](offset), uint(strLen))
offset += uint(strLen)

strData

## Parse data from a given channel.
## schema: (table size:Uint) | [ (option size:Uint) (option data:byte) (arg size: Uint) (arg data:byte)]
proc parseChannelData*(p: pointer): Result[NimbusConfigTable, string] =
# Start reading from base pointer
var readOffset = cast[uint](p)
var recoveredStrings: seq[string]
var totalSize: uint = 0
var
readOffset = cast[uint](p)
confTable = NimbusConfigTable()
totalSize: uint = 0

# length
copyMem(addr totalSize, cast[pointer](readOffset), sizeof(uint))
readOffset += uint(sizeof(uint))

while readOffset < cast[uint](p) + totalSize:
#seq element size
var strLen: uint
copyMem(addr strLen, cast[pointer](readOffset), sizeof(uint))
readOffset += uint(sizeof(uint))

#element
var strData = newString(strLen)
copyMem(addr strData[0], cast[pointer](readOffset), uint(strLen))
readOffset += uint(strLen)

recoveredStrings.add(strData)
let opt = deserializeTableElem(readOffset)
let arg = deserializeTableElem(readOffset)
confTable[opt] = arg

ok recoveredStrings
ok confTable
9 changes: 5 additions & 4 deletions nimbus/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{.push raises: [].}

import
std/[os, atomics],
std/[atomics, tables],
chronicles,
#eth2
beacon_chain/nimbus_binary_common
Expand All @@ -33,22 +33,23 @@ isConfigRead.store(false)

## Nimbus service arguments
type
NimbusConfigTable* = Table[string, string]

ConfigKind* = enum
Execution
Consensus

LayerConfig* = object
case kind*: ConfigKind
of Consensus:
consensusOptions*: seq[string]
consensusOptions*: NimbusConfigTable
of Execution:
executionOptions*: seq[string]
executionOptions*: NimbusConfigTable

NimbusService* = ref object
name*: string
layerConfig*: LayerConfig
serviceHandler*: Thread[ptr Channel[pointer]]
serviceChannel*: ptr Channel[pointer] = nil
serviceFunc*: proc(ch: ptr Channel[pointer]) {.thread.}

Nimbus* = ref object
Expand Down
4 changes: 2 additions & 2 deletions nimbus/consensus/consensus_layer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{.push raises: [].}

import std/[atomics, os], chronicles, ../conf, ../common/utils
import std/[atomics, os], chronos, chronicles, ../conf, ../common/utils, results

logScope:
topics = "Consensus layer"
Expand All @@ -31,7 +31,7 @@ proc consensusLayerHandler*(channel: ptr Channel[pointer]) =
try:
while true:
info "consensus ..."
sleep(cNimbusServiceTimeoutMs + 1000)
sleep(cNimbusServiceTimeoutMs)
except CatchableError as e:
fatal "error", message = e.msg

Expand Down
2 changes: 1 addition & 1 deletion nimbus/execution/execution_layer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{.push raises: [].}

import std/[atomics, os], chronicles, ../conf, ../common/utils
import std/[atomics, os], chronicles, ../conf, ../common/utils, results

logScope:
topics = "Execution layer"
Expand Down
44 changes: 19 additions & 25 deletions nimbus/nimbus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import
chronicles,
consensus/consensus_layer,
execution/execution_layer,
common/utils,
conf

# ------------------------------------------------------------------------------
Expand All @@ -19,31 +20,31 @@ import
## create and configure service
proc startService(nimbus: var Nimbus, service: var NimbusService) =
#channel creation (shared memory)
service.serviceChannel =
var serviceChannel =
cast[ptr Channel[pointer]](allocShared0(sizeof(Channel[pointer])))

service.serviceChannel[].open()
serviceChannel[].open()

#thread read ack
isConfigRead.store(false)

#start thread
createThread(service.serviceHandler, service.serviceFunc, service.serviceChannel)
createThread(service.serviceHandler, service.serviceFunc, serviceChannel)

let optionsList = block:
let optionsTable = block:
case service.layerConfig.kind
of Consensus: service.layerConfig.consensusOptions
of Execution: service.layerConfig.executionOptions

#configs list total size
#configs table total size
var totalSize: uint = 0
totalSize += uint(sizeof(uint))
for word in optionsList:
totalSize += uint(sizeof(uint)) # element type size
totalSize += uint(word.len) # element length
for opt, arg in optionsTable:
totalSize += uint(sizeof(uint)) + uint(opt.len) # option
totalSize += uint(sizeof(uint)) + uint(arg.len) # arg

# Allocate shared memory
# schema: (array size Uint) | [ (element size Uint) (element data)]
# schema: (table size:Uint) | [ (option size:Uint) (option data:byte) (arg size: Uint) (arg data:byte)]
var byteArray = cast[ptr byte](allocShared(totalSize))
if byteArray.isNil:
fatal "Memory allocation failed"
Expand All @@ -56,29 +57,23 @@ proc startService(nimbus: var Nimbus, service: var NimbusService) =
copyMem(cast[pointer](writeOffset), addr totalSize, sizeof(uint))
writeOffset += uint(sizeof(uint))

for word in optionsList:
#elem size
let strLen = uint(word.len)
copyMem(cast[pointer](writeOffset), addr strLen, sizeof(uint))
writeOffset += uint(sizeof(uint))
for opt, arg in optionsTable:
serializeTableElem(writeOffset, opt)
serializeTableElem(writeOffset, arg)

#element data
copyMem(cast[pointer](writeOffset), unsafeAddr word[0], word.len)
writeOffset += uint(word.len)

service.serviceChannel[].send(byteArray)
serviceChannel[].send(byteArray)

#wait for service read ack
while not isConfigRead.load():
sleep(cThreadTimeAck)
isConfigRead.store(true)

#close channel
service.serviceChannel[].close()
serviceChannel[].close()

#dealloc shared data
deallocShared(byteArray)
deallocShared(service.serviceChannel)
deallocShared(serviceChannel)

## Gracefully exits all services
proc monitorServices(nimbus: Nimbus) =
Expand All @@ -95,11 +90,10 @@ proc monitorServices(nimbus: Nimbus) =

## start nimbus client
proc run*(nimbus: var Nimbus) =
# todo
# parse cmd, read options and create configs
# to be filled with command line options after parsed according to service
var
execOpt = newSeq[string]()
consOpt = newSeq[string]()
consOpt, execOpt = NimbusConfigTable()

executionService: NimbusService = NimbusService(
name: "Execution Layer",
serviceFunc: executionLayerHandler,
Expand Down
12 changes: 5 additions & 7 deletions nimbus/tests/consensus/test_consensus_layer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

{.push raises: [].}

import
unittest2,
std/[concurrency/atomics, os],
../../../nimbus/common/utils,
../../../nimbus/conf,
../../../nimbus/consensus/consensus_layer
import unittest2

#tbd
suite "Nimbus consensus layer":
#tbd, given that layer is in development
test "tbd":
check true
11 changes: 5 additions & 6 deletions nimbus/tests/execution/test_execution_layer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
{.push raises: [].}

import
unittest2,
std/[concurrency/atomics, os],
../../../nimbus/common/utils,
../../../nimbus/conf,
../../../nimbus/execution/execution_layer
unittest2

#tbd
suite "Nimbus execution layer":
#tbd, given that layer is in development
test "tbd":
check true
Loading

0 comments on commit e3eaaef

Please sign in to comment.