Skip to content

Commit

Permalink
Initialize Network on constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dionysios Karatzas committed Jun 22, 2022
1 parent abc0ba2 commit 3dbec9d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
67 changes: 35 additions & 32 deletions web3swift/src/Client/BaseEthereumClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,28 @@ 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()
txQueue.name = "web3swift.client.rawTxQueue"
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<EthereumNetwork, EthereumClientError>) -> Void) {
Expand Down Expand Up @@ -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<T>(_ error: Error, completionHandler: @escaping (Result<T, EthereumClientError>) -> Void) {
Expand Down
11 changes: 7 additions & 4 deletions web3swift/src/Client/EthereumHttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
5 changes: 3 additions & 2 deletions web3swift/src/Client/EthereumWebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down

0 comments on commit 3dbec9d

Please sign in to comment.