2019-05-27 10:33:22 -07:00
var mongoose = require ( 'mongoose' )
2020-11-20 16:28:28 -07:00
, 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' )
, fs = require ( 'fs' ) ;
2019-05-27 10:33:22 -07:00
var mode = 'update' ;
var database = 'index' ;
// displays usage and exits
function usage ( ) {
console . log ( 'Usage: node scripts/sync.js [database] [mode]' ) ;
console . log ( '' ) ;
console . log ( 'database: (required)' ) ;
console . log ( 'index [mode] Main index: coin info/stats, transactions & addresses' ) ;
console . log ( 'market Market data: summaries, orderbooks, trade history & chartdata' )
console . log ( '' ) ;
console . log ( 'mode: (required for index database only)' ) ;
2020-12-03 14:16:34 -07:00
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-txcount Rescan and flatten the tx count value for faster access' ) ;
2019-05-27 10:33:22 -07:00
console . log ( '' ) ;
2020-11-19 21:37:42 -07:00
console . log ( 'notes:' ) ;
2019-05-27 10:33:22 -07:00
console . log ( '* \'current block\' is the latest created block when script is executed.' ) ;
console . log ( '* The market database only supports (& defaults to) reindex mode.' ) ;
2020-11-19 21:37:42 -07:00
console . log ( '* If check mode finds missing data(ignoring new data since last sync),' ) ;
2019-05-27 10:33:22 -07:00
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 ) {
usage ( ) ;
} else {
switch ( process . argv [ 3 ] )
{
2020-11-19 21:37:42 -07:00
case 'update' :
mode = 'update' ;
break ;
case 'check' :
mode = 'check' ;
break ;
case 'reindex' :
mode = 'reindex' ;
break ;
2020-11-20 14:06:53 -07:00
case 'reindex-rich' :
mode = 'reindex-rich' ;
2020-12-03 14:16:34 -07:00
case 'reindex-txcount' :
mode = 'reindex-txcount' ;
2020-11-20 14:06:53 -07:00
break ;
2020-11-19 21:37:42 -07:00
default :
usage ( ) ;
2019-05-27 10:33:22 -07:00
}
}
} else if ( process . argv [ 2 ] == 'market' ) {
database = 'market' ;
} else {
usage ( ) ;
}
function create _lock ( cb ) {
if ( database == 'index' ) {
var fname = './tmp/' + database + '.pid' ;
fs . appendFile ( fname , process . pid , function ( err ) {
if ( err ) {
console . log ( "Error: unable to create %s" , fname ) ;
process . exit ( 1 ) ;
} else {
return cb ( ) ;
}
} ) ;
} else {
return cb ( ) ;
}
}
function remove _lock ( cb ) {
if ( database == 'index' ) {
var fname = './tmp/' + database + '.pid' ;
fs . unlink ( fname , function ( err ) {
if ( err ) {
console . log ( "unable to remove lock: %s" , fname ) ;
process . exit ( 1 ) ;
} else {
return cb ( ) ;
}
} ) ;
} else {
return cb ( ) ;
2020-11-19 21:37:42 -07:00
}
2019-05-27 10:33:22 -07:00
}
function is _locked ( cb ) {
if ( database == 'index' ) {
var fname = './tmp/' + database + '.pid' ;
fs . exists ( fname , function ( exists ) {
if ( exists ) {
return cb ( true ) ;
} else {
return cb ( false ) ;
}
} ) ;
} else {
return cb ( ) ;
2020-11-19 21:37:42 -07:00
}
2019-05-27 10:33:22 -07:00
}
function exit ( ) {
remove _lock ( function ( ) {
mongoose . disconnect ( ) ;
process . exit ( 0 ) ;
} ) ;
}
var dbString = 'mongodb://' + settings . dbsettings . user ;
dbString = dbString + ':' + settings . dbsettings . password ;
dbString = dbString + '@' + settings . dbsettings . address ;
dbString = dbString + ':' + settings . dbsettings . port ;
dbString = dbString + '/' + settings . dbsettings . database ;
is _locked ( function ( exists ) {
if ( exists ) {
console . log ( "Script already running.." ) ;
process . exit ( 0 ) ;
} else {
create _lock ( function ( ) {
console . log ( "script launched with pid: " + process . pid ) ;
2020-11-22 17:39:46 -07:00
mongoose . connect ( dbString , { useNewUrlParser : true , useCreateIndex : true , useUnifiedTopology : true , useFindAndModify : false } , function ( err ) {
2019-05-27 10:33:22 -07:00
if ( err ) {
console . log ( 'Unable to connect to database: %s' , dbString ) ;
console . log ( 'Aborting' ) ;
exit ( ) ;
} else if ( database == 'index' ) {
db . check _stats ( settings . coin , function ( exists ) {
if ( exists == false ) {
console . log ( 'Run \'npm start\' to create database structures before running this script.' ) ;
exit ( ) ;
} else {
2020-11-22 17:00:44 -07:00
db . update _db ( settings . coin , function ( stats ) {
if ( settings . heavy == true ) {
db . update _heavy ( settings . coin , stats . count , 20 , function ( ) {
2020-11-19 21:37:42 -07:00
2019-05-27 10:33:22 -07:00
} ) ;
}
if ( mode == 'reindex' ) {
2020-11-20 14:18:32 -07:00
Tx . deleteMany ( { } , function ( err ) {
2020-11-22 18:13:30 -07:00
console . log ( 'TXs cleared.' ) ;
2020-11-20 14:18:32 -07:00
Address . deleteMany ( { } , function ( err2 ) {
2020-11-22 18:13:30 -07:00
console . log ( 'Addresses cleared.' ) ;
2020-11-20 14:18:32 -07:00
AddressTx . deleteMany ( { } , function ( err3 ) {
2020-11-22 18:13:30 -07:00
console . log ( 'Address TXs cleared.' ) ;
2020-11-19 21:37:42 -07:00
Richlist . updateOne ( { coin : settings . coin } , {
received : [ ] ,
balance : [ ] ,
} , function ( err3 ) {
Stats . updateOne ( { coin : settings . coin } , {
last : 0 ,
2020-11-20 14:06:53 -07:00
count : 0 ,
supply : 0
2020-11-19 21:37:42 -07:00
} , function ( ) {
console . log ( 'index cleared (reindex)' ) ;
} ) ;
2020-12-03 14:16:34 -07:00
db . update _tx _db ( settings . coin , 1 , stats . count , stats . txes , settings . update _timeout , function ( ) {
2020-11-19 21:37:42 -07:00
db . update _richlist ( 'received' , function ( ) {
db . update _richlist ( 'balance' , function ( ) {
db . get _stats ( settings . coin , function ( nstats ) {
console . log ( 'reindex complete (block: %s)' , nstats . last ) ;
exit ( ) ;
2019-05-27 10:33:22 -07:00
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2020-11-19 21:37:42 -07:00
} ) ;
2020-11-22 17:00:44 -07:00
} ) ;
} else if ( mode == 'check' ) {
2020-12-03 14:16:34 -07:00
db . update _tx _db ( settings . coin , 1 , stats . count , stats . txes , settings . check _timeout , function ( ) {
2020-11-22 17:00:44 -07:00
db . get _stats ( settings . coin , function ( nstats ) {
console . log ( 'check complete (block: %s)' , nstats . last ) ;
exit ( ) ;
2019-05-27 10:33:22 -07:00
} ) ;
2020-11-22 17:00:44 -07:00
} ) ;
} else if ( mode == 'update' ) {
// Lookup the last block index
Tx . findOne ( { } , { blockindex : 1 } ) . sort ( { blockindex : - 1 } ) . limit ( 1 ) . exec ( function ( err , data ) {
var nLast = stats . last ;
if ( ! err && data ) {
// start from the last block index
nLast = data . blockindex ;
}
2019-10-01 21:25:47 -06:00
2020-12-03 14:16:34 -07:00
db . update _tx _db ( settings . coin , nLast , stats . count , stats . txes , settings . update _timeout , function ( ) {
2020-11-22 17:00:44 -07:00
db . update _richlist ( 'received' , function ( ) {
db . update _richlist ( 'balance' , function ( ) {
db . get _stats ( settings . coin , function ( nstats ) {
console . log ( 'update complete (block: %s)' , nstats . last ) ;
exit ( ) ;
2019-05-27 10:33:22 -07:00
} ) ;
} ) ;
} ) ;
} ) ;
2020-11-22 17:00:44 -07:00
} ) ;
} else if ( mode == 'reindex-rich' ) {
console . log ( 'update started' ) ;
2020-12-03 14:16:34 -07:00
db . update _tx _db ( settings . coin , stats . last , stats . count , stats . txes , settings . check _timeout , function ( ) {
2020-11-22 17:00:44 -07:00
console . log ( 'update finished' ) ;
db . check _richlist ( settings . coin , function ( exists ) {
if ( exists == true ) {
console . log ( 'richlist entry found, deleting now..' ) ;
}
db . delete _richlist ( settings . coin , function ( deleted ) {
if ( deleted == true ) {
console . log ( 'richlist entry deleted' ) ;
}
db . create _richlist ( settings . coin , function ( ) {
console . log ( 'richlist created.' ) ;
db . update _richlist ( 'received' , function ( ) {
console . log ( 'richlist updated received.' ) ;
db . update _richlist ( 'balance' , function ( ) {
console . log ( 'richlist updated balance.' ) ;
db . get _stats ( settings . coin , function ( nstats ) {
console . log ( 'update complete (block: %s)' , nstats . last ) ;
exit ( ) ;
2020-11-20 14:06:53 -07:00
} ) ;
} ) ;
} ) ;
} ) ;
2020-11-22 17:00:44 -07:00
} ) ;
} ) ;
} ) ;
2020-12-03 14:16:34 -07:00
} 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 ( ) ) ;
Stats . updateOne ( { coin : settings . coin } , {
txes : count
} , function ( ) {
console . log ( 'tx count update complete' ) ;
exit ( ) ;
} ) ;
} ) ;
2020-11-22 17:00:44 -07:00
}
2019-05-27 10:33:22 -07:00
} ) ;
}
} ) ;
} else {
//update markets
var markets = settings . markets . enabled ;
var complete = 0 ;
for ( var x = 0 ; x < markets . length ; x ++ ) {
var market = markets [ x ] ;
db . check _market ( market , function ( mkt , exists ) {
if ( exists ) {
db . update _markets _db ( mkt , function ( err ) {
if ( ! err ) {
console . log ( '%s market data updated successfully.' , mkt ) ;
complete ++ ;
2019-06-07 20:05:28 -06:00
if ( complete == markets . length )
get _last _usd _price ( ) ;
2019-05-27 10:33:22 -07:00
} else {
console . log ( '%s: %s' , mkt , err ) ;
complete ++ ;
2019-06-07 20:05:28 -06:00
if ( complete == markets . length )
get _last _usd _price ( ) ;
2019-05-27 10:33:22 -07:00
}
} ) ;
} else {
console . log ( 'error: entry for %s does not exists in markets db.' , mkt ) ;
complete ++ ;
2019-06-07 20:05:28 -06:00
if ( complete == markets . length )
get _last _usd _price ( ) ;
2019-05-27 10:33:22 -07:00
}
} ) ;
}
}
} ) ;
} ) ;
}
2019-06-07 20:05:28 -06:00
} ) ;
function get _last _usd _price ( ) {
// Get the last usd price for coinstats
db . get _last _usd _price ( function ( retVal ) { exit ( ) ; } ) ;
}