aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Morcos <morcos@chaincode.com>2015-09-17 17:43:34 -0400
committerLuke Dashjr <luke-jr+git@utopios.org>2015-11-05 19:56:39 +0000
commit3ad96bdf73f788a65c769a7579bace40cab0bd81 (patch)
treed59eebff8e0c2f6a61f8a4d24f7fbfa0fa81679d
parent9c810058d8f3ae76234dc4efa53a57306694b92c (diff)
downloadbitcoin-3ad96bdf73f788a65c769a7579bace40cab0bd81.tar.xz
Fix locking in GetTransaction.
GetTransaction needs to lock cs_main until ReadBlockFromDisk completes, the data inside CBlockIndex's can change since pruning. This lock was held by all calls to GetTransaction except rest_tx.
-rw-r--r--src/main.cpp68
1 files changed, 33 insertions, 35 deletions
diff --git a/src/main.cpp b/src/main.cpp
index bcb0f20d61..50e01fa400 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1110,47 +1110,45 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
{
CBlockIndex *pindexSlow = NULL;
+
+ LOCK(cs_main);
+
+ if (mempool.lookup(hash, txOut))
{
- LOCK(cs_main);
- {
- if (mempool.lookup(hash, txOut))
- {
- return true;
- }
- }
+ 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 (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;
+ 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 (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;
}
+ }
- if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
- int nHeight = -1;
- {
- CCoinsViewCache &view = *pcoinsTip;
- const CCoins* coins = view.AccessCoins(hash);
- if (coins)
- nHeight = coins->nHeight;
- }
- if (nHeight > 0)
- pindexSlow = chainActive[nHeight];
+ if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
+ int nHeight = -1;
+ {
+ CCoinsViewCache &view = *pcoinsTip;
+ const CCoins* coins = view.AccessCoins(hash);
+ if (coins)
+ nHeight = coins->nHeight;
}
+ if (nHeight > 0)
+ pindexSlow = chainActive[nHeight];
}
if (pindexSlow) {