Browse Source

determine bitcoin network based on config file instead of cli argument

umbrel
kenshin-samourai 4 years ago
parent
commit
b0a32dd42f
  1. 8
      docker/my-dojo/node/restart.sh
  2. 1
      keys/index-example.js
  3. 25
      lib/bitcoin/network.js
  4. 52
      lib/db/mysql-db-wrapper.js

8
docker/my-dojo/node/restart.sh

@ -1,14 +1,14 @@
#!/bin/bash #!/bin/bash
cd /home/node/app/accounts cd /home/node/app/accounts
forever start -a -l /dev/stdout -o /dev/null -e /dev/null index.js "$COMMON_BTC_NETWORK" forever start -a -l /dev/stdout -o /dev/null -e /dev/null index.js
cd /home/node/app/pushtx cd /home/node/app/pushtx
forever start -a -l /dev/stdout -o /dev/null -e /dev/null index.js "$COMMON_BTC_NETWORK" forever start -a -l /dev/stdout -o /dev/null -e /dev/null index.js
forever start -a -l /dev/stdout -o /dev/null -e /dev/null index-orchestrator.js "$COMMON_BTC_NETWORK" forever start -a -l /dev/stdout -o /dev/null -e /dev/null index-orchestrator.js
cd /home/node/app/tracker cd /home/node/app/tracker
forever start -a -l /dev/stdout -o /dev/null -e /dev/null index.js "$COMMON_BTC_NETWORK" forever start -a -l /dev/stdout -o /dev/null -e /dev/null index.js
# Keep the container up # Keep the container up
while true while true

1
keys/index-example.js

@ -6,6 +6,7 @@
/** /**
* Desired structure of /keys/index.js, which is ignored in the repository. * Desired structure of /keys/index.js, which is ignored in the repository.
* index.js should store only one of the 2 sets of parameters (mainnet or testnet)
*/ */
module.exports = { module.exports = {
/* /*

25
lib/bitcoin/network.js

@ -5,8 +5,16 @@
'use strict' 'use strict'
const bitcoin = require('bitcoinjs-lib') const bitcoin = require('bitcoinjs-lib')
const keys = require('../../keys/')
/**
* A set of keywords encoding for mainnet
*/
const MAINNET_KEY = [
'bitcoin'
]
/** /**
* A set of keywords encoding for testnet * A set of keywords encoding for testnet
*/ */
@ -26,15 +34,20 @@ class Network {
* Constructor * Constructor
*/ */
constructor() { constructor() {
this.key = 'bitcoin' // Check if mainnet config is detected in index.js
this.network = bitcoin.networks.bitcoin for (let kw of MAINNET_KEY) {
if (kw in keys) {
this.key = 'bitcoin'
this.network = bitcoin.networks.bitcoin
return
}
}
// Check if testnet config is detected in index.js
for (let kw of TESTNET_KEY) { for (let kw of TESTNET_KEY) {
// Calling like 'node file.js arg1 arg2' if (kw in keys) {
if (process.argv.indexOf(kw) > 1) {
this.key = 'testnet' this.key = 'testnet'
this.network = bitcoin.networks.testnet this.network = bitcoin.networks.testnet
break return
} }
} }
} }

52
lib/db/mysql-db-wrapper.js

@ -5,14 +5,12 @@
'use strict' 'use strict'
const mysql = require('mysql') const mysql = require('mysql')
const path = require('path')
const Logger = require('../logger') const Logger = require('../logger')
const util = require('../util') const util = require('../util')
const errors = require('../errors') const errors = require('../errors')
const hdaHelper = require('../bitcoin/hd-accounts-helper') const hdaHelper = require('../bitcoin/hd-accounts-helper')
const network = require('../bitcoin/network') const network = require('../bitcoin/network')
const keys = require('../../keys/')[network.key] const keys = require('../../keys/')[network.key]
const keysDb = keys.db
const debug = !!(process.argv.indexOf('db-debug') > -1) const debug = !!(process.argv.indexOf('db-debug') > -1)
const queryDebug = !!(process.argv.indexOf('dbquery-debug') > -1) const queryDebug = !!(process.argv.indexOf('dbquery-debug') > -1)
@ -389,8 +387,8 @@ class MySqlDbWrapper {
this.pool.query(query, null, async (err, result, fields) => { this.pool.query(query, null, async (err, result, fields) => {
if (err) { if (err) {
// Retry the request on lock errors // Retry the request on lock errors
if ((err.code == 'ER_LOCK_DEADLOCK' || if ((err.code == 'ER_LOCK_DEADLOCK' ||
err.code == 'ER_LOCK_TIMEOUT' || err.code == 'ER_LOCK_TIMEOUT' ||
err.code == 'ER_LOCK_WAIT_TIMEOUT') && (retries > 0) err.code == 'ER_LOCK_WAIT_TIMEOUT') && (retries > 0)
) { ) {
try { try {
@ -438,7 +436,7 @@ class MySqlDbWrapper {
if (result.length > 0) if (result.length > 0)
return result[0].addrID return result[0].addrID
throw errors.db.ERROR_NO_ADDRESS throw errors.db.ERROR_NO_ADDRESS
} }
@ -455,7 +453,7 @@ class MySqlDbWrapper {
if (result.length > 0) if (result.length > 0)
return result[0].addrID return result[0].addrID
const sqlQuery2 = 'INSERT INTO `addresses` SET ?' const sqlQuery2 = 'INSERT INTO `addresses` SET ?'
const params2 = { addrAddress: address } const params2 = { addrAddress: address }
const query2 = mysql.format(sqlQuery2, params2) const query2 = mysql.format(sqlQuery2, params2)
@ -521,7 +519,7 @@ class MySqlDbWrapper {
async getAddressBalance(address) { async getAddressBalance(address) {
if (address == null) if (address == null)
return null return null
const sqlQuery = 'SELECT SUM(`outputs`.`outAmount`) as balance \ const sqlQuery = 'SELECT SUM(`outputs`.`outAmount`) as balance \
FROM `addresses` \ FROM `addresses` \
INNER JOIN `outputs` ON `outputs`.`addrID` = `addresses`.`addrID` \ INNER JOIN `outputs` ON `outputs`.`addrID` = `addresses`.`addrID` \
@ -613,7 +611,7 @@ class MySqlDbWrapper {
if (result.length > 0) if (result.length > 0)
return result[0].hdID return result[0].hdID
throw errors.db.ERROR_NO_HD_ACCOUNT throw errors.db.ERROR_NO_HD_ACCOUNT
} }
/** /**
@ -639,14 +637,14 @@ class MySqlDbWrapper {
const sqlQuery2 = 'INSERT INTO `hd` SET ?' const sqlQuery2 = 'INSERT INTO `hd` SET ?'
const params2 = { const params2 = {
hdXpub: xpub, hdXpub: xpub,
hdCreated: util.unix(), hdCreated: util.unix(),
hdType: type, hdType: type,
} }
const query2 = mysql.format(sqlQuery2, params2) const query2 = mysql.format(sqlQuery2, params2)
const result2 = await this._query(query2) const result2 = await this._query(query2)
return result2.insertId return result2.insertId
} }
/** /**
@ -737,7 +735,7 @@ class MySqlDbWrapper {
async addAddressesToHDAccount(xpub, addressData) { async addAddressesToHDAccount(xpub, addressData) {
if (addressData.length == 0) if (addressData.length == 0)
return return
const addresses = addressData.map(d => d.address) const addresses = addressData.map(d => d.address)
const hdID = await this.getHDAccountId(xpub) const hdID = await this.getHDAccountId(xpub)
@ -801,10 +799,10 @@ class MySqlDbWrapper {
* { * {
* hd: { * hd: {
* [xpub]: { * [xpub]: {
* hdID: N, * hdID: N,
* hdType: M, * hdType: M,
* addresses:[...] * addresses:[...]
* }, * },
* ... * ...
* } * }
* loose:[...] * loose:[...]
@ -1050,7 +1048,7 @@ class MySqlDbWrapper {
const sqlQuery = 'SELECT COUNT(DISTINCT `r`.`txnTxid`) AS nbTxs \ const sqlQuery = 'SELECT COUNT(DISTINCT `r`.`txnTxid`) AS nbTxs \
FROM (' + subQuery + ') AS `r`' FROM (' + subQuery + ') AS `r`'
let query = mysql.format(sqlQuery) let query = mysql.format(sqlQuery)
const results = await this._query(query) const results = await this._query(query)
@ -1122,7 +1120,7 @@ class MySqlDbWrapper {
if (txsIds.length == 0) if (txsIds.length == 0)
return [] return []
// Prepares subqueries for // Prepares subqueries for
// the query retrieving utxos of interest // the query retrieving utxos of interest
let subQuery2 = '' let subQuery2 = ''
let subQueries2 = [] let subQueries2 = []
@ -1323,7 +1321,7 @@ class MySqlDbWrapper {
async ensureTransactionId(txid) { async ensureTransactionId(txid) {
const sqlQuery = 'INSERT IGNORE INTO `transactions` SET ?' const sqlQuery = 'INSERT IGNORE INTO `transactions` SET ?'
const params = { const params = {
txnTxid: txid, txnTxid: txid,
txnCreated: util.unix() txnCreated: util.unix()
} }
const query = mysql.format(sqlQuery, params) const query = mysql.format(sqlQuery, params)
@ -1473,7 +1471,7 @@ class MySqlDbWrapper {
tx.time = Math.min(tx.time, output.blockTime) tx.time = Math.min(tx.time, output.blockTime)
tx.time = Math.min(tx.time, output.txnCreated) tx.time = Math.min(tx.time, output.txnCreated)
if (output.blockHeight != null) if (output.blockHeight != null)
tx.block_height = output.blockHeight tx.block_height = output.blockHeight
@ -1532,7 +1530,7 @@ class MySqlDbWrapper {
// Remove block height if null // Remove block height if null
if (tx.block_height == null) if (tx.block_height == null)
delete tx.block_height delete tx.block_height
return tx return tx
} }
@ -1665,7 +1663,7 @@ class MySqlDbWrapper {
/** /**
* Get a list of outputs identified by their txid and index. * Get a list of outputs identified by their txid and index.
* The presence of spendingTxnID and spendingInID not null indicate that an * The presence of spendingTxnID and spendingInID not null indicate that an
* input spending the transaction output index is already in the database and * input spending the transaction output index is already in the database and
* may indicate a DOUBLE SPEND. * may indicate a DOUBLE SPEND.
* @param {object[]} spends - array of {txid,index} * @param {object[]} spends - array of {txid,index}
@ -1673,10 +1671,10 @@ class MySqlDbWrapper {
* {addrAddress, outID, outAmount, txnTxid, outIndex, spendingTxnID/null, spendingInID/null} * {addrAddress, outID, outAmount, txnTxid, outIndex, spendingTxnID/null, spendingInID/null}
*/ */
async getOutputSpends(spends) { async getOutputSpends(spends) {
if (spends.length == 0) if (spends.length == 0)
return [] return []
const whereClauses = const whereClauses =
spends.map(s => '(`txnTxid`=' + this.pool.escape(s.txid) + ' AND `outIndex`=' + this.pool.escape(s.index) + ')') spends.map(s => '(`txnTxid`=' + this.pool.escape(s.txid) + ' AND `outIndex`=' + this.pool.escape(s.index) + ')')
const whereClause = whereClauses.join(' OR ') const whereClause = whereClauses.join(' OR ')
@ -1796,10 +1794,10 @@ class MySqlDbWrapper {
const params2 = block.blockHash const params2 = block.blockHash
const query2 = mysql.format(sqlQuery2, params2) const query2 = mysql.format(sqlQuery2, params2)
const result2 = await this._query(query2) const result2 = await this._query(query2)
if (result2.length > 0) if (result2.length > 0)
return result2[0].blockID return result2[0].blockID
throw 'Problem met while trying to insert a new block' throw 'Problem met while trying to insert a new block'
} }
@ -2009,7 +2007,7 @@ class MySqlDbWrapper {
INNER JOIN `transactions` ON `outputs`.`txnID` = `transactions`.`txnID` \ INNER JOIN `transactions` ON `outputs`.`txnID` = `transactions`.`txnID` \
WHERE `hd`.`hdCreated` > `transactions`.`txnCreated` \ WHERE `hd`.`hdCreated` > `transactions`.`txnCreated` \
GROUP BY `hd`.`hdID` LIMIT 100' GROUP BY `hd`.`hdID` LIMIT 100'
return this._query(sqlQuery) return this._query(sqlQuery)
} }
@ -2022,7 +2020,7 @@ class MySqlDbWrapper {
INNER JOIN `blocks` ON `transactions`.`blockID` = `blocks`.`blockID` \ INNER JOIN `blocks` ON `transactions`.`blockID` = `blocks`.`blockID` \
WHERE `transactions`.`txnCreated` > `blocks`.`blockTime` \ WHERE `transactions`.`txnCreated` > `blocks`.`blockTime` \
LIMIT 100' LIMIT 100'
return this._query(sqlQuery) return this._query(sqlQuery)
} }

Loading…
Cancel
Save