Handle insufficient PoW error

pull/33/head
Niels Andriesse 6 years ago
parent 5b26601335
commit 16a284ca02

@ -106,8 +106,8 @@ internal extension Promise {
let oldFailureCount = LokiAPI.failureCount[target] ?? 0
let newFailureCount = oldFailureCount + 1
LokiAPI.failureCount[target] = newFailureCount
print("[Loki] Couldn't reach snode at: \(target.address):\(target.port); setting failure count to: \(newFailureCount).")
if oldFailureCount >= LokiAPI.failureThreshold {
print("[Loki] Couldn't reach snode at: \(target.address):\(target.port); setting failure count to \(newFailureCount).")
if newFailureCount >= LokiAPI.failureThreshold {
print("[Loki] Failure threshold reached for: \(target); dropping it.")
LokiAPI.dropIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey) // Remove it from the swarm cache associated with the given public key
LokiAPI.randomSnodePool.remove(target) // Remove it from the random snode pool
@ -116,6 +116,16 @@ internal extension Promise {
// The snode isn't associated with the given public key anymore
print("[Loki] Invalidating swarm for: \(hexEncodedPublicKey).")
LokiAPI.dropIfNeeded(target, hexEncodedPublicKey: hexEncodedPublicKey)
case 432:
if case NetworkManagerError.taskError(_, let underlyingError) = error, let nsError = underlyingError as? NSError,
let data = nsError.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] as? Data, let json = try? JSONSerialization.jsonObject(with: data, options: []) as? JSON,
let powDifficulty = json["difficulty"] as? Int {
print("[Loki] Setting PoW difficulty to \(powDifficulty).")
LokiAPI.powDifficulty = UInt(powDifficulty)
} else {
print("[Loki] Failed to update PoW difficulty.")
}
break
default: break
}
}

@ -79,6 +79,8 @@ public final class LokiAPI : NSObject {
return Set(swarm.map {
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).")
LokiAPI.powDifficulty = UInt(powDifficulty)
} else {
print("[Loki] Failed to update PoW difficulty from: \(rawResponse).")

@ -311,7 +311,7 @@ dispatch_queue_t NetworkManagerQueue()
[OutageDetection.sharedManager reportConnectionSuccess];
});
};
TSNetworkManagerSuccess failure = ^(NSURLSessionDataTask *task, NSError *error) {
TSNetworkManagerFailure failure = ^(NSURLSessionDataTask *task, NSError *error) {
dispatch_async(NetworkManagerQueue(), ^{
[sessionManagerPool returnToPool:sessionManager];
});

Loading…
Cancel
Save