diff --git a/codex.nim b/codex.nim index 36ae7990..528e48d2 100644 --- a/codex.nim +++ b/codex.nim @@ -91,7 +91,7 @@ when isMainModule: waitFor server.stop() notice "Stopped Codex" - c_signal(SIGTERM, SIGTERMHandler) + c_signal(ansi_c.SIGTERM, SIGTERMHandler) waitFor server.start() notice "Exited codex" diff --git a/codex/blockexchange/network/networkpeer.nim b/codex/blockexchange/network/networkpeer.nim index c9b8405e..cef58bc5 100644 --- a/codex/blockexchange/network/networkpeer.nim +++ b/codex/blockexchange/network/networkpeer.nim @@ -7,6 +7,9 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. +import pkg/upraises +push: {.upraises: [].} + import pkg/chronos import pkg/chronicles import pkg/libp2p @@ -21,6 +24,8 @@ const MaxMessageSize = 100 * 1 shl 20 # manifest files can be big type + ConnProvider* = proc(): Future[Connection] {.gcsafe, closure.} + RPCHandler* = proc(peer: NetworkPeer, msg: Message): Future[void] {.gcsafe.} NetworkPeer* = ref object of RootObj diff --git a/codex/rng.nim b/codex/rng.nim index b1f6266d..19452cd4 100644 --- a/codex/rng.nim +++ b/codex/rng.nim @@ -12,11 +12,11 @@ import pkg/upraises push: {.upraises: [].} import pkg/libp2p/crypto/crypto -import pkg/bearssl +import pkg/bearssl/rand type RngSampleError = object of CatchableError - Rng* = ref BrHmacDrbgContext + Rng* = ref HmacDrbgContext var rng {.threadvar.}: Rng @@ -25,16 +25,15 @@ proc instance*(t: type Rng): Rng = rng = newRng() rng -# Random helpers: similar as in stdlib, but with BrHmacDrbgContext rng +# Random helpers: similar as in stdlib, but with HmacDrbgContext rng # TODO: Move these somewhere else? const randMax = 18_446_744_073_709_551_615'u64 proc rand*(rng: Rng, max: Natural): int = if max == 0: return 0 - var x: uint64 while true: - brHmacDrbgGenerate(addr rng[], addr x, csize_t(sizeof(x))) + let x = rng[].generate(uint64) if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias return int(x mod (uint64(max) + 1'u64)) diff --git a/codex/streams/asyncstreamwrapper.nim b/codex/streams/asyncstreamwrapper.nim index 81406bc3..a0829b7b 100644 --- a/codex/streams/asyncstreamwrapper.nim +++ b/codex/streams/asyncstreamwrapper.nim @@ -7,7 +7,8 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -{.push raises: [Defect].} +import pkg/upraises +push: {.upraises: [].} import pkg/chronos import pkg/chronicles diff --git a/tests/codex/blockexchange/testnetwork.nim b/tests/codex/blockexchange/testnetwork.nim index 8eda6a93..9f3c399e 100644 --- a/tests/codex/blockexchange/testnetwork.nim +++ b/tests/codex/blockexchange/testnetwork.nim @@ -158,9 +158,6 @@ suite "Network - Senders": done = newFuture[void]() switch1 = newStandardSwitch() switch2 = newStandardSwitch() - await switch1.start() - await switch2.start() - network1 = BlockExcNetwork.new( switch = switch1) switch1.mount(network1) @@ -169,6 +166,9 @@ suite "Network - Senders": switch = switch2) switch2.mount(network2) + await switch1.start() + await switch2.start() + await switch1.connect( switch2.peerInfo.peerId, switch2.peerInfo.addrs) @@ -271,8 +271,6 @@ suite "Network - Test Limits": done = newFuture[void]() switch1 = newStandardSwitch() switch2 = newStandardSwitch() - await switch1.start() - await switch2.start() network1 = BlockExcNetwork.new( switch = switch1, @@ -283,6 +281,9 @@ suite "Network - Test Limits": switch = switch2) switch2.mount(network2) + await switch1.start() + await switch2.start() + await switch1.connect( switch2.peerInfo.peerId, switch2.peerInfo.addrs) diff --git a/tests/codex/helpers.nim b/tests/codex/helpers.nim index 475bf7a1..6d431870 100644 --- a/tests/codex/helpers.nim +++ b/tests/codex/helpers.nim @@ -37,12 +37,11 @@ proc corruptBlocks*( manifest: Manifest, blks, bytes: int): Future[seq[int]] {.async.} = var pos: seq[int] - while true: - if pos.len >= blks: - break - var i = -1 - if (i = Rng.instance.rand(manifest.len - 1); pos.find(i) >= 0): + doAssert blks < manifest.len + while pos.len < blks: + let i = Rng.instance.rand(manifest.len - 1) + if pos.find(i) >= 0: continue pos.add(i) @@ -50,15 +49,12 @@ proc corruptBlocks*( blk = (await store.getBlock(manifest[i])).tryGet() bytePos: seq[int] - while true: - if bytePos.len > bytes: - break - - var ii = -1 - if (ii = Rng.instance.rand(blk.data.len - 1); bytePos.find(ii) >= 0): + doAssert bytes < blk.data.len + while bytePos.len <= bytes: + let ii = Rng.instance.rand(blk.data.len - 1) + if bytePos.find(ii) >= 0: continue bytePos.add(ii) blk.data[ii] = byte 0 - return pos