Skip to content

Commit

Permalink
Merge pull request #491 from gavinandresen/manytxfix
Browse files Browse the repository at this point in the history
Optimize database writes for transactions with lots of TxIns.
  • Loading branch information
gavinandresen committed Sep 6, 2011
2 parents dc0c768 + e077cce commit e4626e9
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPoo
// Read txindex
CTxIndex txindex;
bool fFound = true;
if (fMiner && mapTestPool.count(prevout.hash))
if ((fBlock || fMiner) && mapTestPool.count(prevout.hash))
{
// Get txindex from current proposed changes
txindex = mapTestPool[prevout.hash];
Expand Down Expand Up @@ -873,12 +873,7 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPoo
txindex.vSpent[prevout.n] = posThisTx;

// Write back
if (fBlock)
{
if (!txdb.UpdateTxIndex(prevout.hash, txindex))
return error("ConnectInputs() : UpdateTxIndex failed");
}
else if (fMiner)
if (fBlock || fMiner)
{
mapTestPool[prevout.hash] = txindex;
}
Expand All @@ -900,9 +895,8 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, map<uint256, CTxIndex>& mapTestPoo

if (fBlock)
{
// Add transaction to disk index
if (!txdb.AddTxIndex(*this, posThisTx, pindexBlock->nHeight))
return error("ConnectInputs() : AddTxPos failed");
// Add transaction to changes
mapTestPool[GetHash()] = CTxIndex(posThisTx, vout.size());
}
else if (fMiner)
{
Expand Down Expand Up @@ -991,16 +985,22 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
//// issue here: it doesn't know the version
unsigned int nTxPos = pindex->nBlockPos + ::GetSerializeSize(CBlock(), SER_DISK) - 1 + GetSizeOfCompactSize(vtx.size());

map<uint256, CTxIndex> mapUnused;
map<uint256, CTxIndex> mapQueuedChanges;
int64 nFees = 0;
BOOST_FOREACH(CTransaction& tx, vtx)
{
CDiskTxPos posThisTx(pindex->nFile, pindex->nBlockPos, nTxPos);
nTxPos += ::GetSerializeSize(tx, SER_DISK);

if (!tx.ConnectInputs(txdb, mapUnused, posThisTx, pindex, nFees, true, false))
if (!tx.ConnectInputs(txdb, mapQueuedChanges, posThisTx, pindex, nFees, true, false))
return false;
}
// Write queued txindex changes
for (map<uint256, CTxIndex>::iterator mi = mapQueuedChanges.begin(); mi != mapQueuedChanges.end(); ++mi)
{
if (!txdb.UpdateTxIndex((*mi).first, (*mi).second))
return error("ConnectBlock() : UpdateTxIndex failed");
}

if (vtx[0].GetValueOut() > GetBlockValue(pindex->nHeight, nFees))
return false;
Expand Down

0 comments on commit e4626e9

Please sign in to comment.