Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock */
| 1491 | |
| 1492 | /** Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock */ |
| 1493 | bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow) |
| 1494 | { |
| 1495 | CBlockIndex *pindexSlow = NULL; |
| 1496 | { |
| 1497 | LOCK(cs_main); |
| 1498 | { |
| 1499 | if (mempool.lookup(hash, txOut)) |
| 1500 | { |
| 1501 | return true; |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | if (fTxIndex) { |
| 1506 | CDiskTxPos postx; |
| 1507 | if (pblocktree->ReadTxIndex(hash, postx)) { |
| 1508 | CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION); |
| 1509 | if (file.IsNull()) |
| 1510 | return error("%s: OpenBlockFile failed", __func__); |
| 1511 | CBlockHeader header; |
| 1512 | try { |
| 1513 | file >> header; |
| 1514 | fseek(file.Get(), postx.nTxOffset, SEEK_CUR); |
| 1515 | file >> txOut; |
| 1516 | } catch (const std::exception& e) { |
| 1517 | return error("%s: Deserialize or I/O error - %s", __func__, e.what()); |
| 1518 | } |
| 1519 | hashBlock = header.GetHash(); |
| 1520 | if (txOut.GetHash() != hash) |
| 1521 | return error("%s: txid mismatch", __func__); |
| 1522 | return true; |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it |
| 1527 | int nHeight = -1; |
| 1528 | { |
| 1529 | CCoinsViewCache &view = *pcoinsTip; |
| 1530 | const CCoins* coins = view.AccessCoins(hash); |
| 1531 | if (coins) |
| 1532 | nHeight = coins->nHeight; |
| 1533 | } |
| 1534 | if (nHeight > 0) |
| 1535 | pindexSlow = chainActive[nHeight]; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | if (pindexSlow) { |
| 1540 | CBlock block; |
| 1541 | if (ReadBlockFromDisk(block, pindexSlow)) { |
| 1542 | BOOST_FOREACH(const CTransaction &tx, block.vtx) { |
| 1543 | if (tx.GetHash() == hash) { |
| 1544 | txOut = tx; |
| 1545 | hashBlock = pindexSlow->GetBlockHash(); |
| 1546 | return true; |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | } |
no test coverage detected