From 772abc68aa9d5d1da519dc33455de89d3c1de78c Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Wed, 12 Jun 2019 11:55:01 +1000 Subject: [PATCH] Ditch TargetWrapper --- .../src/Loki/API/LokiAPI+Convenience.swift | 4 +-- .../src/Loki/API/LokiAPI+SwarmAPI.swift | 28 ++++++++-------- .../src/Loki/API/LokiAPI+Target.swift | 26 --------------- SignalServiceKit/src/Loki/API/LokiAPI.swift | 8 ++--- .../src/Loki/API/LokiAPITarget.swift | 32 +++++++++++++++++++ .../src/Loki/API/LokiP2PManager.swift | 4 +-- .../src/Loki/API/TargetWrapper.swift | 22 ------------- 7 files changed, 53 insertions(+), 71 deletions(-) delete mode 100644 SignalServiceKit/src/Loki/API/LokiAPI+Target.swift create mode 100644 SignalServiceKit/src/Loki/API/LokiAPITarget.swift delete mode 100644 SignalServiceKit/src/Loki/API/TargetWrapper.swift diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+Convenience.swift b/SignalServiceKit/src/Loki/API/LokiAPI+Convenience.swift index c829f5a0f..c4ec84e8e 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+Convenience.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+Convenience.swift @@ -5,7 +5,7 @@ internal extension LokiAPI { private static let receivedMessageHashValuesKey = "receivedMessageHashValuesKey" private static let receivedMessageHashValuesCollection = "receivedMessageHashValuesCollection" - internal static func getLastMessageHashValue(for target: Target) -> String? { + internal static func getLastMessageHashValue(for target: LokiAPITarget) -> String? { var result: String? = nil // Uses a read/write connection because getting the last message hash value also removes expired messages as needed storage.dbReadWriteConnection.readWrite { transaction in @@ -14,7 +14,7 @@ internal extension LokiAPI { return result } - internal static func setLastMessageHashValue(for target: Target, hashValue: String, expiresAt: UInt64) { + internal static func setLastMessageHashValue(for target: LokiAPITarget, hashValue: String, expiresAt: UInt64) { storage.dbReadWriteConnection.readWrite { transaction in storage.setLastMessageHash(forServiceNode: target.address, hash: hashValue, expiresAt: expiresAt, transaction: transaction) } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift index 5d6b0e168..98fa3d8d0 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift @@ -11,33 +11,31 @@ public extension LokiAPI { private static let swarmCacheKey = "swarmCacheKey" private static let swarmCacheCollection = "swarmCacheCollection" - fileprivate static var swarmCache: [String:[Target]] { + fileprivate static var swarmCache: [String:[LokiAPITarget]] { get { - var result: [String:[Target]]? = nil + var result: [String:[LokiAPITarget]]? = nil storage.dbReadConnection.read { transaction in - let intermediate = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[TargetWrapper]]? - result = intermediate?.mapValues { $0.map { Target(from: $0) } } + result = transaction.object(forKey: swarmCacheKey, inCollection: swarmCacheCollection) as! [String:[LokiAPITarget]]? } return result ?? [:] } set { - let intermediate = newValue.mapValues { $0.map { TargetWrapper(from: $0) } } storage.dbReadWriteConnection.readWrite { transaction in - transaction.setObject(intermediate, forKey: swarmCacheKey, inCollection: swarmCacheCollection) + transaction.setObject(newValue, forKey: swarmCacheKey, inCollection: swarmCacheCollection) } } } // MARK: Internal API - private static func getRandomSnode() -> Promise { - return Promise { seal in - seal.fulfill(Target(address: "http://13.236.173.190", port: defaultSnodePort)) // TODO: For debugging purposes + private static func getRandomSnode() -> Promise { + return Promise { seal in + seal.fulfill(LokiAPITarget(address: "http://13.236.173.190", port: defaultSnodePort)) // TODO: For debugging purposes } } - private static func getSwarm(for hexEncodedPublicKey: String) -> Promise<[Target]> { + private static func getSwarm(for hexEncodedPublicKey: String) -> Promise<[LokiAPITarget]> { if let cachedSwarm = swarmCache[hexEncodedPublicKey], cachedSwarm.count >= minimumSnodeCount { - return Promise<[Target]> { $0.fulfill(cachedSwarm) } + return Promise<[LokiAPITarget]> { $0.fulfill(cachedSwarm) } } else { let parameters: [String:Any] = [ "pubKey" : hexEncodedPublicKey ] return getRandomSnode().then { invoke(.getSwarm, on: $0, associatedWith: hexEncodedPublicKey, parameters: parameters) }.map { parseTargets(from: $0) }.get { swarmCache[hexEncodedPublicKey] = $0 } @@ -45,16 +43,16 @@ public extension LokiAPI { } // MARK: Public API - internal static func getTargetSnodes(for hexEncodedPublicKey: String) -> Promise<[Target]> { + internal static func getTargetSnodes(for hexEncodedPublicKey: String) -> Promise<[LokiAPITarget]> { // shuffled() uses the system's default random generator, which is cryptographically secure return getSwarm(for: hexEncodedPublicKey).map { Array($0.shuffled().prefix(targetSnodeCount)) } } // MARK: Parsing - private static func parseTargets(from rawResponse: Any) -> [Target] { + private static func parseTargets(from rawResponse: Any) -> [LokiAPITarget] { // TODO: For debugging purposes // ======== - let target = Target(address: "http://13.236.173.190", port: defaultSnodePort) + let target = LokiAPITarget(address: "http://13.236.173.190", port: defaultSnodePort) return Array(repeating: target, count: 3) // ======== // guard let json = rawResponse as? JSON, let addresses = json["snodes"] as? [String] else { @@ -68,7 +66,7 @@ public extension LokiAPI { // MARK: Error Handling internal extension Promise { - internal func handlingSwarmSpecificErrorsIfNeeded(for target: LokiAPI.Target, associatedWith hexEncodedPublicKey: String) -> Promise { + internal func handlingSwarmSpecificErrorsIfNeeded(for target: LokiAPITarget, associatedWith hexEncodedPublicKey: String) -> Promise { return recover { error -> Promise in if let error = error as? NetworkManagerError { switch error.statusCode { diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+Target.swift b/SignalServiceKit/src/Loki/API/LokiAPI+Target.swift deleted file mode 100644 index cd15175f6..000000000 --- a/SignalServiceKit/src/Loki/API/LokiAPI+Target.swift +++ /dev/null @@ -1,26 +0,0 @@ - -internal extension LokiAPI { - - internal struct Target : Hashable { - internal let address: String - internal let port: UInt16 - - internal init(address: String, port: UInt16) { - self.address = address - self.port = port - } - - internal init(from targetWrapper: TargetWrapper) { - self.address = targetWrapper.address - self.port = targetWrapper.port - } - - internal enum Method : String { - /// Only supported by snode targets. - case getSwarm = "get_snodes_for_pubkey" - /// Only supported by snode targets. - case getMessages = "retrieve" - case sendMessage = "store" - } - } -} diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index 130e19453..2832f9726 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -31,7 +31,7 @@ import PromiseKit override private init() { } // MARK: Internal API - internal static func invoke(_ method: Target.Method, on target: Target, associatedWith hexEncodedPublicKey: String, parameters: [String:Any] = [:]) -> RawResponsePromise { + internal static func invoke(_ method: LokiAPITarget.Method, on target: LokiAPITarget, associatedWith hexEncodedPublicKey: String, parameters: [String:Any] = [:]) -> RawResponsePromise { let url = URL(string: "\(target.address):\(target.port)/\(version)/storage_rpc")! let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ]) return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject } @@ -56,7 +56,7 @@ import PromiseKit public static func sendSignalMessage(_ signalMessage: SignalMessage, with timestamp: UInt64, onP2PSuccess: @escaping () -> Void) -> Promise> { guard let lokiMessage = Message.from(signalMessage: signalMessage, with: timestamp) else { return Promise(error: Error.messageConversionFailed) } let destination = lokiMessage.destination - func sendLokiMessage(_ lokiMessage: Message, to target: Target) -> RawResponsePromise { + func sendLokiMessage(_ lokiMessage: Message, to target: LokiAPITarget) -> RawResponsePromise { let parameters = lokiMessage.toJSON() return invoke(.sendMessage, on: target, associatedWith: destination, parameters: parameters) } @@ -68,7 +68,7 @@ import PromiseKit }.retryingIfNeeded(maxRetryCount: maxRetryCount) } if let peer = LokiP2PManager.getInfo(for: destination), (lokiMessage.isPing || peer.isOnline) { - let target = Target(address: peer.address, port: peer.port) + let target = LokiAPITarget(address: peer.address, port: peer.port) return Promise.value([ target ]).mapValues { sendLokiMessage(lokiMessage, to: $0) }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount).get { _ in LokiP2PManager.markOnline(destination) onP2PSuccess() @@ -101,7 +101,7 @@ import PromiseKit // The parsing utilities below use a best attempt approach to parsing; they warn for parsing failures but don't throw exceptions. - private static func updateLastMessageHashValueIfPossible(for target: Target, from rawMessages: [JSON]) { + private static func updateLastMessageHashValueIfPossible(for target: LokiAPITarget, from rawMessages: [JSON]) { guard let lastMessage = rawMessages.last, let hashValue = lastMessage["hash"] as? String, let expiresAt = lastMessage["expiration"] as? Int else { Logger.warn("[Loki] Failed to update last message hash value from: \(rawMessages).") return diff --git a/SignalServiceKit/src/Loki/API/LokiAPITarget.swift b/SignalServiceKit/src/Loki/API/LokiAPITarget.swift new file mode 100644 index 000000000..a57bdf515 --- /dev/null +++ b/SignalServiceKit/src/Loki/API/LokiAPITarget.swift @@ -0,0 +1,32 @@ + +internal final class LokiAPITarget : NSObject, NSCoding { + internal let address: String + internal let port: UInt16 + + // MARK: Types + internal enum Method : String { + /// Only supported by snode targets. + case getSwarm = "get_snodes_for_pubkey" + /// Only supported by snode targets. + case getMessages = "retrieve" + case sendMessage = "store" + } + + // MARK: Initialization + internal init(address: String, port: UInt16) { + self.address = address + self.port = port + } + + // MARK: Coding + internal init?(coder: NSCoder) { + address = coder.decodeObject(forKey: "address") as! String + port = coder.decodeObject(forKey: "port") as! UInt16 + super.init() + } + + internal func encode(with coder: NSCoder) { + coder.encode(address, forKey: "address") + coder.encode(port, forKey: "port") + } +} diff --git a/SignalServiceKit/src/Loki/API/LokiP2PManager.swift b/SignalServiceKit/src/Loki/API/LokiP2PManager.swift index c7882981f..514c238d9 100644 --- a/SignalServiceKit/src/Loki/API/LokiP2PManager.swift +++ b/SignalServiceKit/src/Loki/API/LokiP2PManager.swift @@ -17,7 +17,7 @@ } /// Our p2p address - private static var ourP2PAddress: LokiAPI.Target? = nil + private static var ourP2PAddress: LokiAPITarget? = nil /// This is where we store the p2p details of our contacts private static var peerInfo = [String:PeerInfo]() @@ -29,7 +29,7 @@ /// - Parameter url: The url to our local server @objc public static func setOurP2PAddress(url: URL) { guard let scheme = url.scheme, let host = url.host, let port = url.port else { return } - let target = LokiAPI.Target(address: "\(scheme)://\(host)", port: UInt16(port)) + let target = LokiAPITarget(address: "\(scheme)://\(host)", port: UInt16(port)) ourP2PAddress = target } diff --git a/SignalServiceKit/src/Loki/API/TargetWrapper.swift b/SignalServiceKit/src/Loki/API/TargetWrapper.swift deleted file mode 100644 index f8f14fba1..000000000 --- a/SignalServiceKit/src/Loki/API/TargetWrapper.swift +++ /dev/null @@ -1,22 +0,0 @@ - -@objc internal final class TargetWrapper : NSObject, NSCoding { - internal let address: String - internal let port: UInt16 - - internal init(from target: LokiAPI.Target) { - address = target.address - port = target.port - super.init() - } - - internal init?(coder: NSCoder) { - address = coder.decodeObject(forKey: "address") as! String - port = coder.decodeObject(forKey: "port") as! UInt16 - super.init() - } - - internal func encode(with coder: NSCoder) { - coder.encode(address, forKey: "address") - coder.encode(port, forKey: "port") - } -}