From 3a135929b45eb2cdb0cd6d378d116faa4071ddb9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 22 Sep 2014 12:55:43 -0700 Subject: [PATCH] try to GetTransaction working. --- src/bitcoindjs.cc | 64 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 46935542..9ddd31fc 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -877,6 +877,9 @@ async_get_block_after(uv_work_t *req) { * bitcoind.getTx(hash, callback) */ +static bool +GetTransaction_(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow); + NAN_METHOD(GetTx) { NanScope(); @@ -916,7 +919,7 @@ NAN_METHOD(GetTx) { uint256 hashBlock(blockHash); CTransaction tx; - if (!GetTransaction(hash, tx, hashBlock, noBlockHash ? true : false)) { + if (!GetTransaction_(hash, tx, hashBlock, noBlockHash ? true : false)) { Local err = Exception::Error(String::New("Bad Transaction.")); const unsigned argc = 1; Local argv[argc] = { err }; @@ -1031,6 +1034,65 @@ NAN_METHOD(GetTx) { } } +static bool +GetTransaction_(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow) { + CBlockIndex *pindexSlow = NULL; + { + LOCK(cs_main); + { + if (mempool.lookup(hash, txOut)) { + return true; + } + } + + if (0 && fTxIndex) { + CDiskTxPos postx; + if (pblocktree->ReadTxIndex(hash, postx)) { + CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION); + CBlockHeader header; + try { + file >> header; + fseek(file, postx.nTxOffset, SEEK_CUR); + file >> txOut; + } catch (std::exception &e) { + return error("%s : Deserialize or I/O error - %s", __PRETTY_FUNCTION__, e.what()); + } + hashBlock = header.GetHash(); + if (txOut.GetHash() != hash) + return error("%s : txid mismatch", __PRETTY_FUNCTION__); + return true; + } + } + + if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it + int nHeight = -1; + { + CCoinsViewCache &view = *pcoinsTip; + CCoins coins; + if (view.GetCoins(hash, coins)) + nHeight = coins.nHeight; + } + if (nHeight > 0) + pindexSlow = chainActive[nHeight]; + } + } + + if (pindexSlow) { + CBlock block; + if (ReadBlockFromDisk(block, pindexSlow)) { + BOOST_FOREACH(const CTransaction &tx, block.vtx) { + if (tx.GetHash() == hash) { + txOut = tx; + hashBlock = pindexSlow->GetBlockHash(); + return true; + } + } + } + } + + return false; +} + /** * Init */