Browse Source

Merge pull request #185 from SuperNETorg/master

update
pkg_automation_electrum
pbca26 7 years ago
committed by GitHub
parent
commit
bce767fd08
  1. 42
      main.js
  2. 60
      routes/cache.js
  3. 2
      routes/ports.js
  4. 361
      routes/shepherd.js

42
main.js

@ -170,23 +170,23 @@ function createLoadingWindow() {
// Status is 'open' if currently in use or 'closed' if available // Status is 'open' if currently in use or 'closed' if available
if (status === 'closed') { if (status === 'closed') {
server.listen(appConfig.agamaPort, function() { server.listen(appConfig.agamaPort, function() {
console.log(`guiapp and sockets.io are listening on port ${appConfig.agamaPort}`); shepherd.log(`guiapp and sockets.io are listening on port ${appConfig.agamaPort}`);
shepherd.writeLog(`guiapp and sockets.io are listening on port ${appConfig.agamaPort}`); shepherd.writeLog(`guiapp and sockets.io are listening on port ${appConfig.agamaPort}`);
// start sockets.io // start sockets.io
io.set('origins', appConfig.dev ? 'http://127.0.0.1:3000' : `http://127.0.0.1:${appConfig.agamaPort}`); // set origin io.set('origins', appConfig.dev ? 'http://127.0.0.1:3000' : `http://127.0.0.1:${appConfig.agamaPort}`); // set origin
io.on('connection', function(client) { io.on('connection', function(client) {
console.log('EDEX GUI is connected...'); shepherd.log('EDEX GUI is connected...');
shepherd.writeLog('EDEX GUI is connected...'); shepherd.writeLog('EDEX GUI is connected...');
client.on('event', function(data) { // listen for client requests client.on('event', function(data) { // listen for client requests
console.log(data); shepherd.log(data);
}); });
client.on('disconnect', function(data) { client.on('disconnect', function(data) {
console.log('EDEX GUI is disconnected'); shepherd.log('EDEX GUI is disconnected');
}); });
client.on('join', function(data) { client.on('join', function(data) {
console.log(data); shepherd.log(data);
client.emit('messages', 'Sockets server is listening'); client.emit('messages', 'Sockets server is listening');
}); });
}); });
@ -194,11 +194,11 @@ function createLoadingWindow() {
} else { } else {
willQuitApp = true; willQuitApp = true;
server.listen(appConfig.agamaPort + 1, function() { server.listen(appConfig.agamaPort + 1, function() {
console.log(`guiapp and sockets.io are listening on port ${appConfig.agamaPort + 1}`); shepherd.log(`guiapp and sockets.io are listening on port ${appConfig.agamaPort + 1}`);
shepherd.writeLog(`guiapp and sockets.io are listening on port ${appConfig.agamaPort + 1}`); shepherd.writeLog(`guiapp and sockets.io are listening on port ${appConfig.agamaPort + 1}`);
}); });
loadingWindow.loadURL(`http://${appConfig.host}:${appConfig.agamaPort + 1}/gui/startup/agama-instance-error.html`); loadingWindow.loadURL(`http://${appConfig.host}:${appConfig.agamaPort + 1}/gui/startup/agama-instance-error.html`);
console.log('another agama app is already running'); shepherd.log('another agama app is already running');
} }
}); });
@ -407,24 +407,24 @@ function createWindow(status) {
function pm2Exit() { function pm2Exit() {
const ConnectToPm2 = function() { const ConnectToPm2 = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
console.log('Closing Main Window...'); shepherd.log('Closing Main Window...');
shepherd.writeLog('exiting app...'); shepherd.writeLog('exiting app...');
shepherd.dumpCacheBeforeExit(); shepherd.dumpCacheBeforeExit();
shepherd.quitKomodod(1000); shepherd.quitKomodod(1000);
pm2.connect(true, function(err) { pm2.connect(true, function(err) {
console.log('connecting to pm2...'); shepherd.log('connecting to pm2...');
shepherd.writeLog('connecting to pm2...'); shepherd.writeLog('connecting to pm2...');
if (err) { if (err) {
console.log(err); shepherd.log(err);
} }
}); });
const result = 'Connecting To Pm2: done'; const result = 'Connecting To Pm2: done';
console.log(result); shepherd.log(result);
shepherd.writeLog(result); shepherd.writeLog(result);
resolve(result); resolve(result);
}) })
@ -432,12 +432,12 @@ function createWindow(status) {
const KillPm2 = function() { const KillPm2 = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
console.log('killing to pm2...'); shepherd.log('killing to pm2...');
shepherd.writeLog('killing to pm2...'); shepherd.writeLog('killing to pm2...');
pm2.killDaemon(function(err) { pm2.killDaemon(function(err) {
pm2.disconnect(); pm2.disconnect();
console.log('killed to pm2...'); shepherd.log('killed to pm2...');
shepherd.writeLog('killed to pm2...'); shepherd.writeLog('killed to pm2...');
if (err) if (err)
@ -447,7 +447,7 @@ function createWindow(status) {
const result = 'Killing Pm2: done'; const result = 'Killing Pm2: done';
setTimeout(function() { setTimeout(function() {
console.log(result); shepherd.log(result);
shepherd.writeLog(result); shepherd.writeLog(result);
resolve(result); resolve(result);
@ -459,9 +459,9 @@ function createWindow(status) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
const result = 'Hiding Main Window: done'; const result = 'Hiding Main Window: done';
console.log('Exiting App...'); shepherd.log('Exiting App...');
mainWindow = null; mainWindow = null;
console.log(result); shepherd.log(result);
resolve(result); resolve(result);
}); });
} }
@ -479,7 +479,7 @@ function createWindow(status) {
KillPm2(); // required for normal app quit in iguana-less mode KillPm2(); // required for normal app quit in iguana-less mode
app.quit(); app.quit();
console.log(result); shepherd.log(result);
resolve(result); resolve(result);
}); });
} }
@ -529,11 +529,11 @@ app.on('window-all-closed', function() {
// Emitted before the application starts closing its windows. // Emitted before the application starts closing its windows.
// Calling event.preventDefault() will prevent the default behaviour, which is terminating the application. // Calling event.preventDefault() will prevent the default behaviour, which is terminating the application.
app.on('before-quit', function(event) { app.on('before-quit', function(event) {
console.log('before-quit'); shepherd.log('before-quit');
shepherd.killRogueProcess('iguana'); // kill any rogue iguana core instances shepherd.killRogueProcess('iguana'); // kill any rogue iguana core instances
if (!forceQuitApp && mainWindow === null && loadingWindow != null) { // mainWindow not intitialised and loadingWindow not dereferenced if (!forceQuitApp && mainWindow === null && loadingWindow != null) { // mainWindow not intitialised and loadingWindow not dereferenced
// loading window is still open // loading window is still open
console.log('before-quit prevented'); shepherd.log('before-quit prevented');
shepherd.writeLog('quit app after loading is done'); shepherd.writeLog('quit app after loading is done');
closeAppAfterLoading = true; closeAppAfterLoading = true;
let code = `$('#loading_status_text').html('Preparing to shutdown the wallet.<br/>Please wait while all daemons are closed...')`; let code = `$('#loading_status_text').html('Preparing to shutdown the wallet.<br/>Please wait while all daemons are closed...')`;
@ -547,7 +547,7 @@ app.on('before-quit', function(event) {
app.on('will-quit', function(event) { app.on('will-quit', function(event) {
if (!forceQuitApp && mainWindow === null && loadingWindow != null) { if (!forceQuitApp && mainWindow === null && loadingWindow != null) {
// loading window is still open // loading window is still open
console.log('will-quit while loading window active'); shepherd.log('will-quit while loading window active');
event.preventDefault(); event.preventDefault();
} }
}); });
@ -556,7 +556,7 @@ app.on('will-quit', function(event) {
// Calling event.preventDefault() will prevent the default behaviour, which is terminating the application. // Calling event.preventDefault() will prevent the default behaviour, which is terminating the application.
app.on('quit', function(event) { app.on('quit', function(event) {
if (!forceQuitApp && mainWindow === null && loadingWindow != null) { if (!forceQuitApp && mainWindow === null && loadingWindow != null) {
console.log('quit while loading window active'); shepherd.log('quit while loading window active');
event.preventDefault(); event.preventDefault();
} }
}) })

60
routes/cache.js

@ -15,7 +15,7 @@ cache.setVar = function(variable, value) {
*/ */
cache.dumpCacheBeforeExit = function() { cache.dumpCacheBeforeExit = function() {
if (inMemCache) { if (inMemCache) {
console.log('dumping cache before exit'); shepherd.log('dumping cache before exit');
fs.writeFileSync(`${cache.iguanaDir}/shepherd/cache-${inMemPubkey}.json`, JSON.stringify(inMemCache), 'utf8'); fs.writeFileSync(`${cache.iguanaDir}/shepherd/cache-${inMemPubkey}.json`, JSON.stringify(inMemCache), 'utf8');
} }
} }
@ -27,7 +27,7 @@ cache.get = function(req, res, next) {
inMemPubkey = pubkey; inMemPubkey = pubkey;
if (!inMemCache) { if (!inMemCache) {
console.log('serving cache from disk'); shepherd.log('serving cache from disk');
if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`)) { if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`)) {
fs.readFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8', function(err, data) { fs.readFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8', function(err, data) {
@ -49,13 +49,13 @@ cache.get = function(req, res, next) {
inMemCache = parsedJSON; inMemCache = parsedJSON;
res.end(JSON.stringify(successObj)); res.end(JSON.stringify(successObj));
} catch (e) { } catch (e) {
console.log('JSON parse error while reading cache data from disk:'); shepherd.log('JSON parse error while reading cache data from disk:');
console.log(e); shepherd.log(e);
if (e.toString().indexOf('at position') > -1) { if (e.toString().indexOf('at position') > -1) {
const errorPos = e.toString().split(' '); const errorPos = e.toString().split(' ');
console.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`); shepherd.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`);
console.log('attempting to recover JSON data'); shepherd.log('attempting to recover JSON data');
fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, data.substring(0, errorPos[errorPos.length - 1]), function(err) { fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, data.substring(0, errorPos[errorPos.length - 1]), function(err) {
const successObj = { const successObj = {
@ -195,8 +195,8 @@ cache.groomPost = function(req, res) {
res.end(JSON.stringify(errorObj)); res.end(JSON.stringify(errorObj));
} else { } else {
inMemCache = JSON.parse(_payload); inMemCache = JSON.parse(_payload);
console.log('appending groom post to in mem cache'); shepherd.log('appending groom post to in mem cache');
console.log('appending groom post to on disk cache'); shepherd.log('appending groom post to on disk cache');
fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${_filename}.json`, _payload, function(err) { fs.writeFile(`${cache.iguanaDir}/shepherd/cache-${_filename}.json`, _payload, function(err) {
if (err) { if (err) {
@ -326,8 +326,8 @@ cache.one = function(req, res, next) {
inMemPubkey = pubkey; inMemPubkey = pubkey;
callStack[coin] = 1; callStack[coin] = 1;
console.log(callsArray); shepherd.log(callsArray);
console.log(`iguana core port ${iguanaCorePort}`); shepherd.log(`iguana core port ${iguanaCorePort}`);
if (!sessionKey) { if (!sessionKey) {
const errorObj = { const errorObj = {
@ -349,7 +349,7 @@ cache.one = function(req, res, next) {
internalError = true; internalError = true;
} }
console.log('cache-one call started'); shepherd.log('cache-one call started');
function fixJSON(data) { function fixJSON(data) {
if (data && if (data &&
@ -359,12 +359,12 @@ cache.one = function(req, res, next) {
return parsedJSON; return parsedJSON;
} catch (e) { } catch (e) {
console.log(e); shepherd.log(e);
if (e.toString().indexOf('at position') > -1) { if (e.toString().indexOf('at position') > -1) {
const errorPos = e.toString().split(' '); const errorPos = e.toString().split(' ');
console.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`); shepherd.log(`JSON error ---> ${data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20)} | error sequence: ${data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1)}`);
console.log('attempting to recover JSON data'); shepherd.log('attempting to recover JSON data');
return JSON.parse(data.substring(0, errorPos[errorPos.length - 1])); return JSON.parse(data.substring(0, errorPos[errorPos.length - 1]));
} }
if (e.toString().indexOf('Unexpected end of JSON input')) { if (e.toString().indexOf('Unexpected end of JSON input')) {
@ -379,23 +379,23 @@ cache.one = function(req, res, next) {
if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`) && if (fs.existsSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`) &&
coin !== 'all') { coin !== 'all') {
if (inMemCache) { if (inMemCache) {
console.log('cache one from mem'); shepherd.log('cache one from mem');
outObj = inMemCache; outObj = inMemCache;
} else { } else {
const _file = fs.readFileSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8'); const _file = fs.readFileSync(`${cache.iguanaDir}/shepherd/cache-${pubkey}.json`, 'utf8');
console.log('cache one from disk'); shepherd.log('cache one from disk');
outObj = fixJSON(_file); outObj = fixJSON(_file);
} }
if (!outObj || if (!outObj ||
!outObj.basilisk) { !outObj.basilisk) {
console.log('no local basilisk info'); shepherd.log('no local basilisk info');
outObj['basilisk'] = {}; outObj['basilisk'] = {};
outObj['basilisk'][coin] = {}; outObj['basilisk'][coin] = {};
} else { } else {
if (!outObj['basilisk'][coin]) { if (!outObj['basilisk'][coin]) {
console.log('no local coin info'); shepherd.log('no local coin info');
outObj['basilisk'][coin] = {}; outObj['basilisk'][coin] = {};
} }
} }
@ -438,7 +438,7 @@ cache.one = function(req, res, next) {
delete _dexUrls.getbalance; delete _dexUrls.getbalance;
} }
console.log(`${coin} address ${address}`); shepherd.log(`${coin} address ${address}`);
if (!outObj.basilisk[coin][address]) { if (!outObj.basilisk[coin][address]) {
outObj.basilisk[coin][address] = {}; outObj.basilisk[coin][address] = {};
@ -524,7 +524,7 @@ cache.one = function(req, res, next) {
if (key === 'getbalance' && if (key === 'getbalance' &&
coin === 'KMD'/* && coin === 'KMD'/* &&
((_parsedJSON && _parsedJSON.balance === 0) || body === [])*/) { ((_parsedJSON && _parsedJSON.balance === 0) || body === [])*/) {
console.log('fallback to kmd explorer ======>'); shepherd.log('fallback to kmd explorer ======>');
request({ request({
url: `http://kmd.explorer.supernet.org/api/addr/${address}/?noTxList=1`, url: `http://kmd.explorer.supernet.org/api/addr/${address}/?noTxList=1`,
method: 'GET' method: 'GET'
@ -558,10 +558,10 @@ cache.one = function(req, res, next) {
outObj.basilisk[coin][address][key].data = JSON.parse(body); outObj.basilisk[coin][address][key].data = JSON.parse(body);
outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp outObj.basilisk[coin][address][key].timestamp = Date.now(); // add timestamp
outObj.basilisk[coin][address][key].status = 'done'; outObj.basilisk[coin][address][key].status = 'done';
console.log(dexUrl); shepherd.log(dexUrl);
console.log(body); shepherd.log(body);
callStack[coin]--; callStack[coin]--;
console.log(`${coin} _stack len ${callStack[coin]}`); shepherd.log(`${coin} _stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -585,7 +585,7 @@ cache.one = function(req, res, next) {
outObj.basilisk[coin][address][key].timestamp = 1471620867 // add timestamp outObj.basilisk[coin][address][key].timestamp = 1471620867 // add timestamp
outObj.basilisk[coin][address][key].status = 'done'; outObj.basilisk[coin][address][key].status = 'done';
callStack[coin]--; callStack[coin]--;
console.log(`${coin} _stack len ${callStack[coin]}`); shepherd.log(`${coin} _stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -602,9 +602,9 @@ cache.one = function(req, res, next) {
} }
}); });
} else { } else {
console.log(`${key} is fresh, check back in ${(cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp))}s`); shepherd.log(`${key} is fresh, check back in ${(cacheGlobLifetime - checkTimestamp(outObj.basilisk[coin][address][key].timestamp))}s`);
callStack[coin]--; callStack[coin]--;
console.log(`${coin} _stack len ${callStack[coin]}`); shepherd.log(`${coin} _stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -637,7 +637,7 @@ cache.one = function(req, res, next) {
}, },
}); });
outObj.basilisk[coin].addresses = addrArray; outObj.basilisk[coin].addresses = addrArray;
console.log(addrArray); shepherd.log(addrArray);
writeCache(); writeCache();
const addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0; const addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0;
@ -650,7 +650,7 @@ cache.one = function(req, res, next) {
callsArrayBTC--; callsArrayBTC--;
} }
callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length); callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length);
console.log(`${coin} stack len ${callStack[coin]}`); shepherd.log(`${coin} stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
@ -713,7 +713,7 @@ cache.one = function(req, res, next) {
if (response && if (response &&
response.statusCode && response.statusCode &&
response.statusCode === 200) { response.statusCode === 200) {
console.log(JSON.parse(body).basilisk); shepherd.log(JSON.parse(body).basilisk);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {
shepherd: { shepherd: {
@ -777,7 +777,7 @@ cache.one = function(req, res, next) {
} }
callStack[coin] = callStack[coin] + (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length); callStack[coin] = callStack[coin] + (coin === 'BTC' || coin === 'SYS' ? callsArrayBTC : callsArray.length);
console.log(`${coin} stack len ${callStack[coin]}`); shepherd.log(`${coin} stack len ${callStack[coin]}`);
cache.io.emit('messages', { cache.io.emit('messages', {
message: { message: {

2
routes/ports.js

@ -8,7 +8,7 @@ const assetChainPorts = {
'JUMBLR': '15106', 'JUMBLR': '15106',
'BET': '14250', 'BET': '14250',
'CRYPTO': '8516', 'CRYPTO': '8516',
'HODL': '8010', 'HODL': '14431',
'SHARK': '10114', 'SHARK': '10114',
'BOTS': '11964', 'BOTS': '11964',
'MGW': '12386', 'MGW': '12386',

361
routes/shepherd.js

File diff suppressed because it is too large
Loading…
Cancel
Save