From 79f4a5b5a75a1220a44a35db1c2fdaa4c6ebcf00 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Tue, 21 Mar 2017 09:17:12 +0300 Subject: [PATCH 1/4] split shepherd.js (wip) --- main.js | 10 +- routes/cache.js | 940 ++++++++++++++++++++++++++++++++++++++++++++ routes/shepherd.js | 941 +-------------------------------------------- 3 files changed, 950 insertions(+), 941 deletions(-) create mode 100644 routes/cache.js diff --git a/main.js b/main.js index daf6a79..2e7b31a 100644 --- a/main.js +++ b/main.js @@ -113,8 +113,6 @@ io.on('connection', function(client) { shepherd.setIO(io); // pass sockets object to shepherd router -//io.emit('an event sent to all connected clients'); - module.exports = guiapp; // END GUI App Settings @@ -239,7 +237,7 @@ function createWindow (status) { { role: 'copy' }, { type: 'separator' }, { role: 'selectall' }, - ]) + ]); const editMenu = Menu.buildFromTemplate([ //if editable { role: 'undo' }, @@ -250,7 +248,7 @@ function createWindow (status) { { role: 'paste' }, { type: 'separator' }, { role: 'selectall' }, - ]) + ]); // load our index.html (i.e. easyDEX GUI) if (appConfig.edexGuiOnly) { @@ -261,13 +259,13 @@ function createWindow (status) { mainWindow.webContents.on('context-menu', (e, params) => { //context-menu returns params const { selectionText, isEditable } = params; //params obj - + if (isEditable) { editMenu.popup(mainWindow); } else if (selectionText && selectionText.trim() !== '') { staticMenu.popup(mainWindow); } - }) + }); // DEVTOOLS - only for dev purposes - ca333 //mainWindow.webContents.openDevTools() diff --git a/routes/cache.js b/routes/cache.js new file mode 100644 index 0000000..5963c6d --- /dev/null +++ b/routes/cache.js @@ -0,0 +1,940 @@ +const fs = require('fs-extra'); + +var cache = {}; + +cache.setVars = function(variable, value) { + cache[variable] = value; +} + +/* + * type: GET + * params: pubkey + */ +cache.get = function(req, res, next) { + var pubkey = req.query.pubkey; + + if (pubkey) { + if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { + fs.readFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) { + if (err) { + var errorObj = { + 'msg': 'error', + 'result': err + }; + + res.end(JSON.stringify(errorObj)); + } else { + var parsedJSON = 'JSON parse error'; + + try { + parsedJSON = JSON.parse(data); + } catch (e) { + console.log('JSON parse error'); + } + + var successObj = { + 'msg': 'success', + 'result': parsedJSON + }; + + res.end(JSON.stringify(successObj)); + } + }); + } else { + var errorObj = { + 'msg': 'error', + 'result': 'no file with handle ' + pubkey + }; + + res.end(JSON.stringify(errorObj)); + } + } else { + var errorObj = { + 'msg': 'error', + 'result': 'no pubkey provided' + }; + + res.end(JSON.stringify(errorObj)); + } +} + +/* + * type: GET + * params: filename + */ +/*cache.get('/groom', function(req, res, next) { + var _filename = req.query.filename; + + if (_filename) { + if (fs.existsSync(iguanaDir + '/shepherd/cache-' + _filename + '.json')) { + fs.readFile(iguanaDir + '/shepherd/cache-' + _filename + '.json', 'utf8', function (err, data) { + if (err) { + var errorObj = { + 'msg': 'error', + 'result': err + }; + + res.end(JSON.stringify(errorObj)); + } else { + var successObj = { + 'msg': 'success', + 'result': data ? JSON.parse(data) : '' + }; + + res.end(JSON.stringify(successObj)); + } + }); + } else { + var errorObj = { + 'msg': 'error', + 'result': 'no file with name ' + _filename + }; + + res.end(JSON.stringify(errorObj)); + } + } else { + var errorObj = { + 'msg': 'error', + 'result': 'no file name provided' + }; + + res.end(JSON.stringify(errorObj)); + } +}); + +/* + * type: DELETE + * params: filename + */ +/*shepherd.delete('/groom', function(req, res, next) { + var _filename = req.body.filename; + + if (_filename) { + if (fs.existsSync(iguanaDir + '/shepherd/cache-' + _filename + '.json')) { + fs.unlink(iguanaDir + '/shepherd/cache-' + _filename + '.json', function(err) { + if (err) { + var errorObj = { + 'msg': 'error', + 'result': err + }; + + res.end(JSON.stringify(errorObj)); + } else { + var successObj = { + 'msg': 'success', + 'result': 'deleted' + }; + + res.end(JSON.stringify(successObj)); + } + }); + } else { + var errorObj = { + 'msg': 'error', + 'result': 'no file with name ' + _filename + }; + + res.end(JSON.stringify(errorObj)); + } + } else { + var errorObj = { + 'msg': 'error', + 'result': 'no file name provided' + }; + + res.end(JSON.stringify(errorObj)); + } +}); + +/* + * type: POST + * params: filename, payload + */ +/*shepherd.post('/groom', function(req, res) { + var _filename = req.body.filename, + _payload = req.body.payload; + + if (_filename) { + if (!_payload) { + var errorObj = { + 'msg': 'error', + 'result': 'no payload provided' + }; + + res.end(JSON.stringify(errorObj)); + } else { + fs.writeFile(iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) { + if (err) { + var errorObj = { + 'msg': 'error', + 'result': err + }; + + res.end(JSON.stringify(errorObj)); + } else { + var successObj = { + 'msg': 'success', + 'result': 'done' + }; + + res.end(JSON.stringify(successObj)); + } + }); + } + } else { + var errorObj = { + 'msg': 'error', + 'result': 'no file name provided' + }; + + res.end(JSON.stringify(errorObj)); + } +}); + +var cacheCallInProgress = false, + cacheGlobLifetime = 300; // sec + +// TODO: reset calls' states on new /cache call start +/* + * type: GET + * params: userpass, pubkey, skip + */ +/*shepherd.get('/cache-all', function(req, res, next) { + if (req.query.pubkey && !fs.existsSync(iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { + cacheCallInProgress = false; + } + + if (!cacheCallInProgress) { + cacheCallInProgress = true; + + var sessionKey = req.query.userpass, + pubkey = req.query.pubkey, + skipTimeout = req.query.skip, + _obj = { + 'msg': 'error', + 'result': 'error' + }, + outObj = { + basilisk: {} + }, + writeCache = function() { + fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { + if (err) { + return console.log(err); + } + + console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); + }); + }, + callStack = {}, + checkCallStack = function() { + var total = 0; + + for (var coin in callStack) { + total =+ callStack[coin]; + } + + if (total / Object.keys(callStack).length === 1) { + cacheCallInProgress = false; + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'done', + 'resp': 'success' + } + } + }); + } + }, + checkTimestamp = function(dateToCheck) { + var currentEpochTime = new Date(Date.now()) / 1000, + secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); + + return Math.floor(secondsElapsed); + }, + internalError = false; + + if (!sessionKey) { + var errorObj = { + 'msg': 'error', + 'result': 'no session key provided' + }; + + res.end(JSON.stringify(errorObj)); + internalError = true; + } + + if (!pubkey) { + var errorObj = { + 'msg': 'error', + 'result': 'no pubkey provided' + }; + + res.end(JSON.stringify(errorObj)); + internalError = true; + } + + if (!internalError) { + console.log('cache-all call started'); + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress' + } + } + }); + res.end(JSON.stringify({ + 'msg': 'success', + 'result': 'call is initiated' + })); + + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'allcoins', + 'status': 'in progress' + } + } + } + }); + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'allcoins', + 'status': 'done', + 'resp': body + } + } + } + }); + body = JSON.parse(body); + // basilisk coins + if (body.basilisk && body.basilisk.length) { + // get coin addresses + async.each(body.basilisk, function(coin) { + callStack[coin] = 1; + }); + + async.each(body.basilisk, function(coin) { + outObj.basilisk[coin] = {}; + writeCache(); + + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'getaddressesbyaccount', + 'coin': coin, + 'status': 'in progress' + } + } + } + }); + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'getaddressesbyaccount', + 'coin': coin, + 'status': 'done', + 'resp': body + } + } + } + }); + outObj.basilisk[coin].addresses = JSON.parse(body).result; + writeCache(); + var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; + callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? 2 : 4); + console.log(coin + ' stack len ' + callStack[coin]); + + async.each(outObj.basilisk[coin].addresses, function(address) { + var dexUrls = { + 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + }; + if (coin === 'BTC' || coin === 'SYS') { + delete dexUrls.refresh; + delete dexUrls.getbalance; + } + //console.log(JSON.stringify(dexUrls)); + console.log(coin + ' address ' + address); + outObj.basilisk[coin][address] = {}; + writeCache(); + + // set current call status + async.forEachOf(dexUrls, function(dexUrl, key) { + if (!outObj.basilisk[coin][address][key]) { + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].status = 'waiting'; + } else { + outObj.basilisk[coin][address][key].status = 'waiting'; + } + }); + writeCache(); + + async.forEachOf(dexUrls, function(dexUrl, key) { + var tooEarly = false; + if (outObj.basilisk[coin][address][key] && + outObj.basilisk[coin][address][key].timestamp && + checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { + tooEarly = true; + outObj.basilisk[coin][address][key].status = 'done'; + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': 'too early' + } + } + } + }); + } + tooEarly = skipTimeout ? false : tooEarly; + if (!tooEarly) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'in progress' + } + } + } + }); + outObj.basilisk[coin][address][key].status = 'in progress'; + request({ + url: dexUrl, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': body + } + } + } + }); + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].data = JSON.parse(body); + outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp + outObj.basilisk[coin][address][key].status = 'done'; + console.log(dexUrl); + console.log(body); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + + writeCache(); + } + }); + } else { + console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + } + }); + }); + } else { + // TODO: error + } + }); + }); + } else { + // TODO: error + } + } else { + // TODO: error + } + }); + } else { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'done', + 'resp': 'internal error' + } + } + }); + cacheCallInProgress = false; + } + } else { + res.end(JSON.stringify({ + 'msg': 'error', + 'result': 'another call is in progress already' + })); + } +}); + +/* + * type: GET + * params: userpass, pubkey, coin, address, skip + */ +/*shepherd.get('/cache-one', function(req, res, next) { + if (req.query.pubkey && !fs.existsSync(iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { + cacheCallInProgress = false; + } + + if (!cacheCallInProgress) { + // TODO: add check to allow only one cache call/sequence in progress + cacheCallInProgress = true; + var sessionKey = req.query.userpass, + coin = req.query.coin, + address = req.query.address, + pubkey = req.query.pubkey, + skipTimeout = req.query.skip, + callsArray = req.query.calls.split(':'), + errorObj = { + 'msg': 'error', + 'result': 'error' + }, + outObj = {}, + pubkey, + writeCache = function(timeStamp) { + if (timeStamp) { + outObj.timestamp = timeStamp; + } + + fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { + if (err) { + return console.log(err); + } + + console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); + if (timeStamp) { + console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped'); + } + }); + }, + callStack = {}, + checkCallStack = function() { + var total = 0; + + for (var coin in callStack) { + total =+ callStack[coin]; + } + + if (total / Object.keys(callStack).length === 1) { + cacheCallInProgress = false; + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'done', + 'resp': 'success' + } + } + }); + // add timestamp to cache file + // writeCache(Date.now()); + } + }, + checkTimestamp = function(dateToCheck) { + var currentEpochTime = new Date(Date.now()) / 1000, + secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); + + return Math.floor(secondsElapsed); + }, + internalError = false; + + callStack[coin] = 1; + console.log(callsArray); + + if (!sessionKey) { + var errorObj = { + 'msg': 'error', + 'result': 'no session key provided' + }; + + res.end(JSON.stringify(errorObj)); + internalError = true; + } + + if (!pubkey) { + var errorObj = { + 'msg': 'error', + 'result': 'no pubkey provided' + }; + + res.end(JSON.stringify(errorObj)); + internalError = true; + } + + console.log('cache-one call started'); + + if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { + var _file = fs.readFileSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); + outObj = _file ? JSON.parse(_file) : {}; + + if (!outObj || !outObj.basilisk) { + console.log('no local basilisk info'); + outObj['basilisk'] = {}; + outObj['basilisk'][coin] = {}; + } else { + if (!outObj['basilisk'][coin]) { + console.log('no local coin info'); + outObj['basilisk'][coin] = {}; + } + } + } else { + outObj['basilisk'] = {}; + outObj['basilisk'][coin] = {}; + } + + res.end(JSON.stringify({ + 'msg': 'success', + 'result': 'call is initiated' + })); + + if (!internalError) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress' + } + } + }); + // update all available coin addresses + if (!address) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'getaddressesbyaccount', + 'coin': coin, + 'status': 'in progress' + } + } + } + }); + request({ + url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'getaddressesbyaccount', + 'coin': coin, + 'status': 'done', + 'resp': body + } + } + } + }); + outObj.basilisk[coin].addresses = JSON.parse(body).result; + console.log(JSON.parse(body).result); + writeCache(); + var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; + callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length); + console.log(coin + ' stack len ' + callStack[coin]); + + async.each(outObj.basilisk[coin].addresses, function(address) { + var dexUrls = { + 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + }, + _dexUrls = {}; + + for (var a = 0; a < callsArray.length; a++) { + _dexUrls[callsArray[a]] = dexUrls[callsArray[a]]; + } + if (coin === 'BTC' || coin === 'SYS') { + delete _dexUrls.refresh; + delete _dexUrls.getbalance; + } + //console.log(JSON.stringify(dexUrls)); + console.log(coin + ' address ' + address); + if (!outObj.basilisk[coin][address]) { + outObj.basilisk[coin][address] = {}; + writeCache(); + } + + // set current call status + async.forEachOf(_dexUrls, function(dexUrl, key) { + if (!outObj.basilisk[coin][address][key]) { + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].status = 'waiting'; + } else { + outObj.basilisk[coin][address][key].status = 'waiting'; + } + }); + writeCache(); + + async.forEachOf(_dexUrls, function(dexUrl, key) { + var tooEarly = false; + if (outObj.basilisk[coin][address][key] && + outObj.basilisk[coin][address][key].timestamp && + checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { + tooEarly = true; + outObj.basilisk[coin][address][key].status = 'done'; + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': 'too early' + } + } + } + }); + } + tooEarly = skipTimeout ? false : tooEarly; + if (!tooEarly) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'in progress' + } + } + } + }); + outObj.basilisk[coin][address][key].status = 'in progress'; + request({ + url: dexUrl, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': body + } + } + } + }); + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].data = JSON.parse(body); + outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp + outObj.basilisk[coin][address][key].status = 'done'; + console.log(dexUrl); + console.log(body); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + + writeCache(); + } + }); + } else { + console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + } + }); + }); + } else { + // TODO: error + } + }); + } else { + callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); + console.log(coin + ' stack len ' + callStack[coin]); + + var dexUrls = { + 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + }, + _dexUrls = {}; + + for (var a = 0; a < callsArray.length; a++) { + _dexUrls[callsArray[a]] = dexUrls[callsArray[a]]; + } + if (coin === 'BTC' || coin === 'SYS') { + delete _dexUrls.refresh; + delete _dexUrls.getbalance; + } + //console.log(JSON.stringify(dexUrls)); + console.log(coin + ' address ' + address); + if (!outObj.basilisk[coin][address]) { + outObj.basilisk[coin][address] = {}; + writeCache(); + } + console.log(_dexUrls); + + // set current call status + async.forEachOf(_dexUrls, function(dexUrl, key) { + if (!outObj.basilisk[coin][address][key]) { + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].status = 'waiting'; + } else { + outObj.basilisk[coin][address][key].status = 'waiting'; + } + }); + writeCache(); + + async.forEachOf(_dexUrls, function(dexUrl, key) { + var tooEarly = false; + if (outObj.basilisk[coin][address][key] && + outObj.basilisk[coin][address][key].timestamp && + checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { + tooEarly = true; + outObj.basilisk[coin][address][key].status = 'done'; + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': 'too early' + } + } + } + }); + } + if (!tooEarly) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'in progress' + } + } + } + }); + outObj.basilisk[coin][address][key].status = 'in progress'; + request({ + url: dexUrl, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': body + } + } + } + }); + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].data = JSON.parse(body); + outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp + outObj.basilisk[coin][address][key].status = 'done'; + console.log(dexUrl); + console.log(body); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + + writeCache(); + } + }); + } else { + console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + } + }); + } + } else { + shepherd.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-all', + 'status': 'done', + 'resp': 'internal error' + } + } + }); + cacheCallInProgress = false; + } + } else { + res.end(JSON.stringify({ + 'msg': 'error', + 'result': 'another call is in progress already' + })); + } +});*/ + +module.exports = cache; \ No newline at end of file diff --git a/routes/shepherd.js b/routes/shepherd.js index ef67f6e..bb75f0a 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -72,6 +72,8 @@ if (os.platform() === 'win32') { komodoDir = path.normalize(komodoDir); } +shepherd.iguanaDir = iguanaDir; + shepherd.appConfig = { "edexGuiOnly": true, "iguanaGuiOnly": false, @@ -108,945 +110,16 @@ shepherd.get('/sysinfo', function(req, res, next) { res.send(obj); }); -shepherd.get('/socket-test', function(req, res, next) { - res.send('Sockets test'); - shepherd.io.emit('messages', { 'message': 'legacy of grewal' }); -}); - // expose sockets obj shepherd.setIO = function(io) { shepherd.io = io; }; -/* - * type: GET - * params: pubkey - */ +var cache = require('./cache'); +cache.setVar('iguanaDir', iguanaDir); +cache.setVar('appConfig', appConfig); shepherd.get('/cache', function(req, res, next) { - var pubkey = req.query.pubkey; - - if (pubkey) { - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { - fs.readFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) { - if (err) { - var errorObj = { - 'msg': 'error', - 'result': err - }; - - res.end(JSON.stringify(errorObj)); - } else { - var parsedJSON = 'JSON parse error'; - - try { - parsedJSON = JSON.parse(data); - } catch (e) { - console.log('JSON parse error'); - } - - var successObj = { - 'msg': 'success', - 'result': parsedJSON - }; - - res.end(JSON.stringify(successObj)); - } - }); - } else { - var errorObj = { - 'msg': 'error', - 'result': 'no file with handle ' + pubkey - }; - - res.end(JSON.stringify(errorObj)); - } - } else { - var errorObj = { - 'msg': 'error', - 'result': 'no pubkey provided' - }; - - res.end(JSON.stringify(errorObj)); - } -}); - -/* - * type: GET - * params: filename - */ -shepherd.get('/groom', function(req, res, next) { - var _filename = req.query.filename; - - if (_filename) { - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + _filename + '.json')) { - fs.readFile(iguanaDir + '/shepherd/cache-' + _filename + '.json', 'utf8', function (err, data) { - if (err) { - var errorObj = { - 'msg': 'error', - 'result': err - }; - - res.end(JSON.stringify(errorObj)); - } else { - var successObj = { - 'msg': 'success', - 'result': data ? JSON.parse(data) : '' - }; - - res.end(JSON.stringify(successObj)); - } - }); - } else { - var errorObj = { - 'msg': 'error', - 'result': 'no file with name ' + _filename - }; - - res.end(JSON.stringify(errorObj)); - } - } else { - var errorObj = { - 'msg': 'error', - 'result': 'no file name provided' - }; - - res.end(JSON.stringify(errorObj)); - } -}); - -/* - * type: DELETE - * params: filename - */ -shepherd.delete('/groom', function(req, res, next) { - var _filename = req.body.filename; - - if (_filename) { - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + _filename + '.json')) { - fs.unlink(iguanaDir + '/shepherd/cache-' + _filename + '.json', function(err) { - if (err) { - var errorObj = { - 'msg': 'error', - 'result': err - }; - - res.end(JSON.stringify(errorObj)); - } else { - var successObj = { - 'msg': 'success', - 'result': 'deleted' - }; - - res.end(JSON.stringify(successObj)); - } - }); - } else { - var errorObj = { - 'msg': 'error', - 'result': 'no file with name ' + _filename - }; - - res.end(JSON.stringify(errorObj)); - } - } else { - var errorObj = { - 'msg': 'error', - 'result': 'no file name provided' - }; - - res.end(JSON.stringify(errorObj)); - } -}); - -/* - * type: POST - * params: filename, payload - */ -shepherd.post('/groom', function(req, res) { - var _filename = req.body.filename, - _payload = req.body.payload; - - if (_filename) { - if (!_payload) { - var errorObj = { - 'msg': 'error', - 'result': 'no payload provided' - }; - - res.end(JSON.stringify(errorObj)); - } else { - fs.writeFile(iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) { - if (err) { - var errorObj = { - 'msg': 'error', - 'result': err - }; - - res.end(JSON.stringify(errorObj)); - } else { - var successObj = { - 'msg': 'success', - 'result': 'done' - }; - - res.end(JSON.stringify(successObj)); - } - }); - } - } else { - var errorObj = { - 'msg': 'error', - 'result': 'no file name provided' - }; - - res.end(JSON.stringify(errorObj)); - } -}); - -var cacheCallInProgress = false, - cacheGlobLifetime = 300; // sec - -// TODO: reset calls' states on new /cache call start -/* - * type: GET - * params: userpass, pubkey, skip - */ -shepherd.get('/cache-all', function(req, res, next) { - if (req.query.pubkey && !fs.existsSync(iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { - cacheCallInProgress = false; - } - - if (!cacheCallInProgress) { - cacheCallInProgress = true; - - var sessionKey = req.query.userpass, - pubkey = req.query.pubkey, - skipTimeout = req.query.skip, - _obj = { - 'msg': 'error', - 'result': 'error' - }, - outObj = { - basilisk: {} - }, - writeCache = function() { - fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { - if (err) { - return console.log(err); - } - - console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); - }); - }, - callStack = {}, - checkCallStack = function() { - var total = 0; - - for (var coin in callStack) { - total =+ callStack[coin]; - } - - if (total / Object.keys(callStack).length === 1) { - cacheCallInProgress = false; - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'done', - 'resp': 'success' - } - } - }); - } - }, - checkTimestamp = function(dateToCheck) { - var currentEpochTime = new Date(Date.now()) / 1000, - secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); - - return Math.floor(secondsElapsed); - }, - internalError = false; - - if (!sessionKey) { - var errorObj = { - 'msg': 'error', - 'result': 'no session key provided' - }; - - res.end(JSON.stringify(errorObj)); - internalError = true; - } - - if (!pubkey) { - var errorObj = { - 'msg': 'error', - 'result': 'no pubkey provided' - }; - - res.end(JSON.stringify(errorObj)); - internalError = true; - } - - if (!internalError) { - console.log('cache-all call started'); - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress' - } - } - }); - res.end(JSON.stringify({ - 'msg': 'success', - 'result': 'call is initiated' - })); - - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'allcoins', - 'status': 'in progress' - } - } - } - }); - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'allcoins', - 'status': 'done', - 'resp': body - } - } - } - }); - body = JSON.parse(body); - // basilisk coins - if (body.basilisk && body.basilisk.length) { - // get coin addresses - async.each(body.basilisk, function(coin) { - callStack[coin] = 1; - }); - - async.each(body.basilisk, function(coin) { - outObj.basilisk[coin] = {}; - writeCache(); - - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'in progress' - } - } - } - }); - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin].addresses = JSON.parse(body).result; - writeCache(); - var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; - callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? 2 : 4); - console.log(coin + ' stack len ' + callStack[coin]); - - async.each(outObj.basilisk[coin].addresses, function(address) { - var dexUrls = { - 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address - }; - if (coin === 'BTC' || coin === 'SYS') { - delete dexUrls.refresh; - delete dexUrls.getbalance; - } - //console.log(JSON.stringify(dexUrls)); - console.log(coin + ' address ' + address); - outObj.basilisk[coin][address] = {}; - writeCache(); - - // set current call status - async.forEachOf(dexUrls, function(dexUrl, key) { - if (!outObj.basilisk[coin][address][key]) { - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].status = 'waiting'; - } else { - outObj.basilisk[coin][address][key].status = 'waiting'; - } - }); - writeCache(); - - async.forEachOf(dexUrls, function(dexUrl, key) { - var tooEarly = false; - if (outObj.basilisk[coin][address][key] && - outObj.basilisk[coin][address][key].timestamp && - checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { - tooEarly = true; - outObj.basilisk[coin][address][key].status = 'done'; - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': 'too early' - } - } - } - }); - } - tooEarly = skipTimeout ? false : tooEarly; - if (!tooEarly) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'in progress' - } - } - } - }); - outObj.basilisk[coin][address][key].status = 'in progress'; - request({ - url: dexUrl, - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].data = JSON.parse(body); - outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp - outObj.basilisk[coin][address][key].status = 'done'; - console.log(dexUrl); - console.log(body); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - - writeCache(); - } - }); - } else { - console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - } - }); - }); - } else { - // TODO: error - } - }); - }); - } else { - // TODO: error - } - } else { - // TODO: error - } - }); - } else { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'done', - 'resp': 'internal error' - } - } - }); - cacheCallInProgress = false; - } - } else { - res.end(JSON.stringify({ - 'msg': 'error', - 'result': 'another call is in progress already' - })); - } -}); - -/* - * type: GET - * params: userpass, pubkey, coin, address, skip - */ -shepherd.get('/cache-one', function(req, res, next) { - if (req.query.pubkey && !fs.existsSync(iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { - cacheCallInProgress = false; - } - - if (!cacheCallInProgress) { - // TODO: add check to allow only one cache call/sequence in progress - cacheCallInProgress = true; - var sessionKey = req.query.userpass, - coin = req.query.coin, - address = req.query.address, - pubkey = req.query.pubkey, - skipTimeout = req.query.skip, - callsArray = req.query.calls.split(':'), - errorObj = { - 'msg': 'error', - 'result': 'error' - }, - outObj = {}, - pubkey, - writeCache = function(timeStamp) { - if (timeStamp) { - outObj.timestamp = timeStamp; - } - - fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { - if (err) { - return console.log(err); - } - - console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); - if (timeStamp) { - console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped'); - } - }); - }, - callStack = {}, - checkCallStack = function() { - var total = 0; - - for (var coin in callStack) { - total =+ callStack[coin]; - } - - if (total / Object.keys(callStack).length === 1) { - cacheCallInProgress = false; - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'done', - 'resp': 'success' - } - } - }); - // add timestamp to cache file - // writeCache(Date.now()); - } - }, - checkTimestamp = function(dateToCheck) { - var currentEpochTime = new Date(Date.now()) / 1000, - secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); - - return Math.floor(secondsElapsed); - }, - internalError = false; - - callStack[coin] = 1; - console.log(callsArray); - - if (!sessionKey) { - var errorObj = { - 'msg': 'error', - 'result': 'no session key provided' - }; - - res.end(JSON.stringify(errorObj)); - internalError = true; - } - - if (!pubkey) { - var errorObj = { - 'msg': 'error', - 'result': 'no pubkey provided' - }; - - res.end(JSON.stringify(errorObj)); - internalError = true; - } - - console.log('cache-one call started'); - - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { - var _file = fs.readFileSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); - outObj = _file ? JSON.parse(_file) : {}; - - if (!outObj || !outObj.basilisk) { - console.log('no local basilisk info'); - outObj['basilisk'] = {}; - outObj['basilisk'][coin] = {}; - } else { - if (!outObj['basilisk'][coin]) { - console.log('no local coin info'); - outObj['basilisk'][coin] = {}; - } - } - } else { - outObj['basilisk'] = {}; - outObj['basilisk'][coin] = {}; - } - - res.end(JSON.stringify({ - 'msg': 'success', - 'result': 'call is initiated' - })); - - if (!internalError) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress' - } - } - }); - // update all available coin addresses - if (!address) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'in progress' - } - } - } - }); - request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin].addresses = JSON.parse(body).result; - console.log(JSON.parse(body).result); - writeCache(); - var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; - callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length); - console.log(coin + ' stack len ' + callStack[coin]); - - async.each(outObj.basilisk[coin].addresses, function(address) { - var dexUrls = { - 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address - }, - _dexUrls = {}; - - for (var a = 0; a < callsArray.length; a++) { - _dexUrls[callsArray[a]] = dexUrls[callsArray[a]]; - } - if (coin === 'BTC' || coin === 'SYS') { - delete _dexUrls.refresh; - delete _dexUrls.getbalance; - } - //console.log(JSON.stringify(dexUrls)); - console.log(coin + ' address ' + address); - if (!outObj.basilisk[coin][address]) { - outObj.basilisk[coin][address] = {}; - writeCache(); - } - - // set current call status - async.forEachOf(_dexUrls, function(dexUrl, key) { - if (!outObj.basilisk[coin][address][key]) { - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].status = 'waiting'; - } else { - outObj.basilisk[coin][address][key].status = 'waiting'; - } - }); - writeCache(); - - async.forEachOf(_dexUrls, function(dexUrl, key) { - var tooEarly = false; - if (outObj.basilisk[coin][address][key] && - outObj.basilisk[coin][address][key].timestamp && - checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { - tooEarly = true; - outObj.basilisk[coin][address][key].status = 'done'; - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': 'too early' - } - } - } - }); - } - tooEarly = skipTimeout ? false : tooEarly; - if (!tooEarly) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'in progress' - } - } - } - }); - outObj.basilisk[coin][address][key].status = 'in progress'; - request({ - url: dexUrl, - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].data = JSON.parse(body); - outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp - outObj.basilisk[coin][address][key].status = 'done'; - console.log(dexUrl); - console.log(body); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - - writeCache(); - } - }); - } else { - console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - } - }); - }); - } else { - // TODO: error - } - }); - } else { - callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); - console.log(coin + ' stack len ' + callStack[coin]); - - var dexUrls = { - 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address - }, - _dexUrls = {}; - - for (var a = 0; a < callsArray.length; a++) { - _dexUrls[callsArray[a]] = dexUrls[callsArray[a]]; - } - if (coin === 'BTC' || coin === 'SYS') { - delete _dexUrls.refresh; - delete _dexUrls.getbalance; - } - //console.log(JSON.stringify(dexUrls)); - console.log(coin + ' address ' + address); - if (!outObj.basilisk[coin][address]) { - outObj.basilisk[coin][address] = {}; - writeCache(); - } - console.log(_dexUrls); - - // set current call status - async.forEachOf(_dexUrls, function(dexUrl, key) { - if (!outObj.basilisk[coin][address][key]) { - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].status = 'waiting'; - } else { - outObj.basilisk[coin][address][key].status = 'waiting'; - } - }); - writeCache(); - - async.forEachOf(_dexUrls, function(dexUrl, key) { - var tooEarly = false; - if (outObj.basilisk[coin][address][key] && - outObj.basilisk[coin][address][key].timestamp && - checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { - tooEarly = true; - outObj.basilisk[coin][address][key].status = 'done'; - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': 'too early' - } - } - } - }); - } - if (!tooEarly) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'in progress' - } - } - } - }); - outObj.basilisk[coin][address][key].status = 'in progress'; - request({ - url: dexUrl, - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].data = JSON.parse(body); - outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp - outObj.basilisk[coin][address][key].status = 'done'; - console.log(dexUrl); - console.log(body); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - - writeCache(); - } - }); - } else { - console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - } - }); - } - } else { - shepherd.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'done', - 'resp': 'internal error' - } - } - }); - cacheCallInProgress = false; - } - } else { - res.end(JSON.stringify({ - 'msg': 'error', - 'result': 'another call is in progress already' - })); - } + cache.get(req, res, next); }); /* @@ -1947,6 +1020,4 @@ shepherd.SystemInfo = function() { return os_data; } - - module.exports = shepherd; \ No newline at end of file From 286c5d10b7e5a1420e74def64a64a9d8619be2e9 Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Tue, 21 Mar 2017 14:42:07 +0100 Subject: [PATCH 2/4] split shepherd.js #2 --- gui/loading.css | 4 +- private/mainmenu.js | 9 +++ routes/cache.js | 151 ++++++++++++++++++++------------------------ routes/shepherd.js | 53 ++++++++++++++-- 4 files changed, 127 insertions(+), 90 deletions(-) diff --git a/gui/loading.css b/gui/loading.css index 20b5f18..74b3067 100644 --- a/gui/loading.css +++ b/gui/loading.css @@ -12,7 +12,7 @@ body { height: 240px; width: 100%; position: absolute; - top: 0; + top: -30px; bottom: 0; left: 0; right: 0; @@ -21,7 +21,7 @@ body { .pulse-loader { position: absolute; - top: 20px; + top: 10px; left: -100px; margin: 80px 50px; width: 400px !important; diff --git a/private/mainmenu.js b/private/mainmenu.js index 07cbbdf..3fac3f0 100644 --- a/private/mainmenu.js +++ b/private/mainmenu.js @@ -55,6 +55,15 @@ const template = [ focusedWindow.webContents.toggleDevTools(); } }, + /*{ + label: 'Relaunch app', + click (item, focusedWindow) { + if (focusedWindow) + app.relaunch({ args: process.argv.slice(1).concat([ '--relaunch' ]) }); + app.exit(0); + } + TODO: figure out a way to restart iguana and komodod, reload gui + },*/ { type: 'separator' }, diff --git a/routes/cache.js b/routes/cache.js index 5963c6d..c037015 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -1,21 +1,20 @@ -const fs = require('fs-extra'); +const fs = require('fs-extra'), + request = require('request'), + async = require('async'), + rimraf = require('rimraf'); var cache = {}; -cache.setVars = function(variable, value) { +cache.setVar = function(variable, value) { cache[variable] = value; } -/* - * type: GET - * params: pubkey - */ cache.get = function(req, res, next) { var pubkey = req.query.pubkey; if (pubkey) { - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { - fs.readFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) { + if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { + fs.readFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8', function (err, data) { if (err) { var errorObj = { 'msg': 'error', @@ -58,16 +57,12 @@ cache.get = function(req, res, next) { } } -/* - * type: GET - * params: filename - */ -/*cache.get('/groom', function(req, res, next) { +cache.groomGet = function(req, res, next) { var _filename = req.query.filename; if (_filename) { - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + _filename + '.json')) { - fs.readFile(iguanaDir + '/shepherd/cache-' + _filename + '.json', 'utf8', function (err, data) { + if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json')) { + fs.readFile(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', 'utf8', function (err, data) { if (err) { var errorObj = { 'msg': 'error', @@ -100,18 +95,14 @@ cache.get = function(req, res, next) { res.end(JSON.stringify(errorObj)); } -}); +} -/* - * type: DELETE - * params: filename - */ -/*shepherd.delete('/groom', function(req, res, next) { +cache.groomDelete = function(req, res, next) { var _filename = req.body.filename; if (_filename) { - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + _filename + '.json')) { - fs.unlink(iguanaDir + '/shepherd/cache-' + _filename + '.json', function(err) { + if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json')) { + fs.unlink(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', function(err) { if (err) { var errorObj = { 'msg': 'error', @@ -144,13 +135,9 @@ cache.get = function(req, res, next) { res.end(JSON.stringify(errorObj)); } -}); +} -/* - * type: POST - * params: filename, payload - */ -/*shepherd.post('/groom', function(req, res) { +cache.groomPost = function(req, res) { var _filename = req.body.filename, _payload = req.body.payload; @@ -163,7 +150,7 @@ cache.get = function(req, res, next) { res.end(JSON.stringify(errorObj)); } else { - fs.writeFile(iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) { + fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + _filename + '.json', _payload, function (err) { if (err) { var errorObj = { 'msg': 'error', @@ -189,18 +176,14 @@ cache.get = function(req, res, next) { res.end(JSON.stringify(errorObj)); } -}); +} var cacheCallInProgress = false, cacheGlobLifetime = 300; // sec // TODO: reset calls' states on new /cache call start -/* - * type: GET - * params: userpass, pubkey, skip - */ -/*shepherd.get('/cache-all', function(req, res, next) { - if (req.query.pubkey && !fs.existsSync(iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { +cache.all = function(req, res, next) { + if (req.query.pubkey && !fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { cacheCallInProgress = false; } @@ -218,12 +201,12 @@ var cacheCallInProgress = false, basilisk: {} }, writeCache = function() { - fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { + fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { if (err) { return console.log(err); } - console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); + console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); }); }, callStack = {}, @@ -236,7 +219,7 @@ var cacheCallInProgress = false, if (total / Object.keys(callStack).length === 1) { cacheCallInProgress = false; - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -277,7 +260,7 @@ var cacheCallInProgress = false, if (!internalError) { console.log('cache-all call started'); - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -290,7 +273,7 @@ var cacheCallInProgress = false, 'result': 'call is initiated' })); - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -303,11 +286,11 @@ var cacheCallInProgress = false, } }); request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, + url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -332,7 +315,7 @@ var cacheCallInProgress = false, outObj.basilisk[coin] = {}; writeCache(); - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -346,11 +329,11 @@ var cacheCallInProgress = false, } }); request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', + url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -372,10 +355,10 @@ var cacheCallInProgress = false, async.each(outObj.basilisk[coin].addresses, function(address) { var dexUrls = { - 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + 'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + 'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address }; if (coin === 'BTC' || coin === 'SYS') { delete dexUrls.refresh; @@ -404,7 +387,7 @@ var cacheCallInProgress = false, checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { tooEarly = true; outObj.basilisk[coin][address][key].status = 'done'; - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -422,7 +405,7 @@ var cacheCallInProgress = false, } tooEarly = skipTimeout ? false : tooEarly; if (!tooEarly) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -442,7 +425,7 @@ var cacheCallInProgress = false, method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -491,7 +474,7 @@ var cacheCallInProgress = false, } }); } else { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -508,14 +491,14 @@ var cacheCallInProgress = false, 'result': 'another call is in progress already' })); } -}); +} /* * type: GET * params: userpass, pubkey, coin, address, skip */ -/*shepherd.get('/cache-one', function(req, res, next) { - if (req.query.pubkey && !fs.existsSync(iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { +cache.one = function(req, res, next) { + if (req.query.pubkey && !fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { cacheCallInProgress = false; } @@ -539,14 +522,14 @@ var cacheCallInProgress = false, outObj.timestamp = timeStamp; } - fs.writeFile(iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { + fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { if (err) { return console.log(err); } - console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); + console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); if (timeStamp) { - console.log('file ' + iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped'); + console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is timestamped'); } }); }, @@ -560,7 +543,7 @@ var cacheCallInProgress = false, if (total / Object.keys(callStack).length === 1) { cacheCallInProgress = false; - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -606,8 +589,8 @@ var cacheCallInProgress = false, console.log('cache-one call started'); - if (fs.existsSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { - var _file = fs.readFileSync(iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); + if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { + var _file = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); outObj = _file ? JSON.parse(_file) : {}; if (!outObj || !outObj.basilisk) { @@ -631,7 +614,7 @@ var cacheCallInProgress = false, })); if (!internalError) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -641,7 +624,7 @@ var cacheCallInProgress = false, }); // update all available coin addresses if (!address) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -655,11 +638,11 @@ var cacheCallInProgress = false, } }); request({ - url: 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', + url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -682,10 +665,10 @@ var cacheCallInProgress = false, async.each(outObj.basilisk[coin].addresses, function(address) { var dexUrls = { - 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + 'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + 'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address }, _dexUrls = {}; @@ -721,7 +704,7 @@ var cacheCallInProgress = false, checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { tooEarly = true; outObj.basilisk[coin][address][key].status = 'done'; - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -739,7 +722,7 @@ var cacheCallInProgress = false, } tooEarly = skipTimeout ? false : tooEarly; if (!tooEarly) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -759,7 +742,7 @@ var cacheCallInProgress = false, method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -804,10 +787,10 @@ var cacheCallInProgress = false, console.log(coin + ' stack len ' + callStack[coin]); var dexUrls = { - 'listunspent': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + shepherd.appConfig.host + ':' + shepherd.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + 'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + 'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address }, _dexUrls = {}; @@ -844,7 +827,7 @@ var cacheCallInProgress = false, checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { tooEarly = true; outObj.basilisk[coin][address][key].status = 'done'; - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -861,7 +844,7 @@ var cacheCallInProgress = false, }); } if (!tooEarly) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -881,7 +864,7 @@ var cacheCallInProgress = false, method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', @@ -918,7 +901,7 @@ var cacheCallInProgress = false, }); } } else { - shepherd.io.emit('messages', { + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-all', @@ -935,6 +918,6 @@ var cacheCallInProgress = false, 'result': 'another call is in progress already' })); } -});*/ +}; module.exports = cache; \ No newline at end of file diff --git a/routes/shepherd.js b/routes/shepherd.js index bb75f0a..9cfaecb 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -72,8 +72,6 @@ if (os.platform() === 'win32') { komodoDir = path.normalize(komodoDir); } -shepherd.iguanaDir = iguanaDir; - shepherd.appConfig = { "edexGuiOnly": true, "iguanaGuiOnly": false, @@ -110,18 +108,65 @@ shepherd.get('/sysinfo', function(req, res, next) { res.send(obj); }); +var cache = require('./cache'); + // expose sockets obj shepherd.setIO = function(io) { shepherd.io = io; + cache.setVar('io', io); }; -var cache = require('./cache'); cache.setVar('iguanaDir', iguanaDir); -cache.setVar('appConfig', appConfig); +cache.setVar('appConfig', shepherd.appConfig); + +/* + * type: GET + * params: pubkey + */ shepherd.get('/cache', function(req, res, next) { cache.get(req, res, next); }); +/* + * type: GET + * params: filename + */ +shepherd.get('/groom', function(req, res, next) { + cache.groomGet(req, res, next); +}) + +/* + * type: DELETE + * params: filename + */ +shepherd.delete('/groom', function(req, res, next) { + cache.groomDelete(req, res, next); +}); + +/* + * type: POST + * params: filename, payload + */ +shepherd.post('/groom', function(req, res) { + cache.groomPost(req, res, next); +}); + +/* + * type: GET + * params: userpass, pubkey, skip + */ +shepherd.get('/cache-all', function(req, res, next) { + cache.all(req, res, next); +}); + +/* + * type: GET + * params: userpass, pubkey, coin, address, skip + */ +shepherd.get('/cache-one', function(req, res, next) { + cache.one(req, res, next); +}); + /* * type: GET * params: herd, lastLines From 3dc599721bb0992955511200ac8c93dd5d72cf21 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Wed, 22 Mar 2017 09:25:45 +0300 Subject: [PATCH 3/4] cache.js code refactoring --- compile-dev.sh | 10 -- package.json | 2 +- routes/cache.js | 313 ++++++++++++++++++++++++++++-------------------- 3 files changed, 184 insertions(+), 141 deletions(-) delete mode 100755 compile-dev.sh diff --git a/compile-dev.sh b/compile-dev.sh deleted file mode 100755 index 3b866e9..0000000 --- a/compile-dev.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash -cd "tmp/Iguana-application" - -gulp prod - -cd ../../ - -rm "gui/Iguana-GUI" -rf - -cp -rf "tmp/Iguana-application/compiled/prod" "gui/Iguana-GUI" \ No newline at end of file diff --git a/package.json b/package.json index a630fc0..ff2e3b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iguana_app", - "productName": "Iguana", + "productName": "Agama", "version": "0.1.0", "description": "iguana Desktop App", "main": "main.js", diff --git a/routes/cache.js b/routes/cache.js index c037015..3a8a674 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -1,7 +1,6 @@ const fs = require('fs-extra'), request = require('request'), - async = require('async'), - rimraf = require('rimraf'); + async = require('async'); var cache = {}; @@ -182,7 +181,7 @@ var cacheCallInProgress = false, cacheGlobLifetime = 300; // sec // TODO: reset calls' states on new /cache call start -cache.all = function(req, res, next) { +/*cache.all = function(req, res, next) { if (req.query.pubkey && !fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { cacheCallInProgress = false; } @@ -491,7 +490,7 @@ cache.all = function(req, res, next) { 'result': 'another call is in progress already' })); } -} +}*/ /* * type: GET @@ -552,8 +551,6 @@ cache.one = function(req, res, next) { } } }); - // add timestamp to cache file - // writeCache(Date.now()); } }, checkTimestamp = function(dateToCheck) { @@ -637,151 +634,207 @@ cache.one = function(req, res, next) { } } }); - request({ - url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'done', - 'resp': body + function getAddresses(coin) { + request({ + url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + cache.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'getaddressesbyaccount', + 'coin': coin, + 'status': 'done', + 'resp': body + } } } - } - }); - outObj.basilisk[coin].addresses = JSON.parse(body).result; - console.log(JSON.parse(body).result); - writeCache(); - var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; - callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length); - console.log(coin + ' stack len ' + callStack[coin]); - - async.each(outObj.basilisk[coin].addresses, function(address) { - var dexUrls = { - 'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address - }, - _dexUrls = {}; - - for (var a = 0; a < callsArray.length; a++) { - _dexUrls[callsArray[a]] = dexUrls[callsArray[a]]; - } - if (coin === 'BTC' || coin === 'SYS') { - delete _dexUrls.refresh; - delete _dexUrls.getbalance; - } - //console.log(JSON.stringify(dexUrls)); - console.log(coin + ' address ' + address); - if (!outObj.basilisk[coin][address]) { - outObj.basilisk[coin][address] = {}; - writeCache(); - } - - // set current call status - async.forEachOf(_dexUrls, function(dexUrl, key) { - if (!outObj.basilisk[coin][address][key]) { - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].status = 'waiting'; - } else { - outObj.basilisk[coin][address][key].status = 'waiting'; - } }); + outObj.basilisk[coin].addresses = JSON.parse(body).result; + console.log(JSON.parse(body).result); writeCache(); + var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; + callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length); + console.log(coin + ' stack len ' + callStack[coin]); + + async.each(outObj.basilisk[coin].addresses, function(address) { + var dexUrls = { + 'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, + 'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, + 'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + }, + _dexUrls = {}; + + for (var a = 0; a < callsArray.length; a++) { + _dexUrls[callsArray[a]] = dexUrls[callsArray[a]]; + } + if (coin === 'BTC' || coin === 'SYS') { + delete _dexUrls.refresh; + delete _dexUrls.getbalance; + } + //console.log(JSON.stringify(dexUrls)); + console.log(coin + ' address ' + address); + if (!outObj.basilisk[coin][address]) { + outObj.basilisk[coin][address] = {}; + writeCache(); + } - async.forEachOf(_dexUrls, function(dexUrl, key) { - var tooEarly = false; - if (outObj.basilisk[coin][address][key] && - outObj.basilisk[coin][address][key].timestamp && - checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { - tooEarly = true; - outObj.basilisk[coin][address][key].status = 'done'; - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': 'too early' + // set current call status + async.forEachOf(_dexUrls, function(dexUrl, key) { + if (!outObj.basilisk[coin][address][key]) { + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].status = 'waiting'; + } else { + outObj.basilisk[coin][address][key].status = 'waiting'; + } + }); + writeCache(); + + async.forEachOf(_dexUrls, function(dexUrl, key) { + var tooEarly = false; + if (outObj.basilisk[coin][address][key] && + outObj.basilisk[coin][address][key].timestamp && + checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { + tooEarly = true; + outObj.basilisk[coin][address][key].status = 'done'; + cache.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': 'too early' + } } } + }); + } + tooEarly = skipTimeout ? false : tooEarly; + if (!tooEarly) { + cache.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'in progress' + } + } + } + }); + outObj.basilisk[coin][address][key].status = 'in progress'; + request({ + url: dexUrl, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + cache.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': key, + 'coin': coin, + 'address': address, + 'status': 'done', + 'resp': body + } + } + } + }); + outObj.basilisk[coin][address][key] = {}; + outObj.basilisk[coin][address][key].data = JSON.parse(body); + outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp + outObj.basilisk[coin][address][key].status = 'done'; + console.log(dexUrl); + console.log(body); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + + writeCache(); + } + }); + } else { + console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); + callStack[coin]--; + console.log(coin + ' _stack len ' + callStack[coin]); + checkCallStack(); + } + }); + }); + } else { + // TODO: error + } + }); + } + + if (coin === 'all') { + request({ + url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, + method: 'GET' + }, function (error, response, body) { + if (response && response.statusCode && response.statusCode === 200) { + cache.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'allcoins', + 'status': 'done', + 'resp': body } - }); + } } - tooEarly = skipTimeout ? false : tooEarly; - if (!tooEarly) { + }); + body = JSON.parse(body); + // basilisk coins + if (body.basilisk && body.basilisk.length) { + // get coin addresses + async.each(body.basilisk, function(coin) { + callStack[coin] = 1; + }); + + async.each(body.basilisk, function(coin) { + outObj.basilisk[coin] = {}; + writeCache(); + cache.io.emit('messages', { 'message': { 'shepherd': { 'method': 'cache-one', 'status': 'in progress', 'iguanaAPI': { - 'method': key, + 'method': 'getaddressesbyaccount', 'coin': coin, - 'address': address, 'status': 'in progress' } } } }); - outObj.basilisk[coin][address][key].status = 'in progress'; - request({ - url: dexUrl, - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].data = JSON.parse(body); - outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp - outObj.basilisk[coin][address][key].status = 'done'; - console.log(dexUrl); - console.log(body); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - - writeCache(); - } - }); - } else { - console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - } - }); - }); - } else { - // TODO: error - } - }); + + getAddresses(coin); + }); + } + } + }); + } else { + getAddresses(coin); + } } else { callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); console.log(coin + ' stack len ' + callStack[coin]); From 293daa3c80ef6dfb8583671e0c5fb14a8fcbd79a Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Wed, 22 Mar 2017 16:55:15 +0100 Subject: [PATCH 4/4] merged cache-all into cache-one; added mock --- main.js | 2 +- routes/cache.js | 329 ++------------------------------------------- routes/shepherd.js | 8 ++ 3 files changed, 23 insertions(+), 316 deletions(-) diff --git a/main.js b/main.js index 2e7b31a..872fc12 100644 --- a/main.js +++ b/main.js @@ -9,7 +9,7 @@ const electron = require('electron'), os = require('os'), spawn = require('child_process').spawn, exec = require('child_process').exec, - { Menu } = require("electron"), + { Menu } = require('electron'), fixPath = require('fix-path'); var express = require('express'), diff --git a/routes/cache.js b/routes/cache.js index 3a8a674..91742f8 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -181,316 +181,7 @@ var cacheCallInProgress = false, cacheGlobLifetime = 300; // sec // TODO: reset calls' states on new /cache call start -/*cache.all = function(req, res, next) { - if (req.query.pubkey && !fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + req.query.pubkey + '.json')) { - cacheCallInProgress = false; - } - - if (!cacheCallInProgress) { - cacheCallInProgress = true; - - var sessionKey = req.query.userpass, - pubkey = req.query.pubkey, - skipTimeout = req.query.skip, - _obj = { - 'msg': 'error', - 'result': 'error' - }, - outObj = { - basilisk: {} - }, - writeCache = function() { - fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', JSON.stringify(outObj), function(err) { - if (err) { - return console.log(err); - } - - console.log('file ' + cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json is updated'); - }); - }, - callStack = {}, - checkCallStack = function() { - var total = 0; - - for (var coin in callStack) { - total =+ callStack[coin]; - } - - if (total / Object.keys(callStack).length === 1) { - cacheCallInProgress = false; - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'done', - 'resp': 'success' - } - } - }); - } - }, - checkTimestamp = function(dateToCheck) { - var currentEpochTime = new Date(Date.now()) / 1000, - secondsElapsed = Number(currentEpochTime) - Number(dateToCheck / 1000); - - return Math.floor(secondsElapsed); - }, - internalError = false; - - if (!sessionKey) { - var errorObj = { - 'msg': 'error', - 'result': 'no session key provided' - }; - - res.end(JSON.stringify(errorObj)); - internalError = true; - } - - if (!pubkey) { - var errorObj = { - 'msg': 'error', - 'result': 'no pubkey provided' - }; - - res.end(JSON.stringify(errorObj)); - internalError = true; - } - - if (!internalError) { - console.log('cache-all call started'); - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress' - } - } - }); - res.end(JSON.stringify({ - 'msg': 'success', - 'result': 'call is initiated' - })); - - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'allcoins', - 'status': 'in progress' - } - } - } - }); - request({ - url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'allcoins', - 'status': 'done', - 'resp': body - } - } - } - }); - body = JSON.parse(body); - // basilisk coins - if (body.basilisk && body.basilisk.length) { - // get coin addresses - async.each(body.basilisk, function(coin) { - callStack[coin] = 1; - }); - - async.each(body.basilisk, function(coin) { - outObj.basilisk[coin] = {}; - writeCache(); - - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'in progress' - } - } - } - }); - request({ - url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin].addresses = JSON.parse(body).result; - writeCache(); - var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; - callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? 2 : 4); - console.log(coin + ' stack len ' + callStack[coin]); - - async.each(outObj.basilisk[coin].addresses, function(address) { - var dexUrls = { - 'listunspent': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'listtransactions': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, - 'getbalance': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address - }; - if (coin === 'BTC' || coin === 'SYS') { - delete dexUrls.refresh; - delete dexUrls.getbalance; - } - //console.log(JSON.stringify(dexUrls)); - console.log(coin + ' address ' + address); - outObj.basilisk[coin][address] = {}; - writeCache(); - - // set current call status - async.forEachOf(dexUrls, function(dexUrl, key) { - if (!outObj.basilisk[coin][address][key]) { - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].status = 'waiting'; - } else { - outObj.basilisk[coin][address][key].status = 'waiting'; - } - }); - writeCache(); - - async.forEachOf(dexUrls, function(dexUrl, key) { - var tooEarly = false; - if (outObj.basilisk[coin][address][key] && - outObj.basilisk[coin][address][key].timestamp && - checkTimestamp(outObj.basilisk[coin][address][key].timestamp) < cacheGlobLifetime) { - tooEarly = true; - outObj.basilisk[coin][address][key].status = 'done'; - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': 'too early' - } - } - } - }); - } - tooEarly = skipTimeout ? false : tooEarly; - if (!tooEarly) { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'in progress' - } - } - } - }); - outObj.basilisk[coin][address][key].status = 'in progress'; - request({ - url: dexUrl, - method: 'GET' - }, function (error, response, body) { - if (response && response.statusCode && response.statusCode === 200) { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'in progress', - 'iguanaAPI': { - 'method': key, - 'coin': coin, - 'address': address, - 'status': 'done', - 'resp': body - } - } - } - }); - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].data = JSON.parse(body); - outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp - outObj.basilisk[coin][address][key].status = 'done'; - console.log(dexUrl); - console.log(body); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - - writeCache(); - } - }); - } else { - console.log(key + ' is fresh, check back in ' + (cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp)) + 's'); - callStack[coin]--; - console.log(coin + ' _stack len ' + callStack[coin]); - checkCallStack(); - } - }); - }); - } else { - // TODO: error - } - }); - }); - } else { - // TODO: error - } - } else { - // TODO: error - } - }); - } else { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-all', - 'status': 'done', - 'resp': 'internal error' - } - } - }); - cacheCallInProgress = false; - } - } else { - res.end(JSON.stringify({ - 'msg': 'error', - 'result': 'another call is in progress already' - })); - } -}*/ +var mock = require('./mock'); /* * type: GET @@ -504,10 +195,12 @@ cache.one = function(req, res, next) { if (!cacheCallInProgress) { // TODO: add check to allow only one cache call/sequence in progress cacheCallInProgress = true; + var sessionKey = req.query.userpass, coin = req.query.coin, address = req.query.address, pubkey = req.query.pubkey, + mock = req.query.mock, skipTimeout = req.query.skip, callsArray = req.query.calls.split(':'), errorObj = { @@ -551,6 +244,8 @@ cache.one = function(req, res, next) { } } }); + // add timestamp to cache file + // writeCache(Date.now()); } }, checkTimestamp = function(dateToCheck) { @@ -586,7 +281,7 @@ cache.one = function(req, res, next) { console.log('cache-one call started'); - if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json')) { + if (fs.existsSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json') && coin !== 'all') { var _file = fs.readFileSync(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', 'utf8'); outObj = _file ? JSON.parse(_file) : {}; @@ -634,9 +329,11 @@ cache.one = function(req, res, next) { } } }); + function getAddresses(coin) { + var tempUrl = 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*'; request({ - url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*', + url: mock ? 'http://localhost:17777/shepherd/mock?url=' + tempUrl : tempUrl, method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { @@ -736,7 +433,7 @@ cache.one = function(req, res, next) { }); outObj.basilisk[coin][address][key].status = 'in progress'; request({ - url: dexUrl, + url: mock ? 'http://localhost:17777/shepherd/mock?url=' + dexUrl : dexUrl, method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { @@ -783,11 +480,13 @@ cache.one = function(req, res, next) { } if (coin === 'all') { + var tempUrl = 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey; request({ - url: 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey, + url: mock ? 'http://localhost:17777/shepherd/mock?url=' + tempUrl : tempUrl, method: 'GET' }, function (error, response, body) { if (response && response.statusCode && response.statusCode === 200) { + console.log(JSON.parse(body).basilisk); cache.io.emit('messages', { 'message': { 'shepherd': { @@ -833,7 +532,7 @@ cache.one = function(req, res, next) { } }); } else { - getAddresses(coin); + getAddresses(coin); } } else { callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); diff --git a/routes/shepherd.js b/routes/shepherd.js index 9cfaecb..5fe3f75 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -109,6 +109,7 @@ shepherd.get('/sysinfo', function(req, res, next) { }); var cache = require('./cache'); +var mock = require('./mock'); // expose sockets obj shepherd.setIO = function(io) { @@ -167,6 +168,13 @@ shepherd.get('/cache-one', function(req, res, next) { cache.one(req, res, next); }); +/* + * type: GET + */ +shepherd.get('/mock', function(req, res, next) { + mock.get(req, res, next); +}); + /* * type: GET * params: herd, lastLines