From 6bb56ae8977c7872254ea8d1f7d10a8ad8e93b59 Mon Sep 17 00:00:00 2001 From: Bruno Henz Date: Tue, 1 Jun 2021 23:53:03 -0300 Subject: [PATCH 1/2] Add test node method --- src/application/lib/sapi.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/application/lib/sapi.js b/src/application/lib/sapi.js index 169d159..97338bc 100644 --- a/src/application/lib/sapi.js +++ b/src/application/lib/sapi.js @@ -41,7 +41,9 @@ export async function getEnabledNodes() { cache: true, }); const servers = nodes.map((node) => 'http://' + node.ip.replace(':9678', ':8080')); - window.sessionStorage.setItem(SAPI_SERVERS_KEY, JSON.stringify(servers)); + + const fasterNodes = await testNodeResponseTime(servers); + window.sessionStorage.setItem(SAPI_SERVERS_KEY, JSON.stringify(fasterNodes)); return JSON.parse(window.sessionStorage.getItem(SAPI_SERVERS_KEY)); } catch (err) { console.error(err); @@ -64,6 +66,36 @@ async function getEnabledNode(sapis) { return electedSapi; } +export async function testNodeResponseTime(sapis) { + const testedNodes = await Promise.all( + sapis.slice(0, 29).map(async (server) => { + try { + const start = Date.now(); + const res = await request.get(`${server}/v1/client/status`, { + json: true, + cache: true, + }); + const duration = Date.now() - start; + res.duration = duration; + res.statusCode = 200; + console.log(res); + return res; + } catch (e) { + return null; + } + }) + ); + + const fastNodes = testedNodes + .filter((nodeResponse) => nodeResponse && nodeResponse.statusCode === 200 && nodeResponse.duration < 900) + .sort((firstItem, secondItem) => firstItem.duration - secondItem.duration) + .slice(0, 29); + + console.log(fastNodes); + + return fastNodes; +} + export function tryToDecryptAES({ textToDecrypt, password }) { try { const decryptedWallet = CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(textToDecrypt, password)); @@ -225,8 +257,6 @@ export async function createAndSendRawTransaction({ }; } - console.log(tx); - return { status: 200, value: tx.txid, From 88719c5532c649e8fec4882f80a235dbb923a31a Mon Sep 17 00:00:00 2001 From: JP Date: Wed, 2 Jun 2021 00:53:21 -0300 Subject: [PATCH 2/2] modify testNodeResponseTime method in sapi.js --- src/application/lib/sapi.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/application/lib/sapi.js b/src/application/lib/sapi.js index 97338bc..7224732 100644 --- a/src/application/lib/sapi.js +++ b/src/application/lib/sapi.js @@ -43,8 +43,10 @@ export async function getEnabledNodes() { const servers = nodes.map((node) => 'http://' + node.ip.replace(':9678', ':8080')); const fasterNodes = await testNodeResponseTime(servers); + window.sessionStorage.setItem(SAPI_SERVERS_KEY, JSON.stringify(fasterNodes)); - return JSON.parse(window.sessionStorage.getItem(SAPI_SERVERS_KEY)); + + return new Promise((resolve, reject) => resolve(fasterNodes)); } catch (err) { console.error(err); } @@ -78,8 +80,8 @@ export async function testNodeResponseTime(sapis) { const duration = Date.now() - start; res.duration = duration; res.statusCode = 200; - console.log(res); - return res; + + return { duration: res.duration, statusCode: res.statusCode, ip: server }; } catch (e) { return null; } @@ -89,11 +91,10 @@ export async function testNodeResponseTime(sapis) { const fastNodes = testedNodes .filter((nodeResponse) => nodeResponse && nodeResponse.statusCode === 200 && nodeResponse.duration < 900) .sort((firstItem, secondItem) => firstItem.duration - secondItem.duration) - .slice(0, 29); + .slice(0, 29) + .map((node) => node.ip); - console.log(fastNodes); - - return fastNodes; + return new Promise((resolve, reject) => resolve(fastNodes)); } export function tryToDecryptAES({ textToDecrypt, password }) { @@ -455,7 +456,7 @@ export async function getTransactionHistory(address, pageSize = 50) { }, json: true, // Automatically stringifies the body to JSON }; - + return await request.post(options).then((res) => res.data); } catch (err) { console.error(err);