From 293daa3c80ef6dfb8583671e0c5fb14a8fcbd79a Mon Sep 17 00:00:00 2001 From: Petr Balashov Date: Wed, 22 Mar 2017 16:55:15 +0100 Subject: [PATCH] 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