Refactoring

This commit is contained in:
Marto 2024-02-09 16:56:33 +02:00
parent 4186bdae2d
commit ce87cbd769
12 changed files with 12 additions and 271 deletions

View File

@ -167,5 +167,4 @@ jobs:
fi
nim --version
nimble --version
nimble build
nimble test

View File

@ -20,4 +20,4 @@ It will ensure better testability and integrability.
# Run test
`./run_all_tests.sh`
`nimble test`

View File

@ -1,3 +0,0 @@
* testbasictimers - 504 milliseconds, 149 microseconds, and 657 nanoseconds
* testbasicstatemachine - 1 millisecond, 831 microseconds, and 189 nanoseconds
* testbasicclusterelection - 2 minutes, 80 milliseconds, 340 microseconds, and 536 nanoseconds

View File

@ -1,103 +0,0 @@
# nim-raft
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import std/times
import vm_compile_info
import macros, strutils, os, unittest2, osproc
import threadpool
export strutils, os, unittest2, osproc, threadpool
# AppVeyor may go out of memory with the default of 4
setMinPoolSize(2)
proc executeMyself(numModules: int, names: openArray[string]): int =
let appName = getAppFilename()
var elpdList = newSeq[Duration](numModules)
for i in 0..<numModules:
let start = getTime()
let execResult = execCmd(appName & " " & $i)
let elpd = getTime() - start
elpdList[i] = elpd
if execResult != 0:
stderr.writeLine("subtest no: " & $i & " failed: " & names[i])
result = result or execResult
var f = open("all_test.md", fmWrite)
for i in 0..<numModules:
f.write("* " & names[i])
f.write(" - " & $elpdList[i])
f.write("\n")
f.close()
proc getImportStmt(stmtList: NimNode): NimNode =
result = stmtList[0]
result.expectKind nnkImportStmt
proc ofStmt(idx: int, singleModule: NimNode): NimNode =
# remove the "test_" prefix
let moduleName = normalize(singleModule.toStrLit.strVal).substr(4)
let moduleMain = newIdentNode(moduleName & "Main")
# construct `of` branch
# of idx: moduleMain()
result = nnkOfBranch.newTree(
newLit(idx),
newCall(moduleMain)
)
proc toModuleNames(importStmt: NimNode): NimNode =
result = nnkBracket.newTree
for singleModule in importStmt:
let x = normalize(singleModule.toStrLit.strVal)
result.add newLit(x)
macro cliBuilder*(stmtList: typed): untyped =
let importStmt = stmtList.getImportStmt
let moduleCount = importStmt.len
let moduleNames = importStmt.toModuleNames
# case paramStr(1).parseInt
var caseStmt = nnkCaseStmt.newTree(
quote do: paramStr(1).parseInt
)
# of 0: codeStreamMain()
# of 1: gasMeterMain()
# of 2: memoryMain()
# ...
for idx, singleModule in importStmt:
caseStmt.add ofStmt(idx, singleModule)
# else:
# echo "invalid argument"
caseStmt.add nnkElse.newTree(
quote do: echo "invalid argument"
)
result = quote do:
if paramCount() == 0:
const names = `moduleNames`
quit(executeMyself(`moduleCount`, names))
else:
`caseStmt`
# if you want to add new test module(s)
# make sure you define an entry poin
# e.g.
# proc mytestMain*() =
# # put anything you want here
# and then give it a name `test_mytest.nim`
# the `mytest` part should match between
# the proc name and the module name
# if this executable called without any params
# it will execute each of the test by executing itself
# repeatedly until all sub-tests are executed.
# you can execute the sub-test by a number start from zero.

View File

@ -1,31 +0,0 @@
# nim-raft
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
func vmName(): string =
when defined(evmc_enabled):
"evmc"
else:
"nimvm"
const
VmName* = vmName()
warningMsg = block:
var rc = "*** Compiling with " & VmName
when defined(legacy_eth66_enabled):
rc &= ", legacy-eth/66"
when defined(chunked_rlpx_enabled):
rc &= ", chunked-rlpx"
when defined(boehmgc):
rc &= ", boehm/gc"
rc &= " enabled"
rc
{.warning: warningMsg.}
{.used.}

8
nim.projectMapping Normal file
View File

@ -0,0 +1,8 @@
{
"nim.provider": "lsp",
"nim.projectMapping": [{
// everything else - use main.nim as root.
"projectFile": "raft.nim",
"fileRegex": ".*\\.nim"
}]
}

View File

@ -20,38 +20,8 @@ requires "nim >= 1.6.14"
requires "stew >= 0.1.0"
requires "unittest2 >= 0.0.4"
requires "uuids >= 0.1.11"
requires "chronicles >= 0.10.3"
requires "chronos >= 3.0.11"
requires "nimdbx >= 0.4.1"
requires "nimterop >= 0.6.13"
proc buildBinary(name: string, srcDir = "./", params = "", lang = "c") =
if not dirExists "build":
mkDir "build"
# allow something like "nim nimbus --verbosity:0 --hints:off nimbus.nims"
var extra_params = params
for i in 2..<paramCount():
extra_params &= " " & paramStr(i)
exec "nim " & lang & " --threads:on " & extra_params & " --out:build/" & name & " " & srcDir & name & ".nim"
proc test(path: string, name: string, params = "", lang = "c") =
# Verify stack usage is kept low by setting 750k stack limit in tests.
const stackLimitKiB = 750
when not defined(windows):
const (buildOption, runPrefix) = ("", "ulimit -s " & $stackLimitKiB & " && ")
else:
# No `ulimit` in Windows. `ulimit -s` in Bash is accepted but has no effect.
# See https://public-inbox.org/git/alpine.DEB.2.21.1.1709131448390.4132@virtualbox/
# Also, the command passed to NimScript `exec` on Windows is not a shell script.
# Instead, we can set stack size at link time.
const (buildOption, runPrefix) =
(" -d:windowsNoSetStack --passL:-Wl,--stack," & $(stackLimitKiB * 2048), "")
buildBinary name, (path & "/"), params & buildOption
exec runPrefix & "build/" & name
task test, "Run tests":
test "tests", "all_tests", "-d:chronicles_sinks=textlines -d:chronicles_log_level=ERROR -d:unittest2DisableParamFiltering"
exec "nim c -r tests/test_consensus_state_machine.nim "
# Helper functions

View File

@ -1 +0,0 @@
raft.nimble

View File

@ -1,59 +0,0 @@
# nim-raft
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import chronos
template awaitWithTimeout*[T](operation: Future[T],
deadline: Future[void],
body: untyped) =
let f {.inject.} = operation
await f or deadline
if not f.finished:
# If we don't wait for for the cancellation here, it's possible that
# the "next" operation will run concurrently to this one, messing up
# the order of operations (since await/async is not fair)
await cancelAndWait(f)
else:
body
# template awaitWithTimeout*[T](operation: Future[T],
# deadline: Future[void],
# onTimeout: untyped): T =
# let f = operation
# await f or deadline
# if not f.finished:
# # If we don't wait for for the cancellation here, it's possible that
# # the "next" operation will run concurrently to this one, messing up
# # the order of operations (since await/async is not fair)
# await cancelAndWait(f)
# onTimeout
# else:
# f.read
# template awaitWithTimeout*[T](operation: Future[T],
# timeout: Duration,
# onTimeout: untyped): T =
# awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
# template awaitWithTimeout*(operation: Future[void],
# deadline: Future[void],
# onTimeout: untyped) =
# let f = operation
# await f or deadline
# if not f.finished:
# # If we don't wait for for the cancellation here, it's possible that
# # the "next" operation will run concurrently to this one, messing up
# # the order of operations (since await/async is not fair)
# await cancelAndWait(f)
# onTimeout
# template awaitWithTimeout*(operation: Future[void],
# timeout: Duration,
# onTimeout: untyped) =
# awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)

View File

@ -1,34 +0,0 @@
{
"raftPeers":[
{
"id": "f9695ea4-4f37-11ee-8e75-8ff5a48faa42",
"host": "127.0.0.1",
"port": 7771,
"friendly_name": "Raft Node 1"
},
{
"id": "04fd202a-4f38-11ee-9ee2-23aba7dde7d3",
"host": "127.0.0.1",
"port": 7772,
"friendly_name": "Raft Node 2 etc."
},
{
"id": "09f3e6b8-4f38-11ee-a221-4b348f8bbde7",
"host": "127.0.0.1",
"port": 7773,
"friendly_name": "Raft Node 3 etc."
},
{
"id": "0edc0976-4f38-11ee-b1ad-5b3b0f690e65",
"host": "127.0.0.1",
"port": 7774,
"friendly_name": "Raft Node 4 etc."
},
{
"id": "130d0662-4f38-11ee-a6ed-2746aae5bc0b",
"host": "127.0.0.1",
"port": 7775,
"friendly_name": "Raft Node 5 etc."
}
]
}

View File

@ -1,2 +0,0 @@
#!/bin/bash
nim test raft.nims

View File

@ -7,9 +7,6 @@
# This file may not be copied, modified, or distributed except according to
# those terms.
import ../misc/test_macro
{. warning[UnusedImport]:off .}
cliBuilder:
import ./test_consensus_state_machine
import test_consensus_state_machine
export test_consensus_state_machine