From 4a084cdfe1580fb22ed9369e30483f98f190a5ed Mon Sep 17 00:00:00 2001 From: joeuhren <46763106+joeuhren@users.noreply.github.com> Date: Mon, 21 Dec 2020 16:19:14 -0700 Subject: [PATCH] Split out peer cmds into new functions -Add new setting for getpeerinfo cmd -getpeerinfo api call moved to explorer.js -Peer geo location api call moved to explorer.js --- README.md | 1 + lib/explorer.js | 34 ++++++++++++++++++ lib/nodeapi.js | 2 ++ lib/settings.js | 1 + scripts/sync.js | 81 ++++++++++++++++++++++++------------------ settings.json.template | 2 ++ 6 files changed, 86 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index ffc0e13..a7217e8 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ This project is a fork of [Ciquidus Explorer](https://github.com/suprnurd/ciquid - **getblock:** Returns an object with information about the block - **getrawtransaction:** Returns raw transaction data - **getinfo:** Returns an object containing various state info + - **getpeerinfo:** Returns data about each connected network node as a json array of objects - **gettxoutsetinfo:** Returns an object with statistics about the unspent transaction output set - **getsupply:** Returns the current money supply - **getmaxmoney:** Returns the number of coins that will be produced in total diff --git a/lib/explorer.js b/lib/explorer.js index 9059511..916a248 100644 --- a/lib/explorer.js +++ b/lib/explorer.js @@ -733,6 +733,40 @@ module.exports = { }); } }, + + get_peerinfo: function(cb) { + var cmd = prepareRpcCommand(settings.api_cmds.getpeerinfo); + + if (!(cmd.method == '' && cmd.parameters.length == 0)) { + if (settings.use_rpc) { + rpcCommand([{method:cmd.method, parameters: cmd.parameters}], function(response) { + // check if an error msg was received from the rpc server + if (response == 'There was an error. Check your console.') + return cb(null); + else + return cb(response); + }); + } else { + var uri = base_url + 'getpeerinfo'; + request({uri: uri, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, body) { + // check if an error msg was received from the web api server + if (body == 'There was an error. Check your console.') + return cb(null); + else + return cb(body); + }); + } + } else { + // cmd not in use. return null. + return cb(null); + } + }, + + get_geo_location: function(address, cb) { + request({uri: 'https://freegeoip.app/json/' + address, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, geo) { + return cb(error, geo); + }); + }, is_unique: function(array, object, cb) { var unique = true; diff --git a/lib/nodeapi.js b/lib/nodeapi.js index b5ea298..93e77e4 100644 --- a/lib/nodeapi.js +++ b/lib/nodeapi.js @@ -40,6 +40,7 @@ module.exports = function(){ case 'getrawtransaction': case 'getsupply': case 'getinfo': + case 'getpeerinfo': case 'gettxoutsetinfo': case 'getmaxmoney': case 'getmaxvote': @@ -126,6 +127,7 @@ module.exports = function(){ case 'getblockcount': case 'getsupply': case 'getinfo': + case 'getpeerinfo': case 'gettxoutsetinfo': case 'getmaxmoney': case 'getmaxvote': diff --git a/lib/settings.js b/lib/settings.js index b789c51..17a88dc 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -194,6 +194,7 @@ exports.api_cmds = { "getblock": "getblock", "getrawtransaction": "getrawtransaction", "getinfo": "getinfo", + "getpeerinfo": "getpeerinfo", "gettxoutsetinfo": "gettxoutsetinfo", "getsupply": "getsupply", "getmaxmoney": "getmaxmoney", diff --git a/scripts/sync.js b/scripts/sync.js index 0a044a6..672dacc 100644 --- a/scripts/sync.js +++ b/scripts/sync.js @@ -6,8 +6,7 @@ var mongoose = require('mongoose') , AddressTx = require('../models/addresstx') , Richlist = require('../models/richlist') , Stats = require('../models/stats') - , settings = require('../lib/settings') - , request = require('postman-request'); + , settings = require('../lib/settings'); var mode = 'update'; var database = 'index'; @@ -185,43 +184,55 @@ if (database == 'peers') { console.log('Aborting'); exit(); } else { - request({uri: 'http://127.0.0.1:' + settings.port + '/api/getpeerinfo', json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, body) { - lib.syncLoop(body.length, function (loop) { - var i = loop.iteration(); - var address = body[i].addr.substring(0, body[i].addr.lastIndexOf(":")).replace("[","").replace("]",""); - var port = body[i].addr.substring(body[i].addr.lastIndexOf(":") + 1); - var rateLimit = new RateLimit(1, 2000, false); - db.find_peer(address, function(peer) { - if (peer) { - if (isNaN(peer['port']) || peer['port'].length < 2 || peer['country'].length < 1 || peer['country_code'].length < 1) { - db.drop_peers(function() { - console.log('Saved peers missing ports or country, dropping peers. Re-run this script afterwards.'); - exit(); - }); - } - // peer already exists - loop.next(); - } else { - rateLimit.schedule(function() { - request({uri: 'https://freegeoip.app/json/' + address, json: true, headers: {'User-Agent': 'eiquidus'}}, function (error, response, geo) { - db.create_peer({ - address: address, - port: port, - protocol: body[i].version, - version: body[i].subver.replace('/', '').replace('/', ''), - country: geo.country_name, - country_code: geo.country_code - }, function(){ - loop.next(); + lib.get_peerinfo(function (body) { + if (body != null) { + lib.syncLoop(body.length, function (loop) { + var i = loop.iteration(); + var address = body[i].addr.substring(0, body[i].addr.lastIndexOf(":")).replace("[","").replace("]",""); + var port = body[i].addr.substring(body[i].addr.lastIndexOf(":") + 1); + var rateLimit = new RateLimit(1, 2000, false); + db.find_peer(address, function(peer) { + if (peer) { + if (isNaN(peer['port']) || peer['port'].length < 2 || peer['country'].length < 1 || peer['country_code'].length < 1) { + db.drop_peers(function() { + console.log('Saved peers missing ports or country, dropping peers. Re-run this script afterwards.'); + exit(); + }); + } + // peer already exists + loop.next(); + } else { + rateLimit.schedule(function() { + lib.get_geo_location(address, function (error, geo) { + // check if an error was returned + if (error) { + console.log(error); + exit(); + } else { + // add peer to collection + db.create_peer({ + address: address, + port: port, + protocol: body[i].version, + version: body[i].subver.replace('/', '').replace('/', ''), + country: geo.country_name, + country_code: geo.country_code + }, function() { + loop.next(); + }); + } }); }); - }); - } + } + }); + }, function() { + console.log('peer sync complete'); + exit(); }); - }, function() { - console.log('peer sync complete'); + } else { + console.log('no peers found'); exit(); - }); + } }); } }); diff --git a/settings.json.template b/settings.json.template index ad73279..806f823 100644 --- a/settings.json.template +++ b/settings.json.template @@ -269,6 +269,7 @@ // getblock: Returns an object with information about the block. // getrawtransaction: Returns raw transaction data. Can return a hex-encoded string that is serialized or an object with txid information depending on the decrypt value (0(false) or 1(true)) // getinfo: Returns an object containing various state info. + // getpeerinfo: Returns data about each connected network node as a json array of objects. // gettxoutsetinfo: Returns an object with statistics about the unspent transaction output set. // getsupply: Returns the current money supply. This should be a positive whole or decimal number. // getmaxmoney: Returns the number of coins that will be produced in total. This should be a positive whole or decimal number. @@ -291,6 +292,7 @@ "getblock": "getblock", "getrawtransaction": "getrawtransaction", "getinfo": "getinfo", + "getpeerinfo": "getpeerinfo", "gettxoutsetinfo": "gettxoutsetinfo", "getsupply": "getsupply", "getmaxmoney": "getmaxmoney",