From 8f773bb76a81080ba752dafd3bf57f2cd6206df3 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Thu, 23 Mar 2017 11:18:07 +0300 Subject: [PATCH] cache.js code refactor #2 --- routes/cache.js | 366 +++++++++++++++++------------------------------- routes/mock.js | 4 +- 2 files changed, 130 insertions(+), 240 deletions(-) diff --git a/routes/cache.js b/routes/cache.js index 3e4509e..1f94418 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -314,244 +314,8 @@ cache.one = function(req, res, next) { } } }); - // update all available coin addresses - if (!address) { - cache.io.emit('messages', { - 'message': { - 'shepherd': { - 'method': 'cache-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'in progress' - } - } - } - }); - - function getAddresses(coin) { - var tempUrl = 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/bitcoinrpc/getaddressesbyaccount?userpass=' + sessionKey + '&coin=' + coin + '&account=*'; - request({ - url: mock ? 'http://localhost:17777/shepherd/mock?url=' + tempUrl : tempUrl, - 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'; - } - }); - 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: mock ? 'http://localhost:17777/shepherd/mock?url=' + dexUrl : 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(); - } - if (error) { - outObj.basilisk[coin][address][key] = {}; - outObj.basilisk[coin][address][key].data = { 'error': 'request failed' }; - outObj.basilisk[coin][address][key].timestamp = 1471620867 // add timestamp - outObj.basilisk[coin][address][key].status = 'done'; - 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') { - var tempUrl = 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey; - request({ - 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': { - 'method': 'cache-one', - '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-one', - 'status': 'in progress', - 'iguanaAPI': { - 'method': 'getaddressesbyaccount', - 'coin': coin, - 'status': 'in progress' - } - } - } - }); - - getAddresses(coin); - }); - } - } - if (error) { // stop further requests on failure, exit - callStack[coin] = 1; - checkCallStack(); - } - }); - } else { - getAddresses(coin); - } - } else { - callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); - console.log(coin + ' stack len ' + callStack[coin]); + function execDEXRequests(address, coin) { 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, @@ -626,7 +390,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) { @@ -676,6 +440,132 @@ 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: mock ? 'http://localhost:17777/shepherd/mock?url=' + tempUrl : tempUrl, + 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; + var callsArrayBTC = callsArray.length; + if (callsArray.indexOf('getbalance') > - 1) { + callsArrayBTC--; + } + if (callsArray.indexOf('refresh') > - 1) { + callsArrayBTC--; + } + callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length); + console.log(coin + ' stack len ' + callStack[coin]); + + async.each(outObj.basilisk[coin].addresses, function(address) { + execDEXRequests(address, coin); + }); + } else { + // TODO: error + } + }); + } + + // update all available coin addresses + if (!address) { + cache.io.emit('messages', { + 'message': { + 'shepherd': { + 'method': 'cache-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'getaddressesbyaccount', + 'coin': coin, + 'status': 'in progress' + } + } + } + }); + + if (coin === 'all') { + var tempUrl = 'http://' + cache.appConfig.host + ':' + cache.appConfig.iguanaCorePort + '/api/InstantDEX/allcoins?userpass=' + sessionKey; + request({ + 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': { + 'method': 'cache-one', + '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-one', + 'status': 'in progress', + 'iguanaAPI': { + 'method': 'getaddressesbyaccount', + 'coin': coin, + 'status': 'in progress' + } + } + } + }); + + getAddresses(coin); + }); + } + } + if (error) { // stop further requests on failure, exit + callStack[coin] = 1; + checkCallStack(); + } + }); + } else { + getAddresses(coin); + } + } else { + callStack[coin] = callStack[coin] + (coin === 'BTC' ? callsArray.length : callsArray.length - 2); + console.log(coin + ' stack len ' + callStack[coin]); + + execDEXRequests(coin, address); + } } else { cache.io.emit('messages', { 'message': { diff --git a/routes/mock.js b/routes/mock.js index 45564b7..0a87045 100644 --- a/routes/mock.js +++ b/routes/mock.js @@ -28,12 +28,12 @@ mock.get = function(req, res, next) { } if (_url.indexOf('/api/dex/listunspent') > -1 || _url.indexOf('/api/dex/listtransactions') > -1 || - _url.indexOf('/api/dex/getbalance') > -1 || + _url.indexOf('/api/basilisk/getbalance') > -1 || _url.indexOf('/api/dex/refresh') > -1) { res.end(JSON.stringify({ 'some key': 'some value' })); - } + } } module.exports = mock; \ No newline at end of file