Skip to content

Commit

Permalink
Add getBestNode
Browse files Browse the repository at this point in the history
  • Loading branch information
n9lsjr committed Feb 19, 2024
1 parent 9816d00 commit 9f83d4c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
19 changes: 14 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand All @@ -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)
Expand Down
64 changes: 62 additions & 2 deletions utils/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 9f83d4c

Please sign in to comment.