Merge commit 'b20ac237b23e9582cead41693428c346f2bc95cb' into p2p

pull/20/head
Mikunj 6 years ago
commit 05737cde53

@ -63,7 +63,8 @@ public extension LokiAPI {
return Promise<Message> { seal in return Promise<Message> { seal in
DispatchQueue.global(qos: .default).async { DispatchQueue.global(qos: .default).async {
let now = NSDate.ows_millisecondTimeStamp() let now = NSDate.ows_millisecondTimeStamp()
if let nonce = ProofOfWork.calculate(data: self.data as! String, pubKey: self.destination, timestamp: now, ttl: self.ttl) { let ttlInSeconds = ttl / 1000
if let nonce = ProofOfWork.calculate(data: self.data as! String, pubKey: self.destination, timestamp: now, ttl: ttlInSeconds) {
let result = Message(destination: self.destination, data: self.data, ttl: self.ttl, timestamp: now, nonce: nonce) let result = Message(destination: self.destination, data: self.data, ttl: self.ttl, timestamp: now, nonce: nonce)
seal.fulfill(result) seal.fulfill(result)
} else { } else {

@ -5,7 +5,7 @@ import PromiseKit
// MARK: Settings // MARK: Settings
private static let version = "v1" private static let version = "v1"
public static let defaultMessageTTL: UInt64 = 1 * 24 * 60 * 60 public static let defaultMessageTTL: UInt64 = 1 * 24 * 60 * 60 * 1000
internal static let ourHexEncodedPubKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey internal static let ourHexEncodedPubKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey

@ -63,7 +63,7 @@ public enum ProofOfWork {
/// - data: The message data /// - data: The message data
/// - 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, in **seconds**
/// - Returns: A nonce string or `nil` if it failed /// - Returns: A nonce string or `nil` if it failed
public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: UInt64) -> String? { 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 payload = createPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl)

@ -196,7 +196,7 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
#pragma mark - Update With... Methods #pragma mark - Update With... Methods
// When sending a message, when proof of work calculation is started, we should mark it as such // When sending a message, when proof of work calculation is started, we should mark it as such
- (void)updateIsCalculatingProofOfWorkWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)saveIsCalculatingProofOfWork:(BOOL)isCalculatingPoW withTransaction:(YapDatabaseReadWriteTransaction *)transaction;
// This method is used to record a successful send to one recipient. // This method is used to record a successful send to one recipient.
- (void)updateWithSentRecipient:(NSString *)recipientId - (void)updateWithSentRecipient:(NSString *)recipientId

@ -610,7 +610,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
} }
} }
[message setMostRecentFailureText:error.localizedDescription]; [message setMostRecentFailureText:error.localizedDescription];
[message setIsCalculatingPoW:false]; [message setIsCalculatingPoW:NO];
}]; }];
} }
@ -627,7 +627,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
recipientState.state = OWSOutgoingMessageRecipientStateFailed; recipientState.state = OWSOutgoingMessageRecipientStateFailed;
} }
} }
[message setIsCalculatingPoW:false]; [message setIsCalculatingPoW:NO];
}]; }];
} }
@ -674,13 +674,11 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
}]; }];
} }
- (void)updateIsCalculatingProofOfWorkWithTransaction:(YapDatabaseReadWriteTransaction *)transaction - (void)saveIsCalculatingProofOfWork:(BOOL)isCalculatingPoW withTransaction:(YapDatabaseReadWriteTransaction *)transaction
{ {
OWSAssertDebug(transaction); OWSAssertDebug(transaction);
[self applyChangeToSelfAndLatestCopy:transaction changeBlock:^(TSOutgoingMessage *message) {
[self applyChangeToSelfAndLatestCopy:transaction [message setIsCalculatingPoW:isCalculatingPoW];
changeBlock:^(TSOutgoingMessage *message) {
[message setIsCalculatingPoW:true];
}]; }];
} }
@ -700,7 +698,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
} }
recipientState.state = OWSOutgoingMessageRecipientStateSent; recipientState.state = OWSOutgoingMessageRecipientStateSent;
recipientState.wasSentByUD = wasSentByUD; recipientState.wasSentByUD = wasSentByUD;
[message setIsCalculatingPoW:false]; [message setIsCalculatingPoW:NO];
}]; }];
} }
@ -718,7 +716,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
return; return;
} }
recipientState.state = OWSOutgoingMessageRecipientStateSkipped; recipientState.state = OWSOutgoingMessageRecipientStateSkipped;
[message setIsCalculatingPoW:false]; [message setIsCalculatingPoW:NO];
}]; }];
} }
@ -747,7 +745,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
} }
recipientState.state = OWSOutgoingMessageRecipientStateSent; recipientState.state = OWSOutgoingMessageRecipientStateSent;
recipientState.deliveryTimestamp = deliveryTimestamp; recipientState.deliveryTimestamp = deliveryTimestamp;
[message setIsCalculatingPoW:false]; [message setIsCalculatingPoW:NO];
}]; }];
} }
@ -771,7 +769,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
} }
recipientState.state = OWSOutgoingMessageRecipientStateSent; recipientState.state = OWSOutgoingMessageRecipientStateSent;
recipientState.readTimestamp = @(readTimestamp); recipientState.readTimestamp = @(readTimestamp);
[message setIsCalculatingPoW:false]; [message setIsCalculatingPoW:NO];
}]; }];
} }
@ -841,7 +839,7 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
} }
} }
[message setIsCalculatingPoW:false]; [message setIsCalculatingPoW:NO];
if (!isSentUpdate) { if (!isSentUpdate) {
[message setIsFromLinkedDevice:YES]; [message setIsFromLinkedDevice:YES];

@ -1104,7 +1104,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
// Update the state to show that the proof of work is being calculated // Update the state to show that the proof of work is being calculated
[self setIsCalculatingProofOfWorkForMessage:messageSend]; [self saveIsCalculatingProofOfWork:YES forMessage:messageSend];
// Convert the message to a Loki message and send it using the Loki messaging API // Convert the message to a Loki message and send it using the Loki messaging API
NSDictionary *signalMessage = deviceMessages.firstObject; NSDictionary *signalMessage = deviceMessages.firstObject;
// Update the message and thread if needed // Update the message and thread if needed
@ -1144,6 +1144,8 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
if (messageType == TSFriendRequestMessageType) { if (messageType == TSFriendRequestMessageType) {
[message.thread saveFriendRequestStatus:TSThreadFriendRequestStatusNone withTransaction:nil]; [message.thread saveFriendRequestStatus:TSThreadFriendRequestStatusNone withTransaction:nil];
} }
// Update the PoW calculation status
[self saveIsCalculatingProofOfWork:NO forMessage:messageSend];
// Handle the error // Handle the error
NSUInteger statusCode = 0; NSUInteger statusCode = 0;
NSData *_Nullable responseData = nil; NSData *_Nullable responseData = nil;
@ -1161,7 +1163,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[self messageSendDidFail:messageSend deviceMessages:deviceMessages statusCode:statusCode error:error responseData:responseData]; [self messageSendDidFail:messageSend deviceMessages:deviceMessages statusCode:statusCode error:error responseData:responseData];
}) retainUntilComplete]; }) retainUntilComplete];
} }
}) retainUntilComplete]; }) retainUntilComplete];
// Loki: Original code // Loki: Original code
@ -1227,12 +1228,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
*/ */
} }
- (void)setIsCalculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend - (void)saveIsCalculatingProofOfWork:(BOOL)isCalculatingPoW forMessage:(OWSMessageSend *)messageSend
{ {
OWSAssertDebug(messageSend); OWSAssertDebug(messageSend);
dispatch_async(OWSDispatch.sendingQueue, ^{ dispatch_async(OWSDispatch.sendingQueue, ^{
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[messageSend.message updateIsCalculatingProofOfWorkWithTransaction:transaction]; [messageSend.message saveIsCalculatingProofOfWork:isCalculatingPoW withTransaction:transaction];
}]; }];
}); });
} }

Loading…
Cancel
Save