formatting

This commit is contained in:
Diego 2024-05-23 17:07:18 +02:00
parent 980950b147
commit 03668a3e90
No known key found for this signature in database
GPG Key ID: C9DAC9BF68D1F806
2 changed files with 41 additions and 43 deletions

View File

@ -32,7 +32,8 @@ type
## and the machine has 2 interfaces with IPs 172.217.11.174 and 64.233.177.113, the address mapper will
## expand the wildcard address to 172.217.11.174:4001 and 64.233.177.113:4001.
NetworkInterfaceProvider* = proc (addrFamily: AddressFamily): seq[InterfaceAddress] {.gcsafe, raises: [].}
NetworkInterfaceProvider* =
proc(addrFamily: AddressFamily): seq[InterfaceAddress] {.gcsafe, raises: [].}
proc isLoopbackOrUp(networkInterface: NetworkInterface): bool =
if (networkInterface.ifType == IfSoftwareLoopback) or
@ -70,9 +71,7 @@ proc new*(
##
## Returns:
## - A new instance of `WildcardAddressResolverService`.
return T(
networkInterfaceProvider: networkInterfaceProvider,
)
return T(networkInterfaceProvider: networkInterfaceProvider)
proc getProtocolArgument*(ma: MultiAddress, codec: MultiCodec): MaResult[seq[byte]] =
var buffer: seq[byte]
@ -87,9 +86,7 @@ proc getProtocolArgument*(ma: MultiAddress, codec: MultiCodec): MaResult[seq[byt
err("Multiaddress codec has not been found")
proc getWildcardMultiAddresses(
interfaceAddresses: seq[InterfaceAddress],
protocol: Protocol,
port: Port,
interfaceAddresses: seq[InterfaceAddress], protocol: Protocol, port: Port
): seq[MultiAddress] =
var addresses: seq[MultiAddress]
for ifaddr in interfaceAddresses:
@ -119,8 +116,7 @@ proc getWildcardAddress(
return addresses
proc expandWildcardAddresses(
networkInterfaceProvider: NetworkInterfaceProvider,
listenAddrs: seq[MultiAddress],
networkInterfaceProvider: NetworkInterfaceProvider, listenAddrs: seq[MultiAddress]
): seq[MultiAddress] =
var addresses: seq[MultiAddress]
# In this loop we expand bounded addresses like `0.0.0.0` and `::` to list of interface addresses.
@ -171,9 +167,7 @@ method setup*(
listenAddrs: seq[MultiAddress]
): Future[seq[MultiAddress]] {.async.} =
echo listenAddrs
return expandWildcardAddresses(
self.networkInterfaceProvider, listenAddrs
)
return expandWildcardAddresses(self.networkInterfaceProvider, listenAddrs)
debug "Setting up WildcardAddressResolverService"
let hasBeenSetup = await procCall Service(self).setup(switch)

View File

@ -40,28 +40,29 @@ proc getAddressesMock(
proc createSwitch(svc: Service): Switch =
SwitchBuilder
.new()
.withRng(newRng())
.withAddresses(
@[
MultiAddress.init("/ip4/0.0.0.0/tcp/0/").tryGet(),
MultiAddress.init("/ip6/::/tcp/0/").tryGet(),
]
)
.withTcpTransport()
.withMplex()
.withNoise()
.withServices(@[svc])
.build()
.new()
.withRng(newRng())
.withAddresses(
@[
MultiAddress.init("/ip4/0.0.0.0/tcp/0/").tryGet(),
MultiAddress.init("/ip6/::/tcp/0/").tryGet(),
]
)
.withTcpTransport()
.withMplex()
.withNoise()
.withServices(@[svc])
.build()
suite "WildcardAddressResolverService":
teardown:
checkTrackers()
proc setupWildcardService(): Future[tuple[svc: Service, switch: Switch, tcpIp4: MultiAddress, tcpIp6: MultiAddress]] {.async.} =
let svc: Service = WildcardAddressResolverService.new(
networkInterfaceProvider = getAddressesMock
)
proc setupWildcardService(): Future[
tuple[svc: Service, switch: Switch, tcpIp4: MultiAddress, tcpIp6: MultiAddress]
] {.async.} =
let svc: Service =
WildcardAddressResolverService.new(networkInterfaceProvider = getAddressesMock)
let switch = createSwitch(svc)
await switch.start()
let tcpIp4 = switch.peerInfo.addrs[0][multiCodec("tcp")].get # tcp port for ip4
@ -70,19 +71,22 @@ suite "WildcardAddressResolverService":
asyncTest "WildcardAddressResolverService must resolve wildcard addresses and stop doing so when stopped":
let (svc, switch, tcpIp4, tcpIp6) = await setupWildcardService()
check switch.peerInfo.addrs == @[
MultiAddress.init("/ip4/0.0.0.0" & $tcpIp4).get,
MultiAddress.init("/ip6/::" & $tcpIp6).get,
]
check switch.peerInfo.addrs ==
@[
MultiAddress.init("/ip4/0.0.0.0" & $tcpIp4).get,
MultiAddress.init("/ip6/::" & $tcpIp6).get,
]
await svc.run(switch)
check switch.peerInfo.addrs == @[
MultiAddress.init("/ip4/127.0.0.1" & $tcpIp4).get,
MultiAddress.init("/ip4/192.168.1.22" & $tcpIp4).get,
MultiAddress.init("/ip6/::1" & $tcpIp6).get,
MultiAddress.init("/ip6/fe80::1" & $tcpIp6).get,
]
check switch.peerInfo.addrs ==
@[
MultiAddress.init("/ip4/127.0.0.1" & $tcpIp4).get,
MultiAddress.init("/ip4/192.168.1.22" & $tcpIp4).get,
MultiAddress.init("/ip6/::1" & $tcpIp6).get,
MultiAddress.init("/ip6/fe80::1" & $tcpIp6).get,
]
await switch.stop()
check switch.peerInfo.addrs == @[
MultiAddress.init("/ip4/0.0.0.0" & $tcpIp4).get,
MultiAddress.init("/ip6/::" & $tcpIp6).get,
]
check switch.peerInfo.addrs ==
@[
MultiAddress.init("/ip4/0.0.0.0" & $tcpIp4).get,
MultiAddress.init("/ip6/::" & $tcpIp6).get,
]