Merge commit 'b20ac237b23e9582cead41693428c346f2bc95cb' into p2p

This commit is contained in:
Mikunj 2019-05-24 09:37:07 +10:00
commit 05737cde53
6 changed files with 21 additions and 21 deletions

View file

@ -63,7 +63,8 @@ public extension LokiAPI {
return Promise<Message> { seal in
DispatchQueue.global(qos: .default).async {
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)
seal.fulfill(result)
} else {

View file

@ -5,7 +5,7 @@ import PromiseKit
// MARK: Settings
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

View file

@ -63,7 +63,7 @@ public enum ProofOfWork {
/// - data: The message data
/// - pubKey: The message recipient
/// - 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
public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: UInt64) -> String? {
let payload = createPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl)

View file

@ -196,7 +196,7 @@ typedef NS_ENUM(NSInteger, TSGroupMetaMessage) {
#pragma mark - Update With... Methods
// 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.
- (void)updateWithSentRecipient:(NSString *)recipientId

View file

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

View file

@ -1104,7 +1104,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
}
// 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
NSDictionary *signalMessage = deviceMessages.firstObject;
// Update the message and thread if needed
@ -1144,6 +1144,8 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
if (messageType == TSFriendRequestMessageType) {
[message.thread saveFriendRequestStatus:TSThreadFriendRequestStatusNone withTransaction:nil];
}
// Update the PoW calculation status
[self saveIsCalculatingProofOfWork:NO forMessage:messageSend];
// Handle the error
NSUInteger statusCode = 0;
NSData *_Nullable responseData = nil;
@ -1161,7 +1163,6 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[self messageSendDidFail:messageSend deviceMessages:deviceMessages statusCode:statusCode error:error responseData:responseData];
}) retainUntilComplete];
}
}) retainUntilComplete];
// Loki: Original code
@ -1227,12 +1228,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
*/
}
- (void)setIsCalculatingProofOfWorkForMessage:(OWSMessageSend *)messageSend
- (void)saveIsCalculatingProofOfWork:(BOOL)isCalculatingPoW forMessage:(OWSMessageSend *)messageSend
{
OWSAssertDebug(messageSend);
dispatch_async(OWSDispatch.sendingQueue, ^{
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[messageSend.message updateIsCalculatingProofOfWorkWithTransaction:transaction];
[messageSend.message saveIsCalculatingProofOfWork:isCalculatingPoW withTransaction:transaction];
}];
});
}