2025-01-01 19:20:13 -07:00
const mongoose = require ( 'mongoose' ) ;
2025-01-09 20:00:37 -07:00
const Address = require ( '../models/address' ) ;
const Tx = require ( '../models/tx' ) ;
2025-01-01 19:20:13 -07:00
const blkSync = require ( '../lib/block_sync' ) ;
const settings = require ( '../lib/settings' ) ;
2025-01-09 20:00:37 -07:00
const resumeSync = process . argv [ 2 ] == '1' ;
2019-05-27 10:33:22 -07:00
2025-01-01 19:20:13 -07:00
let dbString = ` mongodb:// ${ settings . benchmark . address } : ${ settings . benchmark . port } /admin `
// prevent stopping of the sync script to be able to gracefully shut down
process . on ( 'SIGINT' , ( ) => {
2025-01-09 20:00:37 -07:00
if ( ! blkSync . getStackSizeErrorId ( ) )
console . log ( ` ${ settings . localization . stopping _sync _process } .. ${ settings . localization . please _wait } .. ` ) ;
2025-01-01 19:20:13 -07:00
blkSync . setStopSync ( true ) ;
} ) ;
// prevent killing of the sync script to be able to gracefully shut down
process . on ( 'SIGTERM' , ( ) => {
2025-01-09 20:00:37 -07:00
if ( ! blkSync . getStackSizeErrorId ( ) )
console . log ( ` ${ settings . localization . stopping _sync _process } .. ${ settings . localization . please _wait } .. ` ) ;
2025-01-01 19:20:13 -07:00
blkSync . setStopSync ( true ) ;
} ) ;
2019-05-27 10:33:22 -07:00
2022-04-30 20:53:10 -06:00
function exit ( exitCode ) {
2019-05-27 10:33:22 -07:00
mongoose . disconnect ( ) ;
2022-04-30 20:53:10 -06:00
process . exit ( exitCode ) ;
2019-05-27 10:33:22 -07:00
}
2025-01-01 19:20:13 -07:00
function check _user _exists ( exists , cb ) {
// check if the user exists
if ( exists ) {
// user already exists
return cb ( ) ;
} else {
// user does not exist so create it now
mongoose . connection . client . db ( settings . benchmark . database ) . command ( {
createUser : settings . benchmark . user ,
pwd : settings . benchmark . password ,
roles : [
{
role : 'readWrite' ,
db : settings . benchmark . database
}
]
} )
. then ( ( ) => {
console . log ( 'User created successfully' ) ;
return cb ( ) ;
} )
. catch ( ( error ) => {
console . error ( 'Error creating new user:' , error ) ;
exit ( 2 ) ;
} ) ;
}
}
2022-07-17 16:49:02 -06:00
2025-01-01 19:20:13 -07:00
function check _create _user ( cb ) {
// check if the benchmark database should be checked for the correct user
if ( settings . benchmark . auto _add _user ) {
// connect to the admin database without a username/password
mongoose . connect ( dbString ) . then ( ( ) => {
// determine if the user already exists in the target database
mongoose . connection . db
. command ( {
usersInfo : {
user : settings . benchmark . user ,
db : settings . benchmark . database
2022-07-17 16:49:02 -06:00
}
2025-01-01 19:20:13 -07:00
} )
. then ( ( userInfo ) => {
// check if the user already exists
check _user _exists ( userInfo . users . length > 0 , function ( ) {
// disconnect the current database connection
mongoose . disconnect ( ) . then ( ( ) => {
// finished checking the benchmark database user
return cb ( ) ;
} )
. catch ( ( err ) => {
console . error ( 'Error disconnecting from database:' , err ) ;
exit ( 2 ) ;
} ) ;
2022-07-17 16:49:02 -06:00
} ) ;
2025-01-01 19:20:13 -07:00
} )
. catch ( ( err ) => {
console . error ( 'Error checking if user exists:' , err ) ;
exit ( 2 ) ;
2022-07-17 16:49:02 -06:00
} ) ;
2025-01-01 19:20:13 -07:00
} )
. catch ( ( err ) => {
console . log ( 'Error: Unable to connect to database: %s' , dbString ) ;
exit ( 999 ) ;
} ) ;
} else
return cb ( ) ;
}
2022-07-17 16:49:02 -06:00
2025-01-09 20:00:37 -07:00
function initialize _data _startup ( cb ) {
console . log ( ` ${ settings . localization . initializing _database } .. ${ settings . localization . please _wait } .. ` ) ;
const db = require ( '../lib/database' ) ;
// check if stats collection is initialized
db . check _stats ( settings . coin . name , function ( stats _exists ) {
let skip = true ;
// determine if stats collection already exists
if ( stats _exists == false ) {
console . log ( ` ${ settings . localization . creating _initial _entry . replace ( '{1}' , 'stats' ) } .. ${ settings . localization . please _wait } .. ` ) ;
skip = false ;
}
// initialize the stats collection
db . create _stats ( settings . coin . name , skip , function ( ) {
// get the stats object from the database
db . get _stats ( settings . coin . name , function ( stats ) {
// finished initializing startup data
console . log ( 'Database initialization complete' ) ;
return cb ( stats ) ;
} ) ;
} ) ;
} ) ;
}
function delete _txes ( cb ) {
// check if the benchmark sync is being resumed
if ( resumeSync ) {
// do not delete the list of txes for a resume sync
return cb ( ) ;
} else {
// delete all previous transaction records from the benchmark database
Tx . deleteMany ( { } ) . then ( ( ) => {
return cb ( ) ;
} ) ;
}
}
function delete _addresses ( cb ) {
// check if the benchmark sync is being resumed
if ( resumeSync ) {
// do not delete the list of addresses for a resume sync
return cb ( ) ;
} else {
// delete all previous address records from the benchmark database
Address . deleteMany ( { } ) . then ( ( ) => {
return cb ( ) ;
} ) ;
}
}
function delete _stats ( cb ) {
// check if the benchmark sync is being resumed
if ( resumeSync ) {
// do not delete the database stats for a resume sync
return cb ( ) ;
} else {
const Stats = require ( '../models/stats' ) ;
// delete all previous stat records from the benchmark database
Stats . deleteMany ( { } ) . then ( ( ) => {
return cb ( ) ;
} ) ;
}
}
2025-01-01 19:20:13 -07:00
console . log ( ` ${ settings . localization . script _launched } : ${ process . pid } ` ) ;
2021-03-17 17:54:09 -06:00
2025-01-01 19:20:13 -07:00
mongoose . set ( 'strictQuery' , true ) ;
2021-03-17 17:54:09 -06:00
2025-01-01 19:20:13 -07:00
// ensure the benchmark database user exists
check _create _user ( function ( ) {
// update db string to connect to benchmark database
dbString = 'mongodb://' + encodeURIComponent ( settings . benchmark . user ) ;
dbString = dbString + ':' + encodeURIComponent ( settings . benchmark . password ) ;
dbString = dbString + '@' + settings . benchmark . address ;
dbString = dbString + ':' + settings . benchmark . port ;
dbString = dbString + '/' + settings . benchmark . database ;
// connect to the benchmark database
mongoose . connect ( dbString ) . then ( ( ) => {
// delete all previous transaction records from the benchmark database
2025-01-09 20:00:37 -07:00
delete _txes ( function ( ) {
2025-01-01 19:20:13 -07:00
// delete all previous address records from the benchmark database
2025-01-09 20:00:37 -07:00
delete _addresses ( function ( ) {
// delete all previous stat records from the benchmark database
delete _stats ( function ( ) {
// initialize the benchmark database
initialize _data _startup ( function ( stats ) {
// get the last synced block index value
const last = ( stats . last ? stats . last : 0 ) ;
// get starting timestamp
const s _timer = new Date ( ) . getTime ( ) ;
// start the block sync
blkSync . update _tx _db ( settings . coin . name , last , settings . benchmark . block _to _sync , stats . txes , settings . sync . update _timeout , false , function ( ) {
// get ending timestamp
const e _timer = new Date ( ) . getTime ( ) ;
// get count of transactions
Tx . countDocuments ( { } ) . then ( ( txcount ) => {
// get count of addresses
Address . countDocuments ( { } ) . then ( ( acount ) => {
// check if the script stopped prematurely
if ( blkSync . getStopSync ( ) )
console . log ( 'Block sync was stopped prematurely' ) ;
// output final benchmark stats
console . log ( {
tx _count : txcount ,
address _count : acount ,
seconds : ( e _timer - s _timer ) / 1000 ,
} ) ;
// check if the sync needed to be resumed
if ( resumeSync ) {
// output a warning msg
console . log ( ` \n ${ settings . localization . ex _warning } : The sync ran out of memory during processing and therefore the run time was affected. It is recommended to re-run the benchmark again using a larger stack size such as 25000 or higher with the cmd "node --stack-size=25000 scripts/benchmark.js" to help ensure an accurate benchmark time. ` ) ;
}
exit ( 0 ) ;
} ) ;
2025-01-01 19:20:13 -07:00
} ) ;
} ) ;
2019-05-27 10:33:22 -07:00
} ) ;
} ) ;
} ) ;
} ) ;
2025-01-01 19:20:13 -07:00
} ) . catch ( ( err ) => {
console . log ( 'Error: Unable to connect to database: %s' , dbString ) ;
exit ( 999 ) ;
2019-05-27 10:33:22 -07:00
} ) ;
} ) ;