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) 2022-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_TRUC_POLICY_H6#define BITCOIN_POLICY_TRUC_POLICY_H8#include <consensus/amount.h>9#include <policy/packages.h>10#include <policy/policy.h>11#include <primitives/transaction.h>12#include <txmempool.h>13#include <util/result.h>15#include <set>16#include <string>18// This module enforces rules for BIP 431 TRUC transactions which help make19// RBF abilities more robust. A transaction with version=3 is treated as TRUC.20inline constexpr decltype(CTransaction::version) TRUC_VERSION{3};22// TRUC only allows 1 parent and 1 child when unconfirmed. This translates to a descendant set size23// of 2 and ancestor set size of 2.24/** Maximum number of transactions including an unconfirmed tx and its descendants. */25inline constexpr unsigned int TRUC_DESCENDANT_LIMIT{2};26/** Maximum number of transactions including a TRUC tx and all its mempool ancestors. */27inline constexpr unsigned int TRUC_ANCESTOR_LIMIT{2};29/** Maximum sigop-adjusted virtual size of all v3 transactions. */30inline constexpr int64_t TRUC_MAX_VSIZE{10'000};31inline constexpr int64_t TRUC_MAX_WEIGHT{TRUC_MAX_VSIZE * WITNESS_SCALE_FACTOR};32/** Maximum sigop-adjusted virtual size of a tx which spends from an unconfirmed TRUC transaction. */33inline constexpr int64_t TRUC_CHILD_MAX_VSIZE{1000};34inline constexpr int64_t TRUC_CHILD_MAX_WEIGHT{TRUC_CHILD_MAX_VSIZE * WITNESS_SCALE_FACTOR};35// These limits are within the default cluster limits.36static_assert(TRUC_MAX_VSIZE + TRUC_CHILD_MAX_VSIZE <= DEFAULT_CLUSTER_SIZE_LIMIT_KVB * 1000);38/** Must be called for every transaction, even if not TRUC. Not strictly necessary for transactions39* accepted through AcceptMultipleTransactions.40*41* Checks the following rules:42* 1. A TRUC tx must only have TRUC unconfirmed ancestors.43* 2. A non-TRUC tx must only have non-TRUC unconfirmed ancestors.44* 3. A TRUC's ancestor set, including itself, must be within TRUC_ANCESTOR_LIMIT.45* 4. A TRUC's descendant set, including itself, must be within TRUC_DESCENDANT_LIMIT.46* 5. If a TRUC tx has any unconfirmed ancestors, the tx's sigop-adjusted vsize must be within47* TRUC_CHILD_MAX_VSIZE.48* 6. A TRUC tx must be within TRUC_MAX_VSIZE.49*50*51* @param[in] pool A reference to the mempool.52* @param[in] mempool_parents The in-mempool ancestors of ptx.53* @param[in] direct_conflicts In-mempool transactions this tx conflicts with. These conflicts54* are used to more accurately calculate the resulting descendant55* count of in-mempool ancestors.56* @param[in] vsize The sigop-adjusted virtual size of ptx.57*58* @returns 3 possibilities:59* - std::nullopt if all TRUC checks were applied successfully60* - debug string + pointer to a mempool sibling if this transaction would be the second child in a61* 1-parent-1-child cluster; the caller may consider evicting the specified sibling or return an62* error with the debug string.63* - debug string + nullptr if this transaction violates some TRUC rule and sibling eviction is not64* applicable.65*/66std::optional<std::pair<std::string, CTransactionRef>> SingleTRUCChecks(const CTxMemPool& pool, const CTransactionRef& ptx,67const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents,68const std::set<Txid>& direct_conflicts,69int64_t vsize) EXCLUSIVE_LOCKS_REQUIRED(pool.cs);71/** Must be called for every transaction that is submitted within a package, even if not TRUC.72*73* For each transaction in a package:74* If it's not a TRUC transaction, verify it has no direct TRUC parents in the mempool or the package.76* If it is a TRUC transaction, verify that any direct parents in the mempool or the package are TRUC.77* If such a parent exists, verify that parent has no other children in the package or the mempool,78* and that the transaction itself has no children in the package.79*80* If any TRUC violations in the package exist, this test will fail for one of them:81* - if a TRUC transaction T has a parent in the mempool and a child in the package, then PTRUCC(T) will fail82* - if a TRUC transaction T has a parent in the package and a child in the package, then PTRUCC(T) will fail83* - if a TRUC transaction T and a TRUC (sibling) transaction U have some parent in the mempool,84* then PTRUCC(T) and PTRUCC(U) will fail85* - if a TRUC transaction T and a TRUC (sibling) transaction U have some parent in the package,86* then PTRUCC(T) and PTRUCC(U) will fail87* - if a TRUC transaction T has a parent P and a grandparent G in the package, then88* PTRUCC(P) will fail (though PTRUCC(G) and PTRUCC(T) might succeed).89*90* @returns debug string if an error occurs, std::nullopt otherwise.91* */92std::optional<std::string> PackageTRUCChecks(const CTxMemPool& pool, const CTransactionRef& ptx, int64_t vsize,93const Package& package,94const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents) EXCLUSIVE_LOCKS_REQUIRED(pool.cs);96#endif // BITCOIN_POLICY_TRUC_POLICY_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.