aboutsummaryrefslogtreecommitdiff
path: root/src/policy
diff options
context:
space:
mode:
authorglozow <gloriajzhao@gmail.com>2021-07-16 10:29:11 +0100
committerglozow <gloriajzhao@gmail.com>2021-11-29 12:46:58 +0000
commit9b2fdca7f03911ac40fe0f8a0b5da534bee4554b (patch)
tree8ac96fe74605aaf0d0288ef6ae25b0ddb3bb6435 /src/policy
parent8ae4ba481ce8f7da173bef24432729c87a36cb70 (diff)
downloadbitcoin-9b2fdca7f03911ac40fe0f8a0b5da534bee4554b.tar.xz
[packages] add static IsChildWithParents function
Diffstat (limited to 'src/policy')
-rw-r--r--src/policy/packages.cpp17
-rw-r--r--src/policy/packages.h6
2 files changed, 23 insertions, 0 deletions
diff --git a/src/policy/packages.cpp b/src/policy/packages.cpp
index cfd0539965..21f5488816 100644
--- a/src/policy/packages.cpp
+++ b/src/policy/packages.cpp
@@ -60,3 +60,20 @@ bool CheckPackage(const Package& txns, PackageValidationState& state)
}
return true;
}
+
+bool IsChildWithParents(const Package& package)
+{
+ assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));
+ if (package.size() < 2) return false;
+
+ // The package is expected to be sorted, so the last transaction is the child.
+ const auto& child = package.back();
+ std::unordered_set<uint256, SaltedTxidHasher> input_txids;
+ std::transform(child->vin.cbegin(), child->vin.cend(),
+ std::inserter(input_txids, input_txids.end()),
+ [](const auto& input) { return input.prevout.hash; });
+
+ // Every transaction must be a parent of the last transaction in the package.
+ return std::all_of(package.cbegin(), package.cend() - 1,
+ [&input_txids](const auto& ptx) { return input_txids.count(ptx->GetHash()) > 0; });
+}
diff --git a/src/policy/packages.h b/src/policy/packages.h
index 6b7ac3e450..d2744f1265 100644
--- a/src/policy/packages.h
+++ b/src/policy/packages.h
@@ -41,4 +41,10 @@ class PackageValidationState : public ValidationState<PackageValidationResult> {
*/
bool CheckPackage(const Package& txns, PackageValidationState& state);
+/** Context-free check that a package is exactly one child and its parents; not all parents need to
+ * be present, but the package must not contain any transactions that are not the child's parents.
+ * It is expected to be sorted, which means the last transaction must be the child.
+ */
+bool IsChildWithParents(const Package& package);
+
#endif // BITCOIN_POLICY_PACKAGES_H