Skip to content

Commit

Permalink
feature: InstantSend (txlock) Transaction Attribute (#6)
Browse files Browse the repository at this point in the history
* increase regtest timeouts

* add txlock status to detailed transaction object

* update test coverage
  • Loading branch information
snogcel committed Jan 9, 2017
1 parent 8fcc283 commit eed15c6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/services/bitcoind.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Bitcoin.DEFAULT_REINDEX_INTERVAL = 10000;
Bitcoin.DEFAULT_START_RETRY_INTERVAL = 5000;
Bitcoin.DEFAULT_TIP_UPDATE_INTERVAL = 15000;
Bitcoin.DEFAULT_TRANSACTION_CONCURRENCY = 5;
Bitcoin.DEFAULT_INSTANTSEND_FEE = 100000;
Bitcoin.DEFAULT_CONFIG_SETTINGS = {
server: 1,
whitelist: '127.0.0.1',
Expand Down Expand Up @@ -1981,6 +1982,18 @@ Bitcoin.prototype.getDetailedTransaction = function(txid, callback) {
}
}

function addTxlockToTx(tx,result) {
tx.txlock = false;
if (tx.feeSatoshis >= Bitcoin.DEFAULT_INSTANTSEND_FEE) { // if transaction fee is >= 0.001 check for txlock
var rawTx = new bitcore.Transaction(result.hex);
var hash = bitcore.crypto.Hash.sha256sha256(rawTx.toBuffer());
var id = hash.toString('binary');
if (self.node.services.bitcoind.zmqKnownTransactionLocks.get(id)) {
tx.txlock = true; // if "txlock" notification received through ZMQ, set txlock = true
}
}
}

if (tx) {
return setImmediate(function() {
callback(null, tx);
Expand Down Expand Up @@ -2015,6 +2028,8 @@ Bitcoin.prototype.getDetailedTransaction = function(txid, callback) {
tx.feeSatoshis = 0;
}

addTxlockToTx(tx, result);

self.transactionDetailedCache.set(txid, tx);

done(null, tx);
Expand Down
1 change: 1 addition & 0 deletions regtest/bitcoind.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ describe('Bitcoind Functionality', function() {
tx.outputs[0].spentIndex.should.equal(0);
tx.outputs[0].spentHeight.should.be.a('number');
tx.outputs[0].address.should.be.a('string');
tx.txlock.should.equal(false);
done();
});
});
Expand Down
50 changes: 50 additions & 0 deletions test/services/bitcoind.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,56 @@ describe('Bitcoin Service', function() {
});
});

// TODO: transaction lock test coverage
describe('#_zmqTransactionLockHandler', function() {
it('will emit to subscribers', function(done) {
var bitcoind = new BitcoinService(baseConfig);
var expectedBuffer = new Buffer(txhex, 'hex');
var emitter = new EventEmitter();
bitcoind.subscriptions.transactionlock.push(emitter);
emitter.on('bitcoind/transactionlock', function(hex) {
hex.should.be.a('string');
hex.should.equal(expectedBuffer.toString('hex'));
done();
});
var node = {};
bitcoind._zmqTransactionLockHandler(node, expectedBuffer);
});
it('will NOT emit to subscribers more than once for the same tx', function(done) {
var bitcoind = new BitcoinService(baseConfig);
var expectedBuffer = new Buffer(txhex, 'hex');
var emitter = new EventEmitter();
bitcoind.subscriptions.transactionlock.push(emitter);
emitter.on('bitcoind/transactionlock', function() {
done();
});
var node = {};
bitcoind._zmqTransactionLockHandler(node, expectedBuffer);
bitcoind._zmqTransactionLockHandler(node, expectedBuffer);
});
it('will emit "tx" event', function(done) {
var bitcoind = new BitcoinService(baseConfig);
var expectedBuffer = new Buffer(txhex, 'hex');
bitcoind.on('txlock', function(buffer) {
buffer.should.be.instanceof(Buffer);
buffer.toString('hex').should.equal(expectedBuffer.toString('hex'));
done();
});
var node = {};
bitcoind._zmqTransactionLockHandler(node, expectedBuffer);
});
it('will NOT emit "tx" event more than once for the same tx', function(done) {
var bitcoind = new BitcoinService(baseConfig);
var expectedBuffer = new Buffer(txhex, 'hex');
bitcoind.on('txlock', function() {
done();
});
var node = {};
bitcoind._zmqTransactionLockHandler(node, expectedBuffer);
bitcoind._zmqTransactionLockHandler(node, expectedBuffer);
});
});

describe('#_checkSyncedAndSubscribeZmqEvents', function() {
var sandbox = sinon.sandbox.create();
before(function() {
Expand Down

0 comments on commit eed15c6

Please sign in to comment.