This commit is contained in:
Beaudan 2019-06-03 10:29:11 +10:00
parent 709db4bf54
commit c02d5d4053
3 changed files with 25 additions and 39 deletions

View file

@ -32,22 +32,10 @@ const filterIncomingMessages = async messages => {
const calcNonce = async (messageEventData, pubKey, data64, timestamp, ttl) => {
// Nonce is returned as a base64 string to include in header
try {
window.Whisper.events.trigger('calculatingPoW', messageEventData);
const development = window.getEnvironment() !== 'production';
return callWorker(
'calcPoW',
timestamp,
ttl,
pubKey,
data64,
development
);
} catch (err) {
// Something went horribly wrong
throw err;
}
}
window.Whisper.events.trigger('calculatingPoW', messageEventData);
const development = window.getEnvironment() !== 'production';
return callWorker('calcPoW', timestamp, ttl, pubKey, data64, development);
};
const trySendP2p = async (pubKey, data64, isPing, messageEventData) => {
const p2pDetails = lokiP2pAPI.getContactP2pDetails(pubKey);
@ -78,7 +66,7 @@ const trySendP2p = async (pubKey, data64, isPing, messageEventData) => {
log.warn('Failed to send P2P message, falling back to storage', e);
return false;
}
}
};
class LokiMessageAPI {
constructor({ snodeServerPort }) {
@ -96,15 +84,28 @@ class LokiMessageAPI {
};
const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64');
const p2pSuccess = await trySendP2p(pubKey, data64, isPing, messageEventData);
const p2pSuccess = await trySendP2p(
pubKey,
data64,
isPing,
messageEventData
);
if (p2pSuccess) {
return;
}
const timestamp = Date.now();
const nonce = await calcNonce(messageEventData, pubKey, data64, timestamp, ttl);
const nonce = await calcNonce(
messageEventData,
pubKey,
data64,
timestamp,
ttl
);
// Using timestamp as a unique identifier
this.sendingSwarmNodes[timestamp] = lokiSnodeAPI.getSwarmNodesForPubKey(pubKey);
this.sendingSwarmNodes[timestamp] = lokiSnodeAPI.getSwarmNodesForPubKey(
pubKey
);
if (this.sendingSwarmNodes[timestamp].length < numConnections) {
const freshNodes = await lokiSnodeAPI.getFreshSwarmNodes(pubKey);
await lokiSnodeAPI.updateSwarmNodes(pubKey, freshNodes);
@ -175,10 +176,7 @@ class LokiMessageAPI {
}
}
log.error(`Failed to send to node: ${url}`);
await lokiSnodeAPI.unreachableNode(
params.pubKey,
url
);
await lokiSnodeAPI.unreachableNode(params.pubKey, url);
return false;
}
@ -215,11 +213,7 @@ class LokiMessageAPI {
await sleepFor(successiveFailures * 1000);
try {
let messages = await this.retrieveNextMessages(
url,
nodeData,
ourKey
);
let messages = await this.retrieveNextMessages(url, nodeData, ourKey);
successiveFailures = 0;
if (messages.length) {
const lastMessage = _.last(messages);
@ -266,7 +260,6 @@ class LokiMessageAPI {
// or if there is network issues (ENOUTFOUND due to lokinet)
await Promise.all(promises);
}
}
module.exports = LokiMessageAPI;

View file

@ -8,7 +8,6 @@ const { rpc } = require('./loki_rpc');
// Will be raised (to 3?) when we get more nodes
const MINIMUM_SWARM_NODES = 1;
const FAILURE_THRESHOLD = 3;
const resolve4 = url =>
new Promise((resolve, reject) => {

View file

@ -192,14 +192,8 @@ OutgoingMessage.prototype = {
const options = {
numConnections: NUM_SEND_CONNECTIONS,
isPing: this.isPing,
}
await lokiMessageAPI.sendMessage(
pubKey,
data,
timestamp,
ttl,
options
);
};
await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl, options);
} catch (e) {
if (e.name === 'HTTPError' && (e.code !== 409 && e.code !== 410)) {
// 409 and 410 should bubble and be handled by doSendMessage