Skip to content

Commit

Permalink
enable resolver by default
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed May 26, 2024
2 parents b11e2b0 + 2fa2c44 commit 03f72a8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
25 changes: 15 additions & 10 deletions libp2p/builders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ runnableExamples:
{.push raises: [].}

import
options, tables, chronos, chronicles, sequtils,
options, tables, chronos, chronicles, sequtils
import
switch, peerid, peerinfo, stream/connection, multiaddress,
crypto/crypto, transports/[transport, tcptransport],
muxers/[muxer, mplex/mplex, yamux/yamux],
Expand All @@ -28,6 +29,7 @@ import
connmanager, upgrademngrs/muxedupgrade, observedaddrmanager,
nameresolving/nameresolver,
errors, utility
import services/wildcardresolverservice

export
switch, peerid, peerinfo, connection, multiaddress, crypto, errors
Expand Down Expand Up @@ -59,6 +61,7 @@ type
rdv: RendezVous
services: seq[Service]
observedAddrManager: ObservedAddrManager
enableWildcardResolver: bool

proc new*(T: type[SwitchBuilder]): T {.public.} =
## Creates a SwitchBuilder
Expand All @@ -85,19 +88,18 @@ proc withPrivateKey*(b: SwitchBuilder, privateKey: PrivateKey): SwitchBuilder {.
b.privKey = some(privateKey)
b

proc withAddress*(b: SwitchBuilder, address: MultiAddress): SwitchBuilder {.public.} =
## | Set the listening address of the switch
proc withAddresses*(b: SwitchBuilder, addresses: seq[MultiAddress], enableWildcardResolver: bool = true): SwitchBuilder {.public.} =
## | Set the listening addresses of the switch
## | Calling it multiple time will override the value

b.addresses = @[address]
b.addresses = addresses
b.enableWildcardResolver = enableWildcardResolver
b

proc withAddresses*(b: SwitchBuilder, addresses: seq[MultiAddress]): SwitchBuilder {.public.} =
## | Set the listening addresses of the switch
proc withAddress*(b: SwitchBuilder, address: MultiAddress, enableWildcardResolver: bool = true): SwitchBuilder {.public.} =
## | Set the listening address of the switch
## | Calling it multiple time will override the value
b.withAddresses(@[address], enableWildcardResolver)

b.addresses = addresses
b

proc withSignedPeerRecord*(b: SwitchBuilder, sendIt = true): SwitchBuilder {.public.} =
b.sendSignedPeerRecord = sendIt
Expand Down Expand Up @@ -261,6 +263,9 @@ proc build*(b: SwitchBuilder): Switch
else:
PeerStore.new(identify)

if b.enableWildcardResolver:
b.services.add(WildcardAddressResolverService.new())

let switch = newSwitch(
peerInfo = peerInfo,
transports = transports,
Expand Down Expand Up @@ -312,7 +317,7 @@ proc newStandardSwitch*(
let addrs = when addrs is MultiAddress: @[addrs] else: addrs
var b = SwitchBuilder
.new()
.withAddresses(addrs)
.withAddresses(addrs, true)
.withRng(rng)
.withSignedPeerRecord(sendSignedPeerRecord)
.withMaxConnections(maxConnections)
Expand Down
8 changes: 3 additions & 5 deletions libp2p/muxers/yamux/yamux.nim
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ type
closedRemotely: Future[void].Raising([])
closedLocally: bool
receivedData: AsyncEvent
returnedEof: bool

proc `$`(channel: YamuxChannel): string =
result = if channel.conn.dir == Out: "=> " else: "<= "
Expand Down Expand Up @@ -204,8 +203,8 @@ proc remoteClosed(channel: YamuxChannel) {.async: (raises: []).} =

method closeImpl*(channel: YamuxChannel) {.async: (raises: []).} =
if not channel.closedLocally:
trace "Closing yamux channel locally", streamId = channel.id, conn = channel.conn
channel.closedLocally = true
channel.isEof = true

if not channel.isReset and channel.sendQueue.len == 0:
try: await channel.conn.write(YamuxHeader.data(channel.id, 0, {Fin}))
Expand Down Expand Up @@ -273,17 +272,16 @@ method readOnce*(
newLPStreamClosedError()
else:
newLPStreamConnDownError()
if channel.returnedEof:
if channel.isEof:
raise newLPStreamRemoteClosedError()
if channel.recvQueue.len == 0:
channel.receivedData.clear()
try: # https://github.com/status-im/nim-chronos/issues/516
discard await race(channel.closedRemotely, channel.receivedData.wait())
except ValueError: raiseAssert("Futures list is not empty")
if channel.closedRemotely.completed() and channel.recvQueue.len == 0:
channel.returnedEof = true
channel.isEof = true
return 0
return 0 # we return 0 to indicate that the channel is closed for reading from now on

let toRead = min(channel.recvQueue.len, nbytes)

Expand Down
2 changes: 1 addition & 1 deletion libp2p/protocols/ping.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ method init*(p: Ping) =
trace "handling ping", conn
var buf: array[PingSize, byte]
await conn.readExactly(addr buf[0], PingSize)
trace "echoing ping", conn
trace "echoing ping", conn, pingData = @buf
await conn.write(@buf)
if not isNil(p.pingHandler):
await p.pingHandler(conn.peerId)
Expand Down
1 change: 0 additions & 1 deletion libp2p/services/wildcardresolverservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ proc getAddresses(addrFamily: AddressFamily): seq[InterfaceAddress] =

proc new*(
T: typedesc[WildcardAddressResolverService],
scheduleInterval: Opt[Duration] = Opt.none(Duration),
networkInterfaceProvider: NetworkInterfaceProvider = getAddresses,
): T =
## This procedure initializes a new `WildcardAddressResolverService` with the provided network interface provider.
Expand Down
1 change: 0 additions & 1 deletion tests/testrelayv2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ suite "Circuit Relay V2":
await sleepAsync(chronos.timer.seconds(ttl + 1))

expect(DialFailedError):
check: conn.atEof()
await conn.close()
await src.connect(rel.peerInfo.peerId, rel.peerInfo.addrs)
conn = await src.dial(dst.peerInfo.peerId, @[ addrs ], customProtoCodec)
Expand Down
21 changes: 21 additions & 0 deletions tests/testyamux.nim
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,24 @@ suite "Yamux":
expect LPStreamClosedError: discard await streamA.readLp(100)
blocker.complete()
await streamA.close()

asyncTest "Peer must be able to read from stream after closing it for writing":
mSetup()

yamuxb.streamHandler = proc(conn: Connection) {.async: (raises: []).} =
try:
check (await conn.readLp(100)) == fromHex("1234")
except CancelledError, LPStreamError:
return
try:
await conn.writeLp(fromHex("5678"))
except CancelledError, LPStreamError:
return
await conn.close()

let streamA = await yamuxa.newStream()
check streamA == yamuxa.getStreams()[0]

await streamA.writeLp(fromHex("1234"))
await streamA.close()
check (await streamA.readLp(100)) == fromHex("5678")
2 changes: 1 addition & 1 deletion tests/transport-interop/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ COPY . nim-libp2p/

RUN \
cd nim-libp2p && \
nim c --skipProjCfg --skipParentCfg --NimblePath:./nimbledeps/pkgs -p:nim-libp2p -d:chronicles_log_level=WARN --threads:off ./tests/transport-interop/main.nim
nim c --skipProjCfg --skipParentCfg --NimblePath:./nimbledeps/pkgs -p:nim-libp2p -d:chronicles_log_level=WARN -d:chronicles_default_output_device=stderr --threads:off ./tests/transport-interop/main.nim

ENTRYPOINT ["/app/nim-libp2p/tests/transport-interop/main"]

0 comments on commit 03f72a8

Please sign in to comment.