From 24acfcbf6a8c2959fb952a31fc99bf138f7e3ca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Sun, 29 Aug 2021 19:51:05 +0100 Subject: [PATCH 1/3] feat: update btcd after installing --- lib/blockchain/bitcoin.js | 19 ++++++++++++++++++- lib/blockchain/common.js | 28 +++++++++++++++++++++++----- lib/blockchain/install.js | 8 ++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/blockchain/bitcoin.js b/lib/blockchain/bitcoin.js index 88e38988..c8a3d8b8 100644 --- a/lib/blockchain/bitcoin.js +++ b/lib/blockchain/bitcoin.js @@ -4,7 +4,7 @@ const { utils: coinUtils } = require('lamassu-coins') const common = require('./common') -module.exports = {setup} +module.exports = { setup, updateCore } const coinRec = coinUtils.getCryptoCurrency('BTC') @@ -16,6 +16,23 @@ function setup (dataDir) { common.writeSupervisorConfig(coinRec, cmd) } +function updateCore (coinRec) { + common.logger.info('Updating Bitcoin Core. This may take a minute.') + common.es(`supervisorctl stop bitcoin`) + common.es(`curl -#o /tmp/bitcoin.tar.gz ${coinRec.url}`) + common.es(`tar -xzf /tmp/bitcoin.tar.gz -C /tmp/`) + + common.logger.info('Updating wallet...') + common.es(`cp /tmp/${coinRec.dir}/* /usr/local/bin/`) + common.es(`rm -r /tmp/${coinRec.dir.replace('/bin', '')}`) + common.es(`rm /tmp/bitcoin.tar.gz`) + + common.logger.info('Starting wallet...') + common.es(`supervisorctl start bitcoin`) + + common.logger.info('Bitcoin Core is updated!') +} + function buildConfig () { return `rpcuser=lamassuserver rpcpassword=${common.randomPass()} diff --git a/lib/blockchain/common.js b/lib/blockchain/common.js index 2c0bdc3c..53cc6262 100644 --- a/lib/blockchain/common.js +++ b/lib/blockchain/common.js @@ -3,6 +3,7 @@ const os = require('os') const path = require('path') const cp = require('child_process') const fs = require('fs') +const btc = require('./bitcoin') const _ = require('lodash/fp') @@ -16,13 +17,17 @@ module.exports = { fetchAndInstall, logger, isInstalledSoftware, - writeFile + writeFile, + getBinaries, + isUpdateDependent } const BINARIES = { BTC: { - url: 'https://bitcoincore.org/bin/bitcoin-core-0.20.1/bitcoin-0.20.1-x86_64-linux-gnu.tar.gz', - dir: 'bitcoin-0.20.1/bin' + defaultUrl: 'https://bitcoincore.org/bin/bitcoin-core-0.20.0/bitcoin-0.20.0-x86_64-linux-gnu.tar.gz', + defaultDir: 'bitcoin-0.20.0/bin', + url: 'https://bitcoincore.org/bin/bitcoin-core-0.21.0/bitcoin-0.21.0-x86_64-linux-gnu.tar.gz', + dir: 'bitcoin-0.21.0/bin' }, ETH: { url: 'https://gethstore.blob.core.windows.net/builds/geth-linux-amd64-1.9.25-e7872729.tar.gz', @@ -47,6 +52,8 @@ const BINARIES = { } } +const coinsUpdateDependent = ['BTC'] + function firewall (ports) { if (!ports || ports.length === 0) throw new Error('No ports supplied') const portsString = ports.join(',') @@ -87,14 +94,15 @@ function isInstalledSoftware (coinRec) { } function fetchAndInstall (coinRec) { + const requiresUpdate = isUpdateDependent(coinRec.code) if (isInstalledSoftware(coinRec)) return const binaries = BINARIES[coinRec.cryptoCode] if (!binaries) throw new Error(`No such coin: ${coinRec.code}`) - const url = binaries.url + const url = requiresUpdate ? binaries.defaultUrl : binaries.url const downloadFile = path.basename(url) - const binDir = binaries.dir + const binDir = requiresUpdate ? binaries.defaultDir : binaries.dir es(`wget -q ${url}`) es(`tar -xzf ${downloadFile}`) @@ -121,3 +129,13 @@ function writeFile (path, content) { throw err } } + +function getBinaries (coinCode) { + const binaries = BINARIES[coinCode] + if (!binaries) throw new Error(`No such coin: ${coinCode}`) + return binaries +} + +function isUpdateDependent (coinCode) { + return _.includes(coinCode, coinsUpdateDependent) +} diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 8a71a826..3a8df46b 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -69,6 +69,8 @@ function processCryptos (codes) { ) | crontab -` common.es(rsyncCmd) + _.forEach(updateCrypto, selectedCryptos) + logger.info('Installation complete.') } @@ -91,6 +93,12 @@ function setupCrypto (crypto) { process.chdir(oldDir) } +function updateCrypto (crypto) { + if (!common.isUpdateDependent(crypto)) return + const cryptoPlugin = plugin(crypto) + cryptoPlugin.updateCore(common.getBinaries(crypto)) +} + function plugin (crypto) { const plugin = PLUGINS[crypto.cryptoCode] if (!plugin) throw new Error(`No such plugin: ${crypto.cryptoCode}`) From db905251963a24f4d0acbb16d6ce184bc51c3ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Sun, 29 Aug 2021 19:55:13 +0100 Subject: [PATCH 2/3] fix: function argument --- lib/blockchain/install.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 3a8df46b..0acdf481 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -94,7 +94,7 @@ function setupCrypto (crypto) { } function updateCrypto (crypto) { - if (!common.isUpdateDependent(crypto)) return + if (!common.isUpdateDependent(crypto.code)) return const cryptoPlugin = plugin(crypto) cryptoPlugin.updateCore(common.getBinaries(crypto)) } From 01275a8c9b3f1bbbeb220fd2ec05fd9b90adf52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Oliveira?= Date: Mon, 30 Aug 2021 23:13:59 +0100 Subject: [PATCH 3/3] fix: access crypto code properly and add admin permissions for supervisor cmds --- lib/blockchain/bitcoin.js | 4 ++-- lib/blockchain/common.js | 3 +-- lib/blockchain/install.js | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/blockchain/bitcoin.js b/lib/blockchain/bitcoin.js index c8a3d8b8..b0d14514 100644 --- a/lib/blockchain/bitcoin.js +++ b/lib/blockchain/bitcoin.js @@ -18,7 +18,7 @@ function setup (dataDir) { function updateCore (coinRec) { common.logger.info('Updating Bitcoin Core. This may take a minute.') - common.es(`supervisorctl stop bitcoin`) + common.es(`sudo supervisorctl stop bitcoin`) common.es(`curl -#o /tmp/bitcoin.tar.gz ${coinRec.url}`) common.es(`tar -xzf /tmp/bitcoin.tar.gz -C /tmp/`) @@ -28,7 +28,7 @@ function updateCore (coinRec) { common.es(`rm /tmp/bitcoin.tar.gz`) common.logger.info('Starting wallet...') - common.es(`supervisorctl start bitcoin`) + common.es(`sudo supervisorctl start bitcoin`) common.logger.info('Bitcoin Core is updated!') } diff --git a/lib/blockchain/common.js b/lib/blockchain/common.js index 53cc6262..d9ea483e 100644 --- a/lib/blockchain/common.js +++ b/lib/blockchain/common.js @@ -3,7 +3,6 @@ const os = require('os') const path = require('path') const cp = require('child_process') const fs = require('fs') -const btc = require('./bitcoin') const _ = require('lodash/fp') @@ -94,7 +93,7 @@ function isInstalledSoftware (coinRec) { } function fetchAndInstall (coinRec) { - const requiresUpdate = isUpdateDependent(coinRec.code) + const requiresUpdate = isUpdateDependent(coinRec.cryptoCode) if (isInstalledSoftware(coinRec)) return const binaries = BINARIES[coinRec.cryptoCode] diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index 0acdf481..d7346ac9 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -94,9 +94,9 @@ function setupCrypto (crypto) { } function updateCrypto (crypto) { - if (!common.isUpdateDependent(crypto.code)) return + if (!common.isUpdateDependent(crypto.cryptoCode)) return const cryptoPlugin = plugin(crypto) - cryptoPlugin.updateCore(common.getBinaries(crypto)) + cryptoPlugin.updateCore(common.getBinaries(crypto.cryptoCode)) } function plugin (crypto) {