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 {
switch kind {
case .encryptionKeyPair: return 4 * 24 * 60 * 60 * 1000
default: return 2 * 24 * 60 * 60 * 1000
case .encryptionKeyPair: return 14 * 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 profileKey: Data?
public var contacts: Set<Contact> = []
public override var ttl: UInt64 { 4 * 24 * 60 * 60 * 1000 }
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 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 override init() { }

View File

@ -202,16 +202,9 @@ public final class MessageSender : NSObject {
handleFailure(with: error, using: transaction)
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
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
var isSuccess = false
let promiseCount = promises.count

View File

@ -18,12 +18,13 @@ public final class SnodeAPI : NSObject {
// MARK: Settings
private static let maxRetryCount: UInt = 8
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 targetSwarmSnodeCount = 2
/// - Note: Changing this on the fly is not recommended.
internal static var useOnionRequests = true
internal static let useTestnet = false
public static var powDifficulty: UInt = 1
@ -266,7 +267,7 @@ public final class SnodeAPI : NSObject {
return Promise<Set<Snode>> { $0.fulfill(cachedSwarm) }
} else {
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
attempt(maxRetryCount: 4, recoveringOn: Threading.workQueue) {
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)
}
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) }
}
return promise
@ -303,7 +304,7 @@ public final class SnodeAPI : NSObject {
storage.pruneLastMessageHashInfoIfExpired(for: targetSnode, associatedWith: publicKey, using: transaction)
}
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
parseRawMessagesResponse(rawResponse, from: targetSnode, associatedWith: publicKey)
}
@ -315,7 +316,7 @@ public final class SnodeAPI : NSObject {
public static func sendMessage(_ message: SnodeMessage) -> Promise<Set<RawResponsePromise>> {
let (promise, seal) = Promise<Set<RawResponsePromise>>.pending()
let publicKey = message.recipient
let publicKey = useTestnet ? message.recipient.removing05PrefixIfNeeded() : message.recipient
Threading.workQueue.async {
getTargetSnodes(for: publicKey).map2 { targetSnodes in
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.
public let timestamp: UInt64
/// The base 64 encoded proof of work.
public let nonce: String
// 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.data = data
self.ttl = ttl
self.timestamp = timestamp
self.nonce = nonce
}
// MARK: Coding
@ -29,13 +26,11 @@ public final class SnodeMessage : NSObject, NSCoding { // NSObject/NSCoding conf
guard let recipient = coder.decodeObject(forKey: "recipient") as! String?,
let data = coder.decodeObject(forKey: "data") as! String?,
let ttl = coder.decodeObject(forKey: "ttl") as! UInt64?,
let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64?,
let nonce = coder.decodeObject(forKey: "nonce") as! String? else { return nil }
let timestamp = coder.decodeObject(forKey: "timestamp") as! UInt64? else { return nil }
self.recipient = recipient
self.data = data
self.ttl = ttl
self.timestamp = timestamp
self.nonce = nonce
super.init()
}
@ -44,17 +39,16 @@ public final class SnodeMessage : NSObject, NSCoding { // NSObject/NSCoding conf
coder.encode(data, forKey: "data")
coder.encode(ttl, forKey: "ttl")
coder.encode(timestamp, forKey: "timestamp")
coder.encode(nonce, forKey: "nonce")
}
// MARK: JSON Conversion
public func toJSON() -> JSON {
return [
"pubKey" : recipient,
"pubKey" : SnodeAPI.useTestnet ? recipient.removing05PrefixIfNeeded() : recipient,
"data" : data.description,
"ttl" : String(ttl),
"timestamp" : String(timestamp),
"nonce" : nonce
"nonce" : ""
]
}
}