Skip to content

Commit

Permalink
Merge pull request #66 from SmartCash/fix/faster-nodes
Browse files Browse the repository at this point in the history
Fix/faster nodes
  • Loading branch information
brunohenz committed Jun 2, 2021
2 parents 675994d + 88719c5 commit 2441f31
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/application/lib/sapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ 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));
return JSON.parse(window.sessionStorage.getItem(SAPI_SERVERS_KEY));

const fasterNodes = await testNodeResponseTime(servers);

window.sessionStorage.setItem(SAPI_SERVERS_KEY, JSON.stringify(fasterNodes));

return new Promise((resolve, reject) => resolve(fasterNodes));
} catch (err) {
console.error(err);
}
Expand All @@ -64,6 +68,35 @@ 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;

return { duration: res.duration, statusCode: res.statusCode, ip: server };
} 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)
.map((node) => node.ip);

return new Promise((resolve, reject) => resolve(fastNodes));
}

export function tryToDecryptAES({ textToDecrypt, password }) {
try {
const decryptedWallet = CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(textToDecrypt, password));
Expand Down Expand Up @@ -225,8 +258,6 @@ export async function createAndSendRawTransaction({
};
}

console.log(tx);

return {
status: 200,
value: tx.txid,
Expand Down Expand Up @@ -425,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);
Expand Down

0 comments on commit 2441f31

Please sign in to comment.