Fix not shuffling nodes requested for pubkey

This commit is contained in:
Maxim Shishmarev 2020-07-20 12:31:56 +10:00
parent c843da0e94
commit 8f0d59f9b8
3 changed files with 12 additions and 10 deletions

View file

@ -87,7 +87,9 @@ class LokiMessageAPI {
data: data64,
};
const promises = _.slice(swarm, 0, numConnections).map(snode =>
const usedNodes = _.slice(swarm, 0, numConnections);
const promises = usedNodes.map(snode =>
_openSendConnection(snode, params)
);

View file

@ -152,7 +152,7 @@ interface SendParams {
}
// get snodes for pubkey from random snode. Uses an existing snode
export async function getSnodesForPubkey(
export async function requestSnodesForPubkey(
pubKey: string
): Promise<Array<Snode>> {
const { log } = window;
@ -170,7 +170,7 @@ export async function getSnodesForPubkey(
if (!result) {
log.warn(
`LokiSnodeAPI::_getSnodesForPubkey - lokiRpc on ${snode.ip}:${snode.port} returned falsish value`,
`LokiSnodeAPI::requestSnodesForPubkey - lokiRpc on ${snode.ip}:${snode.port} returned falsish value`,
result
);
return [];
@ -189,7 +189,7 @@ export async function getSnodesForPubkey(
if (!json.snodes) {
// we hit this when snode gives 500s
log.warn(
`LokiSnodeAPI::_getSnodesForPubkey - lokiRpc on ${snode.ip}:${snode.port} returned falsish value for snodes`,
`LokiSnodeAPI::requestSnodesForPubkey - lokiRpc on ${snode.ip}:${snode.port} returned falsish value for snodes`,
result
);
return [];
@ -204,7 +204,7 @@ export async function getSnodesForPubkey(
return [];
}
} catch (e) {
log.error('LokiSnodeAPI::_getSnodesForPubkey - error', e.code, e.message);
log.error('LokiSnodeAPI::requestSnodesForPubkey - error', e.code, e.message);
if (snode) {
markNodeUnreachable(snode);

View file

@ -4,9 +4,9 @@ import {
} from '../../../js/modules/loki_primitives';
import {
getSnodesForPubkey,
getSnodesFromSeedUrl,
getVersion,
requestSnodesForPubkey,
} from './serviceNodeAPI';
import * as Data from '../../../js/modules/data';
@ -117,6 +117,8 @@ export function markNodeUnreachable(snode: Snode): void {
export async function getRandomSnodeAddress(): Promise<Snode> {
// resolve random snode
if (randomSnodePool.length === 0) {
// TODO: ensure that we only call this once at a time
// Should not this be saved to the database?
await refreshRandomPool([]);
if (randomSnodePool.length === 0) {
@ -331,11 +333,9 @@ async function internalUpdateSnodesFor(pubkey: string, edkeys: Array<string>) {
}
export async function getSnodesFor(pubkey: string): Promise<Array<Snode>> {
let maybeNodes = nodesForPubkey.get(pubkey);
const maybeNodes = nodesForPubkey.get(pubkey);
let nodes: Array<string>;
maybeNodes = [];
// NOTE: important that maybeNodes is not [] here
if (maybeNodes === undefined) {
// First time access, try the database:
@ -352,7 +352,7 @@ export async function getSnodesFor(pubkey: string): Promise<Array<Snode>> {
if (goodNodes.length < MIN_NODES) {
// Request new node list from the network
const freshNodes = await getSnodesForPubkey(pubkey);
const freshNodes = _.shuffle(await requestSnodesForPubkey(pubkey));
const edkeys = freshNodes.map((n: Snode) => n.pubkey_ed25519);
// tslint:disable-next-line no-floating-promises