| 2063 | } |
| 2064 | |
| 2065 | bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean) |
| 2066 | { |
| 2067 | assert(pindex->GetBlockHash() == view.GetBestBlock()); |
| 2068 | |
| 2069 | if (pfClean) |
| 2070 | *pfClean = false; |
| 2071 | |
| 2072 | bool fClean = true; |
| 2073 | |
| 2074 | CBlockUndo blockUndo; |
| 2075 | CDiskBlockPos pos = pindex->GetUndoPos(); |
| 2076 | if (pos.IsNull()) |
| 2077 | return error("DisconnectBlock(): no undo data available"); |
| 2078 | if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash())) |
| 2079 | return error("DisconnectBlock(): failure reading undo data"); |
| 2080 | |
| 2081 | if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) |
| 2082 | return error("DisconnectBlock(): block and undo data inconsistent"); |
| 2083 | |
| 2084 | // undo transactions in reverse order |
| 2085 | for (int i = block.vtx.size() - 1; i >= 0; i--) { |
| 2086 | const CTransaction &tx = block.vtx[i]; |
| 2087 | uint256 hash = tx.GetHash(); |
| 2088 | |
| 2089 | // Check that all outputs are available and match the outputs in the block itself |
| 2090 | // exactly. |
| 2091 | { |
| 2092 | CCoinsModifier outs = view.ModifyCoins(hash); |
| 2093 | outs->ClearUnspendable(); |
| 2094 | |
| 2095 | CCoins outsBlock(tx, pindex->nHeight); |
| 2096 | // The CCoins serialization does not serialize negative numbers. |
| 2097 | // No network rules currently depend on the version here, so an inconsistency is harmless |
| 2098 | // but it must be corrected before txout nversion ever influences a network rule. |
| 2099 | if (outsBlock.nVersion < 0) |
| 2100 | outs->nVersion = outsBlock.nVersion; |
| 2101 | if (*outs != outsBlock) |
| 2102 | fClean = fClean && error("DisconnectBlock(): added transaction mismatch? database corrupted"); |
| 2103 | |
| 2104 | // remove outputs |
| 2105 | outs->Clear(); |
| 2106 | } |
| 2107 | |
| 2108 | // restore inputs |
| 2109 | if (i > 0) { // not coinbases |
| 2110 | const CTxUndo &txundo = blockUndo.vtxundo[i-1]; |
| 2111 | if (txundo.vprevout.size() != tx.vin.size()) |
| 2112 | return error("DisconnectBlock(): transaction and undo data inconsistent"); |
| 2113 | for (unsigned int j = tx.vin.size(); j-- > 0;) { |
| 2114 | const COutPoint &out = tx.vin[j].prevout; |
| 2115 | const CTxInUndo &undo = txundo.vprevout[j]; |
| 2116 | if (!ApplyTxInUndo(undo, view, out)) |
| 2117 | fClean = false; |
| 2118 | } |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | // move best block pointer to prevout block |
no test coverage detected