mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-19 14:45:08 +01:00
[policy] detect unsorted packages
This commit is contained in:
parent
9ef643e21b
commit
13650fe2e5
@ -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.
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user