Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Remove potential deadlock when using async-await. Provide network at client initialization instead #336

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions web3sTests/Client/EthereumClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EthereumClientTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
account = try? EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: TestConfig.privateKey))
}

Expand Down Expand Up @@ -435,14 +435,14 @@ class EthereumWebSocketClientTests: EthereumClientTests {

override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)

}
#if os(Linux)
// On Linux some tests are fail. Need investigation
#else
func testWebSocketNoAutomaticOpen() {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false))
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false), network: TestConfig.network)

guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
Expand All @@ -453,7 +453,7 @@ class EthereumWebSocketClientTests: EthereumClientTests {
}

func testWebSocketConnect() {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false))
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: .init(automaticOpen: false), network: TestConfig.network)

guard let client = client as? EthereumWebSocketClient else {
XCTFail("Expected client to be EthereumWebSocketClient")
Expand Down
4 changes: 2 additions & 2 deletions web3sTests/Contract/ABIEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ABIEventTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
}

func test_givenEventWithData4_ItParsesCorrectly() async {
Expand Down Expand Up @@ -60,7 +60,7 @@ class ABIEventTests: XCTestCase {
class ABIEventWebSocketTests: ABIEventTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}

Expand Down
4 changes: 2 additions & 2 deletions web3sTests/ENS/ENSOffchainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ENSOffchainTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
}

func testDNSEncode() {
Expand Down Expand Up @@ -149,6 +149,6 @@ class ENSOffchainTests: XCTestCase {
class ENSOffchainWebSocketTests: ENSOffchainTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}
4 changes: 2 additions & 2 deletions web3sTests/ENS/ENSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class ENSTests: XCTestCase {
class ENSWebSocketTests: ENSTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
mainnetClient = EthereumWebSocketClient(url: URL(string: TestConfig.wssMainnetUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
mainnetClient = EthereumWebSocketClient(url: URL(string: TestConfig.wssMainnetUrl)!, configuration: TestConfig.webSocketConfig, network: .mainnet)
}
}
4 changes: 2 additions & 2 deletions web3sTests/ERC1271/ERC1271Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ERC1271Tests: XCTestCase {
override func setUp() {
super.setUp()
if self.client == nil {
self.client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
self.client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
}
self.erc1271 = ERC1271(client: self.client)
}
Expand Down Expand Up @@ -105,7 +105,7 @@ final class ERC1271WebSocketTests: ERC1271Tests {

override func setUp() {
if self.client == nil {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!)
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, network: TestConfig.network)
}
super.setUp()
}
Expand Down
4 changes: 2 additions & 2 deletions web3sTests/ERC165/ERC165Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ERC165Tests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
erc165 = ERC165(client: client)
}

Expand Down Expand Up @@ -54,6 +54,6 @@ class ERC165Tests: XCTestCase {
class ERC165WebSocketTests: ERC165Tests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}
4 changes: 2 additions & 2 deletions web3sTests/ERC20/ERC20Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ERC20Tests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
erc20 = ERC20(client: client!)
}

Expand Down Expand Up @@ -114,6 +114,6 @@ class ERC20Tests: XCTestCase {
class ERC20WebSocketTests: ERC20Tests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}
12 changes: 6 additions & 6 deletions web3sTests/ERC721/ERC721Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ERC721Tests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
erc721 = ERC721(client: client)
}

Expand Down Expand Up @@ -111,7 +111,7 @@ class ERC721MetadataTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
erc721 = ERC721Metadata(client: client, metadataSession: URLSession.shared)
}

Expand Down Expand Up @@ -163,7 +163,7 @@ class ERC721EnumerableTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
erc721 = ERC721Enumerable(client: client)
}

Expand Down Expand Up @@ -211,20 +211,20 @@ class ERC721EnumerableTests: XCTestCase {
class ERC721WebSocketTests: ERC721Tests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}

class ERC721MetadataWebSocketTests: ERC721MetadataTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}

class ERC721EnumerableWebSocketTests: ERC721EnumerableTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}
4 changes: 2 additions & 2 deletions web3sTests/Multicall/MulticallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MulticallTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
multicall = Multicall(client: client!)
}

Expand Down Expand Up @@ -83,6 +83,6 @@ class MulticallTests: XCTestCase {
class MulticallWebSocketTests: MulticallTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}
4 changes: 2 additions & 2 deletions web3sTests/OffchainLookup/OffchainLookupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class OffchainLookupTests: XCTestCase {

override func setUp() {
super.setUp()
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
account = try? EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: TestConfig.privateKey))
}

Expand Down Expand Up @@ -331,6 +331,6 @@ private func expectedResponse(
class OffchainLookupWebSocketTests: OffchainLookupTests {
override func setUp() {
super.setUp()
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig)
client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, configuration: TestConfig.webSocketConfig, network: TestConfig.network)
}
}
4 changes: 2 additions & 2 deletions web3sTests/SIWE/SIWETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SIWETests: XCTestCase {
override func setUp() {
super.setUp()
if self.client == nil {
self.client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
self.client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!, network: TestConfig.network)
}
self.verifier = SiweVerifier(client: self.client)
}
Expand Down Expand Up @@ -58,7 +58,7 @@ final class SIWEWebSocketTests: SIWETests {

override func setUp() {
if self.client == nil {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!)
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, network: TestConfig.network)
}
super.setUp()
}
Expand Down
2 changes: 1 addition & 1 deletion web3sTests/SIWE/SiweVerifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ final class SiweVerifierWebSocketTests: SiweVerifierTests {

override func setUp() {
if self.client == nil {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!)
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!, network: TestConfig.network)
}
super.setUp()
}
Expand Down
7 changes: 5 additions & 2 deletions web3sTests/TestConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ struct TestConfig {

static let webSocketConfig = WebSocketConfiguration(maxFrameSize: 1_000_000)

static let network = EthereumNetwork.goerli

enum ZKSync {
static let chainId = 280
static let clientURL = URL(string: "https://zksync2-testnet.zksync.dev")!
static let chainId = 280
static let network = EthereumNetwork.custom("\(280)")
static let clientURL = URL(string: "https://zksync2-testnet.zksync.dev")!
}
}

Expand Down
2 changes: 1 addition & 1 deletion web3sTests/ZKSync/EthereumClient+ZKSyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import BigInt

final class EthereumClientZKSyncTests: XCTestCase {
let eoaAccount = try! EthereumAccount(keyStorage: TestEthereumKeyStorage(privateKey: TestConfig.privateKey))
let client = ZKSyncClient(url: TestConfig.ZKSync.clientURL)
let client = ZKSyncClient(url: TestConfig.ZKSync.clientURL, network: TestConfig.ZKSync.network)
var eoaEthTransfer = ZKSyncTransaction(
from: .init(TestConfig.publicKey),
to: .init("0x64d0eA4FC60f27E74f1a70Aa6f39D403bBe56793"),
Expand Down
23 changes: 2 additions & 21 deletions web3swift/src/Client/BaseEthereumClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,18 @@ open class BaseEthereumClient: EthereumClientProtocol {

private let logger: Logger

public var network: EthereumNetwork?
public var network: EthereumNetwork

public init(
networkProvider: NetworkProviderProtocol,
url: URL,
logger: Logger? = nil,
network: EthereumNetwork?
network: EthereumNetwork
) {
self.url = url
self.networkProvider = networkProvider
self.logger = logger ?? Logger(label: "web3.swift.eth-client")
self.network = network

if network == nil {
let semaphore = DispatchSemaphore(value: 0)
Task {
self.network = await fetchNetwork()
semaphore.signal()
}
semaphore.wait()
}
}

private func fetchNetwork() async -> EthereumNetwork? {
do {
return try await net_version()
} catch {
logger.warning("Client has no network: \(error.localizedDescription)")
}

return nil
}

func failureHandler(_ error: Error) -> EthereumClientError {
Expand Down
2 changes: 1 addition & 1 deletion web3swift/src/Client/HTTP/EthereumHttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class EthereumHttpClient: BaseEthereumClient {
url: URL,
sessionConfig: URLSessionConfiguration = URLSession.shared.configuration,
logger: Logger? = nil,
network: EthereumNetwork? = nil
network: EthereumNetwork
) {
let networkQueue = OperationQueue()
networkQueue.name = "web3swift.client.networkQueue"
Expand Down
2 changes: 0 additions & 2 deletions web3swift/src/Client/Protocols/EthereumClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public enum CallResolution {
// MARK: EthereumClient (HTTP or Websocket)

public protocol EthereumClientProtocol: EthereumRPCProtocol, AnyObject {
var network: EthereumNetwork? { get }

// Legacy result-based API
func net_version(completionHandler: @escaping (Result<EthereumNetwork, EthereumClientError>) -> Void)
func eth_gasPrice(completionHandler: @escaping (Result<BigUInt, EthereumClientError>) -> Void)
Expand Down
4 changes: 2 additions & 2 deletions web3swift/src/Client/Protocols/EthereumProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum EthereumClientError: Error, Equatable {

public protocol EthereumRPCProtocol: AnyObject {
var networkProvider: NetworkProviderProtocol { get }
var network: EthereumNetwork? { get }
var network: EthereumNetwork { get }

func eth_getTransactionCount(address: EthereumAddress, block: EthereumBlock) async throws -> Int
func net_version() async throws -> EthereumNetwork
Expand Down Expand Up @@ -179,7 +179,7 @@ extension EthereumRPCProtocol {
var transaction = transaction
transaction.nonce = nonce

if transaction.chainId == nil, let network = network {
if transaction.chainId == nil {
transaction.chainId = network.intValue
}

Expand Down
2 changes: 1 addition & 1 deletion web3swift/src/Client/WSS/EthereumWebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
configuration: WebSocketConfiguration = .init(),
sessionConfig: URLSessionConfiguration = URLSession.shared.configuration,
logger: Logger? = nil,
network: EthereumNetwork? = nil
network: EthereumNetwork
) {
let networkQueue = OperationQueue()
networkQueue.name = "web3swift.client.networkQueue"
Expand Down
6 changes: 5 additions & 1 deletion web3swift/src/ENS/ENSMultiResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ extension EthereumNameService {
self.multicall = Multicall(client: client)
}

private var network: EthereumNetwork {
client.network
}

func resolve(addresses: [EthereumAddress]) async throws -> [AddressResolveOutput] {
let output = RegistryOutput<AddressResolveOutput>(expectedResponsesCount: addresses.count)

Expand Down Expand Up @@ -154,7 +158,7 @@ extension EthereumNameService {
parameters: [ENSRegistryResolverParameter],
handler: @escaping (Int, ENSRegistryResolverParameter, Result<EthereumAddress, Multicall.CallError>) -> Void
) async throws {
guard let network = client.network, let ensRegistryAddress = registryAddress ?? ENSContracts.registryAddress(for: network) else {
guard let ensRegistryAddress = registryAddress ?? ENSContracts.registryAddress(for: network) else {
throw EthereumNameServiceError.noNetwork
}

Expand Down
10 changes: 6 additions & 4 deletions web3swift/src/ENS/EthereumNameService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ public class EthereumNameService: EthereumNameServiceProtocol {
self.maximumRedirections = maximumRedirections
}

private var network: EthereumNetwork {
client.network
}

public func resolve(address: EthereumAddress, mode: ResolutionMode) async throws -> String {
guard let network = client.network,
let registryAddress = registryAddress ?? ENSContracts.registryAddress(for: network) else {
guard let registryAddress = registryAddress ?? ENSContracts.registryAddress(for: network) else {
throw EthereumNameServiceError.noNetwork
}

Expand All @@ -87,8 +90,7 @@ public class EthereumNameService: EthereumNameServiceProtocol {
}

public func resolve(ens: String, mode: ResolutionMode) async throws -> EthereumAddress {
guard let network = client.network,
let registryAddress = registryAddress ?? ENSContracts.registryAddress(for: network) else {
guard let registryAddress = registryAddress ?? ENSContracts.registryAddress(for: network) else {
throw EthereumNameServiceError.noNetwork
}
do {
Expand Down
Loading