[rpc] Allow fetching tx directly from specified block in getrawtransaction #10275
Open
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d2c2f8a
[rpc] Fix fVerbose parsing (remove excess if cases and ensure null is…
kallewoof 7ba57b3
[rpc] Allow getrawtransaction to take optional blockhash to fetch tra…
kallewoof 4d8bf80
[test] Updated rawtransactions.py to assert for adjusted exception.
kallewoof 950ae53
[test] Add tests for getrawtransaction with block hash.
kallewoof 45d371a
f'inMainChain -> inActiveChain
kallewoof 1152f81
f'Added support for block height instead of block hash..
kallewoof 6325671
f'Added tests for block height.
kallewoof
Jump to file or symbol
Failed to load files and symbols.
| @@ -896,43 +896,44 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa | ||
| } | ||
| /** Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock */ | ||
| -bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow) | ||
| +bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow, CBlockIndex* blockIndex) | ||
| { | ||
| - CBlockIndex *pindexSlow = NULL; | ||
| + CBlockIndex* pindexSlow = blockIndex; | ||
| LOCK(cs_main); | ||
| - CTransactionRef ptx = mempool.get(hash); | ||
| - if (ptx) | ||
| - { | ||
| - txOut = ptx; | ||
| - return true; | ||
| - } | ||
| + if (!blockIndex) { | ||
kallewoof
Contributor
|
||
| + CTransactionRef ptx = mempool.get(hash); | ||
| + if (ptx) { | ||
| + txOut = ptx; | ||
| + return true; | ||
| + } | ||
| - if (fTxIndex) { | ||
| - CDiskTxPos postx; | ||
| - if (pblocktree->ReadTxIndex(hash, postx)) { | ||
| - CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION); | ||
| - if (file.IsNull()) | ||
| - return error("%s: OpenBlockFile failed", __func__); | ||
| - CBlockHeader header; | ||
| - try { | ||
| - file >> header; | ||
| - fseek(file.Get(), postx.nTxOffset, SEEK_CUR); | ||
| - file >> txOut; | ||
| - } catch (const std::exception& e) { | ||
| - return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | ||
| + if (fTxIndex) { | ||
| + CDiskTxPos postx; | ||
| + if (pblocktree->ReadTxIndex(hash, postx)) { | ||
| + CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION); | ||
| + if (file.IsNull()) | ||
| + return error("%s: OpenBlockFile failed", __func__); | ||
| + CBlockHeader header; | ||
| + try { | ||
| + file >> header; | ||
| + fseek(file.Get(), postx.nTxOffset, SEEK_CUR); | ||
| + file >> txOut; | ||
| + } catch (const std::exception& e) { | ||
| + return error("%s: Deserialize or I/O error - %s", __func__, e.what()); | ||
| + } | ||
| + hashBlock = header.GetHash(); | ||
| + if (txOut->GetHash() != hash) | ||
| + return error("%s: txid mismatch", __func__); | ||
| + return true; | ||
| } | ||
| - hashBlock = header.GetHash(); | ||
| - if (txOut->GetHash() != hash) | ||
| - return error("%s: txid mismatch", __func__); | ||
| - return true; | ||
| } | ||
| - } | ||
| - if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it | ||
| - const Coin& coin = AccessByTxid(*pcoinsTip, hash); | ||
| - if (!coin.IsSpent()) pindexSlow = chainActive[coin.nHeight]; | ||
| + if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it | ||
| + const Coin& coin = AccessByTxid(*pcoinsTip, hash); | ||
| + if (!coin.IsSpent()) pindexSlow = chainActive[coin.nHeight]; | ||
| + } | ||
| } | ||
| if (pindexSlow) { | ||
Perhaps the diff can be less disruptive by moving everything inside
if (!blockIndex) {...}to a new static function defined right above instead of indenting all of it?