Skip to content

Commit

Permalink
Merge branch 'unstable' into autonat
Browse files Browse the repository at this point in the history
  • Loading branch information
Menduist authored Aug 3, 2022
2 parents 8f1ed87 + 2d86463 commit 5c1f4c0
Show file tree
Hide file tree
Showing 85 changed files with 435 additions and 345 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
cpu: amd64
#- os: windows
#cpu: i386
branch: [version-1-2, devel]
branch: [version-1-2, version-1-6]
include:
- target:
os: linux
Expand Down
2 changes: 0 additions & 2 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
if dirExists("nimbledeps/pkgs"):
switch("NimblePath", "nimbledeps/pkgs")

when (NimMajor, NimMinor) > (1, 2):
switch("hint", "XCannotRaiseY:off")
# begin Nimble config (version 1)
when fileExists("nimble.paths"):
include "nimble.paths"
Expand Down
5 changes: 4 additions & 1 deletion libp2p/builders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ runnableExamples:
# etc
.build()

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import
options, tables, chronos, chronicles, sequtils,
Expand Down
5 changes: 4 additions & 1 deletion libp2p/cid.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

## This module implementes CID (Content IDentifier).

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import tables, hashes
import multibase, multicodec, multihash, vbuffer, varint
Expand Down
111 changes: 32 additions & 79 deletions libp2p/connmanager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import std/[options, tables, sequtils, sets]
import pkg/[chronos, chronicles, metrics]
Expand All @@ -30,9 +33,6 @@ const
type
TooManyConnectionsError* = object of LPError

ConnProvider* = proc(): Future[Connection]
{.gcsafe, closure, raises: [Defect].}

ConnEventKind* {.pure.} = enum
Connected, # A connection was made and securely upgraded - there may be
# more than one concurrent connection thus more than one upgrade
Expand Down Expand Up @@ -81,6 +81,10 @@ type
peerEvents: array[PeerEventKind, OrderedSet[PeerEventHandler]]
peerStore*: PeerStore

ConnectionSlot* = object
connManager: ConnManager
direction: Direction

proc newTooManyConnectionsError(): ref TooManyConnectionsError {.inline.} =
result = newException(TooManyConnectionsError, "Too many connections")

Expand Down Expand Up @@ -401,90 +405,39 @@ proc storeConn*(c: ConnManager, conn: Connection)
trace "Stored connection",
conn, direction = $conn.dir, connections = c.conns.len

proc trackConn(c: ConnManager,
provider: ConnProvider,
sema: AsyncSemaphore):
Future[Connection] {.async.} =
var conn: Connection
try:
conn = await provider()

if isNil(conn):
return

trace "Got connection", conn

proc semaphoreMonitor() {.async.} =
try:
await conn.join()
except CatchableError as exc:
trace "Exception in semaphore monitor, ignoring", exc = exc.msg

sema.release()

asyncSpawn semaphoreMonitor()
except CatchableError as exc:
trace "Exception tracking connection", exc = exc.msg
if not isNil(conn):
await conn.close()

raise exc

return conn

proc trackIncomingConn*(c: ConnManager,
provider: ConnProvider):
Future[Connection] {.async.} =
## await for a connection slot before attempting
## to call the connection provider
##

var conn: Connection
try:
trace "Tracking incoming connection"
await c.inSema.acquire()
conn = await c.trackConn(provider, c.inSema)
if isNil(conn):
trace "Couldn't acquire connection, releasing semaphore slot", dir = $Direction.In
c.inSema.release()

return conn
except CatchableError as exc:
trace "Exception tracking connection", exc = exc.msg
c.inSema.release()
raise exc

proc trackOutgoingConn*(c: ConnManager,
provider: ConnProvider,
forceDial = false):
Future[Connection] {.async.} =
## try acquiring a connection if all slots
## are already taken, raise TooManyConnectionsError
## exception
##

trace "Tracking outgoing connection", count = c.outSema.count,
max = c.outSema.size
proc getIncomingSlot*(c: ConnManager): Future[ConnectionSlot] {.async.} =
await c.inSema.acquire()
return ConnectionSlot(connManager: c, direction: In)

proc getOutgoingSlot*(c: ConnManager, forceDial = false): Future[ConnectionSlot] {.async.} =
if forceDial:
c.outSema.forceAcquire()
elif not c.outSema.tryAcquire():
trace "Too many outgoing connections!", count = c.outSema.count,
max = c.outSema.size
raise newTooManyConnectionsError()
return ConnectionSlot(connManager: c, direction: Out)

var conn: Connection
try:
conn = await c.trackConn(provider, c.outSema)
if isNil(conn):
trace "Couldn't acquire connection, releasing semaphore slot", dir = $Direction.Out
c.outSema.release()
proc release*(cs: ConnectionSlot) =
if cs.direction == In:
cs.connManager.inSema.release()
else:
cs.connManager.outSema.release()

return conn
except CatchableError as exc:
trace "Exception tracking connection", exc = exc.msg
c.outSema.release()
raise exc
proc trackConnection*(cs: ConnectionSlot, conn: Connection) =
if isNil(conn):
cs.release()
return

proc semaphoreMonitor() {.async.} =
try:
await conn.join()
except CatchableError as exc:
trace "Exception in semaphore monitor, ignoring", exc = exc.msg

cs.release()

asyncSpawn semaphoreMonitor()

proc storeMuxer*(c: ConnManager,
muxer: Muxer,
Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/chacha20poly1305.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

# RFC @ https://tools.ietf.org/html/rfc7539

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import bearssl/blockx
from stew/assign2 import assign
Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/crypto.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
# those terms.

## This module implements Public Key and Private Key interface for libp2p.
{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

from strutils import split, strip, cmpIgnoreCase

Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/curve25519.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

# RFC @ https://tools.ietf.org/html/rfc7748

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import bearssl/[ec, rand, hash]
import stew/results
Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/ecnist.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
## BearSSL library <https://bearssl.org/>
## Copyright(C) 2018 Thomas Pornin <[email protected]>.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import bearssl/[ec, rand, hash]
# We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures.
Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/ed25519/ed25519.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
## This code is a port of the public domain, "ref10" implementation of ed25519
## from SUPERCOP.

{.push raises: Defect.}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import bearssl/rand
import constants
Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/hkdf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

# https://tools.ietf.org/html/rfc5869

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import nimcrypto
import bearssl/[kdf, rand, hash]
Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/minasn1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

## This module implements minimal ASN.1 encoding/decoding primitives.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import stew/[endians2, results, ctops]
export results
Expand Down
6 changes: 5 additions & 1 deletion libp2p/crypto/rsa.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
## BearSSL library <https://bearssl.org/>
## Copyright(C) 2018 Thomas Pornin <[email protected]>.

{.push raises: Defect.}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import bearssl/[rsa, rand, hash]
import minasn1
import stew/[results, ctops]
Expand Down
5 changes: 4 additions & 1 deletion libp2p/crypto/secp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import bearssl/rand
import
Expand Down
5 changes: 4 additions & 1 deletion libp2p/daemon/daemonapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

## This module implementes API for `go-libp2p-daemon`.
import std/[os, osproc, strutils, tables, strtabs, sequtils]
Expand Down
5 changes: 4 additions & 1 deletion libp2p/daemon/transpool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

## This module implements Pool of StreamTransport.
import chronos
Expand Down
5 changes: 4 additions & 1 deletion libp2p/dial.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
# This file may not be copied, modified, or distributed except according to
# those terms.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}

import chronos
import peerid,
Expand Down
19 changes: 10 additions & 9 deletions libp2p/dialer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ proc dialAndUpgrade(
let dialed = try:
libp2p_total_dial_attempts.inc()
await transport.dial(hostname, a)
except TooManyConnectionsError as exc:
trace "Connection limit reached!"
raise exc
except CancelledError as exc:
debug "Dialing canceled", msg = exc.msg, peerId
raise exc
Expand All @@ -91,6 +88,7 @@ proc dialAndUpgrade(
except CatchableError as exc:
# If we failed to establish the connection through one transport,
# we won't succeeded through another - no use in trying again
# TODO we should try another address though
await dialed.close()
debug "Upgrade failed", msg = exc.msg, peerId
if exc isnot CancelledError:
Expand Down Expand Up @@ -129,15 +127,18 @@ proc internalConnect(
trace "Reusing existing connection", conn, direction = $conn.dir
return conn

conn = await self.connManager.trackOutgoingConn(
() => self.dialAndUpgrade(peerId, addrs),
forceDial
)
let slot = await self.connManager.getOutgoingSlot(forceDial)
conn =
try:
await self.dialAndUpgrade(peerId, addrs)
except CatchableError as exc:
slot.release()
raise exc
slot.trackConnection(conn)
if isNil(conn): # None of the addresses connected
raise newException(DialFailedError, "Unable to establish outgoing link")

# We already check for this in Connection manager
# but a disconnect could have happened right after
# A disconnect could have happened right after
# we've added the connection so we check again
# to prevent races due to that.
if conn.closed() or conn.atEof():
Expand Down
5 changes: 4 additions & 1 deletion libp2p/multiaddress.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

## This module implements MultiAddress.

{.push raises: [Defect].}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
{.push public.}

import pkg/chronos
Expand Down
Loading

0 comments on commit 5c1f4c0

Please sign in to comment.