Clean up CMerkleTx::SetMerkleBranch.

The case SetMerkleBranch(NULL) was never actually used, and thus the
involved code (loading the block from disk) can be removed and the
implementation simplified.
This commit is contained in:
Daniel Kraft 2014-08-29 20:24:16 +02:00
parent 9f3d476779
commit 4b0deb3b2d
2 changed files with 19 additions and 33 deletions

View File

@ -644,7 +644,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pbl
CWalletTx wtx(this,tx); CWalletTx wtx(this,tx);
// Get merkle branch if transaction was found in a block // Get merkle branch if transaction was found in a block
if (pblock) if (pblock)
wtx.SetMerkleBranch(pblock); wtx.SetMerkleBranch(*pblock);
return AddToWallet(wtx); return AddToWallet(wtx);
} }
} }
@ -2195,48 +2195,34 @@ CWalletKey::CWalletKey(int64_t nExpires)
nTimeExpires = nExpires; nTimeExpires = nExpires;
} }
int CMerkleTx::SetMerkleBranch(const CBlock* pblock) int CMerkleTx::SetMerkleBranch(const CBlock& block)
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
CBlock blockTmp; CBlock blockTmp;
if (pblock == NULL) { // Update the tx's hashBlock
CCoins coins; hashBlock = block.GetHash();
if (pcoinsTip->GetCoins(GetHash(), coins)) {
CBlockIndex *pindex = chainActive[coins.nHeight]; // Locate the transaction
if (pindex) { for (nIndex = 0; nIndex < (int)block.vtx.size(); nIndex++)
if (!ReadBlockFromDisk(blockTmp, pindex)) if (block.vtx[nIndex] == *(CTransaction*)this)
return 0; break;
pblock = &blockTmp; if (nIndex == (int)block.vtx.size())
} {
} vMerkleBranch.clear();
nIndex = -1;
LogPrintf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
return 0;
} }
if (pblock) { // Fill in merkle branch
// Update the tx's hashBlock vMerkleBranch = block.GetMerkleBranch(nIndex);
hashBlock = pblock->GetHash();
// Locate the transaction
for (nIndex = 0; nIndex < (int)pblock->vtx.size(); nIndex++)
if (pblock->vtx[nIndex] == *(CTransaction*)this)
break;
if (nIndex == (int)pblock->vtx.size())
{
vMerkleBranch.clear();
nIndex = -1;
LogPrintf("ERROR: SetMerkleBranch() : couldn't find tx in block\n");
return 0;
}
// Fill in merkle branch
vMerkleBranch = pblock->GetMerkleBranch(nIndex);
}
// Is the tx in a block that's in the main chain // Is the tx in a block that's in the main chain
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock); map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
if (mi == mapBlockIndex.end()) if (mi == mapBlockIndex.end())
return 0; return 0;
CBlockIndex* pindex = (*mi).second; const CBlockIndex* pindex = (*mi).second;
if (!pindex || !chainActive.Contains(pindex)) if (!pindex || !chainActive.Contains(pindex))
return 0; return 0;

View File

@ -500,7 +500,7 @@ public:
) )
int SetMerkleBranch(const CBlock* pblock=NULL); int SetMerkleBranch(const CBlock& block);
// Return depth of transaction in blockchain: // Return depth of transaction in blockchain:
// -1 : not in blockchain, and not in memory pool (conflicted transaction) // -1 : not in blockchain, and not in memory pool (conflicted transaction)