Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <policy/packages.h>6#include <policy/policy.h>7#include <primitives/transaction.h>8#include <uint256.h>9#include <util/check.h>11#include <algorithm>12#include <cassert>13#include <iterator>14#include <memory>15#include <numeric>17/** IsTopoSortedPackage where a set of txids has been pre-populated. The set is assumed to be correct and18* is mutated within this function (even if return value is false). */19bool IsTopoSortedPackage(const Package& txns, std::unordered_set<Txid, SaltedTxidHasher>& later_txids)20{21// Avoid misusing this function: later_txids should contain the txids of txns.22Assume(txns.size() == later_txids.size());24// later_txids always contains the txids of this transaction and the ones that come later in25// txns. If any transaction's input spends a tx in that set, we've found a parent placed later26// than its child.27for (const auto& tx : txns) {28for (const auto& input : tx->vin) {29if (later_txids.contains(input.prevout.hash)) {30// The parent is a subsequent transaction in the package.31return false;32}33}34// Avoid misusing this function: later_txids must contain every tx.35Assume(later_txids.erase(tx->GetHash()) == 1);36}38// Avoid misusing this function: later_txids should have contained the txids of txns.39Assume(later_txids.empty());40return true;41}43bool IsTopoSortedPackage(const Package& txns)44{45std::unordered_set<Txid, SaltedTxidHasher> later_txids;46std::transform(txns.cbegin(), txns.cend(), std::inserter(later_txids, later_txids.end()),47[](const auto& tx) { return tx->GetHash(); });49return IsTopoSortedPackage(txns, later_txids);50}52bool IsConsistentPackage(const Package& txns)53{54// Don't allow any conflicting transactions, i.e. spending the same inputs, in a package.55std::unordered_set<COutPoint, SaltedOutpointHasher> inputs_seen;56for (const auto& tx : txns) {57if (tx->vin.empty()) {58// This function checks consistency based on inputs, and we can't do that if there are59// no inputs. Duplicate empty transactions are also not consistent with one another.60// This doesn't create false negatives, as unconfirmed transactions are not allowed to61// have no inputs.62return false;63}64for (const auto& input : tx->vin) {65if (inputs_seen.contains(input.prevout)) {66// This input is also present in another tx in the package.67return false;68}69}70// Batch-add all the inputs for a tx at a time. If we added them 1 at a time, we could71// catch duplicate inputs within a single tx. This is a more severe, consensus error,72// and we want to report that from CheckTransaction instead.73std::transform(tx->vin.cbegin(), tx->vin.cend(), std::inserter(inputs_seen, inputs_seen.end()),74[](const auto& input) { return input.prevout; });75}76return true;77}79bool IsWellFormedPackage(const Package& txns, PackageValidationState& state)80{81const unsigned int package_count = txns.size();83if (package_count > MAX_PACKAGE_COUNT) {84return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-many-transactions");85}87const int64_t total_weight = std::accumulate(txns.cbegin(), txns.cend(), 0,88[](int64_t sum, const auto& tx) { return sum + GetTransactionWeight(*tx); });89// If the package only contains 1 tx, it's better to report the policy violation on individual tx weight.90if (package_count > 1 && total_weight > MAX_PACKAGE_WEIGHT) {91return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-too-large");92}94std::unordered_set<Txid, SaltedTxidHasher> later_txids;95std::transform(txns.cbegin(), txns.cend(), std::inserter(later_txids, later_txids.end()),96[](const auto& tx) { return tx->GetHash(); });98// Package must not contain any duplicate transactions, which is checked by txid. This also99// includes transactions with duplicate wtxids and same-txid-different-witness transactions.100if (later_txids.size() != txns.size()) {101return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-contains-duplicates");102}104// Require the package to be sorted in order of dependency, i.e. parents appear before children.105// An unsorted package will fail anyway on missing-inputs, but it's better to quit earlier and106// fail on something less ambiguous (missing-inputs could also be an orphan or trying to107// spend nonexistent coins).108if (!IsTopoSortedPackage(txns, later_txids)) {109return state.Invalid(PackageValidationResult::PCKG_POLICY, "package-not-sorted");110}112// Don't allow any conflicting transactions, i.e. spending the same inputs, in a package.113if (!IsConsistentPackage(txns)) {114return state.Invalid(PackageValidationResult::PCKG_POLICY, "conflict-in-package");115}116return true;117}119bool IsChildWithParents(const Package& package)120{121assert(std::all_of(package.cbegin(), package.cend(), [](const auto& tx){return tx != nullptr;}));122if (package.size() < 2) return false;124// The package is expected to be sorted, so the last transaction is the child.125const auto& child = package.back();126std::unordered_set<Txid, SaltedTxidHasher> input_txids;127std::transform(child->vin.cbegin(), child->vin.cend(),128std::inserter(input_txids, input_txids.end()),129[](const auto& input) { return input.prevout.hash; });131// Every transaction must be a parent of the last transaction in the package.132return std::all_of(package.cbegin(), package.cend() - 1,133[&input_txids](const auto& ptx) { return input_txids.contains(ptx->GetHash()); });134}136bool IsChildWithParentsTree(const Package& package)137{138if (!IsChildWithParents(package)) return false;139std::unordered_set<Txid, SaltedTxidHasher> parent_txids;140std::transform(package.cbegin(), package.cend() - 1, std::inserter(parent_txids, parent_txids.end()),141[](const auto& ptx) { return ptx->GetHash(); });142// Each parent must not have an input who is one of the other parents.143return std::all_of(package.cbegin(), package.cend() - 1, [&](const auto& ptx) {144for (const auto& input : ptx->vin) {145if (parent_txids.contains(input.prevout.hash)) return false;146}147return true;148});149}151uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions)152{153// Create a vector of the wtxids.154std::vector<Wtxid> wtxids_copy;155std::transform(transactions.cbegin(), transactions.cend(), std::back_inserter(wtxids_copy),156[](const auto& tx){ return tx->GetWitnessHash(); });158// Sort in ascending order159std::sort(wtxids_copy.begin(), wtxids_copy.end(), [](const auto& lhs, const auto& rhs) {160return std::lexicographical_compare(std::make_reverse_iterator(lhs.end()), std::make_reverse_iterator(lhs.begin()),161std::make_reverse_iterator(rhs.end()), std::make_reverse_iterator(rhs.begin()));162});164// Get sha256 hash of the wtxids concatenated in this order165HashWriter hashwriter;166for (const auto& wtxid : wtxids_copy) {167hashwriter << wtxid;168}169return hashwriter.GetSHA256();170}