Skip to content

Commit

Permalink
chore: fix devel compilation issues (#1195)
Browse files Browse the repository at this point in the history
- fixes #1194.
- fixes ambiguous `KeyError`
- removes an unnecessary type param for `newSeqWith`
- fixes tests for `safeConvert`

The main fixes relate to Nim 2.2 being more strict and not accepting
calls with a wrong number of type parameters.
  • Loading branch information
diegomrsantos authored Sep 19, 2024
1 parent b37133c commit 68306cf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
18 changes: 10 additions & 8 deletions libp2p/crypto/crypto.nim
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ proc random*(
case scheme
of PKScheme.RSA:
when supported(PKScheme.RSA):
let rsakey = ?RsaPrivateKey.random(rng, bits).orError(KeyError)
let rsakey = ?RsaPrivateKey.random(rng, bits).orError(CryptoError.KeyError)
ok(PrivateKey(scheme: scheme, rsakey: rsakey))
else:
err(SchemeError)
Expand All @@ -210,7 +210,8 @@ proc random*(
err(SchemeError)
of PKScheme.ECDSA:
when supported(PKScheme.ECDSA):
let eckey = ?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError)
let eckey =
?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(CryptoError.KeyError)
ok(PrivateKey(scheme: scheme, eckey: eckey))
else:
err(SchemeError)
Expand All @@ -237,10 +238,11 @@ proc random*(
let skkey = SkPrivateKey.random(rng)
ok(PrivateKey(scheme: PKScheme.Secp256k1, skkey: skkey))
elif supported(PKScheme.RSA):
let rsakey = ?RsaPrivateKey.random(rng, bits).orError(KeyError)
let rsakey = ?RsaPrivateKey.random(rng, bits).orError(CryptoError.KeyError)
ok(PrivateKey(scheme: PKScheme.RSA, rsakey: rsakey))
elif supported(PKScheme.ECDSA):
let eckey = ?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(KeyError)
let eckey =
?ecnist.EcPrivateKey.random(Secp256r1, rng).orError(CryptoError.KeyError)
ok(PrivateKey(scheme: PKScheme.ECDSA, eckey: eckey))
else:
err(SchemeError)
Expand All @@ -258,7 +260,7 @@ proc random*(
case scheme
of PKScheme.RSA:
when supported(PKScheme.RSA):
let pair = ?RsaKeyPair.random(rng, bits).orError(KeyError)
let pair = ?RsaKeyPair.random(rng, bits).orError(CryptoError.KeyError)
ok(
KeyPair(
seckey: PrivateKey(scheme: scheme, rsakey: pair.seckey),
Expand All @@ -280,7 +282,7 @@ proc random*(
err(SchemeError)
of PKScheme.ECDSA:
when supported(PKScheme.ECDSA):
let pair = ?EcKeyPair.random(Secp256r1, rng).orError(KeyError)
let pair = ?EcKeyPair.random(Secp256r1, rng).orError(CryptoError.KeyError)
ok(
KeyPair(
seckey: PrivateKey(scheme: scheme, eckey: pair.seckey),
Expand Down Expand Up @@ -583,15 +585,15 @@ proc init*(t: typedesc[PrivateKey], data: openArray[byte]): CryptoResult[Private
## Create new private key from libp2p's protobuf serialized binary form.
var res: t
if not res.init(data):
err(KeyError)
err(CryptoError.KeyError)
else:
ok(res)

proc init*(t: typedesc[PublicKey], data: openArray[byte]): CryptoResult[PublicKey] =
## Create new public key from libp2p's protobuf serialized binary form.
var res: t
if not res.init(data):
err(KeyError)
err(CryptoError.KeyError)
else:
ok(res)

Expand Down
3 changes: 2 additions & 1 deletion libp2p/utility.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ when defined(libp2p_agents_metrics):
KnownLibP2PAgents* {.strdefine.} = "nim-libp2p"
KnownLibP2PAgentsSeq* = KnownLibP2PAgents.safeToLowerAscii().tryGet().split(",")

template safeConvert*[T: SomeInteger, S: Ordinal](value: S): T =
proc safeConvert*[T: SomeInteger](value: SomeOrdinal): T =
type S = typeof(value)
## Converts `value` from S to `T` iff `value` is guaranteed to be preserved.
when int64(T.low) <= int64(S.low()) and uint64(T.high) >= uint64(S.high):
T(value)
Expand Down
2 changes: 1 addition & 1 deletion tests/pubsub/testgossipinternal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ suite "GossipSub internal":
var sentMessages = initHashSet[seq[byte]]()

for i, size in enumerate([size1, size2]):
let data = newSeqWith[byte](size, i.byte)
let data = newSeqWith(size, i.byte)
sentMessages.incl(data)

let msg =
Expand Down
4 changes: 2 additions & 2 deletions tests/pubsub/testgossipsub.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ suite "GossipSub":

# Simulate sending an undecodable message
await gossip1.peers[gossip0.switch.peerInfo.peerId].sendEncoded(
newSeqWith[byte](33, 1.byte), isHighPriority = true
newSeqWith(33, 1.byte), isHighPriority = true
)
await sleepAsync(300.millis)

Expand All @@ -1069,7 +1069,7 @@ suite "GossipSub":
# Disconnect peer when rate limiting is enabled
gossip1.parameters.disconnectPeerAboveRateLimit = true
await gossip0.peers[gossip1.switch.peerInfo.peerId].sendEncoded(
newSeqWith[byte](35, 1.byte), isHighPriority = true
newSeqWith(35, 1.byte), isHighPriority = true
)

checkUntilTimeout gossip1.switch.isConnected(gossip0.switch.peerInfo.peerId) == false
Expand Down
26 changes: 11 additions & 15 deletions tests/testutility.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,42 @@ import ../libp2p/utility
suite "Utility":

test "successful safeConvert from int8 to int16":
let res = safeConvert[int16, int8]((-128).int8)
let res = safeConvert[int16]((-128).int8)
check res == -128'i16

test "unsuccessful safeConvert from int16 to int8":
check not (compiles do:
result: int8 = safeConvert[int8, int16](32767'i16))
result: int8 = safeConvert[int8](32767'i16))

test "successful safeConvert from uint8 to uint16":
let res: uint16 = safeConvert[uint16, uint8](255'u8)
let res: uint16 = safeConvert[uint16](255'u8)
check res == 255'u16

test "unsuccessful safeConvert from uint16 to uint8":
check not (compiles do:
result: uint8 = safeConvert[uint8, uint16](256'u16))

test "successful safeConvert from char to int":
let res: int = safeConvert[int, char]('A')
check res == 65
result: uint8 = safeConvert[uint8](256'u16))

test "unsuccessful safeConvert from int to char":
check not (compiles do:
result: char = safeConvert[char, int](128))
result: char = safeConvert[char](128))

test "successful safeConvert from bool to int":
let res: int = safeConvert[int, bool](true)
let res: int = safeConvert[int](true)
check res == 1

test "unsuccessful safeConvert from int to bool":
check not (compiles do:
result: bool = safeConvert[bool, int](2))
result: bool = safeConvert[bool](2))

test "successful safeConvert from enum to int":
type Color = enum red, green, blue
let res: int = safeConvert[int, Color](green)
let res: int = safeConvert[int](green)
check res == 1

test "unsuccessful safeConvert from int to enum":
type Color = enum red, green, blue
check not (compiles do:
result: Color = safeConvert[Color, int](3))
result: Color = safeConvert[Color](3))

test "successful safeConvert from range to int":
let res: int = safeConvert[int, range[1..10]](5)
Expand All @@ -68,11 +64,11 @@ suite "Utility":

test "unsuccessful safeConvert from int to uint":
check not (compiles do:
result: uint = safeConvert[uint, int](11))
result: uint = safeConvert[uint](11))

test "unsuccessful safeConvert from uint to int":
check not (compiles do:
result: uint = safeConvert[int, uint](11.uint))
result: uint = safeConvert[int](11.uint))

suite "withValue and valueOr templates":
type
Expand Down

0 comments on commit 68306cf

Please sign in to comment.