sync up with master

This commit is contained in:
Dmitriy Ryajov 2022-11-25 12:58:08 -06:00
parent 3a53bb1338
commit 5331dc9606
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
6 changed files with 26 additions and 24 deletions

View File

@ -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"

View File

@ -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

View File

@ -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))

View File

@ -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

View File

@ -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)

View File

@ -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