Skip to content

Commit

Permalink
Merge pull request #216 from argentlabs/issue/ens-resolution-wildcard…
Browse files Browse the repository at this point in the history
…-caching

[FIX] ENS wildcard resolution caching
  • Loading branch information
DarthMike authored May 31, 2022
2 parents 706fb02 + 1f5eb4a commit dc06647
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
64 changes: 64 additions & 0 deletions web3sTests/ENS/ENSOffchainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,69 @@ class ENSOffchainTests: XCTestCase {
XCTFail("Expected ens but failed \(error).")
}
}

func testGivenRopstenRegistry_WhenWildcardNOTSupported_AndAddressHasSubdomain_ThenFailsResolving() async {
do {
let nameService = EthereumNameService(client: client!)

_ = try await nameService.resolve(
ens: "1.resolver.eth",
mode: .allowOffchainLookup
)

XCTFail("Expected error")
} catch {
XCTAssertEqual(error as? EthereumNameServiceError, .ensUnknown)
}
}

func testGivenRopstenRegistry_WhenTwoRequestsWithAndWithoutSubdomain_ThenBothResolveCorrectly() async {
let nameService = EthereumNameService(client: client!)

do {
let ens = try await nameService.resolve(
ens: "resolver.eth",
mode: .allowOffchainLookup
)
XCTAssertEqual(EthereumAddress("0x42d63ae25990889e35f215bc95884039ba354115"), ens)
} catch {
XCTFail("Expected ens but failed \(error).")
}
do {
_ = try await nameService.resolve(
ens: "1.resolver.eth",
mode: .allowOffchainLookup
)

XCTFail("Expected error")
} catch {
XCTAssertEqual(error as? EthereumNameServiceError, .ensUnknown)
}
}

func testGivenRopstenRegistry_WhenTwoRequestsWithoutAndWithSubdomain_ThenBothResolveCorrectly() async {
let nameService = EthereumNameService(client: client!)

do {
_ = try await nameService.resolve(
ens: "1.resolver.eth",
mode: .allowOffchainLookup
)

XCTFail("Expected error")
} catch {
XCTAssertEqual(error as? EthereumNameServiceError, .ensUnknown)
}

do {
let ens = try await nameService.resolve(
ens: "resolver.eth",
mode: .allowOffchainLookup
)
XCTAssertEqual(EthereumAddress("0x42d63ae25990889e35f215bc95884039ba354115"), ens)
} catch {
XCTFail("Expected ens but failed \(error).")
}
}
}

10 changes: 4 additions & 6 deletions web3swift/src/ENS/ENSResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@ class ENSResolver {
let address: EthereumAddress
let callResolution: CallResolution
private (set) var supportsWildCard: Bool?
private let mustSupportWilcard: Bool

private let client: EthereumClientProtocol

init(
address: EthereumAddress,
client: EthereumClientProtocol,
callResolution: CallResolution,
supportsWildCard: Bool? = nil,
mustSupportWildcard: Bool = false
supportsWildCard: Bool? = nil
) {
self.address = address
self.callResolution = callResolution
self.client = client
self.supportsWildCard = supportsWildCard
self.mustSupportWilcard = mustSupportWildcard
}

func resolve(
name: String
name: String,
supportingWildcard mustSupportWildCard: Bool
) async throws -> EthereumAddress {
let wildcardResolution: Bool
if let supportsWildCard = self.supportsWildCard {
Expand All @@ -42,7 +40,7 @@ class ENSResolver {
}
self.supportsWildCard = wildcardResolution

if mustSupportWilcard && !wildcardResolution {
if mustSupportWildCard && !wildcardResolution {
// Wildcard name resolution (ENSIP-10)
throw EthereumNameServiceError.ensUnknown
}
Expand Down
12 changes: 6 additions & 6 deletions web3swift/src/ENS/EthereumNameService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ public class EthereumNameService: EthereumNameServiceProtocol {
}
Task {
do {
let resolver = try await getResolver(
let (resolver, supportingWildCard) = try await getResolver(
for: ens,
fullName: ens,
registryAddress: registryAddress,
mode: mode
)

let address = try await resolver.resolve(
name: ens
name: ens,
supportingWildcard: supportingWildCard
)
completion(nil, address)
} catch let error {
Expand Down Expand Up @@ -229,7 +230,7 @@ extension EthereumNameService {
fullName: String,
registryAddress: EthereumAddress,
mode: ResolutionMode
) async throws -> ENSResolver {
) async throws -> (ENSResolver, Bool) {
let function = ENSContracts.ENSRegistryFunctions.resolver(
contract: registryAddress,
parameter: .name(name)
Expand Down Expand Up @@ -263,11 +264,10 @@ extension EthereumNameService {
let resolver = resolversByAddress[resolverAddress] ?? ENSResolver(
address: resolverAddress,
client: client,
callResolution: mode.callResolution(maxRedirects: self.maximumRedirections),
mustSupportWildcard: fullName != name
callResolution: mode.callResolution(maxRedirects: self.maximumRedirections)
)
self.resolversByAddress[resolverAddress] = resolver
return resolver
return (resolver, fullName != name)
} catch {
throw error as? EthereumNameServiceError ?? .ensUnknown
}
Expand Down

0 comments on commit dc06647

Please sign in to comment.