Implement new snode pool updating method
This commit is contained in:
parent
8aa25af4d5
commit
2f67073a5a
|
@ -13,7 +13,7 @@ enum ProofOfWork {
|
||||||
let payload = payloadAsString.bytes
|
let payload = payloadAsString.bytes
|
||||||
// Calculate target
|
// Calculate target
|
||||||
let numerator = UInt64.max
|
let numerator = UInt64.max
|
||||||
let difficulty = UInt64(SnodeAPI.powDifficulty)
|
let difficulty = UInt64(1)
|
||||||
let totalSize = UInt64(payload.count + nonceSize)
|
let totalSize = UInt64(payload.count + nonceSize)
|
||||||
let ttlInSeconds = ttl / 1000
|
let ttlInSeconds = ttl / 1000
|
||||||
let denominator = difficulty * (totalSize + (ttlInSeconds * totalSize) / UInt64(UInt16.max))
|
let denominator = difficulty * (totalSize + (ttlInSeconds * totalSize) / UInt64(UInt16.max))
|
||||||
|
|
|
@ -33,6 +33,7 @@ public final class SnodeAPI : NSObject {
|
||||||
case generic
|
case generic
|
||||||
case clockOutOfSync
|
case clockOutOfSync
|
||||||
case snodePoolUpdatingFailed
|
case snodePoolUpdatingFailed
|
||||||
|
case inconsistentSnodePools
|
||||||
// ONS
|
// ONS
|
||||||
case decryptionFailed
|
case decryptionFailed
|
||||||
case hashingFailed
|
case hashingFailed
|
||||||
|
@ -43,6 +44,7 @@ public final class SnodeAPI : NSObject {
|
||||||
case .generic: return "An error occurred."
|
case .generic: return "An error occurred."
|
||||||
case .clockOutOfSync: return "Your clock is out of sync with the Service Node network. Please check that your device's clock is set to automatic time."
|
case .clockOutOfSync: return "Your clock is out of sync with the Service Node network. Please check that your device's clock is set to automatic time."
|
||||||
case .snodePoolUpdatingFailed: return "Failed to update the Service Node pool."
|
case .snodePoolUpdatingFailed: return "Failed to update the Service Node pool."
|
||||||
|
case .inconsistentSnodePools: return "Received inconsistent Service Node pool information from the Service Node network."
|
||||||
// ONS
|
// ONS
|
||||||
case .decryptionFailed: return "Couldn't decrypt ONS name."
|
case .decryptionFailed: return "Couldn't decrypt ONS name."
|
||||||
case .hashingFailed: return "Couldn't compute ONS name hash."
|
case .hashingFailed: return "Couldn't compute ONS name hash."
|
||||||
|
@ -225,7 +227,31 @@ public final class SnodeAPI : NSObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func getSnodePoolFromSnode() -> Promise<Set<Snode>> {
|
private static func getSnodePoolFromSnode() -> Promise<Set<Snode>> {
|
||||||
return Promise(error: Error.generic)
|
return getSnodePool().then2 { snodePool -> Promise<Set<Snode>> in
|
||||||
|
var snodePool = snodePool
|
||||||
|
var snodes: Set<Snode> = []
|
||||||
|
(0..<3).forEach { _ in
|
||||||
|
let snode = snodePool.randomElement()!
|
||||||
|
snodePool.remove(snode)
|
||||||
|
snodes.insert(snode)
|
||||||
|
}
|
||||||
|
let rawSnodePoolPromises: [Promise<Set<Snode>>] = snodes.map { snode in
|
||||||
|
return attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) {
|
||||||
|
return invoke(.getAllSnodes, on: snode, parameters: [:]).map2 { rawResponse in
|
||||||
|
return parseSnodes(from: rawResponse)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return when(fulfilled: rawSnodePoolPromises).map2 { results in
|
||||||
|
var result: Set<Snode> = results[0]
|
||||||
|
results.forEach { result = result.union($0) }
|
||||||
|
if result.count > 196 { // We want the snodes to agree on at least this many snodes
|
||||||
|
return result
|
||||||
|
} else {
|
||||||
|
throw Error.inconsistentSnodePools
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Public API
|
// MARK: Public API
|
||||||
|
|
Loading…
Reference in New Issue