Code cleanup

-Replace tabs with double-spaces
-Betting spacing and lining up of code functions
-Add missing semi-colons
-Remove extra characters and spaces where applicable
-Remove commented-out code fragments
-Add missing 2021 date to LICENSE
-Small touchups and other misc nitpickings
This commit is contained in:
joeuhren
2021-03-17 17:54:09 -06:00
parent 8304eb211d
commit 20c0a382a3
42 changed files with 1001 additions and 860 deletions
+147 -124
View File
@@ -1,18 +1,18 @@
var mongoose = require('mongoose')
, Stats = require('../models/stats')
, Markets = require('../models/markets')
, Masternode = require('../models/masternode')
, Address = require('../models/address')
, AddressTx = require('../models/addresstx')
, Tx = require('../models/tx')
, Richlist = require('../models/richlist')
, Peers = require('../models/peers')
, Heavy = require('../models/heavy')
, lib = require('./explorer')
, settings = require('./settings')
, fs = require('fs')
, coindesk = require('./apis/coindesk')
, async = require('async');
var mongoose = require('mongoose'),
Stats = require('../models/stats'),
Markets = require('../models/markets'),
Masternode = require('../models/masternode'),
Address = require('../models/address'),
AddressTx = require('../models/addresstx'),
Tx = require('../models/tx'),
Richlist = require('../models/richlist'),
Peers = require('../models/peers'),
Heavy = require('../models/heavy'),
lib = require('./explorer'),
settings = require('./settings'),
fs = require('fs'),
coindesk = require('./apis/coindesk'),
async = require('async');
function find_address(hash, caseSensitive, cb) {
if (caseSensitive) {
@@ -36,21 +36,19 @@ function find_address(hash, caseSensitive, cb) {
function find_address_tx(address, hash, cb) {
AddressTx.findOne({a_id: address, txid: hash}, function(err, address_tx) {
if(address_tx) {
if (address_tx)
return cb(address_tx);
} else {
else
return cb();
}
});
}
function find_richlist(coin, cb) {
Richlist.findOne({coin: coin}, function(err, richlist) {
if(richlist) {
if (richlist)
return cb(richlist);
} else {
else
return cb();
}
});
}
@@ -58,9 +56,10 @@ function update_address(hash, blockheight, txid, amount, type, cb) {
var to_sent = false;
var to_received = false;
var addr_inc = {}
if ( hash == 'coinbase' ) {
if (hash == 'coinbase')
addr_inc.sent = amount;
} else {
else {
if (type == 'vin') {
addr_inc.sent = amount;
addr_inc.balance = -amount;
@@ -69,16 +68,17 @@ function update_address(hash, blockheight, txid, amount, type, cb) {
addr_inc.balance = amount;
}
}
Address.findOneAndUpdate({a_id: hash}, {
$inc: addr_inc
}, {
new: true,
upsert: true
}, function (err, address) {
if (err) {
if (err)
return cb(err);
} else {
if ( hash != 'coinbase' ) {
else {
if (hash != 'coinbase') {
AddressTx.findOneAndUpdate({a_id: hash, txid: txid}, {
$inc: {
amount: addr_inc.balance
@@ -92,26 +92,23 @@ function update_address(hash, blockheight, txid, amount, type, cb) {
new: true,
upsert: true
}, function (err,addresstx) {
if (err) {
if (err)
return cb(err);
} else {
else
return cb();
}
});
} else {
} else
return cb();
}
}
});
}
function find_tx(txid, cb) {
Tx.findOne({txid: txid}, function(err, tx) {
if(tx) {
if (tx)
return cb(tx);
} else {
else
return cb(null);
}
});
}
@@ -122,12 +119,14 @@ function save_tx(txid, blockheight, cb) {
lib.prepare_vout(tx.vout, txid, vin, ((!settings.blockchain_specific.zksnarks.enabled || typeof tx.vjoinsplit === 'undefined' || tx.vjoinsplit == null) ? [] : tx.vjoinsplit), function(vout, nvin) {
lib.syncLoop(vin.length, function (loop) {
var i = loop.iteration();
update_address(nvin[i].addresses, blockheight, txid, nvin[i].amount, 'vin', function() {
loop.next();
});
}, function() {
lib.syncLoop(vout.length, function (subloop) {
var t = subloop.iteration();
if (vout[t].addresses) {
update_address(vout[t].addresses, blockheight, txid, vout[t].amount, 'vout', function() {
subloop.next();
@@ -143,8 +142,9 @@ function save_tx(txid, blockheight, cb) {
total: total.toFixed(8),
timestamp: tx.time,
blockhash: tx.blockhash,
blockindex: blockheight,
blockindex: blockheight
});
newTx.save(function(err) {
if (err)
return cb(err, false);
@@ -164,6 +164,7 @@ function save_tx(txid, blockheight, cb) {
function get_market_data(market, coin_symbol, pair_symbol, cb) {
if (fs.existsSync('./lib/markets/' + market + '.js')) {
exMarket = require('./markets/' + market);
exMarket.get_data({coin: coin_symbol, exchange: pair_symbol}, function(err, obj) {
return cb(err, obj);
});
@@ -216,6 +217,7 @@ module.exports = {
// ensure that if this address exists in the richlist that it displays the new alias
module.exports.get_richlist(settings.coin.name, function(richlist) {
var updated = false;
// loop through received addresses
for (r = 0; r < richlist.received.length; r++) {
// check if this is the correct address
@@ -226,6 +228,7 @@ module.exports = {
updated = true;
}
}
// loop through balance addresses
for (b = 0; b < richlist.balance.length; b++) {
// check if this is the correct address
@@ -233,9 +236,10 @@ module.exports = {
// update the claim name
richlist.balance[b]['name'] = claim_name;
// mark as updated
updated = true;
updated = true;
}
}
// check if the address was updated in the richlist
if (updated) {
// save the richlist back to collection
@@ -263,6 +267,7 @@ module.exports = {
// ensure that if this address exists in the masternode that it displays the new alias
module.exports.get_masternodes(function(masternodes) {
var updated = false;
// loop through masternode addresses
for (m = 0; m < masternodes.length; m++) {
// check if this is the correct address
@@ -273,6 +278,7 @@ module.exports = {
updated = true;
}
}
// check if the address was updated in the masternode list
if (updated) {
// save the updated masternode back to collection
@@ -295,30 +301,31 @@ module.exports = {
check_stats: function(coin, cb) {
Stats.findOne({coin: coin}, function(err, stats) {
if(stats) {
// collection exists, now check if it is missing the last_usd_price column
Stats.findOne({last_usd_price: {$exists: false}}, function(err, stats) {
if (stats) {
// the last_usd_price needs to be added to the collection
Stats.updateOne({coin: coin}, {
last_usd_price: 0,
}, function() { return cb(null); });
}
});
return cb(true);
} else {
if (stats) {
// collection exists, now check if it is missing the last_usd_price column
Stats.findOne({last_usd_price: {$exists: false}}, function(err, stats) {
if (stats) {
// the last_usd_price needs to be added to the collection
Stats.updateOne({coin: coin}, {
last_usd_price: 0
}, function() {
return cb(null);
});
}
});
return cb(true);
} else
return cb(false);
}
});
},
get_stats: function(coin, cb) {
Stats.findOne({coin: coin}, function(err, stats) {
if(stats) {
if (stats)
return cb(stats);
} else {
else
return cb(null);
}
});
},
@@ -334,20 +341,19 @@ module.exports = {
return cb();
} else {
console.log("initial stats entry created for %s", coin);
//console.log(newStats);
return cb();
}
});
},
get_address: function(hash, caseSensitive, cb) {
find_address(hash, caseSensitive, function(address){
find_address(hash, caseSensitive, function(address) {
return cb(address);
});
},
get_richlist: function(coin, cb) {
find_richlist(coin, function(richlist){
find_richlist(coin, function(richlist) {
return cb(richlist);
});
},
@@ -358,6 +364,7 @@ module.exports = {
var total_addresses = 100;
// create the burn address array so that we omit burned coins from the rich list
var burn_addresses = settings.richlist_page.burned_coins.addresses;
// always omit the private address from the richlist
burn_addresses.push("private_tx");
@@ -387,6 +394,7 @@ module.exports = {
Address.find({}, 'a_id name balance received').sort({balance: 'desc'}).limit(total_addresses + burn_addresses.length).exec(function(err, addresses) {
var return_addresses = [];
var burned_balance = 0.0;
// loop through all richlist addresses
addresses.forEach(function (address) {
// check if this is a burned coin address
@@ -398,6 +406,7 @@ module.exports = {
return_addresses.push(address);
}
});
// update the rich list collection
Richlist.updateOne({coin: settings.coin.name}, {
balance: return_addresses,
@@ -411,24 +420,25 @@ module.exports = {
},
get_tx: function(txid, cb) {
find_tx(txid, function(tx){
find_tx(txid, function(tx) {
return cb(tx);
});
},
get_txs: function(block, cb) {
var txs = [];
lib.syncLoop(block.tx.length, function (loop) {
var i = loop.iteration();
find_tx(block.tx[i], function(tx){
find_tx(block.tx[i], function(tx) {
if (tx) {
txs.push(tx);
loop.next();
} else {
} else
loop.next();
}
})
}, function(){
});
}, function() {
return cb(txs);
});
},
@@ -440,12 +450,14 @@ module.exports = {
for (i = 0; i < txs.length; i++) {
if (internal) {
var row = [];
row.push(txs[i].blockindex);
row.push(txs[i].blockhash);
row.push(txs[i].txid);
row.push(txs[i].vout.length);
row.push((txs[i].total / 100000000));
row.push(txs[i].timestamp);
data.push(row);
} else {
data.push({
@@ -458,6 +470,7 @@ module.exports = {
});
}
}
return cb(data, count);
});
},
@@ -467,13 +480,12 @@ module.exports = {
if (min > 0) {
// min is greater than zero which means we must pull record count from the txes collection
Tx.find({'total': {$gte: min}}).countDocuments(function(err, count) {
// Get last transactions where there is at least 1 vout
// get last transactions where there is at least 1 vout
Tx.find({'total': {$gte: min}, 'vout': { $gte: { $size: 1 }}}).sort({blockindex: -1}).skip(Number(start)).limit(Number(length)).exec(function(err, txs) {
if (err) {
if (err)
return cb(err);
} else {
else
return cb(txs, count);
}
});
});
} else {
@@ -481,11 +493,10 @@ module.exports = {
Stats.findOne({coin: settings.coin.name}, function(err, stats) {
// Get last transactions where there is at least 1 vout
Tx.find({'total': {$gte: min}, 'vout': { $gte: { $size: 1 }}}).sort({blockindex: -1}).skip(Number(start)).limit(Number(length)).exec(function(err, txs) {
if (err) {
if (err)
return cb(err);
} else {
else
return cb(txs, stats.txes);
}
});
});
}
@@ -493,11 +504,13 @@ module.exports = {
get_address_txs_ajax: function(hash, start, length, cb) {
var totalCount = 0;
AddressTx.find({a_id: hash}).countDocuments(function(err, count){
if(err) {
AddressTx.find({a_id: hash}).countDocuments(function(err, count) {
if (err)
return cb(err);
} else {
else {
totalCount = count;
AddressTx.aggregate([
{ $match: { a_id: hash } },
{ $sort: {blockindex: -1} },
@@ -516,21 +529,21 @@ module.exports = {
},
{ $sort: {blockindex: -1} }
], function (err,balance_sum) {
if (err) {
if (err)
return cb(err);
} else {
else {
AddressTx.find({a_id: hash}).sort({blockindex: -1}).skip(Number(start)).limit(Number(length)).exec(function (err, address_tx) {
if (err) {
if (err)
return cb(err);
} else {
else {
var txs = [];
var count = address_tx.length;
var running_balance = balance_sum.length > 0 ? balance_sum[0].balance : 0;
var txs = [];
lib.syncLoop(count, function (loop) {
var i = loop.iteration();
find_tx(address_tx[i].txid, function (tx) {
if (tx && !txs.includes(tx)) {
tx.balance = running_balance;
@@ -539,11 +552,11 @@ module.exports = {
} else if (!txs.includes(tx)) {
txs.push("1. Not found");
loop.next();
} else {
} else
loop.next();
}
running_balance = running_balance - address_tx[i].amount;
})
});
}, function () {
return cb(txs, totalCount);
});
@@ -559,7 +572,7 @@ module.exports = {
var newMarkets = new Markets({
market: market,
coin_symbol: coin_symbol,
pair_symbol: pair_symbol,
pair_symbol: pair_symbol
});
newMarkets.save(function(err) {
@@ -593,15 +606,15 @@ module.exports = {
// creates initial richlist entry in database; called on first launch of explorer
create_richlist: function(coin, cb) {
var newRichlist = new Richlist({
coin: coin,
coin: coin
});
newRichlist.save(function(err) {
if (err) {
console.log(err);
return cb();
} else {
console.log("initial richlist entry created for %s", coin);
//console.log(newRichlist);
return cb();
}
});
@@ -610,28 +623,28 @@ module.exports = {
// drops richlist data for given coin
delete_richlist: function(coin, cb) {
Richlist.findOneAndRemove({coin: coin}, function(err, exists) {
if(exists) {
if (exists)
return cb(true);
} else {
else
return cb(false);
}
});
},
// checks richlist data exists for given coin
check_richlist: function(coin, cb) {
Richlist.findOne({coin: coin}, function(err, exists) {
if(exists) {
if (exists)
return cb(true);
} else {
else
return cb(false);
}
});
},
create_heavy: function(coin, cb) {
var newHeavy = new Heavy({
coin: coin,
coin: coin
});
newHeavy.save(function(err) {
if (err) {
console.log(err);
@@ -645,21 +658,19 @@ module.exports = {
check_heavy: function(coin, cb) {
Heavy.findOne({coin: coin}, function(err, exists) {
if(exists) {
if (exists)
return cb(true);
} else {
else
return cb(false);
}
});
},
get_heavy: function(coin, cb) {
Heavy.findOne({coin: coin}, function(err, heavy) {
if(heavy) {
if (heavy)
return cb(heavy);
} else {
else
return cb(null);
}
});
},
@@ -710,6 +721,7 @@ module.exports = {
distribution.t_51_75.total = parseFloat(distribution.t_51_75.total).toFixed(8);
distribution.t_76_100.percent = parseFloat(distribution.t_76_100.percent).toFixed(2);
distribution.t_76_100.total = parseFloat(distribution.t_76_100.total).toFixed(8);
return cb(distribution);
});
},
@@ -729,6 +741,7 @@ module.exports = {
lib.get_nextin( function (nextin) {
lib.syncLoop(count, function (loop) {
var i = loop.iteration();
lib.get_blockhash(height - i, function (hash) {
lib.get_block(hash, function (block) {
newVotes.push({ count: height - i, reward: block.reward, vote: (block && block.vote ? block.vote : 0) });
@@ -783,7 +796,7 @@ module.exports = {
if (market == settings.markets_page.default_exchange.exchange_name && settings.markets_page.default_exchange.trading_pair.toUpperCase() == coin_symbol.toUpperCase() + '/' + pair_symbol.toUpperCase() && pair_symbol.toUpperCase() == 'BTC') {
// this is the default market so update the last price stats
Stats.updateOne({coin: settings.coin.name}, {
last_price: obj.stats.last,
last_price: obj.stats.last
}, function() {
// finished updating market data
return cb(null);
@@ -813,7 +826,7 @@ module.exports = {
Stats.findOne({coin: settings.coin.name}, function(err, stats) {
// update the last usd price
Stats.updateOne({coin: settings.coin.name}, {
last_usd_price: (last_usd * stats.last_price),
last_usd_price: (last_usd * stats.last_price)
}, function() {
return cb(null);
});
@@ -831,8 +844,10 @@ module.exports = {
// check to ensure count is a positive number
if (!count || (count != null && typeof count === 'number' && count < 0)) {
console.log('Unable to connect to explorer API');
return cb(false);
}
lib.get_supply(function (supply) {
lib.get_connectioncount(function (connections) {
Stats.findOne({coin: coin}, function(err, stats) {
@@ -843,9 +858,9 @@ module.exports = {
supply: (supply ? supply : 0),
connections: (connections ? connections : 0)
}, function(err) {
if (err) {
if (err)
console.log("Error during Stats Update: ", err);
}
return cb({
coin: coin,
count : count,
@@ -870,11 +885,18 @@ module.exports = {
var complete = false;
var blocks_to_scan = [];
var task_limit_blocks = settings.sync.block_parallel_tasks;
if (typeof start === 'undefined' || start < 1) start = 1; // fix for invalid block height (skip genesis block as it should not have valid txs)
if (task_limit_blocks < 1) { task_limit_blocks = 1; }
var task_limit_txs = 1;
for (i=start; i<(end+1); i++)
// fix for invalid block height (skip genesis block as it should not have valid txs)
if (typeof start === 'undefined' || start < 1)
start = 1;
if (task_limit_blocks < 1)
task_limit_blocks = 1;
for (i = start; i < (end + 1); i++)
blocks_to_scan.push(i);
async.eachLimit(blocks_to_scan, task_limit_blocks, function(block_height, next_block) {
if (block_height % settings.sync.save_stats_after_sync_blocks === 0) {
Stats.updateOne({coin: coin}, {
@@ -882,13 +904,14 @@ module.exports = {
txes: txes
}, function() {});
}
lib.get_blockhash(block_height, function(blockhash) {
if (blockhash) {
lib.get_block(blockhash, function(block) {
if (block) {
async.eachLimit(block.tx, task_limit_txs, function(txid, next_tx) {
Tx.findOne({txid: txid}, function(err, tx) {
if(tx) {
if (tx) {
setTimeout( function() {
tx = null;
next_tx();
@@ -902,6 +925,7 @@ module.exports = {
if (tx_has_vout)
txes++;
setTimeout( function() {
tx = null;
next_tx();
@@ -918,6 +942,7 @@ module.exports = {
});
} else {
console.log('block not found: %s', blockhash);
setTimeout( function() {
next_block();
}, timeout);
@@ -941,28 +966,27 @@ module.exports = {
create_peer: function(params, cb) {
var newPeer = new Peers(params);
newPeer.save(function(err) {
if (err) {
console.log(err);
return cb();
} else {
} else
return cb();
}
});
},
find_peer: function(address, cb) {
Peers.findOne({address: address}, function(err, peer) {
if (err) {
if (err)
return cb(null);
} else {
if (peer) {
return cb(peer);
} else {
return cb (null)
}
else {
if (peer)
return cb(peer);
else
return cb (null)
}
})
});
},
drop_peer: function(address, cb) {
@@ -970,10 +994,9 @@ module.exports = {
if (err) {
console.log(err);
return cb();
} else {
return cb ()
}
})
} else
return cb();
});
},
drop_peers: function(cb) {
@@ -981,19 +1004,17 @@ module.exports = {
if (err) {
console.log(err);
return cb();
} else {
return cb ()
}
})
} else
return cb();
});
},
get_peers: function(cb) {
Peers.find({}, function(err, peers) {
if (err) {
if (err)
return cb([]);
} else {
else
return cb(peers);
}
});
},
@@ -1012,6 +1033,7 @@ module.exports = {
// save blank claim name to masternode obejct
raw_masternode.claim_name = '';
}
// add/update the masternode
module.exports.add_update_masternode(raw_masternode, (masternode == null), function(success) {
return cb(success);
@@ -1032,6 +1054,7 @@ module.exports = {
console.log('Masternode Update - TX or Outidx is missing');
console.log(masternode.txhash);
console.log(masternode.outidx);
return cb(false);
} else {
var mn = new Masternode({