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#ifndef BITCOIN_POLICY_PACKAGES_H6#define BITCOIN_POLICY_PACKAGES_H8#include <consensus/consensus.h>9#include <consensus/validation.h>10#include <policy/policy.h>11#include <primitives/transaction.h>12#include <util/hasher.h>14#include <cstdint>15#include <unordered_set>16#include <vector>18/** Default maximum number of transactions in a package. */19inline constexpr uint32_t MAX_PACKAGE_COUNT{25};20/** Default maximum total weight of transactions in a package in weight21to allow for context-less checks. This must allow a superset of sigops22weighted vsize limited transactions to not disallow transactions we would23have otherwise accepted individually. */24inline constexpr uint32_t MAX_PACKAGE_WEIGHT = 404'000;25static_assert(MAX_PACKAGE_WEIGHT >= MAX_STANDARD_TX_WEIGHT);27// Packages are part of a single cluster, so ensure that the package limits are28// set within the mempool's cluster size limits.29static_assert(DEFAULT_CLUSTER_LIMIT >= MAX_PACKAGE_COUNT);30static_assert(MAX_PACKAGE_WEIGHT <= DEFAULT_CLUSTER_SIZE_LIMIT_KVB * WITNESS_SCALE_FACTOR * 1000);32/** A "reason" why a package was invalid. It may be that one or more of the included33* transactions is invalid or the package itself violates our rules.34* We don't distinguish between consensus and policy violations right now.35*/36enum class PackageValidationResult {37PCKG_RESULT_UNSET = 0, //!< Initial value. The package has not yet been rejected.38PCKG_POLICY, //!< The package itself is invalid (e.g. too many transactions).39PCKG_TX, //!< At least one tx is invalid.40PCKG_MEMPOOL_ERROR, //!< Mempool logic error.41};43/** A package is an ordered list of transactions. The transactions cannot conflict with (spend the44* same inputs as) one another. */45using Package = std::vector<CTransactionRef>;47class PackageValidationState : public ValidationState<PackageValidationResult> {};49/** If any direct dependencies exist between transactions (i.e. a child spending the output of a50* parent), checks that all parents appear somewhere in the list before their respective children.51* No other ordering is enforced. This function cannot detect indirect dependencies (e.g. a52* transaction's grandparent if its parent is not present).53* @returns true if sorted. False if any tx spends the output of a tx that appears later in txns.54*/55bool IsTopoSortedPackage(const Package& txns);57/** Checks that these transactions don't conflict, i.e., spend the same prevout. This includes58* checking that there are no duplicate transactions. Since these checks require looking at the inputs59* of a transaction, returns false immediately if any transactions have empty vin.60*61* Does not check consistency of a transaction with oneself; does not check if a transaction spends62* the same prevout multiple times (see bad-txns-inputs-duplicate in CheckTransaction()).63*64* @returns true if there are no conflicts. False if any two transactions spend the same prevout.65* */66bool IsConsistentPackage(const Package& txns);68/** Context-free package policy checks:69* 1. The number of transactions cannot exceed MAX_PACKAGE_COUNT.70* 2. The total weight cannot exceed MAX_PACKAGE_WEIGHT.71* 3. If any dependencies exist between transactions, parents must appear before children.72* 4. Transactions cannot conflict, i.e., spend the same inputs.73*/74bool IsWellFormedPackage(const Package& txns, PackageValidationState& state);76/** Context-free check that a package is exactly one child and its parents; not all parents need to77* be present, but the package must not contain any transactions that are not the child's parents.78* It is expected to be sorted, which means the last transaction must be the child.79*/80bool IsChildWithParents(const Package& package);82/** Context-free check that a package IsChildWithParents() and none of the parents depend on each83* other (the package is a "tree").84*/85bool IsChildWithParentsTree(const Package& package);87/** Get the hash of the concatenated wtxids of transactions, with wtxids88* treated as a little-endian numbers and sorted in ascending numeric order.89*/90uint256 GetPackageHash(const std::vector<CTransactionRef>& transactions);92#endif // BITCOIN_POLICY_PACKAGES_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.