Fix TTL usage

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

@ -28,7 +28,7 @@ public struct LokiMessage {
let ttl = LokiMessagingAPI.defaultTTL
if isPoWRequired {
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)
seal.fulfill(result)
} else {

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

Loading…
Cancel
Save