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
+16 -13
View File
@@ -1,13 +1,12 @@
var mongoose = require('mongoose')
, db = require('../lib/database')
, Tx = require('../models/tx')
, Address = require('../models/address')
, settings = require('../lib/settings');
var mongoose = require('mongoose'),
db = require('../lib/database'),
Tx = require('../models/tx'),
Address = require('../models/address'),
settings = require('../lib/settings');
var COUNT = 5000; // number of blocks to index
var COUNT = 5000; //number of blocks to index
function exit() {
function exit() {
mongoose.disconnect();
process.exit(0);
}
@@ -24,18 +23,22 @@ mongoose.connect(dbString, { useNewUrlParser: true, useCreateIndex: true, useUni
console.log('Aborting');
exit();
}
Tx.deleteMany({}, function(err) {
Address.deleteMany({}, function(err2) {
Tx.deleteMany({}, function(err) {
Address.deleteMany({}, function(err2) {
var s_timer = new Date().getTime();
db.update_tx_db(settings.coin.name, 1, COUNT, 0, settings.sync.update_timeout, function(){
db.update_tx_db(settings.coin.name, 1, COUNT, 0, settings.sync.update_timeout, function() {
var e_timer = new Date().getTime();
Tx.countDocuments({}, function(txerr, txcount){
Address.countDocuments({}, function(aerr, acount){
Tx.countDocuments({}, function(txerr, txcount) {
Address.countDocuments({}, function(aerr, acount) {
var stats = {
tx_count: txcount,
address_count: acount,
seconds: (e_timer - s_timer)/1000,
};
console.log(stats);
exit();
});
+22 -22
View File
@@ -5,37 +5,37 @@ BACKUP_PATH="$(dirname $(dirname $(readlink -f "$0")))/backups"
ARCHIVE_SUFFIX=".tar.gz"
if [ -n "${1}" ]; then
# use the backup filename passed into this script
BACKUP_FILENAME="${1}"
# use the backup filename passed into this script
BACKUP_FILENAME="${1}"
else
# no backup filename passed in, use todays date as the backup filename
BACKUP_FILENAME=$(date +"%Y-%b-%d")
# no backup filename passed in, use todays date as the backup filename
BACKUP_FILENAME=$(date +"%Y-%b-%d")
fi
if ends_with "${ARCHIVE_SUFFIX}" "${BACKUP_FILENAME}"; then
# remove the archive suffix from the backup filename
BACKUP_FILENAME=${BACKUP_FILENAME%"${ARCHIVE_SUFFIX}"}
# remove the archive suffix from the backup filename
BACKUP_FILENAME=${BACKUP_FILENAME%"${ARCHIVE_SUFFIX}"}
fi
if [ $(dirname "${BACKUP_FILENAME}") != "." ]; then
# The backup filename is a full path
# Split out the path and filename
BACKUP_DIR="$(dirname ${BACKUP_FILENAME})"
TEMP_FILENAME=${BACKUP_FILENAME#"${BACKUP_DIR}/"}
BACKUP_PATH=${BACKUP_FILENAME%"/${TEMP_FILENAME}"}
BACKUP_FILENAME="${TEMP_FILENAME}"
# The backup filename is a full path
# Split out the path and filename
BACKUP_DIR="$(dirname ${BACKUP_FILENAME})"
TEMP_FILENAME=${BACKUP_FILENAME#"${BACKUP_DIR}/"}
BACKUP_PATH=${BACKUP_FILENAME%"/${TEMP_FILENAME}"}
BACKUP_FILENAME="${TEMP_FILENAME}"
fi
if [ ! -f "${BACKUP_PATH}/${BACKUP_FILENAME}${ARCHIVE_SUFFIX}" ]; then
# execute backup
eval "mongodump -d explorerdb -o ${BACKUP_PATH}/${BACKUP_FILENAME}"
# archive the backup
cd "${BACKUP_PATH}" && tar -cvzf "${BACKUP_PATH}/${BACKUP_FILENAME}${ARCHIVE_SUFFIX}" "${BACKUP_FILENAME}"
# delete the uncompressed backup directory
rm -rf "${BACKUP_PATH}/${BACKUP_FILENAME}"
# finished msg
echo "Backup saved successfully to ${BACKUP_PATH}/${BACKUP_FILENAME}${ARCHIVE_SUFFIX}."
# execute backup
eval "mongodump -d explorerdb -o ${BACKUP_PATH}/${BACKUP_FILENAME}"
# archive the backup
cd "${BACKUP_PATH}" && tar -cvzf "${BACKUP_PATH}/${BACKUP_FILENAME}${ARCHIVE_SUFFIX}" "${BACKUP_FILENAME}"
# delete the uncompressed backup directory
rm -rf "${BACKUP_PATH}/${BACKUP_FILENAME}"
# finished msg
echo "Backup saved successfully to ${BACKUP_PATH}/${BACKUP_FILENAME}${ARCHIVE_SUFFIX}."
else
# backup already exists
echo "A backup named ${BACKUP_FILENAME}${ARCHIVE_SUFFIX} already exists."
# backup already exists
echo "A backup named ${BACKUP_FILENAME}${ARCHIVE_SUFFIX} already exists."
fi
+1 -1
View File
@@ -45,4 +45,4 @@ if [ -f "${SCRIPT_PATH}/del.tmp" ]; then
echo "Finished deleting database."
else
echo "Process aborted. Nothing was deleted."
fi
fi
+4 -2
View File
@@ -14,7 +14,9 @@ main() {
if [ $currentblockcount -ne $endingblockcount ]; then
forcesync $currentblockcount
currentblockcount=$((currentblockcount + 1))
else exit; fi
else
exit;
fi
main
}
@@ -22,4 +24,4 @@ startingblockcount=1213133
endingblockcount=1213143
echo "Syncing..."
currentblockcount=$startingblockcount
main
main
+4 -2
View File
@@ -17,10 +17,12 @@ main() {
if [ $currentblockcount -ne $previousblockcount ]; then
echo "New block found. Syncing..."
forcesync $currentblockcount
else echo "No new block found. Sleeping..."; fi
else
echo "No new block found. Sleeping...";
fi
sleep 20
main
}
currentblockcount=0
main
main
+41 -41
View File
@@ -4,32 +4,32 @@ ARCHIVE_SUFFIX=".tar.gz"
# Check if a backup file was specified
if [ -n "${1}" ]; then
BACKUP_PATH="${1}"
# Check if the backup file exists as a full path
if [ ! -f "${BACKUP_PATH}" ]; then
# Check if the backup is valid by adding the archive suffix
if [ -f "${BACKUP_PATH}${ARCHIVE_SUFFIX}" ]; then
# The backup file is valid after adding the archive suffix
BACKUP_PATH="${BACKUP_PATH}${ARCHIVE_SUFFIX}"
else
# Prepend the default backup path
BACKUP_PATH="$(dirname $(dirname $(readlink -f "$0")))/backups/${BACKUP_PATH}"
fi
fi
# Check for the backup file (again)
if [ ! -f "${BACKUP_PATH}" ]; then
# Append the default archive suffix
BACKUP_PATH="${BACKUP_PATH}${ARCHIVE_SUFFIX}"
fi
# Check for the backup file (last time)
if [ -f "${BACKUP_PATH}" ]; then
# Extract the backup archive
DIR_NAME=$(dirname "${BACKUP_PATH}")
tar -zxvf "${BACKUP_PATH}" -C "${DIR_NAME}"
# Check if this is a valid backup archive now that the files have been extracted
if [ -d ${BACKUP_PATH%"${ARCHIVE_SUFFIX}"}/explorerdb ]; then
BACKUP_DIR=${BACKUP_PATH%"${ARCHIVE_SUFFIX}"}
# Erase entire database
BACKUP_PATH="${1}"
# Check if the backup file exists as a full path
if [ ! -f "${BACKUP_PATH}" ]; then
# Check if the backup is valid by adding the archive suffix
if [ -f "${BACKUP_PATH}${ARCHIVE_SUFFIX}" ]; then
# The backup file is valid after adding the archive suffix
BACKUP_PATH="${BACKUP_PATH}${ARCHIVE_SUFFIX}"
else
# Prepend the default backup path
BACKUP_PATH="$(dirname $(dirname $(readlink -f "$0")))/backups/${BACKUP_PATH}"
fi
fi
# Check for the backup file (again)
if [ ! -f "${BACKUP_PATH}" ]; then
# Append the default archive suffix
BACKUP_PATH="${BACKUP_PATH}${ARCHIVE_SUFFIX}"
fi
# Check for the backup file (last time)
if [ -f "${BACKUP_PATH}" ]; then
# Extract the backup archive
DIR_NAME=$(dirname "${BACKUP_PATH}")
tar -zxvf "${BACKUP_PATH}" -C "${DIR_NAME}"
# Check if this is a valid backup archive now that the files have been extracted
if [ -d ${BACKUP_PATH%"${ARCHIVE_SUFFIX}"}/explorerdb ]; then
BACKUP_DIR=${BACKUP_PATH%"${ARCHIVE_SUFFIX}"}
# Erase entire database
sudo mongo <<EOF
use explorerdb
db.addresses.remove({})
@@ -52,20 +52,20 @@ db.txes.remove({})
db.txes.drop()
exit
EOF
# Restore mongo database from the backup directory
eval "mongorestore -d explorerdb ${BACKUP_DIR}/explorerdb"
# Remove the backup directory
rm -rf "${BACKUP_DIR}"
# Finished msg
echo "Backup restored from ${BACKUP_PATH} successfully."
else
# Backup file is not a valid mongo database backup
echo "${BACKUP_PATH} is not a valid backup file."
fi
else
# Backup does not exist
echo "${BACKUP_PATH} cannot be found."
fi
# Restore mongo database from the backup directory
eval "mongorestore -d explorerdb ${BACKUP_DIR}/explorerdb"
# Remove the backup directory
rm -rf "${BACKUP_DIR}"
# Finished msg
echo "Backup restored from ${BACKUP_PATH} successfully."
else
# Backup file is not a valid mongo database backup
echo "${BACKUP_PATH} is not a valid backup file."
fi
else
# Backup does not exist
echo "${BACKUP_PATH} cannot be found."
fi
else
echo "no backup file specified."
echo "no backup file specified."
fi
+68 -52
View File
@@ -1,13 +1,12 @@
var mongoose = require('mongoose')
, lib = require('../lib/explorer')
, db = require('../lib/database')
, Tx = require('../models/tx')
, Address = require('../models/address')
, AddressTx = require('../models/addresstx')
, Richlist = require('../models/richlist')
, Stats = require('../models/stats')
, settings = require('../lib/settings');
var mongoose = require('mongoose'),
lib = require('../lib/explorer'),
db = require('../lib/database'),
Tx = require('../models/tx'),
Address = require('../models/address'),
AddressTx = require('../models/addresstx'),
Richlist = require('../models/richlist'),
Stats = require('../models/stats'),
settings = require('../lib/settings');
var mode = 'update';
var database = 'index';
@@ -19,7 +18,7 @@ function usage() {
console.log('update Updates index from last sync to current block');
console.log('check Checks index for (and adds) any missing transactions/addresses');
console.log('reindex Clears index then resyncs from genesis to current block');
console.log('reindex-rich Clears and recreates the richlist data');
console.log('reindex-rich Clears and recreates the richlist data');
console.log('reindex-txcount Rescan and flatten the tx count value for faster access');
console.log('reindex-last Rescan and flatten the last blockindex value for faster access');
console.log('market Updates market summaries, orderbooks, trade history + charts');
@@ -30,18 +29,17 @@ function usage() {
console.log('- \'current block\' is the latest created block when script is executed.');
console.log('- The market + peers databases only support (& defaults to) reindex mode.');
console.log('- If check mode finds missing data (ignoring new data since last sync),');
console.log(' index_timeout in settings.json is set too low.')
console.log(' index_timeout in settings.json is set too low.');
console.log('');
process.exit(0);
}
// check options
if (process.argv[2] == 'index') {
if (process.argv.length <3) {
if (process.argv.length < 3)
usage();
} else {
switch(process.argv[3])
{
else {
switch(process.argv[3]) {
case 'update':
mode = 'update';
break;
@@ -64,65 +62,61 @@ if (process.argv[2] == 'index') {
usage();
}
}
} else if (process.argv[2] == 'market') {
} else if (process.argv[2] == 'market')
database = 'market';
} else if (process.argv[2] == 'peers') {
else if (process.argv[2] == 'peers')
database = 'peers';
} else if (process.argv[2] == 'masternodes') {
else if (process.argv[2] == 'masternodes')
database = 'masternodes';
} else {
else
usage();
}
function create_lock(cb) {
if ( database == 'index' ) {
if (database == 'index') {
var fname = './tmp/' + database + '.pid';
db.fs.appendFile(fname, process.pid.toString(), function (err) {
if (err) {
console.log("Error: unable to create %s", fname);
process.exit(1);
} else {
} else
return cb();
}
});
} else {
} else
return cb();
}
}
function remove_lock(cb) {
if ( database == 'index' ) {
if (database == 'index') {
var fname = './tmp/' + database + '.pid';
db.fs.unlink(fname, function (err) {
if(err) {
if (err) {
console.log("unable to remove lock: %s", fname);
process.exit(1);
} else {
} else
return cb();
}
});
} else {
} else
return cb();
}
}
function is_locked(cb) {
if ( database == 'index' ) {
if (database == 'index') {
var fname = './tmp/' + database + '.pid';
db.fs.exists(fname, function (exists) {
if(exists) {
if (exists)
return cb(true);
} else {
else
return cb(false);
}
});
} else {
} else
return cb();
}
}
function exit() {
remove_lock(function(){
remove_lock(function() {
mongoose.disconnect();
process.exit(0);
});
@@ -137,6 +131,7 @@ dbString = dbString + '/' + settings.dbsettings.database;
if (database == 'peers') {
var rateLimitLib = require('../lib/ratelimit');
console.log('syncing peers.. please wait..');
// syncing peers does not require a lock
mongoose.connect(dbString, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true, useFindAndModify: false }, function(err) {
if (err) {
@@ -148,7 +143,7 @@ if (database == 'peers') {
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 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 rateLimitLib.RateLimit(1, 2000, false);
db.find_peer(address, function(peer) {
@@ -159,8 +154,9 @@ if (database == 'peers') {
exit();
});
}
console.log('Updated peer %s [%s/%s]', address, (i + 1).toString(), body.length.toString());
// peer already exists
console.log('Updated peer %s [%s/%s]', address, (i + 1).toString(), body.length.toString());
loop.next();
} else {
rateLimit.schedule(function() {
@@ -203,6 +199,7 @@ if (database == 'peers') {
});
} else if (database == 'masternodes') {
console.log('syncing masternodes.. please wait..');
// syncing masternodes does not require a lock
mongoose.connect(dbString, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true, useFindAndModify: false }, function(err) {
if (err) {
@@ -214,6 +211,7 @@ if (database == 'peers') {
if (body != null) {
lib.syncLoop(body.length, function (loop) {
var i = loop.iteration();
db.save_masternode(body[i], function (success) {
if (success)
loop.next();
@@ -244,7 +242,7 @@ if (database == 'peers') {
console.log("Script already running..");
process.exit(0);
} else {
create_lock(function (){
create_lock(function () {
console.log("script launched with pid: " + process.pid);
mongoose.connect(dbString, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true, useFindAndModify: false }, function(err) {
if (err) {
@@ -265,13 +263,16 @@ if (database == 'peers') {
if (mode == 'reindex') {
Tx.deleteMany({}, function(err) {
console.log('TXs cleared.');
Address.deleteMany({}, function(err2) {
console.log('Addresses cleared.');
AddressTx.deleteMany({}, function(err3) {
console.log('Address TXs cleared.');
Richlist.updateOne({coin: settings.coin.name}, {
received: [],
balance: [],
balance: []
}, function(err3) {
Stats.updateOne({coin: settings.coin.name}, {
last: 0,
@@ -308,8 +309,9 @@ if (database == 'peers') {
});
} else if (mode == 'check') {
console.log('starting check.. please wait..');
db.update_tx_db(settings.coin.name, 1, stats.count, stats.txes, settings.sync.check_timeout, function(){
db.get_stats(settings.coin.name, function(nstats){
db.update_tx_db(settings.coin.name, 1, stats.count, stats.txes, settings.sync.check_timeout, function() {
db.get_stats(settings.coin.name, function(nstats) {
console.log('check complete (block: %s)', nstats.last);
exit();
});
@@ -322,10 +324,10 @@ if (database == 'peers') {
// Check if the sync msg should be shown
check_show_sync_message(count - last);
db.update_tx_db(settings.coin.name, last, count, stats.txes, settings.sync.update_timeout, function(){
db.update_richlist('received', function(){
db.update_richlist('balance', function(){
db.get_stats(settings.coin.name, function(nstats){
db.update_tx_db(settings.coin.name, last, count, stats.txes, settings.sync.update_timeout, function() {
db.update_richlist('received', function() {
db.update_richlist('balance', function() {
db.get_stats(settings.coin.name, function(nstats) {
// always check for and remove the sync msg if exists
remove_sync_message();
// update richlist_last_updated value
@@ -342,14 +344,21 @@ if (database == 'peers') {
});
} else if (mode == 'reindex-rich') {
console.log('check richlist');
db.check_richlist(settings.coin.name, function(exists) {
if (exists) console.log('richlist entry found, deleting now..');
if (exists)
console.log('richlist entry found, deleting now..');
db.delete_richlist(settings.coin.name, function(deleted) {
if (deleted) console.log('richlist entry deleted');
if (deleted)
console.log('richlist entry deleted');
db.create_richlist(settings.coin.name, function() {
console.log('richlist created.');
db.update_richlist('received', function() {
console.log('richlist updated received.');
db.update_richlist('balance', function() {
// update richlist_last_updated value
db.update_last_updated_stats(settings.coin.name, { richlist_last_updated: Math.floor(new Date() / 1000) }, function (cb) {
@@ -363,6 +372,7 @@ if (database == 'peers') {
});
} else if (mode == 'reindex-txcount') {
console.log('calculating tx count.. please wait..');
// Resetting the transaction counter requires a single lookup on the txes collection to find all txes that have a positive or zero total and 1 or more vout
Tx.find({'total': {$gte: 0}, 'vout': { $gte: { $size: 1 }}}).countDocuments(function(err, count) {
console.log('found tx count: ' + count.toString());
@@ -375,11 +385,13 @@ if (database == 'peers') {
});
} else if (mode == 'reindex-last') {
console.log('finding last blockindex.. please wait..');
// Resetting the last blockindex counter requires a single lookup on the txes collection to find the last indexed blockindex
Tx.find({}, {blockindex:1, _id:0}).sort({blockindex: -1}).limit(1).exec(function(err, tx) {
// check if any blocks exists
if (err != null || tx == null || tx.length == 0) {
console.log('no blocks found. setting last blockindex to 0.');
Stats.updateOne({coin: settings.coin.name}, {
last: 0
}, function() {
@@ -388,6 +400,7 @@ if (database == 'peers') {
});
} else {
console.log('found last blockindex: ' + tx[0].blockindex.toString());
Stats.updateOne({coin: settings.coin.name}, {
last: tx[0].blockindex
}, function() {
@@ -457,11 +470,13 @@ if (database == 'peers') {
if (!err) {
console.log('%s[%s]: market data updated successfully.', key, pair_key);
complete++;
if (complete == total_pairs)
get_last_usd_price();
} else {
console.log('%s[%s] error: %s', key, pair_key, err);
complete++;
if (complete == total_pairs)
get_last_usd_price();
}
@@ -471,7 +486,7 @@ if (database == 'peers') {
console.log('error: entry for %s does not exist in markets database.', key);
complete++;
if (complete == total_pairs)
get_last_usd_price();
get_last_usd_price();
}
});
}
@@ -480,6 +495,7 @@ if (database == 'peers') {
// market not installed
console.log('%s market not installed', key);
complete++;
if (complete == total_pairs)
get_last_usd_price();
}