Skip to content

Commit

Permalink
Apply nph
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Oct 16, 2024
1 parent 57ed560 commit 806fde1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
25 changes: 14 additions & 11 deletions libp2p/nameresolving/dnsresolver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ proc questionToBuf(address: string, kind: QKind): seq[byte] =

proc getDnsResponse(
dnsServer: TransportAddress, address: string, kind: QKind
): Future[Response] {.async: (raises: [
CancelledError, IOError, OSError, TransportError, ValueError]).} =
): Future[Response] {.
async: (raises: [CancelledError, IOError, OSError, TransportError, ValueError])
.} =
var sendBuf = questionToBuf(address, kind)

if sendBuf.len == 0:
Expand Down Expand Up @@ -98,18 +99,18 @@ proc getDnsResponse(
await sock.closeWait()

method resolveIp*(
self: DnsResolver,
address: string,
port: Port,
domain: Domain = Domain.AF_UNSPEC
self: DnsResolver, address: string, port: Port, domain: Domain = Domain.AF_UNSPEC
): Future[seq[TransportAddress]] {.
async: (raises: [CancelledError, TransportAddressError]).} =
trace "Resolving IP using DNS",
address, servers = self.nameServers.mapIt($it), domain
async: (raises: [CancelledError, TransportAddressError])
.} =
trace "Resolving IP using DNS", address, servers = self.nameServers.mapIt($it), domain
for _ in 0 ..< self.nameServers.len:
let server = self.nameServers[0]
var responseFutures: seq[Future[Response].Raising([
CancelledError, IOError, OSError, TransportError, ValueError])]
var responseFutures: seq[
Future[Response].Raising(
[CancelledError, IOError, OSError, TransportError, ValueError]
)
]
if domain == Domain.AF_INET or domain == Domain.AF_UNSPEC:
responseFutures.add(getDnsResponse(server, address, A))

Expand All @@ -128,6 +129,7 @@ method resolveIp*(
info "Failed to query DNS", address, error = e.msg
resolveFailed = true
break

for fut in responseFutures:
try:
let resp = await fut
Expand Down Expand Up @@ -172,6 +174,7 @@ method resolveTxt*(
self.nameServers.add(self.nameServers[0])
self.nameServers.delete(0)
continue

try:
let response = await getDnsResponse(server, address, TXT)
trace "Got TXT response",
Expand Down
8 changes: 3 additions & 5 deletions libp2p/nameresolving/mockresolver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ type MockResolver* = ref object of NameResolver
ipResponses*: Table[(string, bool), seq[string]]

method resolveIp*(
self: MockResolver,
address: string,
port: Port,
domain: Domain = Domain.AF_UNSPEC
self: MockResolver, address: string, port: Port, domain: Domain = Domain.AF_UNSPEC
): Future[seq[TransportAddress]] {.
async: (raises: [CancelledError, TransportAddressError]).} =
async: (raises: [CancelledError, TransportAddressError])
.} =
var res: seq[TransportAddress]

if domain == Domain.AF_INET or domain == Domain.AF_UNSPEC:
Expand Down
22 changes: 10 additions & 12 deletions libp2p/nameresolving/nameresolver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ method resolveTxt*(
raiseAssert "Not implemented!"

method resolveIp*(
self: NameResolver,
address: string,
port: Port,
domain: Domain = Domain.AF_UNSPEC
self: NameResolver, address: string, port: Port, domain: Domain = Domain.AF_UNSPEC
): Future[seq[TransportAddress]] {.
async: (raises: [CancelledError, TransportAddressError]), base.} =
async: (raises: [CancelledError, TransportAddressError]), base
.} =
## Resolve the specified address
raiseAssert "Not implemented!"

Expand All @@ -45,12 +43,10 @@ proc getHostname*(ma: MultiAddress): string =
""

proc resolveOneAddress(
self: NameResolver,
ma: MultiAddress,
domain: Domain = Domain.AF_UNSPEC,
prefix = ""
self: NameResolver, ma: MultiAddress, domain: Domain = Domain.AF_UNSPEC, prefix = ""
): Future[seq[MultiAddress]] {.
async: (raises: [CancelledError, MaError, TransportAddressError]).} =
async: (raises: [CancelledError, MaError, TransportAddressError])
.} =
# Resolve a single address
let portPart = ma[1].valueOr:
raise maErr error
Expand Down Expand Up @@ -80,7 +76,8 @@ proc resolveOneAddress(
proc resolveDnsAddr*(
self: NameResolver, ma: MultiAddress, depth: int = 0
): Future[seq[MultiAddress]] {.
async: (raises: [CancelledError, MaError, TransportAddressError]).} =
async: (raises: [CancelledError, MaError, TransportAddressError])
.} =
if not DNSADDR.matchPartial(ma):
return @[ma]

Expand Down Expand Up @@ -127,7 +124,8 @@ proc resolveDnsAddr*(
proc resolveMAddress*(
self: NameResolver, address: MultiAddress
): Future[seq[MultiAddress]] {.
async: (raises: [CancelledError, MaError, TransportAddressError]).} =
async: (raises: [CancelledError, MaError, TransportAddressError])
.} =
var res = initOrderedSet[MultiAddress]()
if not DNS.matchPartial(address):
res.incl(address)
Expand Down

0 comments on commit 806fde1

Please sign in to comment.