diff options
author | glozow <gloriajzhao@gmail.com> | 2024-04-16 11:58:25 +0100 |
---|---|---|
committer | glozow <gloriajzhao@gmail.com> | 2024-04-26 10:28:27 +0100 |
commit | 092c978a42e8f4a02291b994713505ba8aac8b28 (patch) | |
tree | 7e267f6faf0fe2aae3eb9a30a472720cecac952d /src/policy | |
parent | c3c1e15831c463df7968b028a77e787da7e6256d (diff) |
[txpackages] add canonical way to get hash of package
Diffstat (limited to 'src/policy')
-rw-r--r-- | src/policy/packages.cpp | 18 | ||||
-rw-r--r-- | src/policy/packages.h | 5 |
2 files changed, 23 insertions, 0 deletions
diff --git a/src/policy/packages.cpp b/src/policy/packages.cpp index 3a63a9fe46..99d2a6d514 100644 --- a/src/policy/packages.cpp +++ b/src/policy/packages.cpp @@ -147,3 +147,21 @@ bool IsChildWithParentsTree(const Package& package) return true; }); } + +uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions) +{ + // Create a vector of the wtxids. + std::vector<Wtxid> wtxids_copy; + std::transform(transactions.cbegin(), transactions.cend(), std::back_inserter(wtxids_copy), + [](const auto& tx){ return tx->GetWitnessHash(); }); + + // Sort in ascending order + std::sort(wtxids_copy.begin(), wtxids_copy.end(), [](const auto& lhs, const auto& rhs) { return lhs.GetHex() < rhs.GetHex(); }); + + // Get sha256 hash of the wtxids concatenated in this order + HashWriter hashwriter; + for (const auto& wtxid : wtxids_copy) { + hashwriter << wtxid; + } + return hashwriter.GetSHA256(); +} diff --git a/src/policy/packages.h b/src/policy/packages.h index 537d8476e2..3050320122 100644 --- a/src/policy/packages.h +++ b/src/policy/packages.h @@ -88,4 +88,9 @@ bool IsChildWithParents(const Package& package); * other (the package is a "tree"). */ bool IsChildWithParentsTree(const Package& package); + +/** Get the hash of these transactions' wtxids, concatenated in lexicographical order (treating the + * wtxids as little endian encoded uint256, smallest to largest). */ +uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions); + #endif // BITCOIN_POLICY_PACKAGES_H |