chore: add support for Nim 2 (#51)

- Add Nim 2.0.8 to CI.
- Increase CI timeout to 30 minutes as the macOS building was timing out
during Nim installation.
- Use `refc` memory management.
- Upgrade ngtcp2 to a version that works with Nim 2.0.8.
- Use int instead of uint64 for `PacketNumber` and `VarIntCompatible` as
it makes implementation easier and avoids a lot of casts.
- Add required {.gcsafe.} in some procs.

fixes https://github.com/vacp2p/nim-quic/issues/43
This commit is contained in:
diegomrsantos 2024-09-10 18:46:19 +02:00 committed by GitHub
parent 57562b7f96
commit 8a97eeeb80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 28 additions and 27 deletions

View File

@ -10,11 +10,11 @@ on:
jobs:
test:
runs-on: ${{ matrix.os }}
timeout-minutes: 20
timeout-minutes: 30
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
nim: [1.6.20]
nim: [1.6.20, 2.0.8]
steps:
- uses: actions/checkout@v2
- uses: iffy/install-nim@v3

View File

@ -2,3 +2,4 @@
-d:chronosStrictException
--styleCheck:usages
--styleCheck:hint
--mm:refc

View File

@ -5,9 +5,9 @@ description = "QUIC protocol implementation"
license = "MIT"
requires "nim >= 1.6.0"
requires "stew >= 0.1.0 & < 0.2.0"
requires "stew#head"
requires "chronos >= 4.0.3 & < 5.0.0"
requires "nimcrypto >= 0.6.0 & < 0.7.0"
requires "ngtcp2 >= 0.34.0"
requires "unittest2 >= 0.2.2 & < 0.3.0"
requires "ngtcp2#6834f4756b6af58356ac9c4fef3d71db3c3ae5fe"
requires "unittest2"
requires "chronicles >= 0.10.2"

View File

@ -2,10 +2,10 @@ import std/math
import pkg/stew/endians2
type
PacketNumber* = range[0'u64..2'u64^62-1]
PacketNumber* = range[0..2^62-1]
proc toMinimalBytes*(packetnumber: PacketNumber): seq[byte] =
let bytes = packetnumber.toBytesBE
let bytes = packetnumber.uint64.toBytesBE
var length = bytes.len
while length > 1 and bytes[bytes.len - length] == 0:
length = length - 1

View File

@ -109,7 +109,7 @@ proc `payload=`(packet: var Packet, payload: seq[byte]) =
else: discard
proc readPacketLength(reader: var PacketReader,
datagram: openArray[byte]): uint64 =
datagram: openArray[byte]): int =
case reader.packet.form:
of formLong: reader.readVarInt(datagram)
of formShort: datagram.len - reader.next
@ -122,7 +122,7 @@ proc readPacketNumberAndPayload*(reader: var PacketReader,
datagram: openArray[byte]) =
let length = reader.readPacketLength(datagram)
let packetnumberLength = reader.readPacketNumberLength(datagram)
let payloadLength = length - packetnumberLength.uint64
let payloadLength = length - packetnumberLength
reader.readPacketNumber(datagram, packetnumberLength)
reader.readPayload(datagram, payloadLength.int)

View File

@ -2,7 +2,7 @@ import std/math
import pkg/stew/endians2
type
VarIntCompatible* = range[0'u64..2'u64^62-1]
VarIntCompatible* = range[0..2^62-1]
proc toVarInt*(value: VarIntCompatible): seq[byte] =
case value
@ -16,7 +16,7 @@ proc toVarInt*(value: VarIntCompatible): seq[byte] =
@(toBytesBE(length or value.uint32))
else:
const length = 0b11'u64 shl 62
@(toBytesBE(length or value))
@(toBytesBE(length or value.uint64))
proc varintlen*(varint: openArray[byte]): int =
2^(varint[0] shr 6)

View File

@ -42,10 +42,10 @@ method openStream*(state: ConnectionState,
unidirectional: bool): Future[Stream] =
doAssert false # override this method
method drop*(state: ConnectionState): Future[void] =
method drop*(state: ConnectionState): Future[void] {.gcsafe.} =
doAssert false # override this method
method close*(state: ConnectionState): Future[void] =
method close*(state: ConnectionState): Future[void] {.gcsafe.} =
doAssert false # override this method
{.pop.}

View File

@ -6,7 +6,7 @@ suite "packet numbers":
test "packet numbers are in the range 0 to 2^62-1":
check PacketNumber.low == 0
check PacketNumber.high == 2'u64 ^ 62 - 1
check PacketNumber.high == 2 ^ 62 - 1
test "conversion to bytes":
check 0.toMinimalBytes == @[0'u8]

View File

@ -76,7 +76,7 @@ suite "packet reading":
check readPacket(datagram[0..<length]).retry.integrity == integrity
test "reads packet number from handshake packet":
const packetnumber = 0xABCD'u16
const packetnumber = 0xABCD
var packet = handshakePacket()
packet.handshake.packetnumber = packetnumber
datagram.write(packet)
@ -90,7 +90,7 @@ suite "packet reading":
check readPacket(datagram).handshake.payload == payload
test "reads packet number from 0-RTT packet":
const packetnumber = 0xABCD'u16
const packetnumber = 0xABCD
var packet = zeroRttPacket()
packet.rtt.packetnumber = packetnumber
datagram.write(packet)
@ -111,7 +111,7 @@ suite "packet reading":
check readPacket(datagram).initial.token == token
test "reads packet number from initial packet":
const packetnumber = 0xABCD'u16
const packetnumber = 0xABCD
var packet = initialPacket()
packet.initial.packetnumber = packetnumber
datagram.write(packet)
@ -150,7 +150,7 @@ suite "packet reading":
check readPacket(datagram).destination == destination
test "reads packet number from short packet":
const packetnumber = 0xABCD'u16
const packetnumber = 0xABCD
var packet = shortPacket()
packet.short.packetnumber = packetnumber
packet.destination = ConnectionId(repeat(0'u8, DefaultConnectionIdLength))

View File

@ -33,30 +33,30 @@ suite "variable length integer encoding":
suite "variable length integer decoding":
test "decodes 1 byte integers":
check fromVarInt(@[0'u8]) == 0'u64
check fromVarInt(@[0'u8]) == 0
check fromVarInt(@[42'u8]) == 42
check fromVarInt(@[63'u8]) == 63
test "decodes 2 byte integers":
check fromVarInt(@[0b01000000'u8, 0'u8]) == 0'u64
check fromVarInt(@[0b01000000'u8, 0'u8]) == 0
check fromVarInt(@[0b01000001'u8, 0'u8]) == 256
check fromVarInt(@[0b01111111'u8, 0xFF'u8]) == 2'u64^14-1
check fromVarInt(@[0b01111111'u8, 0xFF'u8]) == 2^14-1
test "decodes 4 byte integers":
check fromVarInt(@[0b10000000'u8, 0'u8, 0'u8, 0'u8]) == 0'u64
check fromVarInt(@[0b10000001'u8, 0'u8, 0'u8, 0'u8]) == 2'u64^24
check fromVarInt(@[0b10111111'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]) == 2'u64^30-1
check fromVarInt(@[0b10000000'u8, 0'u8, 0'u8, 0'u8]) == 0
check fromVarInt(@[0b10000001'u8, 0'u8, 0'u8, 0'u8]) == 2^24
check fromVarInt(@[0b10111111'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8]) == 2^30-1
test "decodes 8 byte integers":
check fromVarInt(@[
0b11000000'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8
]) == 0'u64
]) == 0
check fromVarInt(@[
0b11000001'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8, 0'u8
]) == 2'u64^56
]) == 2^56
check fromVarInt(@[
0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8, 0xFF'u8
]) == 2'u64^62-1
]) == 2^62-1
suite "variable length":