Use PoW difficulty returned by snodes

pull/33/head
Niels Andriesse 6 years ago
parent a7e3a2b534
commit 2cf4385fb6

@ -12,6 +12,7 @@ public final class LokiAPI : NSObject {
private static let defaultTimeout: TimeInterval = 20
private static let longPollingTimeout: TimeInterval = 40
public static let defaultMessageTTL: UInt64 = 24 * 60 * 60 * 1000
internal static var powDifficulty: UInt = 100
// MARK: Types
public typealias RawResponse = Any
@ -81,7 +82,16 @@ public final class LokiAPI : NSObject {
let powPromise = lokiMessage.calculatePoW()
let swarmPromise = getTargetSnodes(for: destination)
return when(fulfilled: powPromise, swarmPromise).map { lokiMessageWithPoW, swarm in
return Set(swarm.map { sendLokiMessage(lokiMessageWithPoW, to: $0) })
return Set(swarm.map {
sendLokiMessage(lokiMessageWithPoW, to: $0).map { rawResponse in
if let json = rawResponse as? JSON, let powDifficulty = json["difficulty"] as? Int {
LokiAPI.powDifficulty = UInt(powDifficulty)
} else {
print("[Loki] Failed to update PoW difficulty from: \(rawResponse).")
}
return rawResponse
}
})
}.retryingIfNeeded(maxRetryCount: maxRetryCount)
}
if let peer = LokiP2PAPI.getInfo(for: destination), (lokiMessage.isPing || peer.isOnline) {

@ -47,13 +47,6 @@ public enum ProofOfWork {
// If this changes then we also have to use something other than UInt64 to support the new length
private static let nonceLength = 8
private static let nonceTrialCount: Int = {
switch BuildConfiguration.current {
case .debug: return 10
case .production: return 100
}
}()
/// Calculate a proof of work with the given configuration
///
@ -67,7 +60,7 @@ public enum ProofOfWork {
/// - Returns: A nonce string or `nil` if it failed
public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: UInt64) -> String? {
let payload = createPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl)
let target = calcTarget(ttl: ttl, payloadLength: payload.count, nonceTrials: nonceTrialCount)
let target = calcTarget(ttl: ttl, payloadLength: payload.count, nonceTrials: Int(LokiAPI.powDifficulty))
// Start with the max value
var trialValue = UInt64.max

Loading…
Cancel
Save