diff options
-rw-r--r-- | src/validation.cpp | 26 | ||||
-rwxr-xr-x | test/functional/rpc_packages.py | 6 |
2 files changed, 24 insertions, 8 deletions
diff --git a/src/validation.cpp b/src/validation.cpp index 07308aab39..ebe88ba04d 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -42,6 +42,7 @@ #include <uint256.h> #include <undo.h> #include <util/check.h> // For NDEBUG compile time check +#include <util/hasher.h> #include <util/moneystr.h> #include <util/rbf.h> #include <util/strencodings.h> @@ -1093,12 +1094,29 @@ PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(const std:: return PackageMempoolAcceptResult(package_state, {}); } + // Construct workspaces and check package policies. std::vector<Workspace> workspaces{}; workspaces.reserve(package_count); - std::transform(txns.cbegin(), txns.cend(), std::back_inserter(workspaces), [](const auto& tx) { - return Workspace(tx); - }); - + { + std::unordered_set<uint256, SaltedTxidHasher> later_txids; + std::transform(txns.cbegin(), txns.cend(), std::inserter(later_txids, later_txids.end()), + [](const auto& tx) { return tx->GetHash(); }); + // Require the package to be sorted in order of dependency, i.e. parents appear before children. + // An unsorted package will fail anyway on missing-inputs, but it's better to quit earlier and + // fail on something less ambiguous (missing-inputs could also be an orphan or trying to + // spend nonexistent coins). + for (const auto& tx : txns) { + for (const auto& input : tx->vin) { + if (later_txids.find(input.prevout.hash) != later_txids.end()) { + // The parent is a subsequent transaction in the package. + package_state.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-sorted"); + return PackageMempoolAcceptResult(package_state, {}); + } + } + later_txids.erase(tx->GetHash()); + workspaces.emplace_back(Workspace(tx)); + } + } std::map<const uint256, const MempoolAcceptResult> results; { // Don't allow any conflicting transactions, i.e. spending the same inputs, in a package. diff --git a/test/functional/rpc_packages.py b/test/functional/rpc_packages.py index 79e8d8e3ce..3d8d81d6b8 100755 --- a/test/functional/rpc_packages.py +++ b/test/functional/rpc_packages.py @@ -169,10 +169,8 @@ class RPCPackagesTest(BitcoinTestFramework): chain_txns.append(tx) self.log.info("Check that testmempoolaccept requires packages to be sorted by dependency") - testres_multiple_unsorted = node.testmempoolaccept(rawtxs=chain_hex[::-1]) - assert_equal(testres_multiple_unsorted, - [{"txid": chain_txns[-1].rehash(), "wtxid": chain_txns[-1].getwtxid(), "allowed": False, "reject-reason": "missing-inputs"}] - + [{"txid": tx.rehash(), "wtxid": tx.getwtxid()} for tx in chain_txns[::-1]][1:]) + assert_equal(node.testmempoolaccept(rawtxs=chain_hex[::-1]), + [{"txid": tx.rehash(), "wtxid": tx.getwtxid(), "package-error": "package-not-sorted"} for tx in chain_txns[::-1]]) self.log.info("Testmempoolaccept a chain of 25 transactions") testres_multiple = node.testmempoolaccept(rawtxs=chain_hex) |