diff --git a/web3swift/src/Client/BaseEthereumClient.swift b/web3swift/src/Client/BaseEthereumClient.swift index ae8afb4e..f8fdee55 100644 --- a/web3swift/src/Client/BaseEthereumClient.swift +++ b/web3swift/src/Client/BaseEthereumClient.swift @@ -17,35 +17,14 @@ public class BaseEthereumClient: EthereumClientProtocol { let concurrentQueue: OperationQueue let networkProvider: NetworkProviderProtocol - private var retreivedNetwork: EthereumNetwork? private let logger: Logger - public var network: EthereumNetwork? { - if let _ = retreivedNetwork { - return retreivedNetwork - } - - let group = DispatchGroup() - group.enter() - - var network: EthereumNetwork? - net_version { result in - switch result { - case .success(let data): - network = data - self.retreivedNetwork = network - case .failure(let error): - self.logger.warning("Client has no network: \(error.localizedDescription)") - } - - group.leave() - } + public var network: EthereumNetwork? - group.wait() - return network - } - - init(networkProvider: NetworkProviderProtocol, url: URL, logger: Logger? = nil) { + init(networkProvider: NetworkProviderProtocol, + url: URL, + logger: Logger? = nil, + network: EthereumNetwork?) { self.url = url let txQueue = OperationQueue() @@ -53,9 +32,13 @@ public class BaseEthereumClient: EthereumClientProtocol { txQueue.qualityOfService = .background txQueue.maxConcurrentOperationCount = 1 self.concurrentQueue = txQueue - self.networkProvider = networkProvider self.logger = logger ?? Logger(label: "web3.swift.eth-client") + self.network = network + + if network == nil { + self.network = fetchNetwork() + } } public func net_version(completionHandler: @escaping (Result) -> Void) { @@ -358,12 +341,32 @@ public class BaseEthereumClient: EthereumClientProtocol { private func eth_getLogs(addresses: [EthereumAddress]?, topics: Topics?, fromBlock from: EthereumBlock, toBlock to: EthereumBlock, completion: @escaping((Result<[EthereumLog], EthereumClientError>) -> Void)) { DispatchQueue.global(qos: .default) - .async { - let result = RecursiveLogCollector(ethClient: self) - .getAllLogs(addresses: addresses, topics: topics, from: from, to: to) + .async { + let result = RecursiveLogCollector(ethClient: self) + .getAllLogs(addresses: addresses, topics: topics, from: from, to: to) - completion(result) - } + completion(result) + } + } + + private func fetchNetwork() -> EthereumNetwork? { + let group = DispatchGroup() + group.enter() + + var network: EthereumNetwork? + net_version { result in + switch result { + case .success(let data): + network = data + case .failure(let error): + self.logger.warning("Client has no network: \(error.localizedDescription)") + } + + group.leave() + } + + group.wait() + return network } func failureHandler(_ error: Error, completionHandler: @escaping (Result) -> Void) { diff --git a/web3swift/src/Client/EthereumHttpClient.swift b/web3swift/src/Client/EthereumHttpClient.swift index 2fc2c752..5fc7606b 100644 --- a/web3swift/src/Client/EthereumHttpClient.swift +++ b/web3swift/src/Client/EthereumHttpClient.swift @@ -12,15 +12,18 @@ import FoundationNetworking public class EthereumHttpClient: BaseEthereumClient { let networkQueue: OperationQueue - - public init(url: URL, sessionConfig: URLSessionConfiguration = URLSession.shared.configuration, logger: Logger? = nil) { + + public init(url: URL, + sessionConfig: URLSessionConfiguration = URLSession.shared.configuration, + logger: Logger? = nil, + network: EthereumNetwork? = nil) { let networkQueue = OperationQueue() networkQueue.name = "web3swift.client.networkQueue" networkQueue.qualityOfService = .background networkQueue.maxConcurrentOperationCount = 4 self.networkQueue = networkQueue - + let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: networkQueue) - super.init(networkProvider: HttpNetworkProvider(session: session, url: url), url: url, logger: logger) + super.init(networkProvider: HttpNetworkProvider(session: session, url: url), url: url, logger: logger, network: network) } } diff --git a/web3swift/src/Client/EthereumWebSocketClient.swift b/web3swift/src/Client/EthereumWebSocketClient.swift index 1c8c226b..b2337d2d 100644 --- a/web3swift/src/Client/EthereumWebSocketClient.swift +++ b/web3swift/src/Client/EthereumWebSocketClient.swift @@ -54,7 +54,8 @@ public class EthereumWebSocketClient: BaseEthereumClient, EthereumClientWebSocke eventLoopGroupProvider: EventLoopGroupProvider = .createNew, configuration: WebSocketConfiguration = .init(), sessionConfig: URLSessionConfiguration = URLSession.shared.configuration, - logger: Logger? = nil) { + logger: Logger? = nil, + network: EthereumNetwork? = nil) { let networkQueue = OperationQueue() networkQueue.name = "web3swift.client.networkQueue" networkQueue.qualityOfService = .background @@ -69,7 +70,7 @@ public class EthereumWebSocketClient: BaseEthereumClient, EthereumClientWebSocke session: session, logger: logger) self.provider = provider - super.init(networkProvider: provider, url: url, logger: logger) + super.init(networkProvider: provider, url: url, logger: logger, network: network) } public func connect() {