From 9f83d4c9b21a0ec6fefe737d7372bb76f75b56ef Mon Sep 17 00:00:00 2001 From: n9lsjr Date: Mon, 19 Feb 2024 19:11:50 +0100 Subject: [PATCH] Add getBestNode --- server.js | 19 +++++++++++---- utils/wallet.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index fcdcf5d..f3459ac 100644 --- a/server.js +++ b/server.js @@ -139,9 +139,17 @@ app.listen(process.env.SYS_API_PORT, async () => { // do not start the hugin syncer if we want to test the endpoints if (process.env.NODE_ENV !== 'test') { + + //Get best node + let [url, port] = await getBestNode() + //Fallback + if (!url) { + url = "privacymine.net" + port = 21898 + } - // initializing daemon and wallet - const daemon = new WB.Daemon('pool.kryptokrona.se', 11898) + // Initializing daemon and wallet + const daemon = new WB.Daemon(url, port) global.wallet = await openWallet(daemon) await wallet.start() console.log('👛 Started wallet.') @@ -150,13 +158,14 @@ app.listen(process.env.SYS_API_PORT, async () => { saveWallet(wallet) wallet.on('createdtx', async () => { - optimizeMessages(wallet) + optimizeMessages(wallet, 20) }) - wallet.on('incomingtx', async () => { - optimizeMessages(wallet) + wallet.on('transaction', async () => { + optimizeMessages(wallet, 20) }) + // starting hugin sync while (true) { await sleep(process.env.SYS_HUGIN_SYNCER_SLEEP) diff --git a/utils/wallet.js b/utils/wallet.js index a44fd11..ade60f2 100644 --- a/utils/wallet.js +++ b/utils/wallet.js @@ -152,7 +152,7 @@ async function walletExistsInDb() { } } -async function optimizeMessages(nbrOfTxs, fee = 10000, attempt = 0) { +async function optimizeMessages(wallet, nbrOfTxs, fee = 10000, attempt = 0) { if (attempt > 10) { return false } @@ -209,8 +209,68 @@ async function optimizeMessages(nbrOfTxs, fee = 10000, attempt = 0) { return true } +const fetchNodes = async () => { + try { + const response = await fetch(uri) + const result = await response.json() + + } catch (e) { + console.log("Error fetching nodes") + return false + } + + return result.nodes +} + +export const getBestNode = async (ssl=true) => { + + let recommended_node = undefined; + const nodes = await fetchNodes() + if (!nodes) return [false, 0] + let node_requests = []; + let ssl_nodes =[]; + if (ssl) { + ssl_nodes = nodes.filter(node => {return node.ssl}); + } else { + ssl_nodes = nodes.filter(node => {return !node.ssl}); + } + + ssl_nodes = ssl_nodes.sort((a, b) => 0.5 - Math.random()); + + console.log(ssl_nodes); + + let i = 0; + + while (i < ssl_nodes.length) { + + + let this_node = ssl_nodes[i]; + + let nodeURL = `${this_node.ssl ? 'https://' : 'http://'}${this_node.url}:${this_node.port}/info`; + try { + const resp = await fetch(nodeURL, { + method: 'GET', + }, 2000); + + if (resp.ok) { + + recommended_node = [this_node.url, this_node.port]; + console.log("resp ok!", recommended_node) + return recommended_node; + } + } catch (e) { + console.log(e); + } + i++; + } + + return false + + } + module.exports = { openWallet, saveWallet, - optimizeMessages + optimizeMessages, + getBestNode }