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() waitFor server.stop()
notice "Stopped Codex" notice "Stopped Codex"
c_signal(SIGTERM, SIGTERMHandler) c_signal(ansi_c.SIGTERM, SIGTERMHandler)
waitFor server.start() waitFor server.start()
notice "Exited codex" notice "Exited codex"

View File

@ -7,6 +7,9 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
import pkg/upraises
push: {.upraises: [].}
import pkg/chronos import pkg/chronos
import pkg/chronicles import pkg/chronicles
import pkg/libp2p import pkg/libp2p
@ -21,6 +24,8 @@ const
MaxMessageSize = 100 * 1 shl 20 # manifest files can be big MaxMessageSize = 100 * 1 shl 20 # manifest files can be big
type type
ConnProvider* = proc(): Future[Connection] {.gcsafe, closure.}
RPCHandler* = proc(peer: NetworkPeer, msg: Message): Future[void] {.gcsafe.} RPCHandler* = proc(peer: NetworkPeer, msg: Message): Future[void] {.gcsafe.}
NetworkPeer* = ref object of RootObj NetworkPeer* = ref object of RootObj

View File

@ -12,11 +12,11 @@ import pkg/upraises
push: {.upraises: [].} push: {.upraises: [].}
import pkg/libp2p/crypto/crypto import pkg/libp2p/crypto/crypto
import pkg/bearssl import pkg/bearssl/rand
type type
RngSampleError = object of CatchableError RngSampleError = object of CatchableError
Rng* = ref BrHmacDrbgContext Rng* = ref HmacDrbgContext
var rng {.threadvar.}: Rng var rng {.threadvar.}: Rng
@ -25,16 +25,15 @@ proc instance*(t: type Rng): Rng =
rng = newRng() rng = newRng()
rng 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? # TODO: Move these somewhere else?
const randMax = 18_446_744_073_709_551_615'u64 const randMax = 18_446_744_073_709_551_615'u64
proc rand*(rng: Rng, max: Natural): int = proc rand*(rng: Rng, max: Natural): int =
if max == 0: return 0 if max == 0: return 0
var x: uint64
while true: 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 if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias
return int(x mod (uint64(max) + 1'u64)) 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 ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
{.push raises: [Defect].} import pkg/upraises
push: {.upraises: [].}
import pkg/chronos import pkg/chronos
import pkg/chronicles import pkg/chronicles

View File

@ -158,9 +158,6 @@ suite "Network - Senders":
done = newFuture[void]() done = newFuture[void]()
switch1 = newStandardSwitch() switch1 = newStandardSwitch()
switch2 = newStandardSwitch() switch2 = newStandardSwitch()
await switch1.start()
await switch2.start()
network1 = BlockExcNetwork.new( network1 = BlockExcNetwork.new(
switch = switch1) switch = switch1)
switch1.mount(network1) switch1.mount(network1)
@ -169,6 +166,9 @@ suite "Network - Senders":
switch = switch2) switch = switch2)
switch2.mount(network2) switch2.mount(network2)
await switch1.start()
await switch2.start()
await switch1.connect( await switch1.connect(
switch2.peerInfo.peerId, switch2.peerInfo.peerId,
switch2.peerInfo.addrs) switch2.peerInfo.addrs)
@ -271,8 +271,6 @@ suite "Network - Test Limits":
done = newFuture[void]() done = newFuture[void]()
switch1 = newStandardSwitch() switch1 = newStandardSwitch()
switch2 = newStandardSwitch() switch2 = newStandardSwitch()
await switch1.start()
await switch2.start()
network1 = BlockExcNetwork.new( network1 = BlockExcNetwork.new(
switch = switch1, switch = switch1,
@ -283,6 +281,9 @@ suite "Network - Test Limits":
switch = switch2) switch = switch2)
switch2.mount(network2) switch2.mount(network2)
await switch1.start()
await switch2.start()
await switch1.connect( await switch1.connect(
switch2.peerInfo.peerId, switch2.peerInfo.peerId,
switch2.peerInfo.addrs) switch2.peerInfo.addrs)

View File

@ -37,12 +37,11 @@ proc corruptBlocks*(
manifest: Manifest, manifest: Manifest,
blks, bytes: int): Future[seq[int]] {.async.} = blks, bytes: int): Future[seq[int]] {.async.} =
var pos: seq[int] var pos: seq[int]
while true:
if pos.len >= blks:
break
var i = -1 doAssert blks < manifest.len
if (i = Rng.instance.rand(manifest.len - 1); pos.find(i) >= 0): while pos.len < blks:
let i = Rng.instance.rand(manifest.len - 1)
if pos.find(i) >= 0:
continue continue
pos.add(i) pos.add(i)
@ -50,15 +49,12 @@ proc corruptBlocks*(
blk = (await store.getBlock(manifest[i])).tryGet() blk = (await store.getBlock(manifest[i])).tryGet()
bytePos: seq[int] bytePos: seq[int]
while true: doAssert bytes < blk.data.len
if bytePos.len > bytes: while bytePos.len <= bytes:
break let ii = Rng.instance.rand(blk.data.len - 1)
if bytePos.find(ii) >= 0:
var ii = -1
if (ii = Rng.instance.rand(blk.data.len - 1); bytePos.find(ii) >= 0):
continue continue
bytePos.add(ii) bytePos.add(ii)
blk.data[ii] = byte 0 blk.data[ii] = byte 0
return pos return pos