Fix TTL usage

pull/10/head
Niels Andriesse 7 years ago
parent d05b4aa2bf
commit 243a9e906d

@ -28,7 +28,7 @@ public struct LokiMessage {
let ttl = LokiMessagingAPI.defaultTTL let ttl = LokiMessagingAPI.defaultTTL
if isPoWRequired { if isPoWRequired {
let timestamp = UInt64(Date().timeIntervalSince1970) let timestamp = UInt64(Date().timeIntervalSince1970)
if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: Int(ttl)) { if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: ttl) {
let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: timestamp, nonce: nonce) let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: timestamp, nonce: nonce)
seal.fulfill(result) seal.fulfill(result)
} else { } else {

@ -66,9 +66,9 @@ private extension MutableCollection where Element == UInt8, Index == Int {
/// - pubKey: The message recipient /// - pubKey: The message recipient
/// - timestamp: The timestamp /// - timestamp: The timestamp
/// - ttl: The message time to live /// - ttl: The message time to live
/// - Returns: A nonce string or nil if it failed /// - Returns: A nonce string or `nil` if it failed
@objc public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: Int) -> String? { @objc public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: UInt64) -> String? {
let payload = getPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl) 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: nonceTrialCount)
// Start with the max value // Start with the max value
@ -91,7 +91,7 @@ private extension MutableCollection where Element == UInt8, Index == Int {
} }
/// Get the proof of work payload /// Get the proof of work payload
private static func getPayload(pubKey: String, data: String, timestamp: UInt64, ttl: Int) -> [UInt8] { private static func createPayload(pubKey: String, data: String, timestamp: UInt64, ttl: UInt64) -> [UInt8] {
let timestampString = String(timestamp) let timestampString = String(timestamp)
let ttlString = String(ttl) let ttlString = String(ttl)
let payloadString = timestampString + ttlString + pubKey + data let payloadString = timestampString + ttlString + pubKey + data
@ -99,16 +99,13 @@ private extension MutableCollection where Element == UInt8, Index == Int {
} }
/// Calculate the target we need to reach /// Calculate the target we need to reach
private static func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> UInt64 { private static func calcTarget(ttl: UInt64, payloadLength: Int, nonceTrials: Int) -> UInt64 {
let two16 = UInt64(pow(2, 16) - 1) let two16 = UInt64(pow(2, 16) - 1)
let two64 = UInt64(pow(2, 64) - 1) let two64 = UInt64(pow(2, 64) - 1)
// ttl converted to seconds
let ttlSeconds = ttl / 1000
// Do all the calculations // Do all the calculations
let totalLength = UInt64(payloadLength + nonceLength) let totalLength = UInt64(payloadLength + nonceLength)
let ttlMult = UInt64(ttlSeconds) * totalLength let ttlMult = ttl * totalLength
// UInt64 values // UInt64 values
let innerFrac = ttlMult / two16 let innerFrac = ttlMult / two16

Loading…
Cancel
Save