Make idempotent send work, together with a test

This commit is contained in:
Maciej Małecki 2014-04-16 16:45:32 +02:00
parent f90ded6144
commit aa81cfe33b
2 changed files with 37 additions and 17 deletions

View file

@ -26,8 +26,8 @@ PostgresqlInterface.prototype.summonTransaction =
// If it worked, go ahead with transaction // If it worked, go ahead with transaction
// If duplicate, fetch status and return // If duplicate, fetch status and return
var self = this; var self = this;
this.client.query('INSERT INTO transactions (id, status, deviceFingerprint, ' + this.client.query('INSERT INTO transactions (id, status, "deviceFingerprint", ' +
'toAddress, satoshis, currencyCode, fiat) ' + '"toAddress", satoshis, "currencyCode", fiat) ' +
'VALUES ($1, $2, $3, $4, $5, $6, $7)', [tx.txId, 'pending', deviceFingerprint, 'VALUES ($1, $2, $3, $4, $5, $6, $7)', [tx.txId, 'pending', deviceFingerprint,
tx.toAddress, tx.satoshis, tx.currencyCode, tx.fiat], tx.toAddress, tx.satoshis, tx.currencyCode, tx.fiat],
function (err) { function (err) {
@ -47,7 +47,7 @@ PostgresqlInterface.prototype.reportTransactionError =
PostgresqlInterface.prototype.completeTransaction = PostgresqlInterface.prototype.completeTransaction =
function completeTransaction(tx, txHash) { function completeTransaction(tx, txHash) {
if (txHash) if (txHash)
this.client.query('UPDATE transactions SET txHash=$1, status=$2, completed=now() WHERE id=$3', this.client.query('UPDATE transactions SET "txHash"=$1, status=$2, completed=now() WHERE id=$3',
[txHash, 'completed', tx.txId]); [txHash, 'completed', tx.txId]);
else else
this.client.query('UPDATE transactions SET status=$1, error=$2 WHERE id=$3', this.client.query('UPDATE transactions SET status=$1, error=$2 WHERE id=$3',
@ -56,14 +56,14 @@ PostgresqlInterface.prototype.completeTransaction =
PostgresqlInterface.prototype._fetchTransaction = PostgresqlInterface.prototype._fetchTransaction =
function _fetchTransaction(txId, cb) { function _fetchTransaction(txId, cb) {
this.client.query('SELECT status, txHash FROM transactions WHERE id=$1', this.client.query('SELECT status, "txHash" FROM transactions WHERE id=$1',
[txId], function (err, rows) { [txId], function (err, results) {
if (err) return cb(err); if (err) return cb(err);
// This should never happen, since we already checked for existence // This should never happen, since we already checked for existence
if (rows === 0) return cb(new Error('Couldn\'t find transaction.')); if (results.rows.length === 0) return cb(new Error('Couldn\'t find transaction.'));
var result = rows[0]; var result = results.rows[0];
cb(null, false, result.txHash); cb(null, false, result.txHash);
}); });
}; };

View file

@ -12,8 +12,12 @@ var psqlInterface = new PostgresqlInterface(db);
var TRANSACTION_FEE = 1; var TRANSACTION_FEE = 1;
var FINGERPRINT = 'CB:3D:78:49:03:39:BA:47:0A:33:29:3E:31:25:F7:C6:4F:74:71:D7'; var FINGERPRINT = 'CB:3D:78:49:03:39:BA:47:0A:33:29:3E:31:25:F7:C6:4F:74:71:D7';
var TXID = '216dabdb692670bae940deb71e59486038a575f637903d3c9af601ddd48057fc'; var TXID = '216dabdb692670bae940deb71e59486038a575f637903d3c9af601ddd48057fc';
var ADDRESS = '1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64';
var SATOSHIS = 1337;
var CURRENCY = 'USD'; var CURRENCY = 'USD';
var OUR_TXID = uuid();
describe('trader/send', function () { describe('trader/send', function () {
var trader = new Trader(psqlInterface); var trader = new Trader(psqlInterface);
trader.config = { trader.config = {
@ -27,14 +31,10 @@ describe('trader/send', function () {
trader.pollRate = function () {}; trader.pollRate = function () {};
it('should call `sendBitcoins` on the transfer exchange', function (done) { it('should call `sendBitcoins` on the transfer exchange', function (done) {
var address = '1LhkU2R8nJaU8Zj6jB8VjWrMpvVKGqCZ64';
var txId = uuid();
var satoshis = 1337;
trader.transferExchange = { trader.transferExchange = {
sendBitcoins: function (address_, satoshis_, transactionFee, callback) { sendBitcoins: function (address, satoshis, transactionFee, callback) {
assert.equal(address, address_); assert.equal(ADDRESS, address);
assert.equal(satoshis, satoshis_); assert.equal(SATOSHIS, satoshis);
assert.equal(transactionFee, TRANSACTION_FEE); assert.equal(transactionFee, TRANSACTION_FEE);
callback(null, TXID); callback(null, TXID);
} }
@ -42,10 +42,30 @@ describe('trader/send', function () {
trader.sendBitcoins(FINGERPRINT, { trader.sendBitcoins(FINGERPRINT, {
fiat: 100, fiat: 100,
txId: txId, txId: OUR_TXID,
currencyCode: CURRENCY, currencyCode: CURRENCY,
toAddress: address, toAddress: ADDRESS,
satoshis: satoshis satoshis: SATOSHIS
}, function (err, txId) {
assert.notOk(err);
assert.equal(txId, TXID);
done();
});
});
it('should not call `sendBitcoins` on the transfer exchange with same send', function (done) {
trader.transferExchange = {
sendBitcoins: function () {
throw new Error('This should not have been called');
}
};
trader.sendBitcoins(FINGERPRINT, {
fiat: 100,
txId: OUR_TXID,
currencyCode: CURRENCY,
toAddress: ADDRESS,
satoshis: SATOSHIS
}, function (err, txId) { }, function (err, txId) {
assert.notOk(err); assert.notOk(err);
assert.equal(txId, TXID); assert.equal(txId, TXID);