mirror of
https://github.com/Retropex/umbrel-bitcoin.git
synced 2025-05-12 19:20:49 +02:00
check bitcoind config on app start
This commit is contained in:
parent
6e682b5b42
commit
aae3d137bd
54
bin/www
54
bin/www
@ -8,11 +8,28 @@ var app = require('../app');
|
||||
var debug = require('debug')('nodejs-regular-webapp2:server');
|
||||
var http = require('http');
|
||||
|
||||
const diskLogic = require('../logic/disk');
|
||||
const diskService = require('../services/disk');
|
||||
const configLogic = require('../logic/config');
|
||||
const bitcoindLogic = require('../logic/bitcoind');
|
||||
const constants = require('../utils/const');
|
||||
|
||||
/**
|
||||
* Update umbrel-bitcoin.conf if it is out-of-date (e.g., due to new config options introduced by an app update).
|
||||
*/
|
||||
|
||||
(async () => {
|
||||
const config = await configLogic.getJsonStore();
|
||||
|
||||
if(! await configLogic.isUmbrelBitcoinConfUpToDate(config)) {
|
||||
console.log('umbrel-bitcoin.conf is not up to date, generating new config...');
|
||||
|
||||
// set torProxyForClearnet to false for pre-advanced-settings installs that are just now getting advanced settings
|
||||
if (constants.BITCOIN_INITIALIZE_WITH_CLEARNET_OVER_TOR) config.torProxyForClearnet = true;
|
||||
|
||||
await configLogic.applyCustomBitcoinConfig(config);
|
||||
await bitcoindLogic.restartBitcoindWithRetries();
|
||||
}
|
||||
})();
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
@ -34,34 +51,6 @@ server.listen(port);
|
||||
server.on('error', onError);
|
||||
server.on('listening', onListening);
|
||||
|
||||
/**
|
||||
* Function to create default bitcoin core config files if they do not already exist.
|
||||
*/
|
||||
|
||||
async function createConfFilesAndRestartBitcoind() {
|
||||
console.log('umbrel-bitcoin.conf does not exist, creating config files with Umbrel default values');
|
||||
const config = await diskLogic.getJsonStore();
|
||||
|
||||
// set torProxyForClearnet to false for existing installs
|
||||
if (constants.BITCOIN_INITIALIZE_WITH_CLEARNET_OVER_TOR) config.torProxyForClearnet = true;
|
||||
|
||||
await diskLogic.applyCustomBitcoinConfig(config);
|
||||
|
||||
const MAX_TRIES = 60;
|
||||
let tries = 0;
|
||||
|
||||
while (tries < MAX_TRIES) {
|
||||
try {
|
||||
await bitcoindLogic.stop();
|
||||
break;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
tries++;
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a port into a number, string, or false.
|
||||
*/
|
||||
@ -121,9 +110,4 @@ async function onListening() {
|
||||
: 'port ' + addr.port;
|
||||
debug('Listening on ' + bind);
|
||||
console.log('Listening on ' + bind);
|
||||
|
||||
// if umbrel-bitcoin.conf does not exist, create default bitcoin core config files and restart bitcoind.
|
||||
if (! await diskService.fileExists(constants.UMBREL_BITCOIN_CONF_FILEPATH)) {
|
||||
createConfFilesAndRestartBitcoind();
|
||||
}
|
||||
}
|
||||
|
@ -283,6 +283,22 @@ async function stop() {
|
||||
return {stopResponse};
|
||||
}
|
||||
|
||||
async function restartBitcoindWithRetries(MAX_TRIES = 60) {
|
||||
let tries = 0;
|
||||
|
||||
while (tries < MAX_TRIES) {
|
||||
try {
|
||||
await stop();
|
||||
break;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
console.log(`Failed to stop bitcoind. Retrying in 1 second. (Try ${tries + 1} of ${MAX_TRIES})`);
|
||||
tries++;
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getBlockHash,
|
||||
getTransaction,
|
||||
@ -298,5 +314,6 @@ module.exports = {
|
||||
getVersion,
|
||||
nodeStatusDump,
|
||||
nodeStatusSummary,
|
||||
stop
|
||||
stop,
|
||||
restartBitcoindWithRetries
|
||||
};
|
||||
|
@ -4,7 +4,6 @@ const path = require("path");
|
||||
const constants = require("utils/const.js");
|
||||
const diskService = require("services/disk");
|
||||
|
||||
// TODO - consider moving these unit conversions to utils/const.js
|
||||
const GB_TO_MiB = 953.674;
|
||||
const MB_TO_MiB = 0.953674;
|
||||
|
||||
@ -185,8 +184,21 @@ function settingsToMultilineConfString(settings) {
|
||||
return umbrelBitcoinConfig.join('\n');
|
||||
}
|
||||
|
||||
// checks to see if umbrel-bitcoin.conf is up to date, which we use to determine if we need to regenerate the config and restart bitcoind
|
||||
async function isUmbrelBitcoinConfUpToDate(config) {
|
||||
const newUmbrelBitcoinConf = await settingsToMultilineConfString(config);
|
||||
|
||||
let existingUmbrelBitcoinConf = await diskService.fileExists(constants.UMBREL_BITCOIN_CONF_FILEPATH)
|
||||
? await diskService.readUtf8File(constants.UMBREL_BITCOIN_CONF_FILEPATH)
|
||||
: '';
|
||||
|
||||
// compare new config to existing umbrel-bitcoin.conf
|
||||
return newUmbrelBitcoinConf === existingUmbrelBitcoinConf;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getJsonStore,
|
||||
applyCustomBitcoinConfig,
|
||||
applyDefaultBitcoinConfig,
|
||||
isUmbrelBitcoinConfUpToDate
|
||||
};
|
@ -2,7 +2,7 @@ const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const systemLogic = require('logic/system.js');
|
||||
const diskLogic = require('logic/disk');
|
||||
const configLogic = require('logic/config');
|
||||
const bitcoindLogic = require('logic/bitcoind.js');
|
||||
const safeHandler = require('utils/safeHandler');
|
||||
|
||||
@ -19,7 +19,7 @@ router.get('/bitcoin-rpc-connection-details', safeHandler(async(req, res) => {
|
||||
}));
|
||||
|
||||
router.get('/bitcoin-config', safeHandler(async(req, res) => {
|
||||
const bitcoinConfig = await diskLogic.getJsonStore();
|
||||
const bitcoinConfig = await configLogic.getJsonStore();
|
||||
return res.json(bitcoinConfig);
|
||||
}));
|
||||
|
||||
@ -27,18 +27,18 @@ router.get('/bitcoin-config', safeHandler(async(req, res) => {
|
||||
|
||||
router.post('/update-bitcoin-config', safeHandler(async(req, res) => {
|
||||
// store old bitcoinConfig in memory to revert to in case of errors setting new config and restarting bitcoind
|
||||
const oldBitcoinConfig = await diskLogic.getJsonStore();
|
||||
const oldBitcoinConfig = await configLogic.getJsonStore();
|
||||
const newBitcoinConfig = req.body.bitcoinConfig;
|
||||
|
||||
try {
|
||||
await diskLogic.applyCustomBitcoinConfig(newBitcoinConfig);
|
||||
await configLogic.applyCustomBitcoinConfig(newBitcoinConfig);
|
||||
await bitcoindLogic.stop();
|
||||
|
||||
res.json({success: true});
|
||||
|
||||
} catch (error) {
|
||||
// revert everything to old config values
|
||||
await diskLogic.applyCustomBitcoinConfig(oldBitcoinConfig);
|
||||
await configLogic.applyCustomBitcoinConfig(oldBitcoinConfig);
|
||||
|
||||
res.json({success: false}); // show error to user in UI
|
||||
}
|
||||
@ -46,17 +46,17 @@ router.post('/update-bitcoin-config', safeHandler(async(req, res) => {
|
||||
|
||||
router.post('/restore-default-bitcoin-config', safeHandler(async(req, res) => {
|
||||
// store old bitcoinConfig in memory to revert to in case of errors setting new config and restarting bitcoind
|
||||
const oldBitcoinConfig = await diskLogic.getJsonStore();
|
||||
const oldBitcoinConfig = await configLogic.getJsonStore();
|
||||
|
||||
try {
|
||||
await diskLogic.applyDefaultBitcoinConfig();
|
||||
await configLogic.applyDefaultBitcoinConfig();
|
||||
await bitcoindLogic.stop();
|
||||
|
||||
res.json({success: true});
|
||||
|
||||
} catch (error) {
|
||||
// revert everything to old config values
|
||||
await diskLogic.applyCustomBitcoinConfig(oldBitcoinConfig);
|
||||
await configLogic.applyCustomBitcoinConfig(oldBitcoinConfig);
|
||||
|
||||
res.json({success: false}); // show error to user in UI
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user