|
|
@ -498,8 +498,8 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C |
|
|
|
// during reorgs to ensure COINBASE_MATURITY is still met.
|
|
|
|
// during reorgs to ensure COINBASE_MATURITY is still met.
|
|
|
|
bool fSpendsCoinbase = false; |
|
|
|
bool fSpendsCoinbase = false; |
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.vin) { |
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.vin) { |
|
|
|
const CCoins *coins = view.AccessCoins(txin.prevout.hash); |
|
|
|
const Coin &coin = view.AccessCoin(txin.prevout); |
|
|
|
if (coins->IsCoinBase()) { |
|
|
|
if (coin.IsCoinBase()) { |
|
|
|
fSpendsCoinbase = true; |
|
|
|
fSpendsCoinbase = true; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -818,15 +818,8 @@ bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
|
|
|
|
if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
|
|
|
|
int nHeight = -1; |
|
|
|
const Coin& coin = AccessByTxid(*pcoinsTip, hash); |
|
|
|
{ |
|
|
|
if (!coin.IsPruned()) pindexSlow = chainActive[coin.nHeight]; |
|
|
|
const CCoinsViewCache& view = *pcoinsTip; |
|
|
|
|
|
|
|
const CCoins* coins = view.AccessCoins(hash); |
|
|
|
|
|
|
|
if (coins) |
|
|
|
|
|
|
|
nHeight = coins->nHeight; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (nHeight > 0) |
|
|
|
|
|
|
|
pindexSlow = chainActive[nHeight]; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (pindexSlow) { |
|
|
|
if (pindexSlow) { |
|
|
@ -1074,19 +1067,12 @@ void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txund |
|
|
|
if (!tx.IsCoinBase()) { |
|
|
|
if (!tx.IsCoinBase()) { |
|
|
|
txundo.vprevout.reserve(tx.vin.size()); |
|
|
|
txundo.vprevout.reserve(tx.vin.size()); |
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.vin) { |
|
|
|
BOOST_FOREACH(const CTxIn &txin, tx.vin) { |
|
|
|
CCoinsModifier coins = inputs.ModifyCoins(txin.prevout.hash); |
|
|
|
txundo.vprevout.emplace_back(); |
|
|
|
unsigned nPos = txin.prevout.n; |
|
|
|
inputs.SpendCoin(txin.prevout, &txundo.vprevout.back()); |
|
|
|
|
|
|
|
|
|
|
|
if (nPos >= coins->vout.size() || coins->vout[nPos].IsNull()) |
|
|
|
|
|
|
|
assert(false); |
|
|
|
|
|
|
|
// mark an outpoint spent, and construct undo information
|
|
|
|
|
|
|
|
txundo.vprevout.emplace_back(coins->vout[nPos], coins->nHeight, coins->fCoinBase); |
|
|
|
|
|
|
|
bool ret = coins->Spend(nPos); |
|
|
|
|
|
|
|
assert(ret); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// add outputs
|
|
|
|
// add outputs
|
|
|
|
inputs.ModifyNewCoins(tx.GetHash(), tx.IsCoinBase())->FromTx(tx, nHeight); |
|
|
|
AddCoins(inputs, tx, nHeight); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight) |
|
|
|
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight) |
|
|
@ -1260,24 +1246,21 @@ int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out) |
|
|
|
{ |
|
|
|
{ |
|
|
|
bool fClean = true; |
|
|
|
bool fClean = true; |
|
|
|
|
|
|
|
|
|
|
|
CCoinsModifier coins = view.ModifyCoins(out.hash); |
|
|
|
if (view.HaveCoins(out)) fClean = false; // overwriting transaction output
|
|
|
|
if (undo.nHeight != 0) { |
|
|
|
|
|
|
|
if (!coins->IsPruned()) { |
|
|
|
if (undo.nHeight == 0) { |
|
|
|
if (coins->fCoinBase != undo.fCoinBase || (uint32_t)coins->nHeight != undo.nHeight) fClean = false; // metadata mismatch
|
|
|
|
// Missing undo metadata (height and coinbase). Older versions included this
|
|
|
|
} |
|
|
|
// information only in undo records for the last spend of a transactions'
|
|
|
|
// restore height/coinbase tx metadata from undo data
|
|
|
|
// outputs. This implies that it must be present for some other output of the same tx.
|
|
|
|
coins->fCoinBase = undo.fCoinBase; |
|
|
|
const Coin& alternate = AccessByTxid(view, out.hash); |
|
|
|
coins->nHeight = undo.nHeight; |
|
|
|
if (!alternate.IsPruned()) { |
|
|
|
|
|
|
|
undo.nHeight = alternate.nHeight; |
|
|
|
|
|
|
|
undo.fCoinBase = alternate.fCoinBase; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// Undo data does not contain height/coinbase. This should never happen
|
|
|
|
return DISCONNECT_FAILED; // adding output for transaction without known metadata
|
|
|
|
// for newly created undo entries. Previously, this data was only saved
|
|
|
|
} |
|
|
|
// for the last spend of a transaction's outputs, so check IsPruned().
|
|
|
|
|
|
|
|
if (coins->IsPruned()) fClean = false; // adding output to missing transaction
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (coins->IsAvailable(out.n)) fClean = false; // overwriting existing output
|
|
|
|
view.AddCoin(out, std::move(undo), undo.fCoinBase); |
|
|
|
if (coins->vout.size() < out.n+1) |
|
|
|
|
|
|
|
coins->vout.resize(out.n+1); |
|
|
|
|
|
|
|
coins->vout[out.n] = std::move(undo.out); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN; |
|
|
|
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN; |
|
|
|
} |
|
|
|
} |
|
|
@ -1313,15 +1296,15 @@ static DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* |
|
|
|
|
|
|
|
|
|
|
|
// Check that all outputs are available and match the outputs in the block itself
|
|
|
|
// Check that all outputs are available and match the outputs in the block itself
|
|
|
|
// exactly.
|
|
|
|
// exactly.
|
|
|
|
{ |
|
|
|
for (size_t o = 0; o < tx.vout.size(); o++) { |
|
|
|
CCoinsModifier outs = view.ModifyCoins(hash); |
|
|
|
if (!tx.vout[o].scriptPubKey.IsUnspendable()) { |
|
|
|
outs->ClearUnspendable(); |
|
|
|
COutPoint out(hash, o); |
|
|
|
|
|
|
|
Coin coin; |
|
|
|
CCoins outsBlock(tx, pindex->nHeight); |
|
|
|
view.SpendCoin(out, &coin); |
|
|
|
if (*outs != outsBlock) fClean = false; // transaction mismatch
|
|
|
|
if (tx.vout[o] != coin.out) { |
|
|
|
|
|
|
|
fClean = false; // transaction output mismatch
|
|
|
|
// remove outputs
|
|
|
|
} |
|
|
|
outs->Clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// restore inputs
|
|
|
|
// restore inputs
|
|
|
@ -1518,12 +1501,14 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd |
|
|
|
|
|
|
|
|
|
|
|
if (fEnforceBIP30) { |
|
|
|
if (fEnforceBIP30) { |
|
|
|
for (const auto& tx : block.vtx) { |
|
|
|
for (const auto& tx : block.vtx) { |
|
|
|
const CCoins* coins = view.AccessCoins(tx->GetHash()); |
|
|
|
for (size_t o = 0; o < tx->vout.size(); o++) { |
|
|
|
if (coins && !coins->IsPruned()) |
|
|
|
if (view.HaveCoins(COutPoint(tx->GetHash(), o))) { |
|
|
|
return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"), |
|
|
|
return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"), |
|
|
|
REJECT_INVALID, "bad-txns-BIP30"); |
|
|
|
REJECT_INVALID, "bad-txns-BIP30"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// BIP16 didn't become active until Apr 1 2012
|
|
|
|
// BIP16 didn't become active until Apr 1 2012
|
|
|
|
int64_t nBIP16SwitchTime = 1333238400; |
|
|
|
int64_t nBIP16SwitchTime = 1333238400; |
|
|
@ -1588,7 +1573,7 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd |
|
|
|
// be in ConnectBlock because they require the UTXO set
|
|
|
|
// be in ConnectBlock because they require the UTXO set
|
|
|
|
prevheights.resize(tx.vin.size()); |
|
|
|
prevheights.resize(tx.vin.size()); |
|
|
|
for (size_t j = 0; j < tx.vin.size(); j++) { |
|
|
|
for (size_t j = 0; j < tx.vin.size(); j++) { |
|
|
|
prevheights[j] = view.AccessCoins(tx.vin[j].prevout.hash)->nHeight; |
|
|
|
prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) { |
|
|
|
if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) { |
|
|
|