Only store transactions with missing inputs in the orphan pool.

All previous versions of bitcoin could store some types of
invalid transactions in the orphan-transaction list.
This commit is contained in:
Gavin Andresen 2012-01-18 13:36:44 -05:00
parent dc77dce07c
commit 149f580c82
2 changed files with 22 additions and 7 deletions

View file

@ -492,8 +492,11 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi
{ {
MapPrevTx mapInputs; MapPrevTx mapInputs;
map<uint256, CTxIndex> mapUnused; map<uint256, CTxIndex> mapUnused;
if (!FetchInputs(txdb, mapUnused, false, false, mapInputs)) bool fInvalid = false;
if (!FetchInputs(txdb, mapUnused, false, false, mapInputs, fInvalid))
{ {
if (fInvalid)
return error("AcceptToMemoryPool() : FetchInputs found invalid tx %s", hash.ToString().substr(0,10).c_str());
if (pfMissingInputs) if (pfMissingInputs)
*pfMissingInputs = true; *pfMissingInputs = true;
return error("AcceptToMemoryPool() : FetchInputs failed %s", hash.ToString().substr(0,10).c_str()); return error("AcceptToMemoryPool() : FetchInputs failed %s", hash.ToString().substr(0,10).c_str());
@ -546,8 +549,6 @@ bool CTransaction::AcceptToMemoryPool(CTxDB& txdb, bool fCheckInputs, bool* pfMi
// This is done last to help prevent CPU exhaustion denial-of-service attacks. // This is done last to help prevent CPU exhaustion denial-of-service attacks.
if (!ConnectInputs(mapInputs, mapUnused, CDiskTxPos(1,1,1), pindexBest, false, false)) if (!ConnectInputs(mapInputs, mapUnused, CDiskTxPos(1,1,1), pindexBest, false, false))
{ {
if (pfMissingInputs)
*pfMissingInputs = true;
return error("AcceptToMemoryPool() : ConnectInputs failed %s", hash.ToString().substr(0,10).c_str()); return error("AcceptToMemoryPool() : ConnectInputs failed %s", hash.ToString().substr(0,10).c_str());
} }
} }
@ -923,8 +924,14 @@ bool CTransaction::DisconnectInputs(CTxDB& txdb)
bool CTransaction::FetchInputs(CTxDB& txdb, const map<uint256, CTxIndex>& mapTestPool, bool CTransaction::FetchInputs(CTxDB& txdb, const map<uint256, CTxIndex>& mapTestPool,
bool fBlock, bool fMiner, MapPrevTx& inputsRet) bool fBlock, bool fMiner, MapPrevTx& inputsRet, bool& fInvalid)
{ {
// FetchInputs can return false either because we just haven't seen some inputs
// (in which case the transaction should be stored as an orphan)
// or because the transaction is malformed (in which case the transaction should
// be dropped). If tx is definitely invalid, fInvalid will be set to true.
fInvalid = false;
if (IsCoinBase()) if (IsCoinBase())
return true; // Coinbase transactions have no inputs to fetch. return true; // Coinbase transactions have no inputs to fetch.
@ -980,7 +987,12 @@ bool CTransaction::FetchInputs(CTxDB& txdb, const map<uint256, CTxIndex>& mapTes
const CTxIndex& txindex = inputsRet[prevout.hash].first; const CTxIndex& txindex = inputsRet[prevout.hash].first;
const CTransaction& txPrev = inputsRet[prevout.hash].second; const CTransaction& txPrev = inputsRet[prevout.hash].second;
if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size()) if (prevout.n >= txPrev.vout.size() || prevout.n >= txindex.vSpent.size())
{
// Revisit this if/when transaction replacement is implemented and allows
// adding inputs:
fInvalid = true;
return DoS(100, error("FetchInputs() : %s prevout.n out of range %d %d %d prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str())); return DoS(100, error("FetchInputs() : %s prevout.n out of range %d %d %d prev tx %s\n%s", GetHash().ToString().substr(0,10).c_str(), prevout.n, txPrev.vout.size(), txindex.vSpent.size(), prevout.hash.ToString().substr(0,10).c_str(), txPrev.ToString().c_str()));
}
} }
return true; return true;
@ -1203,7 +1215,8 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
MapPrevTx mapInputs; MapPrevTx mapInputs;
if (!tx.IsCoinBase()) if (!tx.IsCoinBase())
{ {
if (!tx.FetchInputs(txdb, mapQueuedChanges, true, false, mapInputs)) bool fInvalid;
if (!tx.FetchInputs(txdb, mapQueuedChanges, true, false, mapInputs, fInvalid))
return false; return false;
int nTxOps = tx.GetSigOpCount(mapInputs); int nTxOps = tx.GetSigOpCount(mapInputs);
@ -3063,7 +3076,8 @@ CBlock* CreateNewBlock(CReserveKey& reservekey)
// because we're already processing them in order of dependency // because we're already processing them in order of dependency
map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool); map<uint256, CTxIndex> mapTestPoolTmp(mapTestPool);
MapPrevTx mapInputs; MapPrevTx mapInputs;
if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs)) bool fInvalid;
if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs, fInvalid))
continue; continue;
int64 nFees = tx.GetValueIn(mapInputs)-tx.GetValueOut(); int64 nFees = tx.GetValueIn(mapInputs)-tx.GetValueOut();

View file

@ -684,10 +684,11 @@ public:
@param[in] fBlock True if being called to add a new best-block to the chain @param[in] fBlock True if being called to add a new best-block to the chain
@param[in] fMiner True if being called by CreateNewBlock @param[in] fMiner True if being called by CreateNewBlock
@param[out] inputsRet Pointers to this transaction's inputs @param[out] inputsRet Pointers to this transaction's inputs
@param[out] fInvalid returns true if transaction is invalid
@return Returns true if all inputs are in txdb or mapTestPool @return Returns true if all inputs are in txdb or mapTestPool
*/ */
bool FetchInputs(CTxDB& txdb, const std::map<uint256, CTxIndex>& mapTestPool, bool FetchInputs(CTxDB& txdb, const std::map<uint256, CTxIndex>& mapTestPool,
bool fBlock, bool fMiner, MapPrevTx& inputsRet); bool fBlock, bool fMiner, MapPrevTx& inputsRet, bool& fInvalid);
/** Sanity check previous transactions, then, if all checks succeed, /** Sanity check previous transactions, then, if all checks succeed,
mark them as spent by this transaction. mark them as spent by this transaction.