From bc4bd456dae65640b0251dc67caad4c92be6f544 Mon Sep 17 00:00:00 2001 From: Damian Mee Date: Tue, 9 Sep 2014 08:00:41 +0200 Subject: [PATCH] refactor(psql): fns made a little bit more atomic --- lib/plugins.js | 47 ++++++++++++++++++++----------------- lib/postgresql_interface.js | 28 +++++++++++++++------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/lib/plugins.js b/lib/plugins.js index 3f82cf28..2c3d9c5b 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -242,33 +242,38 @@ exports.fiatBalance = function fiatBalance() { return fiatTransferBalance; }; +function _sendBitcoins(tx, callback) { + logger.debug('executing tx: %j', tx); + walletPlugin.sendBitcoins( + tx.toAddress, + tx.satoshis, + cachedConfig.exchanges.settings.transactionFee, + + function(err, txHash) { + if (err) { + var status = err.name === 'InsufficientFunds' ? + 'insufficientFunds' : + 'failed'; + + db.reportTransactionError(tx, err.message, status); + return callback(err); + } + + db.completeTransaction(tx, txHash); + pollBalance(); + callback(null, txHash); + } + ); +} + exports.sendBitcoins = function sendBitcoins(deviceFingerprint, tx, callback) { db.summonTransaction(deviceFingerprint, tx, function(err, txInfo) { if (err) return callback(err); if (!txInfo || txInfo.status === 'partial') { + // TODO: make sure session exists, to prevent sending coins twice clearSession(deviceFingerprint); - - return walletPlugin.sendBitcoins( - tx.toAddress, - tx.satoshis, - cachedConfig.exchanges.settings.transactionFee, - - function(err, txHash) { - if (err) { - var status = err.name === 'InsufficientFunds' ? - 'insufficientFunds' : - 'failed'; - - db.reportTransactionError(tx, err.message, status); - return callback(err); - } - - db.completeTransaction(tx, txHash); - pollBalance(); - callback(null, txHash); - } - ); + return _sendBitcoins(tx, callback); } // Out of bitcoins: special case diff --git a/lib/postgresql_interface.js b/lib/postgresql_interface.js index c0e65665..9b85a739 100644 --- a/lib/postgresql_interface.js +++ b/lib/postgresql_interface.js @@ -54,18 +54,28 @@ function updatePartialTransaction(values, cb) { values2, cb); } -function fetchTransaction(txId, cb) { - client.query('SELECT status, tx_hash, error FROM transactions WHERE id=$1', +exports.getTransaction = function getTransaction(txId, cb) { + client.query('SELECT * FROM transactions WHERE id=$1', [txId], - function (err, results) { + function(err, results) { if (err) return cb(err); - // This should never happen, since we already checked for existence - if (results.rows.length === 0) return cb(new Error('Couldn\'t find transaction.')); - - var result = results.rows[0]; - cb(null, {txHash: result.tx_hash, err: result.error, status: result.status}); + cb(null, results.rows.length >= 0 && results.rows[0]); }); +}; +exports.fetchTransaction = function fetchTransaction(txId, cb) { + exports.getTransaction(txId, function(err, tx) { + if (err) return cb(err); + + if (!tx) + return cb(new Error('Couldn\'t find transaction.')); + + cb(null, { + txHash: tx.tx_hash, + err: tx.error, + status: tx.status + }); + }); } exports.summonTransaction = function summonTransaction(deviceFingerprint, tx, cb) { var status = tx.status || 'pending'; @@ -95,7 +105,7 @@ exports.summonTransaction = function summonTransaction(deviceFingerprint, tx, cb if (status === 'partial') return updatePartialTransaction(values, cb); - return fetchTransaction(tx.txId, cb); + return exports.fetchTransaction(tx.txId, cb); } return cb(err);