annotate `test_fixture_fork_choice` with `{.raises.}` (#5903)

The fork choice test fixture is one of the more complex ones. Annotate
its functions with `{.raises.}` to make exception flow explicit.
This commit is contained in:
Etan Kissling 2024-02-17 22:52:11 +01:00 committed by GitHub
parent 9cd6b270df
commit 71444a371a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 13 deletions

View File

@ -5,6 +5,7 @@
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). # * 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. # at your option. This file may not be copied, modified, or distributed except according to those terms.
{.push raises: [].}
{.used.} {.used.}
import import
@ -71,7 +72,8 @@ type
proc initialLoad( proc initialLoad(
path: string, db: BeaconChainDB, path: string, db: BeaconChainDB,
StateType, BlockType: typedesc StateType, BlockType: typedesc
): tuple[dag: ChainDAGRef, fkChoice: ref ForkChoice] = ): tuple[dag: ChainDAGRef, fkChoice: ref ForkChoice] {.raises: [
IOError, UnconsumedInput].} =
let let
forkedState = loadForkedState( forkedState = loadForkedState(
path/"anchor_state.ssz_snappy", path/"anchor_state.ssz_snappy",
@ -98,7 +100,11 @@ proc initialLoad(
(dag, fkChoice) (dag, fkChoice)
proc loadOps(path: string, fork: ConsensusFork): seq[Operation] = proc loadOps(
path: string, fork: ConsensusFork
): seq[Operation] {.raises: [
IOError, KeyError, UnconsumedInput, ValueError,
YamlParserError, YamlConstructionError].} =
let stepsYAML = os_ops.readFile(path/"steps.yaml") let stepsYAML = os_ops.readFile(path/"steps.yaml")
let steps = yaml.loadToJson(stepsYAML) let steps = yaml.loadToJson(stepsYAML)
@ -163,7 +169,7 @@ proc loadOps(path: string, fork: ConsensusFork): seq[Operation] =
result.add Operation(kind: opChecks, result.add Operation(kind: opChecks,
checks: step["checks"]) checks: step["checks"])
else: else:
doAssert false, "Unknown test step: " & $step raiseAssert "Unknown test step: " & $step
if step.hasKey"valid": if step.hasKey"valid":
doAssert step.len == 2 + numExtraFields doAssert step.len == 2 + numExtraFields
@ -247,11 +253,10 @@ proc stepOnBlock(
blockAdded blockAdded
proc stepChecks( proc stepChecks(
checks: JsonNode, checks: JsonNode,
dag: ChainDAGRef, dag: ChainDAGRef,
fkChoice: ref ForkChoice, fkChoice: ref ForkChoice,
time: BeaconTime time: BeaconTime) {.raises: [KeyError].} =
) =
doAssert checks.len >= 1, "No checks found" doAssert checks.len >= 1, "No checks found"
for check, val in checks: for check, val in checks:
if check == "time": if check == "time":
@ -287,9 +292,13 @@ proc stepChecks(
# We do not store genesis in fork choice.. # We do not store genesis in fork choice..
discard discard
else: else:
doAssert false, "Unsupported check '" & $check & "'" raiseAssert "Unsupported check '" & $check & "'"
proc doRunTest(path: string, fork: ConsensusFork) = proc doRunTest(
path: string, fork: ConsensusFork
) {.raises: [
IOError, KeyError, UnconsumedInput, ValueError,
YamlParserError, YamlConstructionError].} =
let db = BeaconChainDB.new("", inMemory = true) let db = BeaconChainDB.new("", inMemory = true)
defer: defer:
db.close() db.close()
@ -300,7 +309,13 @@ proc doRunTest(path: string, fork: ConsensusFork) =
path, db, consensusFork.BeaconState, consensusFork.BeaconBlock) path, db, consensusFork.BeaconState, consensusFork.BeaconBlock)
rng = HmacDrbgContext.new() rng = HmacDrbgContext.new()
taskpool = Taskpool.new() taskpool =
try:
Taskpool.new()
except Exception as exc:
fatal "Failed to initialize Taskpool", exc = exc.msg
fail()
return
var verifier = BatchVerifier.init(rng, taskpool) var verifier = BatchVerifier.init(rng, taskpool)
let steps = loadOps(path, fork) let steps = loadOps(path, fork)
@ -340,7 +355,7 @@ proc doRunTest(path: string, fork: ConsensusFork) =
of opChecks: of opChecks:
stepChecks(step.checks, stores.dag, stores.fkChoice, time) stepChecks(step.checks, stores.dag, stores.fkChoice, time)
else: else:
doAssert false, "Unsupported" raiseAssert "Unsupported"
proc runTest(suiteName: static[string], path: string, fork: ConsensusFork) = proc runTest(suiteName: static[string], path: string, fork: ConsensusFork) =
const SKIP = [ const SKIP = [
@ -363,7 +378,12 @@ proc runTest(suiteName: static[string], path: string, fork: ConsensusFork) =
"basic_is_head_root", "basic_is_head_root",
] ]
test suiteName & " - " & path.relativePath(SszTestsDir): let relativePathComponent =
try:
path.relativePath(SszTestsDir)
except Exception as exc:
raiseAssert "relativePath failed unexpectedly: " & $exc.msg
test suiteName & " - " & relativePathComponent:
when defined(windows): when defined(windows):
# Some test files have very long paths # Some test files have very long paths
skip() skip()