Make use of all seed nodes

This commit is contained in:
Niels Andriesse 2019-07-25 13:09:22 +10:00
parent 40edf1992a
commit 239d13c33e
3 changed files with 21 additions and 12 deletions

View File

@ -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<Void>]> in
getSwarm(for: userHexEncodedPublicKey).then { _ -> Guarantee<[Result<Void>]> in
var promises = [Promise<Void>]()
let connections = 3
for i in 0..<connections {
@ -59,7 +59,7 @@ public extension LokiAPI {
}
private static func getUnusedSnodes() -> [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()
}
}

View File

@ -39,14 +39,16 @@ public extension LokiAPI {
}
// MARK: Clearnet Setup
fileprivate static let seedNodePool: Set<String> = [ "http://3.104.19.14:22023", "http://13.238.53.205:38157", "http://imaginary.stream:38157" ]
fileprivate static var randomSnodePool: Set<LokiAPITarget> = []
// MARK: Internal API
private static func getRandomSnode() -> Promise<LokiAPITarget> {
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)." }

View File

@ -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<Set<MessageListPromise>> {
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]) {