Skip to content

Commit f98618e

Browse files
author
pmmiranda
committed
- swap sequence with table data type in order to
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
1 parent 7f4d78a commit f98618e

File tree

11 files changed

+159
-78
lines changed

11 files changed

+159
-78
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ nimbus: | build deps
379379

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

384384
# Note about building Nimbus as a library:
385385
#

nimbus.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,4 @@ task nimbus, "Build Nimbus":
145145
buildBinary "nimbus", "nimbus/", "-d:chronicles_log_level=TRACE"
146146

147147
task nimbus_test, "Run Nimbus tests":
148-
test "nimbus/tests/","all_tests", "-d:chronicles_log_level=ERROR"
148+
test "nimbus/tests/", "all_tests_nimbus", "-d:chronicles_log_level=ERROR -d:testing"

nimbus/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ tbd
2727
- mac os, windows, and linux
2828

2929
]$ make nimbus
30-
## colaborate
31-
We welcome contributions to Nimbus! Please adhere to the following guidelines:
32-
33-
- Use the [Status Nim style guide](https://status-im.github.io/nim-style-guide/) to maintain code consistency.
34-
- 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.
30+
## collaborate
31+
- Use [Status Nim style guide](https://status-im.github.io/nim-style-guide/) to maintain code consistency.
32+
- 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.
3533
## License
3634

3735
Licensed and distributed under either of

nimbus/common/utils.nim

+42-21
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,53 @@
77

88
{.push raises: [].}
99

10-
import results
11-
export results
12-
13-
#Parses specific data from a given channel if given in following binary format:
14-
# (array size Uint) | [ (element size Uint) (element data)]
15-
proc parseChannelData*(p: pointer): Result[seq[string], string] =
10+
import results, ../conf, chronicles
11+
12+
## Serialize table string elements
13+
proc serializeTableElem*(offset: var uint, elem: string) =
14+
if offset <= 0:
15+
fatal "memory offset can't be zero"
16+
quit(QuitFailure)
17+
18+
#element size
19+
let optLen = uint(elem.len)
20+
copyMem(cast[pointer](offset), addr optLen, sizeof(uint))
21+
offset += uint(sizeof(uint))
22+
23+
#element data
24+
copyMem(cast[pointer](offset), unsafeAddr elem[0], elem.len)
25+
offset += uint(elem.len)
26+
27+
## Deserialize table string elements
28+
proc deserializeTableElem*(offset: var uint): string =
29+
#element size
30+
var strLen: uint
31+
copyMem(addr strLen, cast[pointer](offset), sizeof(uint))
32+
offset += uint(sizeof(uint))
33+
34+
#element
35+
var strData = newString(strLen)
36+
copyMem(addr strData[0], cast[pointer](offset), uint(strLen))
37+
offset += uint(strLen)
38+
39+
strData
40+
41+
## Parse data from a given channel.
42+
## schema: (table size:Uint) | [ (option size:Uint) (option data:byte) (arg size: Uint) (arg data:byte)]
43+
proc parseChannelData*(p: pointer): Result[NimbusConfigTable, string] =
1644
# Start reading from base pointer
17-
var readOffset = cast[uint](p)
18-
var recoveredStrings: seq[string]
19-
var totalSize: uint = 0
45+
var
46+
readOffset = cast[uint](p)
47+
confTable = NimbusConfigTable()
48+
totalSize: uint = 0
2049

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

2554
while readOffset < cast[uint](p) + totalSize:
26-
#seq element size
27-
var strLen: uint
28-
copyMem(addr strLen, cast[pointer](readOffset), sizeof(uint))
29-
readOffset += uint(sizeof(uint))
30-
31-
#element
32-
var strData = newString(strLen)
33-
copyMem(addr strData[0], cast[pointer](readOffset), uint(strLen))
34-
readOffset += uint(strLen)
35-
36-
recoveredStrings.add(strData)
55+
let opt = deserializeTableElem(readOffset)
56+
let arg = deserializeTableElem(readOffset)
57+
confTable[opt] = arg
3758

38-
ok recoveredStrings
59+
ok confTable

nimbus/conf.nim

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{.push raises: [].}
99

1010
import
11-
std/[os, atomics],
11+
std/[atomics, tables],
1212
chronicles,
1313
#eth2
1414
beacon_chain/nimbus_binary_common
@@ -33,22 +33,23 @@ isConfigRead.store(false)
3333

3434
## Nimbus service arguments
3535
type
36+
NimbusConfigTable* = Table[string, string]
37+
3638
ConfigKind* = enum
3739
Execution
3840
Consensus
3941

4042
LayerConfig* = object
4143
case kind*: ConfigKind
4244
of Consensus:
43-
consensusOptions*: seq[string]
45+
consensusOptions*: NimbusConfigTable
4446
of Execution:
45-
executionOptions*: seq[string]
47+
executionOptions*: NimbusConfigTable
4648

4749
NimbusService* = ref object
4850
name*: string
4951
layerConfig*: LayerConfig
5052
serviceHandler*: Thread[ptr Channel[pointer]]
51-
serviceChannel*: ptr Channel[pointer] = nil
5253
serviceFunc*: proc(ch: ptr Channel[pointer]) {.thread.}
5354

5455
Nimbus* = ref object

nimbus/consensus/consensus_layer.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
{.push raises: [].}
99

10-
import std/[atomics, os], chronicles, ../conf, ../common/utils
10+
import std/[atomics, os], chronos, chronicles, ../conf, ../common/utils, results
1111

1212
logScope:
1313
topics = "Consensus layer"
@@ -31,7 +31,7 @@ proc consensusLayerHandler*(channel: ptr Channel[pointer]) =
3131
try:
3232
while true:
3333
info "consensus ..."
34-
sleep(cNimbusServiceTimeoutMs + 1000)
34+
sleep(cNimbusServiceTimeoutMs)
3535
except CatchableError as e:
3636
fatal "error", message = e.msg
3737

nimbus/execution/execution_layer.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
{.push raises: [].}
99

10-
import std/[atomics, os], chronicles, ../conf, ../common/utils
10+
import std/[atomics, os], chronicles, ../conf, ../common/utils, results
1111

1212
logScope:
1313
topics = "Execution layer"

nimbus/nimbus.nim

+19-25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import
1010
chronicles,
1111
consensus/consensus_layer,
1212
execution/execution_layer,
13+
common/utils,
1314
conf
1415

1516
# ------------------------------------------------------------------------------
@@ -19,31 +20,31 @@ import
1920
## create and configure service
2021
proc startService(nimbus: var Nimbus, service: var NimbusService) =
2122
#channel creation (shared memory)
22-
service.serviceChannel =
23+
var serviceChannel =
2324
cast[ptr Channel[pointer]](allocShared0(sizeof(Channel[pointer])))
2425

25-
service.serviceChannel[].open()
26+
serviceChannel[].open()
2627

2728
#thread read ack
2829
isConfigRead.store(false)
2930

3031
#start thread
31-
createThread(service.serviceHandler, service.serviceFunc, service.serviceChannel)
32+
createThread(service.serviceHandler, service.serviceFunc, serviceChannel)
3233

33-
let optionsList = block:
34+
let optionsTable = block:
3435
case service.layerConfig.kind
3536
of Consensus: service.layerConfig.consensusOptions
3637
of Execution: service.layerConfig.executionOptions
3738

38-
#configs list total size
39+
#configs table total size
3940
var totalSize: uint = 0
4041
totalSize += uint(sizeof(uint))
41-
for word in optionsList:
42-
totalSize += uint(sizeof(uint)) # element type size
43-
totalSize += uint(word.len) # element length
42+
for opt, arg in optionsTable:
43+
totalSize += uint(sizeof(uint)) + uint(opt.len) # option
44+
totalSize += uint(sizeof(uint)) + uint(arg.len) # arg
4445

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

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

65-
#element data
66-
copyMem(cast[pointer](writeOffset), unsafeAddr word[0], word.len)
67-
writeOffset += uint(word.len)
68-
69-
service.serviceChannel[].send(byteArray)
64+
serviceChannel[].send(byteArray)
7065

7166
#wait for service read ack
7267
while not isConfigRead.load():
7368
sleep(cThreadTimeAck)
7469
isConfigRead.store(true)
7570

7671
#close channel
77-
service.serviceChannel[].close()
72+
serviceChannel[].close()
7873

7974
#dealloc shared data
8075
deallocShared(byteArray)
81-
deallocShared(service.serviceChannel)
76+
deallocShared(serviceChannel)
8277

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

9691
## start nimbus client
9792
proc run*(nimbus: var Nimbus) =
98-
# todo
99-
# parse cmd, read options and create configs
93+
# to be filled with command line options after parsed according to service
10094
var
101-
execOpt = newSeq[string]()
102-
consOpt = newSeq[string]()
95+
consOpt, execOpt = NimbusConfigTable()
96+
10397
executionService: NimbusService = NimbusService(
10498
name: "Execution Layer",
10599
serviceFunc: executionLayerHandler,

nimbus/tests/consensus/test_consensus_layer.nim

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
{.push raises: [].}
99

10-
import
11-
unittest2,
12-
std/[concurrency/atomics, os],
13-
../../../nimbus/common/utils,
14-
../../../nimbus/conf,
15-
../../../nimbus/consensus/consensus_layer
10+
import unittest2
1611

17-
#tbd
12+
suite "Nimbus consensus layer":
13+
#tbd, given that layer is in development
14+
test "tbd":
15+
check true

nimbus/tests/execution/test_execution_layer.nim

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
{.push raises: [].}
99

1010
import
11-
unittest2,
12-
std/[concurrency/atomics, os],
13-
../../../nimbus/common/utils,
14-
../../../nimbus/conf,
15-
../../../nimbus/execution/execution_layer
11+
unittest2
1612

17-
#tbd
13+
suite "Nimbus execution layer":
14+
#tbd, given that layer is in development
15+
test "tbd":
16+
check true

0 commit comments

Comments
 (0)