Increase TTL & remove PoW

This commit is contained in:
nielsandriesse 2021-04-14 14:05:26 +10:00
parent 51576acec1
commit a76c43973f
6 changed files with 15 additions and 29 deletions

View File

@ -5,8 +5,8 @@ public final class ClosedGroupControlMessage : ControlMessage {
public override var ttl: UInt64 { public override var ttl: UInt64 {
switch kind { switch kind {
case .encryptionKeyPair: return 4 * 24 * 60 * 60 * 1000 case .encryptionKeyPair: return 14 * 24 * 60 * 60 * 1000
default: return 2 * 24 * 60 * 60 * 1000 default: return 14 * 24 * 60 * 60 * 1000
} }
} }

View File

@ -8,8 +8,6 @@ public final class ConfigurationMessage : ControlMessage {
public var profilePictureURL: String? public var profilePictureURL: String?
public var profileKey: Data? public var profileKey: Data?
public var contacts: Set<Contact> = [] public var contacts: Set<Contact> = []
public override var ttl: UInt64 { 4 * 24 * 60 * 60 * 1000 }
public override var isSelfSendValid: Bool { true } public override var isSelfSendValid: Bool { true }

View File

@ -11,7 +11,7 @@ public class Message : NSObject, NSCoding { // NSObject/NSCoding conformance is
public var groupPublicKey: String? public var groupPublicKey: String?
public var openGroupServerMessageID: UInt64? public var openGroupServerMessageID: UInt64?
public var ttl: UInt64 { 2 * 24 * 60 * 60 * 1000 } public var ttl: UInt64 { 14 * 24 * 60 * 60 * 1000 }
public var isSelfSendValid: Bool { false } public var isSelfSendValid: Bool { false }
public override init() { } public override init() { }

View File

@ -202,16 +202,9 @@ public final class MessageSender : NSObject {
handleFailure(with: error, using: transaction) handleFailure(with: error, using: transaction)
return promise return promise
} }
// Calculate proof of work
let recipient = message.recipient!
let base64EncodedData = wrappedMessage.base64EncodedString()
guard let (timestamp, nonce) = ProofOfWork.calculate(ttl: message.ttl, publicKey: recipient, data: base64EncodedData) else {
SNLog("Proof of work calculation failed.")
handleFailure(with: Error.proofOfWorkCalculationFailed, using: transaction)
return promise
}
// Send the result // Send the result
let snodeMessage = SnodeMessage(recipient: recipient, data: base64EncodedData, ttl: message.ttl, timestamp: timestamp, nonce: nonce) let base64EncodedData = wrappedMessage.base64EncodedString()
let snodeMessage = SnodeMessage(recipient: message.recipient!, data: base64EncodedData, ttl: message.ttl, timestamp: NSDate.millisecondTimestamp())
SnodeAPI.sendMessage(snodeMessage).done(on: DispatchQueue.global(qos: .userInitiated)) { promises in SnodeAPI.sendMessage(snodeMessage).done(on: DispatchQueue.global(qos: .userInitiated)) { promises in
var isSuccess = false var isSuccess = false
let promiseCount = promises.count let promiseCount = promises.count

View File

@ -18,12 +18,13 @@ public final class SnodeAPI : NSObject {
// MARK: Settings // MARK: Settings
private static let maxRetryCount: UInt = 8 private static let maxRetryCount: UInt = 8
private static let minimumSwarmSnodeCount = 3 private static let minimumSwarmSnodeCount = 3
private static let seedNodePool: Set<String> = [ "https://storage.seed1.loki.network:4433", "https://storage.seed3.loki.network:4433", "https://public.loki.foundation:4433" ] private static let seedNodePool: Set<String> = [ "http://public.loki.foundation:38157" ]
private static let snodeFailureThreshold = 3 private static let snodeFailureThreshold = 3
private static let targetSwarmSnodeCount = 2 private static let targetSwarmSnodeCount = 2
/// - Note: Changing this on the fly is not recommended. /// - Note: Changing this on the fly is not recommended.
internal static var useOnionRequests = true internal static var useOnionRequests = true
internal static let useTestnet = false
public static var powDifficulty: UInt = 1 public static var powDifficulty: UInt = 1
@ -266,7 +267,7 @@ public final class SnodeAPI : NSObject {
return Promise<Set<Snode>> { $0.fulfill(cachedSwarm) } return Promise<Set<Snode>> { $0.fulfill(cachedSwarm) }
} else { } else {
SNLog("Getting swarm for: \((publicKey == SNSnodeKitConfiguration.shared.storage.getUserPublicKey()) ? "self" : publicKey).") SNLog("Getting swarm for: \((publicKey == SNSnodeKitConfiguration.shared.storage.getUserPublicKey()) ? "self" : publicKey).")
let parameters: [String:Any] = [ "pubKey" : publicKey ] let parameters: [String:Any] = [ "pubKey" : useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey ]
return getRandomSnode().then2 { snode in return getRandomSnode().then2 { snode in
attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) { attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) {
invoke(.getSwarm, on: snode, associatedWith: publicKey, parameters: parameters) invoke(.getSwarm, on: snode, associatedWith: publicKey, parameters: parameters)
@ -287,7 +288,7 @@ public final class SnodeAPI : NSObject {
storage.pruneLastMessageHashInfoIfExpired(for: snode, associatedWith: publicKey, using: transaction) storage.pruneLastMessageHashInfoIfExpired(for: snode, associatedWith: publicKey, using: transaction)
} }
let lastHash = storage.getLastMessageHash(for: snode, associatedWith: publicKey) ?? "" let lastHash = storage.getLastMessageHash(for: snode, associatedWith: publicKey) ?? ""
let parameters = [ "pubKey" : publicKey, "lastHash" : lastHash ] let parameters = [ "pubKey" : useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
invoke(.getMessages, on: snode, associatedWith: publicKey, parameters: parameters).done2 { seal.fulfill($0) }.catch2 { seal.reject($0) } invoke(.getMessages, on: snode, associatedWith: publicKey, parameters: parameters).done2 { seal.fulfill($0) }.catch2 { seal.reject($0) }
} }
return promise return promise
@ -303,7 +304,7 @@ public final class SnodeAPI : NSObject {
storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey, using: transaction) storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey, using: transaction)
} }
let lastHash = storage.getLastMessageHash(for: targetSnode, associatedWith: publicKey) ?? "" let lastHash = storage.getLastMessageHash(for: targetSnode, associatedWith: publicKey) ?? ""
let parameters = [ "pubKey" : publicKey, "lastHash" : lastHash ] let parameters = [ "pubKey" : useTestnet ? publicKey.removing05PrefixIfNeeded() : publicKey, "lastHash" : lastHash ]
return invoke(.getMessages, on: targetSnode, associatedWith: publicKey, parameters: parameters).map2 { rawResponse in return invoke(.getMessages, on: targetSnode, associatedWith: publicKey, parameters: parameters).map2 { rawResponse in
parseRawMessagesResponse(rawResponse, from: targetSnode, associatedWith: publicKey) parseRawMessagesResponse(rawResponse, from: targetSnode, associatedWith: publicKey)
} }
@ -315,7 +316,7 @@ public final class SnodeAPI : NSObject {
public static func sendMessage(_ message: SnodeMessage) -> Promise<Set<RawResponsePromise>> { public static func sendMessage(_ message: SnodeMessage) -> Promise<Set<RawResponsePromise>> {
let (promise, seal) = Promise<Set<RawResponsePromise>>.pending() let (promise, seal) = Promise<Set<RawResponsePromise>>.pending()
let publicKey = message.recipient let publicKey = useTestnet ? message.recipient.removing05PrefixIfNeeded() : message.recipient
Threading.workQueue.async { Threading.workQueue.async {
getTargetSnodes(for: publicKey).map2 { targetSnodes in getTargetSnodes(for: publicKey).map2 { targetSnodes in
let parameters = message.toJSON() let parameters = message.toJSON()

View File

@ -12,16 +12,13 @@ public final class SnodeMessage : NSObject, NSCoding { // NSObject/NSCoding conf
/// ///
/// - Note: Expressed as milliseconds since 00:00:00 UTC on 1 January 1970. /// - Note: Expressed as milliseconds since 00:00:00 UTC on 1 January 1970.
public let timestamp: UInt64 public let timestamp: UInt64
/// The base 64 encoded proof of work.
public let nonce: String
// MARK: Initialization // MARK: Initialization
public init(recipient: String, data: LosslessStringConvertible, ttl: UInt64, timestamp: UInt64, nonce: String) { public init(recipient: String, data: LosslessStringConvertible, ttl: UInt64, timestamp: UInt64) {
self.recipient = recipient self.recipient = recipient
self.data = data self.data = data
self.ttl = ttl self.ttl = ttl
self.timestamp = timestamp self.timestamp = timestamp
self.nonce = nonce
} }
// MARK: Coding // MARK: Coding
@ -29,13 +26,11 @@ public final class SnodeMessage : NSObject, NSCoding { // NSObject/NSCoding conf
guard let recipient = coder.decodeObject(forKey: "recipient") as! String?, guard let recipient = coder.decodeObject(forKey: "recipient") as! String?,
let data = coder.decodeObject(forKey: "data") as! String?, let data = coder.decodeObject(forKey: "data") as! String?,
let ttl = coder.decodeObject(forKey: "ttl") as! UInt64?, let ttl = coder.decodeObject(forKey: "ttl") as! UInt64?,
let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64?, let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64? else { return nil }
let nonce = coder.decodeObject(forKey: "nonce") as! String? else { return nil }
self.recipient = recipient self.recipient = recipient
self.data = data self.data = data
self.ttl = ttl self.ttl = ttl
self.timestamp = timestamp self.timestamp = timestamp
self.nonce = nonce
super.init() super.init()
} }
@ -44,17 +39,16 @@ public final class SnodeMessage : NSObject, NSCoding { // NSObject/NSCoding conf
coder.encode(data, forKey: "data") coder.encode(data, forKey: "data")
coder.encode(ttl, forKey: "ttl") coder.encode(ttl, forKey: "ttl")
coder.encode(timestamp, forKey: "timestamp") coder.encode(timestamp, forKey: "timestamp")
coder.encode(nonce, forKey: "nonce")
} }
// MARK: JSON Conversion // MARK: JSON Conversion
public func toJSON() -> JSON { public func toJSON() -> JSON {
return [ return [
"pubKey" : recipient, "pubKey" : SnodeAPI.useTestnet ? recipient.removing05PrefixIfNeeded() : recipient,
"data" : data.description, "data" : data.description,
"ttl" : String(ttl), "ttl" : String(ttl),
"timestamp" : String(timestamp), "timestamp" : String(timestamp),
"nonce" : nonce "nonce" : ""
] ]
} }
} }