Skip to content

Commit 9b2fdca

Browse files
committed
[packages] add static IsChildWithParents function
1 parent 8ae4ba4 commit 9b2fdca

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/policy/packages.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,20 @@ bool CheckPackage(const Package& txns, PackageValidationState& state)
6060
}
6161
return true;
6262
}
63+
64+
bool IsChildWithParents(const Package& package)
65+
{
66+
assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));
67+
if (package.size() < 2) return false;
68+
69+
// The package is expected to be sorted, so the last transaction is the child.
70+
const auto& child = package.back();
71+
std::unordered_set<uint256, SaltedTxidHasher> input_txids;
72+
std::transform(child->vin.cbegin(), child->vin.cend(),
73+
std::inserter(input_txids, input_txids.end()),
74+
[](const auto& input) { return input.prevout.hash; });
75+
76+
// Every transaction must be a parent of the last transaction in the package.
77+
return std::all_of(package.cbegin(), package.cend() - 1,
78+
[&input_txids](const auto& ptx) { return input_txids.count(ptx->GetHash()) > 0; });
79+
}

src/policy/packages.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ class PackageValidationState : public ValidationState<PackageValidationResult> {
4141
*/
4242
bool CheckPackage(const Package& txns, PackageValidationState& state);
4343

44+
/** Context-free check that a package is exactly one child and its parents; not all parents need to
45+
* be present, but the package must not contain any transactions that are not the child's parents.
46+
* It is expected to be sorted, which means the last transaction must be the child.
47+
*/
48+
bool IsChildWithParents(const Package& package);
49+
4450
#endif // BITCOIN_POLICY_PACKAGES_H

0 commit comments

Comments
 (0)