|
|
|
@ -19,14 +19,10 @@ public final class SnodeAPI : NSObject {
|
|
|
|
|
// MARK: Settings
|
|
|
|
|
private static let maxRetryCount: UInt = 8
|
|
|
|
|
private static let minSwarmSnodeCount = 3
|
|
|
|
|
private static let seedNodePool: Set<String> = useTestnet ? [ "http://public.loki.foundation:38157" ] : [ "https://storage.seed1.loki.network:4433", "https://storage.seed3.loki.network:4433", "https://public.loki.foundation:4433" ]
|
|
|
|
|
private static let seedNodePool: Set<String> = Features.useTestnet ? [ "http://public.loki.foundation:38157" ] : [ "https://storage.seed1.loki.network:4433", "https://storage.seed3.loki.network:4433", "https://public.loki.foundation:4433" ]
|
|
|
|
|
private static let snodeFailureThreshold = 3
|
|
|
|
|
private static let targetSwarmSnodeCount = 2
|
|
|
|
|
private static let minSnodePoolCount = 12
|
|
|
|
|
|
|
|
|
|
/// - Note: Changing this on the fly is not recommended.
|
|
|
|
|
internal static var useOnionRequests = true
|
|
|
|
|
internal static let useTestnet = false
|
|
|
|
|
|
|
|
|
|
// MARK: Error
|
|
|
|
|
public enum Error : LocalizedError {
|
|
|
|
@ -123,7 +119,7 @@ public final class SnodeAPI : NSObject {
|
|
|
|
|
|
|
|
|
|
// MARK: Internal API
|
|
|
|
|
internal static func invoke(_ method: Snode.Method, on snode: Snode, associatedWith publicKey: String? = nil, parameters: JSON) -> RawResponsePromise {
|
|
|
|
|
if useOnionRequests {
|
|
|
|
|
if Features.useOnionRequests {
|
|
|
|
|
return OnionRequestAPI.sendOnionRequest(to: snode, invoking: method, with: parameters, associatedWith: publicKey).map2 { $0 as Any }
|
|
|
|
|
} else {
|
|
|
|
|
let url = "\(snode.address):\(snode.port)/storage_rpc/v1"
|
|
|
|
@ -349,7 +345,7 @@ public final class SnodeAPI : NSObject {
|
|
|
|
|
return Promise<Set<Snode>> { $0.fulfill(cachedSwarm) }
|
|
|
|
|
} else {
|
|
|
|
|
SNLog("Getting swarm for: \((publicKey == SNSnodeKitConfiguration.shared.storage.getUserPublicKey()) ? "self" : publicKey).")
|
|
|
|
|
let parameters: [String:Any] = [ "pubKey" : useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey ]
|
|
|
|
|
let parameters: [String:Any] = [ "pubKey" : Features.useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey ]
|
|
|
|
|
return getRandomSnode().then2 { snode in
|
|
|
|
|
attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) {
|
|
|
|
|
invoke(.getSwarm, on: snode, associatedWith: publicKey, parameters: parameters)
|
|
|
|
@ -370,7 +366,7 @@ public final class SnodeAPI : NSObject {
|
|
|
|
|
storage.pruneLastMessageHashInfoIfExpired(for: snode, associatedWith: publicKey, using: transaction)
|
|
|
|
|
}
|
|
|
|
|
let lastHash = storage.getLastMessageHash(for: snode, associatedWith: publicKey) ?? ""
|
|
|
|
|
let parameters = [ "pubKey" : useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
|
|
|
|
|
let parameters = [ "pubKey" : Features.useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
|
|
|
|
|
invoke(.getMessages, on: snode, associatedWith: publicKey, parameters: parameters).done2 { seal.fulfill($0) }.catch2 { seal.reject($0) }
|
|
|
|
|
}
|
|
|
|
|
return promise
|
|
|
|
@ -386,7 +382,7 @@ public final class SnodeAPI : NSObject {
|
|
|
|
|
storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey, using: transaction)
|
|
|
|
|
}
|
|
|
|
|
let lastHash = storage.getLastMessageHash(for: targetSnode, associatedWith: publicKey) ?? ""
|
|
|
|
|
let parameters = [ "pubKey" : useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
|
|
|
|
|
let parameters = [ "pubKey" : Features.useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
|
|
|
|
|
return invoke(.getMessages, on: targetSnode, associatedWith: publicKey, parameters: parameters).map2 { rawResponse in
|
|
|
|
|
parseRawMessagesResponse(rawResponse, from: targetSnode, associatedWith: publicKey)
|
|
|
|
|
}
|
|
|
|
@ -398,7 +394,7 @@ public final class SnodeAPI : NSObject {
|
|
|
|
|
|
|
|
|
|
public static func sendMessage(_ message: SnodeMessage) -> Promise<Set<RawResponsePromise>> {
|
|
|
|
|
let (promise, seal) = Promise<Set<RawResponsePromise>>.pending()
|
|
|
|
|
let publicKey = useTestnet ? message.recipient.removing05PrefixIfNeeded() : message.recipient
|
|
|
|
|
let publicKey = Features.useTestnet ? message.recipient.removing05PrefixIfNeeded() : message.recipient
|
|
|
|
|
Threading.workQueue.async {
|
|
|
|
|
getTargetSnodes(for: publicKey).map2 { targetSnodes in
|
|
|
|
|
let parameters = message.toJSON()
|
|
|
|
|