From db8f8ba36f0f01530e046416d96bdd0570b5f886 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Thu, 11 Apr 2019 10:56:17 +1000 Subject: [PATCH 1/3] Multiple all ttl values by 1000 and stop dividing timestamp by 1000 so they are both milliseconds --- js/modules/loki_message_api.js | 2 +- libloki/proof-of-work.js | 3 ++- libloki/test/metrics.js | 2 +- libloki/test/proof-of-work_test.js | 2 +- libtextsecure/outgoing_message.js | 8 ++++---- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index ca7404871..ef09bb19e 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -14,7 +14,7 @@ class LokiMessageAPI { } async sendMessage(pubKey, data, messageTimeStamp, ttl, isPing = false) { - const timestamp = Math.floor(Date.now() / 1000); + const timestamp = Date.now(); // Data required to identify a message in a conversation const messageEventData = { diff --git a/libloki/proof-of-work.js b/libloki/proof-of-work.js index 3901f13e9..9a9abbfe3 100644 --- a/libloki/proof-of-work.js +++ b/libloki/proof-of-work.js @@ -76,7 +76,8 @@ const pow = { const nonceTrials = _nonceTrials || (development ? DEV_NONCE_TRIALS : PROD_NONCE_TRIALS); - const target = pow.calcTarget(ttl, payload.length, nonceTrials); + // ttl always calculated from hour values, so this floor is redundant + const target = pow.calcTarget(Math.floor(ttl/1000), payload.length, nonceTrials); let nonce = new Uint8Array(NONCE_LEN); nonce = pow.incrementNonce(nonce, startNonce); // initial value diff --git a/libloki/test/metrics.js b/libloki/test/metrics.js index 9614a54ef..cdd49059d 100644 --- a/libloki/test/metrics.js +++ b/libloki/test/metrics.js @@ -25,7 +25,7 @@ async function run(messageLength, numWorkers = 1, nonceTrials = 100, ttl = 72) { jobId, 'calcPoW', timestamp, - ttl * 60 * 60, + ttl * 60 * 60 * 1000, pubKey, data, false, diff --git a/libloki/test/proof-of-work_test.js b/libloki/test/proof-of-work_test.js index 06ec6bc38..9b5aaa9f6 100644 --- a/libloki/test/proof-of-work_test.js +++ b/libloki/test/proof-of-work_test.js @@ -46,7 +46,7 @@ describe('Proof of Work', () => { it('should calculate a correct difficulty target', () => { // These values will need to be updated if we adjust the difficulty settings let payloadLen = 625; - const ttl = 86400; + const ttl = 86400000; let expectedTarget = new Uint8Array([0, 4, 119, 164, 35, 224, 222, 64]); let actualTarget = calcTarget(ttl, payloadLen, 10); diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 4d940ae9c..f2bb059ef 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -184,7 +184,7 @@ OutgoingMessage.prototype = { }, // Default ttl to 24 hours if no value provided - async transmitMessage(number, data, timestamp, ttl = 24 * 60 * 60) { + async transmitMessage(number, data, timestamp, ttl = 24 * 60 * 60 * 1000) { const pubKey = number; try { await lokiMessageAPI.sendMessage( @@ -345,12 +345,12 @@ OutgoingMessage.prototype = { } let ttl; if (this.messageType === 'friend-request') { - ttl = 4 * 24 * 60 * 60; // 4 days for friend request message + ttl = 4 * 24 * 60 * 60 * 1000; // 4 days for friend request message } else if (this.messageType === 'onlineBroadcast') { - ttl = 60; // 1 minute for online broadcast message + ttl = 60 * 1000; // 1 minute for online broadcast message } else { const hours = window.getMessageTTL() || 24; // 1 day default for any other message - ttl = hours * 60 * 60; + ttl = hours * 60 * 60 * 1000; } return { From b9c11a229b164f445ae47700faed24d964890da3 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Thu, 11 Apr 2019 12:14:01 +1000 Subject: [PATCH 2/3] Fix tests by handling millisecond ttl better --- libloki/proof-of-work.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libloki/proof-of-work.js b/libloki/proof-of-work.js index 9a9abbfe3..5be655485 100644 --- a/libloki/proof-of-work.js +++ b/libloki/proof-of-work.js @@ -77,7 +77,7 @@ const pow = { const nonceTrials = _nonceTrials || (development ? DEV_NONCE_TRIALS : PROD_NONCE_TRIALS); // ttl always calculated from hour values, so this floor is redundant - const target = pow.calcTarget(Math.floor(ttl/1000), payload.length, nonceTrials); + const target = pow.calcTarget(ttl, payload.length, nonceTrials); let nonce = new Uint8Array(NONCE_LEN); nonce = pow.incrementNonce(nonce, startNonce); // initial value @@ -107,8 +107,10 @@ const pow = { calcTarget(ttl, payloadLen, nonceTrials = PROD_NONCE_TRIALS) { // payloadLength + NONCE_LEN const totalLen = JSBI.add(JSBI.BigInt(payloadLen), JSBI.BigInt(NONCE_LEN)); + // ttl converted to seconds + const ttlSeconds = JSBI.divide(JSBI.BigInt(ttl), JSBI.BigInt(1000)); // ttl * totalLen - const ttlMult = JSBI.multiply(JSBI.BigInt(ttl), JSBI.BigInt(totalLen)); + const ttlMult = JSBI.multiply(ttlSeconds, JSBI.BigInt(totalLen)); // 2^16 - 1 const two16 = JSBI.subtract( JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(16)), // 2^16 From ee813a112664c0a2a47dc75fc589fe617678152f Mon Sep 17 00:00:00 2001 From: Beaudan Campbell-Brown Date: Thu, 11 Apr 2019 13:04:44 +1000 Subject: [PATCH 3/3] Update libloki/proof-of-work.js --- libloki/proof-of-work.js | 1 - 1 file changed, 1 deletion(-) diff --git a/libloki/proof-of-work.js b/libloki/proof-of-work.js index 5be655485..039e2395d 100644 --- a/libloki/proof-of-work.js +++ b/libloki/proof-of-work.js @@ -76,7 +76,6 @@ const pow = { const nonceTrials = _nonceTrials || (development ? DEV_NONCE_TRIALS : PROD_NONCE_TRIALS); - // ttl always calculated from hour values, so this floor is redundant const target = pow.calcTarget(ttl, payload.length, nonceTrials); let nonce = new Uint8Array(NONCE_LEN);