From cbf38b5ec7cb823fa82b574d95142975450e4ef2 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Tue, 21 Nov 2017 14:50:06 +0300 Subject: [PATCH] rm komodo/chain data --- routes/shepherd/daemonControl.js | 2 +- routes/shepherd/dashboardUpdate.js | 24 +++--- routes/shepherd/init.js | 77 +++++++++-------- routes/shepherd/kickstart.js | 133 +++-------------------------- routes/shepherd/quitDaemon.js | 18 ++-- version | 2 +- version_build | 2 +- 7 files changed, 82 insertions(+), 176 deletions(-) diff --git a/routes/shepherd/daemonControl.js b/routes/shepherd/daemonControl.js index 9d3c8fe..5228669 100644 --- a/routes/shepherd/daemonControl.js +++ b/routes/shepherd/daemonControl.js @@ -289,7 +289,7 @@ module.exports = (shepherd) => { try { // check if komodod instance is already running - shepherd.portscanner.checkPortStatus(_port, '127.0.0.1', (error, status) => { + portscanner.checkPortStatus(_port, '127.0.0.1', (error, status) => { // Status is 'open' if currently in use or 'closed' if available if (status === 'closed') { // start komodod via exec diff --git a/routes/shepherd/dashboardUpdate.js b/routes/shepherd/dashboardUpdate.js index 5361c8a..9ca48ff 100644 --- a/routes/shepherd/dashboardUpdate.js +++ b/routes/shepherd/dashboardUpdate.js @@ -1,3 +1,5 @@ +const Promise = require('bluebird'); + module.exports = (shepherd) => { /* * Combined native dashboard update same as in gui @@ -5,9 +7,9 @@ module.exports = (shepherd) => { * params: coin */ shepherd.post('/native/dashboard/update', (req, res, next) => { + const _coin = req.body.coin; let _returnObj; let _promiseStack; - const _coin = req.body.coin; if (_coin === 'CHIPS') { _returnObj = { @@ -49,8 +51,8 @@ module.exports = (shepherd) => { type.pop(); } - shepherd.Promise.all(type.map((_type, index) => { - return new shepherd.Promise((resolve, reject) => { + Promise.all(type.map((_type, index) => { + return new Promise((resolve, reject) => { _bitcoinRPC( coin, _type === 'public' ? 'getaddressesbyaccount' : 'z_listaddresses', @@ -70,7 +72,9 @@ module.exports = (shepherd) => { result[0].length) { const calcBalance = (result, json) => { if (json && - json.length) { + json.length && + json[0] && + json[0].address) { const allAddrArray = json.map(res => res.address).filter((x, i, a) => a.indexOf(x) == i); for (let a = 0; a < allAddrArray.length; a++) { @@ -146,8 +150,8 @@ module.exports = (shepherd) => { // get zaddr balance if (result[1] && result[1].length) { - shepherd.Promise.all(result[1].map((_address, index) => { - return new shepherd.Promise((resolve, reject) => { + Promise.all(result[1].map((_address, index) => { + return new Promise((resolve, reject) => { _bitcoinRPC(coin, 'z_getbalance', [_address]) .then((__json) => { __json = JSON.parse(__json); @@ -155,7 +159,7 @@ module.exports = (shepherd) => { __json.error) { resolve(0); } else { - resolve(__json.result) + resolve(__json.result); newAddressArray[1][index] = { address: _address, amount: __json.result, @@ -229,7 +233,7 @@ module.exports = (shepherd) => { } const _bitcoinRPC = (coin, cmd, params) => { - return new shepherd.Promise((resolve, reject) => { + return new Promise((resolve, reject) => { let _payload; if (params) { @@ -269,7 +273,7 @@ module.exports = (shepherd) => { }); } - shepherd.Promise.all(_promiseStack.map((_call, index) => { + Promise.all(_promiseStack.map((_call, index) => { let _params; if (_call === 'listtransactions') { @@ -280,7 +284,7 @@ module.exports = (shepherd) => { ]; } - return new shepherd.Promise((resolve, reject) => { + return new Promise((resolve, reject) => { _bitcoinRPC( _coin, _call, diff --git a/routes/shepherd/init.js b/routes/shepherd/init.js index 8ee7972..1746c40 100644 --- a/routes/shepherd/init.js +++ b/routes/shepherd/init.js @@ -1,5 +1,6 @@ const fs = require('fs-extra'); const path = require('path'); +let _foldersInitRan = false; module.exports = (shepherd) => { shepherd.readVersionFile = () => { @@ -11,54 +12,58 @@ module.exports = (shepherd) => { } shepherd.createAgamaDirs = () => { - const rootLocation = path.join(__dirname, '../../'); + if (!_foldersInitRan) { + const rootLocation = path.join(__dirname, '../../'); - fs.readdir(rootLocation, (err, items) => { - for (let i = 0; i < items.length; i++) { - if (items[i].substr(0, 3) === 'gen') { - console.log(items[i]); - fs.unlinkSync(rootLocation + items[i]); + fs.readdir(rootLocation, (err, items) => { + for (let i = 0; i < items.length; i++) { + if (items[i].substr(0, 3) === 'gen') { + console.log(items[i]); + fs.unlinkSync(rootLocation + items[i]); + } } - } - }); + }); - if (!fs.existsSync(shepherd.agamaDir)) { - fs.mkdirSync(shepherd.agamaDir); + if (!fs.existsSync(shepherd.agamaDir)) { + fs.mkdirSync(shepherd.agamaDir); - if (fs.existsSync(shepherd.agamaDir)) { - shepherd.log(`created agama folder at ${shepherd.agamaDir}`); - shepherd.writeLog(`created agama folder at ${shepherd.agamaDir}`); + if (fs.existsSync(shepherd.agamaDir)) { + shepherd.log(`created agama folder at ${shepherd.agamaDir}`); + shepherd.writeLog(`created agama folder at ${shepherd.agamaDir}`); + } + } else { + shepherd.log('agama folder already exists'); } - } else { - shepherd.log('agama folder already exists'); - } - if (!fs.existsSync(`${shepherd.agamaDir}/shepherd`)) { - fs.mkdirSync(`${shepherd.agamaDir}/shepherd`); + if (!fs.existsSync(`${shepherd.agamaDir}/shepherd`)) { + fs.mkdirSync(`${shepherd.agamaDir}/shepherd`); - if (fs.existsSync(`${shepherd.agamaDir}/shepherd`)) { - shepherd.log(`created shepherd folder at ${shepherd.agamaDir}/shepherd`); - shepherd.writeLog(`create shepherd folder at ${shepherd.agamaDir}/shepherd`); + if (fs.existsSync(`${shepherd.agamaDir}/shepherd`)) { + shepherd.log(`created shepherd folder at ${shepherd.agamaDir}/shepherd`); + shepherd.writeLog(`create shepherd folder at ${shepherd.agamaDir}/shepherd`); + } + } else { + shepherd.log('agama/shepherd folder already exists'); } - } else { - shepherd.log('agama/shepherd folder already exists'); - } - if (!fs.existsSync(`${shepherd.agamaDir}/shepherd/pin`)) { - fs.mkdirSync(`${shepherd.agamaDir}/shepherd/pin`); + if (!fs.existsSync(`${shepherd.agamaDir}/shepherd/pin`)) { + fs.mkdirSync(`${shepherd.agamaDir}/shepherd/pin`); - if (fs.existsSync(`${shepherd.agamaDir}/shepherd/pin`)) { - shepherd.log(`created pin folder at ${shepherd.agamaDir}/shepherd/pin`); - shepherd.writeLog(`create pin folder at ${shepherd.agamaDir}/shepherd/pin`); + if (fs.existsSync(`${shepherd.agamaDir}/shepherd/pin`)) { + shepherd.log(`created pin folder at ${shepherd.agamaDir}/shepherd/pin`); + shepherd.writeLog(`create pin folder at ${shepherd.agamaDir}/shepherd/pin`); + } + } else { + shepherd.log('shepherd/pin folder already exists'); + } + + if (!fs.existsSync(shepherd.zcashParamsDir)) { + fs.mkdirSync(shepherd.zcashParamsDir); + } else { + shepherd.log('zcashparams folder already exists'); } - } else { - shepherd.log('shepherd/pin folder already exists'); - } - if (!fs.existsSync(shepherd.zcashParamsDir)) { - fs.mkdirSync(shepherd.zcashParamsDir); - } else { - shepherd.log('zcashparams folder already exists'); + _foldersInitRan = true; } } diff --git a/routes/shepherd/kickstart.js b/routes/shepherd/kickstart.js index cbe7c09..99d26e5 100644 --- a/routes/shepherd/kickstart.js +++ b/routes/shepherd/kickstart.js @@ -1,12 +1,14 @@ +const fs = require('fs-extra'); +const path = require('path'); + module.exports = (shepherd) => { /* * type: GET * params: coin, type - * TODO: reorganize to work with coind */ shepherd.get('/kick', (req, res, next) => { const _coin = req.query.coin; - const _type = req.query.type; + const _keepWallet = req.query.keepwallet; if (!_coin) { const errorObj = { @@ -15,131 +17,24 @@ module.exports = (shepherd) => { }; res.end(JSON.stringify(errorObj)); - } - - if (!_type) { - const errorObj = { - msg: 'error', - result: 'no type provided', - }; - - res.end(JSON.stringify(errorObj)); - } - - const kickStartDirs = { - soft: [ - { - name: 'DB/[coin]', - type: 'pattern', - match: 'balancecrc.', - }, - { - name: 'DB/[coin]/utxoaddrs', - type: 'file', - }, - { - name: 'DB/[coin]/accounts', - type: 'folder', - }, - { - name: 'DB/[coin]/fastfind', - type: 'folder', - }, - { - name: 'tmp/[coin]', - type: 'folder', - } - ], - hard: [ - { - name: 'DB/[coin]', - type: 'pattern', - match: 'balancecrc.', - }, - { - name: 'DB/[coin]/utxoaddrs', - type: 'file', - }, - { - name: 'DB/[coin]', - type: 'pattern', - match: 'utxoaddrs.', - }, - { - name: 'DB/[coin]/accounts', - type: 'folder', - }, - { - name: 'DB/[coin]/fastfind', - type: 'folder', - }, - { - name: 'DB/[coin]/spends', - type: 'folder', - }, - { - name: 'tmp/[coin]', - type: 'folder', - } - ], - brutal: [ // delete all coin related data - { - name: 'DB/[coin]', - type: 'folder', - }, - { - name: 'DB/purgeable/[coin]', - type: 'folder', - }, - { - name: 'DB/ro/[coin]', - type: 'folder', - }, - { - name: 'tmp/[coin]', - type: 'folder', - } - ] - }; - - if (_coin && - _type) { - for (let i = 0; i < kickStartDirs[_type].length; i++) { - let currentKickItem = kickStartDirs[_type][i]; - - shepherd.log('deleting ' + currentKickItem.type + (currentKickItem.match ? ' ' + currentKickItem.match : '') + ' ' + iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin)); - if (currentKickItem.type === 'folder' || - currentKickItem.type === 'file') { - /*rimraf(`${iguanaDir}/${currentKickItem.name.replace('[coin]', _coin)}`, function(err) { - if (err) { - shepherd.writeLog(`kickstart err: ${err}`); - shepherd.log(`kickstart err: ${err}`); - } - });*/ - } else if (currentKickItem.type === 'pattern') { - let dirItems = shepherd.fs.readdirSync(`${iguanaDir}/currentKickItem.name.replace('[coin]', _coin)`); + } else { + const _location = path.join(_coin === 'KMD' ? shepherd.komodoDir : `${shepherd.komodoDir}/${_coin}`); - if (dirItems && - dirItems.length) { - for (let j = 0; j < dirItems.length; j++) { - if (dirItems[j].indexOf(currentKickItem.match) > -1) { - /*rimraf(`${iguanaDir}/${currentKickItem.name.replace('[coin]', _coin)}/${dirItems[j]}`, function(err) { - if (err) { - shepherd.writeLog(`kickstart err: ${err}`); - shepherd.log(`kickstart err: ${err}`); - } - });*/ + if (fs.existsSync(_location)) { + const items = fs.readdirSync(_location); - shepherd.log(`deleting ${dirItems[j]}`); - } - } + for (let i = 0; i < items.length; i++) { + if (items[i].indexOf('wallet.dat') === -1) { + fs.removeSync(`${_location}/${items[i]}`); + } else if (!_keepWallet) { + fs.removeSync(`${_location}/${items[i]}`); } } } const successObj = { msg: 'success', - result: 'kickstart: brutal is executed', + result: `${_coin} native is kicked`, }; res.end(JSON.stringify(successObj)); diff --git a/routes/shepherd/quitDaemon.js b/routes/shepherd/quitDaemon.js index d60a4a7..a1e0bec 100644 --- a/routes/shepherd/quitDaemon.js +++ b/routes/shepherd/quitDaemon.js @@ -1,3 +1,6 @@ +const portscanner = require('portscanner'); +const execFile = require('child_process').execFile; + module.exports = (shepherd) => { shepherd.quitKomodod = (timeout = 100) => { // if komodod is under heavy load it may not respond to cli stop the first time @@ -32,7 +35,7 @@ module.exports = (shepherd) => { } _arg.push('stop'); - shepherd.execFile(`${_coindQuitCmd}`, _arg, (error, stdout, stderr) => { + execFile(`${_coindQuitCmd}`, _arg, (error, stdout, stderr) => { shepherd.log(`stdout: ${stdout}`); shepherd.log(`stderr: ${stderr}`); @@ -48,7 +51,7 @@ module.exports = (shepherd) => { // workaround for AGT-65 const _port = shepherd.assetChainPorts[key]; setTimeout(() => { - shepherd.portscanner.checkPortStatus(_port, '127.0.0.1', (error, status) => { + portscanner.checkPortStatus(_port, '127.0.0.1', (error, status) => { // Status is 'open' if currently in use or 'closed' if available if (status === 'closed') { delete shepherd.coindInstanceRegistry[key]; @@ -92,12 +95,11 @@ module.exports = (shepherd) => { _arg.push(`-datadir=${shepherd.appConfig.dataDir}`); } - console.log(JSON.stringify(shepherd.coindInstanceRegistry, null, '\t')); - _arg.push('stop'); - shepherd.execFile(`${_coindQuitCmd}`, _arg, (error, stdout, stderr) => { + execFile(`${_coindQuitCmd}`, _arg, (error, stdout, stderr) => { shepherd.log(`stdout: ${stdout}`); shepherd.log(`stderr: ${stderr}`); + shepherd.log(`send stop sig to ${_chain}`); if (stdout.indexOf('EOF reached') > -1 || stderr.indexOf('EOF reached') > -1 || @@ -134,9 +136,9 @@ module.exports = (shepherd) => { }); shepherd.post('/coins/remove', (req, res) => { - if (req.body.mode === 'native') { - const _chain = req.body.chain; + const _chain = req.body.chain; + if (req.body.mode === 'native') { delete shepherd.coindInstanceRegistry[_chain ? _chain : 'komodod']; const obj = { @@ -146,7 +148,7 @@ module.exports = (shepherd) => { res.end(JSON.stringify(obj)); } else { - delete shepherd.electrumCoins[req.body.chain === 'komodo' ? 'KMD' : req.body.chain]; + delete shepherd.electrumCoins[_chain === 'komodo' ? 'KMD' : _chain]; if (Object.keys(shepherd.electrumCoins).length - 1 === 0) { shepherd.electrumCoins.auth = false; diff --git a/version b/version index 6f7de9f..972de55 100644 --- a/version +++ b/version @@ -1,3 +1,3 @@ -version=0.2.0.24c +version=0.2.0.24d type=e-beta minversion=0.2.0.2 \ No newline at end of file diff --git a/version_build b/version_build index 6c79e0e..df967fa 100644 --- a/version_build +++ b/version_build @@ -1 +1 @@ -0.2.0.24c-beta \ No newline at end of file +0.2.0.24d-beta \ No newline at end of file