Skip to content

Commit

Permalink
Advancing the test
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Nov 6, 2023
1 parent 2364698 commit 88d5f21
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions hole-punch-interop/impl/nim/hole_punching.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import libp2p/[builders,
import libp2p/protocols/connectivity/relay/client as rclient
import tests/stubs/autonatclientstub
import libp2p/protocols/ping
import libp2p/utils/heartbeat

proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch =
let rng = newRng()
var builder = SwitchBuilder.new()
.withRng(rng)
.withAddresses(@[ MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet() ])
.withObservedAddrManager(ObservedAddrManager.new(minCount = 1))
.withTcpTransport()
.withTcpTransport({ServerFlags.TcpNoDelay})
.withYamux()
.withAutonat()
.withNoise()
Expand All @@ -34,6 +35,12 @@ proc createSwitch(r: Relay = nil, hpService: Service = nil): Switch =
s.mount(Ping.new(rng=rng))
return s


proc ping(conn: Connection) {.async.} =
let pingProtocol = Ping.new()
heartbeat "Ping background proc", 30.seconds:
discard await pingProtocol.ping(conn)

proc main() {.async.} =
let relayClient = RelayClient.new()
let autoRelayService = AutoRelayService.new(1, relayClient, nil, newRng())
Expand All @@ -48,31 +55,46 @@ proc main() {.async.} =
let
isListener = getEnv("MODE") == "listen"
redisClient = open("redis", 6379.Port)

debug "Connected to redis"

if isListener:
let relayAddr =
try:
redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 20)
except Exception as e:
raise newException(CatchableError, e.msg)
debug "Got relay address"
let relayAddr =
try:
redisClient.bLPop(@["RELAY_TCP_ADDRESS"], 0)
except Exception as e:
raise newException(CatchableError, e.msg)
let relayMA = MultiAddress.init(relayAddr[1]).tryGet()
debug "Got relay address", relayMA

let relayMA = MultiAddress.init(relayAddr[1]).tryGet()
if isListener:
let relayId = await switch.connect(relayMA)
debug "Connected to relay", relayId

await sleepAsync(20.seconds)
let conn = await switch.dial(relayId, @[relayMA], PingCodec)
asyncSpawn conn.ping()

while switch.peerInfo.addrs.len == 0:
debug "Waiting for addresses"
await sleepAsync(200.milliseconds)

let listenerPeerId = switch.peerInfo.peerId
discard redisClient.rPush("LISTEN_CLIENT_PEER_ID", $listenerPeerId)
debug "Addresses", addrs = $(switch.peerInfo.addrs)
debug "Pushed listener client peer id to redis", listenerPeerId
await sleepAsync(2.minutes)
await conn.close()
else:
let listenerId =
try:
redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 20)
except Exception as e:
raise newException(CatchableError, e.msg)
let listenerId =
try:
PeerId.init(redisClient.bLPop(@["LISTEN_CLIENT_PEER_ID"], 0)[1]).tryGet()
except Exception as e:
raise newException(CatchableError, e.msg)
debug "Got listener peer id", listenerId
let listenerRelayAddrStr = $relayMA & "/p2p-circuit"
debug "Listener relay address string", listenerRelayAddrStr
let listenerRelayAddr = MultiAddress.init(listenerRelayAddrStr).tryGet()
debug "Dialing listener relay address", listenerRelayAddr
await switch.connect(listenerId, @[listenerRelayAddr])
await sleepAsync(2.minutes)
# var i = 1
# var flags = Flags(transport: "tcp")
# while i < paramCount():
Expand All @@ -98,7 +120,7 @@ proc main() {.async.} =
# else:
# await runClient(flags)
try:
discard waitFor(main().withTimeout(2.minutes))
discard waitFor(main().withTimeout(4.minutes))
except Exception as e:
error "Unexpected error", msg = e.msg
quit(1)

0 comments on commit 88d5f21

Please sign in to comment.