pull/295/head
Beaudan 6 years ago
parent 709db4bf54
commit c02d5d4053

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

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

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

Loading…
Cancel
Save