From 239d13c33eb10ca0428b2b049de3d6b622c5dd45 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 25 Jul 2019 13:09:22 +1000 Subject: [PATCH] Make use of all seed nodes --- .../src/Loki/API/LokiAPI+LongPolling.swift | 6 +++--- .../src/Loki/API/LokiAPI+SwarmAPI.swift | 6 ++++-- SignalServiceKit/src/Loki/API/LokiAPI.swift | 21 ++++++++++++------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+LongPolling.swift b/SignalServiceKit/src/Loki/API/LokiAPI+LongPolling.swift index dd0a16f64..9a9821015 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+LongPolling.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+LongPolling.swift @@ -35,7 +35,7 @@ public extension LokiAPI { // This is here so we can stop the infinite loop guard !shouldStopPolling else { return } - getSwarm(for: userPublicKey).then { _ -> Guarantee<[Result]> in + getSwarm(for: userHexEncodedPublicKey).then { _ -> Guarantee<[Result]> in var promises = [Promise]() let connections = 3 for i in 0.. [LokiAPITarget] { - let snodes = LokiAPI.swarmCache[userPublicKey] ?? [] + let snodes = LokiAPI.swarmCache[userHexEncodedPublicKey] ?? [] return snodes.filter { !usedSnodes.contains($0) } } @@ -105,7 +105,7 @@ public extension LokiAPI { // Connect to the next snode if we haven't cancelled // We also need to remove the cached snode so we don't contact it again - dropIfNeeded(nextSnode, hexEncodedPublicKey: userPublicKey) + dropIfNeeded(nextSnode, hexEncodedPublicKey: userHexEncodedPublicKey) return connectToNextSnode() } } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift index dd60ed844..15d8e09e8 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI+SwarmAPI.swift @@ -39,14 +39,16 @@ public extension LokiAPI { } // MARK: Clearnet Setup + fileprivate static let seedNodePool: Set = [ "http://3.104.19.14:22023", "http://13.238.53.205:38157", "http://imaginary.stream:38157" ] fileprivate static var randomSnodePool: Set = [] // MARK: Internal API private static func getRandomSnode() -> Promise { if randomSnodePool.isEmpty { - let url = URL(string: "http://3.104.19.14:22023/json_rpc")! + let target = seedNodePool.randomElement()! + let url = URL(string: "\(target)/json_rpc")! let request = TSRequest(url: url, method: "POST", parameters: [ "method" : "get_service_nodes" ]) - print("[Loki] Invoking get_service_nodes on http://3.104.19.14:22023 (i.e. the seed node).") + print("[Loki] Invoking get_service_nodes on \(target).") return TSNetworkManager.shared().makePromise(request: request).map { intermediate in let rawResponse = intermediate.responseObject guard let json = rawResponse as? JSON, let intermediate = json["result"] as? JSON, let rawTargets = intermediate["service_node_states"] as? [JSON] else { throw "Failed to update random snode pool from: \(rawResponse)." } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index ea32ba3a3..1a604f7af 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -4,7 +4,7 @@ import PromiseKit public final class LokiAPI : NSObject { internal static let storage = OWSPrimaryStorage.shared() - internal static var userPublicKey: String { return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey } + internal static var userHexEncodedPublicKey: String { return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey } // MARK: Settings private static let version = "v1" @@ -52,15 +52,15 @@ public final class LokiAPI : NSObject { internal static func getRawMessages(from target: LokiAPITarget, usingLongPolling useLongPolling: Bool) -> RawResponsePromise { let lastHashValue = getLastMessageHashValue(for: target) ?? "" - let parameters = [ "pubKey" : userPublicKey, "lastHash" : lastHashValue ] + let parameters = [ "pubKey" : userHexEncodedPublicKey, "lastHash" : lastHashValue ] let headers: [String:String]? = useLongPolling ? [ "X-Loki-Long-Poll" : "true" ] : nil let timeout: TimeInterval? = useLongPolling ? longPollingTimeout : nil - return invoke(.getMessages, on: target, associatedWith: userPublicKey, parameters: parameters, headers: headers, timeout: timeout) + return invoke(.getMessages, on: target, associatedWith: userHexEncodedPublicKey, parameters: parameters, headers: headers, timeout: timeout) } // MARK: Public API public static func getMessages() -> Promise> { - return getTargetSnodes(for: userPublicKey).mapValues { targetSnode in + return getTargetSnodes(for: userHexEncodedPublicKey).mapValues { targetSnode in return getRawMessages(from: targetSnode, usingLongPolling: false).map { parseRawMessagesResponse($0, from: targetSnode) } }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount) } @@ -80,10 +80,10 @@ public final class LokiAPI : NSObject { sendLokiMessage(lokiMessageWithPoW, to: $0).map { rawResponse in if let json = rawResponse as? JSON, let powDifficulty = json["difficulty"] as? Int { guard powDifficulty != LokiAPI.powDifficulty else { return rawResponse } - print("[Loki] Setting PoW difficulty to \(powDifficulty).") + print("[Loki] Setting proof of work difficulty to \(powDifficulty).") LokiAPI.powDifficulty = UInt(powDifficulty) } else { - print("[Loki] Failed to update PoW difficulty from: \(rawResponse).") + print("[Loki] Failed to update proof of work difficulty from: \(rawResponse).") } return rawResponse } @@ -128,7 +128,14 @@ public final class LokiAPI : NSObject { guard let json = rawResponse as? JSON, let rawMessages = json["messages"] as? [JSON] else { return [] } updateLastMessageHashValueIfPossible(for: target, from: rawMessages) let newRawMessages = removeDuplicates(from: rawMessages) - return parseProtoEnvelopes(from: newRawMessages) + let newMessages = parseProtoEnvelopes(from: newRawMessages) + let newMessageCount = newMessages.count + if newMessageCount == 1 { + print("[Loki] Retrieved 1 new message.") + } else if (newMessageCount != 0) { + print("[Loki] Retrieved \(newMessageCount) new messages.") + } + return newMessages } private static func updateLastMessageHashValueIfPossible(for target: LokiAPITarget, from rawMessages: [JSON]) {