mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
[packages] add static IsChildWithParents function
This commit is contained in:
parent
8ae4ba481c
commit
9b2fdca7f0
2 changed files with 23 additions and 0 deletions
|
@ -60,3 +60,20 @@ bool CheckPackage(const Package& txns, PackageValidationState& state)
|
||||||
}
|
}
|
||||||
return true;
|
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; });
|
||||||
|
}
|
||||||
|
|
|
@ -41,4 +41,10 @@ class PackageValidationState : public ValidationState<PackageValidationResult> {
|
||||||
*/
|
*/
|
||||||
bool CheckPackage(const Package& txns, PackageValidationState& state);
|
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
|
#endif // BITCOIN_POLICY_PACKAGES_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue