assert exceptions that statically cannot occur in test code (#5989)

There are some spurious exceptions that statically cannot occur.
Add `raiseAssert` for these.
This commit is contained in:
Etan Kissling 2024-02-28 21:15:49 +01:00 committed by GitHub
parent 84b752c7a1
commit 078f4ac347
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 16 deletions

View File

@ -5,12 +5,18 @@
# * 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.
{.push raises: [].}
import ../beacon_chain/spec/datatypes/phase0
import std/[typetraits, strformat, strutils]
proc print(t: auto, n: string, indent: int) =
echo fmt"{sizeof(t):>8} {spaces(indent)}{n}: {typeof(t).name}"
try:
echo fmt"{sizeof(t):>8} {spaces(indent)}{n}: {typeof(t).name}"
except ValueError:
# https://github.com/nim-lang/Nim/pull/23356
raiseAssert "Arguments match the format string"
when t is object|tuple:
for n, p in t.fieldPairs:

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).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.push raises: [].}
{.used.}
import
@ -19,14 +20,17 @@ proc new(T: type Eth2DiscoveryProtocol,
enrIp: Option[IpAddress], enrTcpPort, enrUdpPort: Option[Port],
bindPort: Port, bindIp: IpAddress,
enrFields: openArray[(string, seq[byte])] = [],
rng: ref HmacDrbgContext):
T {.raises: [CatchableError].} =
rng: ref HmacDrbgContext): T =
newProtocol(pk, enrIp, enrTcpPort, enrUdpPort, enrFields,
bindPort = bindPort, bindIp = bindIp, rng = rng)
proc generateNode(rng: ref HmacDrbgContext, port: Port,
enrFields: openArray[(string, seq[byte])] = []): Eth2DiscoveryProtocol =
let ip = parseIpAddress("127.0.0.1")
let ip =
try:
parseIpAddress("127.0.0.1")
except ValueError:
raiseAssert "Argument is a valid IP address"
Eth2DiscoveryProtocol.new(keys.PrivateKey.random(rng[]),
some(ip), some(port), some(port), port, ip, enrFields, rng = rng)

View File

@ -1,10 +1,12 @@
# nimbus_signing_node
# beacon_chain
# Copyright (c) 2023-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.
{.push raises: [].}
import
std/algorithm,
unittest2, chronicles, stew/[results, byteutils, io2],
@ -17,8 +19,6 @@ import
from std/os import getEnv, osErrorMsg
{.push raises: [].}
{.pop.} # TODO want actual revert, so placate linter
{.used.}
const
@ -100,20 +100,25 @@ func init(T: type ForkedBeaconBlock, contents: ProduceBlockResponseV2): T =
of ConsensusFork.Deneb:
return ForkedBeaconBlock.init(contents.denebData.`block`)
proc getBlock(fork: ConsensusFork,
feeRecipient = SigningExpectedFeeRecipient): ForkedBeaconBlock =
proc getBlock(
fork: ConsensusFork,
feeRecipient = SigningExpectedFeeRecipient
): ForkedBeaconBlock {.raises: [ResultError[cstring]].} =
let
blckData =
case fork
of ConsensusFork.Phase0: Phase0Block
of ConsensusFork.Altair: AltairBlock
of ConsensusFork.Bellatrix: BellatrixBlock % [feeRecipient]
of ConsensusFork.Capella: CapellaBlock % [feeRecipient]
of ConsensusFork.Deneb: DenebBlockContents % [feeRecipient]
try:
case fork
of ConsensusFork.Phase0: Phase0Block
of ConsensusFork.Altair: AltairBlock
of ConsensusFork.Bellatrix: BellatrixBlock % [feeRecipient]
of ConsensusFork.Capella: CapellaBlock % [feeRecipient]
of ConsensusFork.Deneb: DenebBlockContents % [feeRecipient]
except ValueError:
# https://github.com/nim-lang/Nim/pull/23356
raiseAssert "Arguments match the format string"
contentType = ContentTypeData(
mediaType: MediaType.init("application/json"))
let b = decodeBytes(ProduceBlockResponseV2,
blckData.toOpenArrayByte(0, len(blckData) - 1),
Opt.some(contentType),