From 8c1022148023d794a980c796ff34cd2e517d507f Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 4 Mar 2016 13:47:22 -0500 Subject: [PATCH] bindings: fixes confirmation issue with orphaned block transactions --- integration/regtest-node.js | 62 +++++++++++++++++++++++++++++++++++++ src/libbitcoind.cc | 6 +++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/integration/regtest-node.js b/integration/regtest-node.js index e05e3039..9523f79a 100644 --- a/integration/regtest-node.js +++ b/integration/regtest-node.js @@ -796,4 +796,66 @@ describe('Node Functionality', function() { }); }); + + describe('Orphaned Transactions', function() { + var orphanedTransaction; + + before(function(done) { + var count; + var invalidatedBlockHash; + + async.series([ + function(next) { + client.getBlockCount(function(err, response) { + if (err) { + return next(err); + } + count = response.result; + next(); + }); + }, + function(next) { + client.getBlockHash(count, function(err, response) { + if (err) { + return next(err); + } + invalidatedBlockHash = response.result; + next(); + }); + }, + function(next) { + client.getBlock(invalidatedBlockHash, function(err, response) { + if (err) { + return next(err); + } + orphanedTransaction = response.result.tx[1]; + next(); + }); + }, + function(next) { + client.invalidateBlock(invalidatedBlockHash, next); + } + ], function(err) { + if (err) { + throw err; + } + done(); + }); + }); + + it('will not show confirmation count for orphaned transaction', function(done) { + // This test verifies that in the situation that the transaction is not in the mempool and + // is included in an orphaned block transaction index that the confirmation count will be unconfirmed. + node.services.bitcoind.getTransactionWithBlockInfo(orphanedTransaction, false, function(err, data) { + if (err) { + return done(err); + } + should.exist(data.height); + data.height.should.equal(-1); + done(); + }); + }); + + }); + }); diff --git a/src/libbitcoind.cc b/src/libbitcoind.cc index 882dabbb..4627b6d3 100644 --- a/src/libbitcoind.cc +++ b/src/libbitcoind.cc @@ -1230,7 +1230,11 @@ async_get_tx_and_info(uv_work_t *req) { data->height = -1; } else { blockIndex = mapBlockIndex[blockHash]; - data->height = blockIndex->nHeight; + if (!chainActive.Contains(blockIndex)) { + data->height = -1; + } else { + data->height = blockIndex->nHeight; + } } }