rename serviceNodeApi to SNodeApi

This commit is contained in:
Audric Ackermann 2021-05-13 14:52:13 +10:00
parent 8cb50ebc91
commit c63d5a4581
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4
5 changed files with 30 additions and 79 deletions

View File

@ -1,5 +1,5 @@
import _ from 'lodash';
import { SendParams, storeOnNode } from '../snode_api/serviceNodeAPI';
import { SendParams, storeOnNode } from '../snode_api/SNodeAPI';
import { getSwarm, Snode } from '../snode_api/snodePool';
import { firstTrue } from '../utils/Promise';

View File

@ -22,7 +22,7 @@ import {
markNodeUnreachable,
requiredSnodesForAgreement,
Snode,
updateSnodesFor,
updateSwarmFor,
} from './snodePool';
import { Constants } from '..';
import { sleepFor } from '../utils/Promise';
@ -438,7 +438,7 @@ export async function storeOnNode(targetNode: Snode, params: SendParams): Promis
);
if (e instanceof textsecure.WrongSwarmError) {
const { newSwarm } = e;
await updateSnodesFor(params.pubKey, newSwarm);
await updateSwarmFor(params.pubKey, newSwarm);
return false;
} else if (e instanceof textsecure.NotFoundError) {
// TODO: Handle resolution error
@ -458,7 +458,7 @@ export async function storeOnNode(targetNode: Snode, params: SendParams): Promis
node => node.pubkey_ed25519 !== targetNode.pubkey_ed25519
);
await updateSnodesFor(params.pubKey, updatedSwarm);
await updateSwarmFor(params.pubKey, updatedSwarm);
}
successiveFailures += 1;
}
@ -497,7 +497,7 @@ export async function retrieveNextMessages(
window.log.warn('loki_message:::retrieveNextMessages - send error:', e.code, e.message);
if (e instanceof window.textsecure.WrongSwarmError) {
const { newSwarm } = e;
await updateSnodesFor(params.pubKey, newSwarm);
await updateSwarmFor(params.pubKey, newSwarm);
return [];
} else if (e instanceof window.textsecure.InvalidateSwarm) {
const existingSwarm = await getSwarm(params.pubKey);
@ -505,7 +505,7 @@ export async function retrieveNextMessages(
node => node.pubkey_ed25519 !== targetNode.pubkey_ed25519
);
await updateSnodesFor(params.pubKey, updatedSwarm);
await updateSwarmFor(params.pubKey, updatedSwarm);
return [];
}
}

View File

@ -196,70 +196,38 @@ const processOnionResponse = async (
): Promise<SnodeResponse | RequestError> => {
const { log, libloki } = window;
let content;
let ciphertext = '';
try {
content = await response.text();
ciphertext = await response.text();
} catch (e) {
window.log.warn(e);
content = '';
}
// if (response.status !== 200) {
// debugger;
// }
if (abortSignal?.aborted) {
log.warn(`(${reqIdx}) [path] Call aborted`);
return RequestError.ABORTED;
}
// detect SNode is deregisted?
if (response.status === 502) {
log.warn(`(${reqIdx}) [path] Got 502: snode not found`);
// detect SNode is deregisted, or SNode is not ready (not in swarm; not done syncing, ...)
if (
response.status === 502 ||
response.status === 503 ||
response.status === 504 ||
response.status === 404 ||
response.status !== 200
) {
log.warn(`(${reqIdx}) [path] Got status: ${response.status}`);
return RequestError.BAD_PATH;
}
// detect SNode is not ready (not in swarm; not done syncing)
if (response.status === 503) {
log.warn(`(${reqIdx}) [path] Got 503: snode not ready`);
return RequestError.BAD_PATH;
}
if (response.status === 504) {
log.warn(`(${reqIdx}) [path] Got 504: Gateway timeout`);
return RequestError.BAD_PATH;
}
if (response.status === 404) {
// Why would we get this error on testnet?
log.warn(`(${reqIdx}) [path] Got 404: Gateway timeout`);
return RequestError.BAD_PATH;
}
if (response.status !== 200) {
log.warn(
`(${reqIdx}) [path] lokiRpc::processingOnionResponse - fetch unhandled error code: ${response.status}: ${content}`
);
// FIXME audric
// this is pretty strong but on the current setup.
// we have to increase a snode invididually and only mark later the path as bad
// the way it works on mobile is that we treat a node as bad in that case, and if it then reaches a failure count of 3 or so we kick it out and rebuild the path
return RequestError.BAD_PATH;
}
let ciphertext = content;
if (!ciphertext) {
log.warn(
`(${reqIdx}) [path] lokiRpc::processingOnionResponse - Target node return empty ciphertext`
);
return RequestError.OTHER;
}
if (debug) {
log.debug(`(${reqIdx}) [path] lokiRpc::processingOnionResponse - ciphertext`, ciphertext);
}
let plaintext;
let ciphertextBuffer;
@ -272,19 +240,7 @@ const processOnionResponse = async (
}
try {
ciphertextBuffer = fromBase64ToArrayBuffer(ciphertext);
if (debug) {
log.debug(
`(${reqIdx}) [path] lokiRpc::processingOnionResponse - ciphertextBuffer`,
toHex(ciphertextBuffer)
);
}
const plaintextBuffer = await libloki.crypto.DecryptAESGCM(symmetricKey, ciphertextBuffer);
if (debug) {
log.debug('lokiRpc::processingOnionResponse - plaintextBuffer', plaintextBuffer.toString());
}
plaintext = new TextDecoder().decode(plaintextBuffer);
} catch (e) {
log.error(`(${reqIdx}) [path] lokiRpc::processingOnionResponse - decode error`, e);
@ -459,13 +415,13 @@ const sendOnionRequest = async (
async function sendOnionRequestSnodeDest(
reqIdx: any,
nodePath: Array<Snode>,
onionPath: Array<Snode>,
targetNode: Snode,
plaintext?: string
) {
return sendOnionRequest(
reqIdx,
nodePath,
onionPath,
targetNode.pubkey_x25519,
{
destination_ed25519_hex: targetNode.pubkey_ed25519,
@ -479,7 +435,7 @@ async function sendOnionRequestSnodeDest(
// need relay node's pubkey_x25519_hex
export async function sendOnionRequestLsrpcDest(
reqIdx: number,
nodePath: Array<Snode>,
onionPath: Array<Snode>,
destX25519Any: string,
finalRelayOptions: FinalRelayOptions,
payloadObj: FinalDestinationOptions,
@ -488,7 +444,7 @@ export async function sendOnionRequestLsrpcDest(
): Promise<SnodeResponse | RequestError> {
return sendOnionRequest(
reqIdx,
nodePath,
onionPath,
destX25519Any,
payloadObj,
finalRelayOptions,

View File

@ -1,15 +1,10 @@
import semver from 'semver';
import _ from 'lodash';
import {
getSnodePoolFromSnodes,
getSnodesFromSeedUrl,
requestSnodesForPubkey,
} from './serviceNodeAPI';
import { getSnodePoolFromSnodes, getSnodesFromSeedUrl, requestSnodesForPubkey } from './SNodeAPI';
import * as Data from '../../../ts/data/data';
export type SnodeEdKey = string;
import { allowOnlyOneAtATime } from '../utils/Promise';
import pRetry from 'p-retry';
@ -36,7 +31,7 @@ export interface Snode {
ip: string;
port: number;
pubkey_x25519: string;
pubkey_ed25519: SnodeEdKey;
pubkey_ed25519: string;
version: string;
}
@ -44,7 +39,7 @@ export interface Snode {
let randomSnodePool: Array<Snode> = [];
// We only store nodes' identifiers here,
const nodesForPubkey: Map<string, Array<SnodeEdKey>> = new Map();
const nodesForPubkey: Map<string, Array<string>> = new Map();
export type SeedNode = {
url: string;
@ -118,7 +113,7 @@ export function markNodeUnreachable(snode: Snode): void {
for (const [pubkey, nodes] of nodesForPubkey) {
const edkeys = _.filter(nodes, edkey => edkey !== snode.pubkey_ed25519);
void internalUpdateSnodesFor(pubkey, edkeys);
void internalUpdateSwarmFor(pubkey, edkeys);
}
log.warn(
@ -288,12 +283,12 @@ export async function refreshRandomPool(): Promise<void> {
});
}
export async function updateSnodesFor(pubkey: string, snodes: Array<Snode>): Promise<void> {
export async function updateSwarmFor(pubkey: string, snodes: Array<Snode>): Promise<void> {
const edkeys = snodes.map((sn: Snode) => sn.pubkey_ed25519);
await internalUpdateSnodesFor(pubkey, edkeys);
await internalUpdateSwarmFor(pubkey, edkeys);
}
async function internalUpdateSnodesFor(pubkey: string, edkeys: Array<string>) {
async function internalUpdateSwarmFor(pubkey: string, edkeys: Array<string>) {
// update our in-memory cache
nodesForPubkey.set(pubkey, edkeys);
// write this change to the db
@ -321,7 +316,7 @@ export async function getSwarm(pubkey: string): Promise<Array<Snode>> {
const freshNodes = _.shuffle(await requestSnodesForPubkey(pubkey));
const edkeys = freshNodes.map((n: Snode) => n.pubkey_ed25519);
await internalUpdateSnodesFor(pubkey, edkeys);
await internalUpdateSwarmFor(pubkey, edkeys);
return freshNodes;
} else {

View File

@ -1,6 +1,6 @@
import { PubKey } from '../types';
import { getSwarm, Snode } from './snodePool';
import { retrieveNextMessages } from './serviceNodeAPI';
import { retrieveNextMessages } from './SNodeAPI';
import { SignalService } from '../../protobuf';
import * as Receiver from '../../receiver/receiver';
import _ from 'lodash';