| 180 | } |
| 181 | |
| 182 | ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<CTransactionRef>& vtx_missing, bool check_pow) { |
| 183 | assert(!header.IsNull()); |
| 184 | uint256 hash = header.GetHash(); |
| 185 | block = header; |
| 186 | block.vtx.resize(txn_available.size()); |
| 187 | |
| 188 | size_t tx_missing_offset = 0; |
| 189 | for (size_t i = 0; i < txn_available.size(); i++) { |
| 190 | if (!txn_available[i]) { |
| 191 | if (vtx_missing.size() <= tx_missing_offset) |
| 192 | return READ_STATUS_INVALID; |
| 193 | block.vtx[i] = vtx_missing[tx_missing_offset++]; |
| 194 | } else |
| 195 | block.vtx[i] = std::move(txn_available[i]); |
| 196 | } |
| 197 | |
| 198 | // Make sure we can't call FillBlock again. |
| 199 | header.SetNull(); |
| 200 | txn_available.clear(); |
| 201 | |
| 202 | if (vtx_missing.size() != tx_missing_offset) |
| 203 | return READ_STATUS_INVALID; |
| 204 | |
| 205 | BlockValidationState state; |
| 206 | if (!CheckBlock(block, state, Params().GetConsensus(), check_pow)) { |
| 207 | // TODO: We really want to just check merkle tree manually here, |
| 208 | // but that is expensive, and CheckBlock caches a block's |
| 209 | // "checked-status" (in the CBlock?). CBlock should be able to |
| 210 | // check its own merkle root and cache that check. |
| 211 | if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) |
| 212 | return READ_STATUS_FAILED; // Possible Short ID collision |
| 213 | return READ_STATUS_CHECKBLOCK_FAILED; |
| 214 | } |
| 215 | |
| 216 | LogPrint(BCLog::CMPCTBLOCK, "Successfully reconstructed block %s with %lu txn prefilled, %lu txn from mempool (incl at least %lu from extra pool) and %lu txn requested\n", hash.ToString(), prefilled_count, mempool_count, extra_count, vtx_missing.size()); |
| 217 | if (vtx_missing.size() < 5) { |
| 218 | for (const auto& tx : vtx_missing) { |
| 219 | LogPrint(BCLog::CMPCTBLOCK, "Reconstructed block %s required tx %s\n", hash.ToString(), tx->GetHash().ToString()); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return READ_STATUS_OK; |
| 224 | } |