| 1342 | } |
| 1343 | |
| 1344 | PackageMempoolAcceptResult MemPoolAccept::AcceptPackage(const Package& package, ATMPArgs& args) |
| 1345 | { |
| 1346 | AssertLockHeld(cs_main); |
| 1347 | PackageValidationState package_state; |
| 1348 | |
| 1349 | // Check that the package is well-formed. If it isn't, we won't try to validate any of the |
| 1350 | // transactions and thus won't return any MempoolAcceptResults, just a package-wide error. |
| 1351 | |
| 1352 | // Context-free package checks. |
| 1353 | if (!CheckPackage(package, package_state)) return PackageMempoolAcceptResult(package_state, {}); |
| 1354 | |
| 1355 | // All transactions in the package must be a parent of the last transaction. This is just an |
| 1356 | // opportunity for us to fail fast on a context-free check without taking the mempool lock. |
| 1357 | if (!IsChildWithParents(package)) { |
| 1358 | package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-child-with-parents"); |
| 1359 | return PackageMempoolAcceptResult(package_state, {}); |
| 1360 | } |
| 1361 | |
| 1362 | // IsChildWithParents() guarantees the package is > 1 transactions. |
| 1363 | assert(package.size() > 1); |
| 1364 | // The package must be 1 child with all of its unconfirmed parents. The package is expected to |
| 1365 | // be sorted, so the last transaction is the child. |
| 1366 | const auto& child = package.back(); |
| 1367 | std::unordered_set<uint256, SaltedTxidHasher> unconfirmed_parent_txids; |
| 1368 | std::transform(package.cbegin(), package.cend() - 1, |
| 1369 | std::inserter(unconfirmed_parent_txids, unconfirmed_parent_txids.end()), |
| 1370 | [](const auto& tx) { return tx->GetHash(); }); |
| 1371 | |
| 1372 | // All child inputs must refer to a preceding package transaction or a confirmed UTXO. The only |
| 1373 | // way to verify this is to look up the child's inputs in our current coins view (not including |
| 1374 | // mempool), and enforce that all parents not present in the package be available at chain tip. |
| 1375 | // Since this check can bring new coins into the coins cache, keep track of these coins and |
| 1376 | // uncache them if we don't end up submitting this package to the mempool. |
| 1377 | const CCoinsViewCache& coins_tip_cache = m_active_chainstate.CoinsTip(); |
| 1378 | for (const auto& input : child->vin) { |
| 1379 | if (!coins_tip_cache.HaveCoinInCache(input.prevout)) { |
| 1380 | args.m_coins_to_uncache.push_back(input.prevout); |
| 1381 | } |
| 1382 | } |
| 1383 | // Using the MemPoolAccept m_view cache allows us to look up these same coins faster later. |
| 1384 | // This should be connecting directly to CoinsTip, not to m_viewmempool, because we specifically |
| 1385 | // require inputs to be confirmed if they aren't in the package. |
| 1386 | m_view.SetBackend(m_active_chainstate.CoinsTip()); |
| 1387 | const auto package_or_confirmed = [this, &unconfirmed_parent_txids](const auto& input) { |
| 1388 | return unconfirmed_parent_txids.count(input.prevout.hash) > 0 || m_view.HaveCoin(input.prevout); |
| 1389 | }; |
| 1390 | if (!std::all_of(child->vin.cbegin(), child->vin.cend(), package_or_confirmed)) { |
| 1391 | package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-child-with-unconfirmed-parents"); |
| 1392 | return PackageMempoolAcceptResult(package_state, {}); |
| 1393 | } |
| 1394 | // Protect against bugs where we pull more inputs from disk that miss being added to |
| 1395 | // coins_to_uncache. The backend will be connected again when needed in PreChecks. |
| 1396 | m_view.SetBackend(m_dummy); |
| 1397 | |
| 1398 | LOCK(m_pool.cs); |
| 1399 | std::map<const uint256, const MempoolAcceptResult> results; |
| 1400 | // Node operators are free to set their mempool policies however they please, nodes may receive |
| 1401 | // transactions in different orders, and malicious counterparties may try to take advantage of |
no test coverage detected