fuzz coverage

Coverage Report

Created: 2026-04-24 13:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/policy/truc_policy.cpp
Line
Count
Source
1
// Copyright (c) 2022-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <policy/truc_policy.h>
6
7
#include <coins.h>
8
#include <consensus/amount.h>
9
#include <tinyformat.h>
10
#include <util/check.h>
11
12
#include <algorithm>
13
#include <numeric>
14
#include <vector>
15
16
/** Helper for PackageTRUCChecks: Returns a vector containing the indices of transactions (within
17
 * package) that are direct parents of ptx. */
18
std::vector<size_t> FindInPackageParents(const Package& package, const CTransactionRef& ptx)
19
148k
{
20
148k
    std::vector<size_t> in_package_parents;
21
22
148k
    std::set<Txid> possible_parents;
23
148k
    for (auto &input : ptx->vin) {
24
148k
        possible_parents.insert(input.prevout.hash);
25
148k
    }
26
27
178k
    for (size_t i{0}; i < package.size(); 
++i30.6k
) {
28
178k
        const auto& tx = package.at(i);
29
        // We assume the package is sorted, so that we don't need to continue
30
        // looking past the transaction itself.
31
178k
        if (&(*tx) == &(*ptx)) 
break148k
;
32
30.6k
        if (possible_parents.contains(tx->GetHash())) {
33
30.6k
            in_package_parents.push_back(i);
34
30.6k
        }
35
30.6k
    }
36
148k
    return in_package_parents;
37
148k
}
38
39
/** Helper for PackageTRUCChecks, storing info for a mempool or package parent. */
40
struct ParentInfo {
41
    /** Txid used to identify this parent by prevout */
42
    const Txid& m_txid;
43
    /** Wtxid used for debug string */
44
    const Wtxid& m_wtxid;
45
    /** version used to check inheritance of TRUC and non-TRUC */
46
    decltype(CTransaction::version) m_version;
47
    /** If parent is in mempool, whether it has any descendants in mempool. */
48
    bool m_has_mempool_descendant;
49
50
    ParentInfo() = delete;
51
    ParentInfo(const Txid& txid, const Wtxid& wtxid, decltype(CTransaction::version) version, bool has_mempool_descendant) :
52
90.6k
        m_txid{txid}, m_wtxid{wtxid}, m_version{version},
53
90.6k
        m_has_mempool_descendant{has_mempool_descendant}
54
90.6k
    {}
55
};
56
57
std::optional<std::string> PackageTRUCChecks(const CTxMemPool& pool, const CTransactionRef& ptx, int64_t vsize,
58
                                           const Package& package,
59
                                           const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents)
60
148k
{
61
148k
    AssertLockHeld(pool.cs);
Line
Count
Source
144
148k
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
62
    // This function is specialized for these limits, and must be reimplemented if they ever change.
63
148k
    static_assert(TRUC_ANCESTOR_LIMIT == 2);
64
148k
    static_assert(TRUC_DESCENDANT_LIMIT == 2);
65
66
148k
    const auto in_package_parents{FindInPackageParents(package, ptx)};
67
68
    // Now we have all parents, so we can start checking TRUC rules.
69
148k
    if (ptx->version == TRUC_VERSION) {
70
        // SingleTRUCChecks should have checked this already.
71
91.3k
        if (!Assume(vsize <= TRUC_MAX_VSIZE)) {
Line
Count
Source
125
91.3k
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
72
0
            return strprintf("version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
Line
Count
Source
1172
0
#define strprintf tfm::format
73
0
                             ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE);
74
0
        }
75
76
91.3k
        if (mempool_parents.size() + in_package_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
77
0
            return strprintf("tx %s (wtxid=%s) would have too many ancestors",
Line
Count
Source
1172
0
#define strprintf tfm::format
78
0
                             ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
79
0
        }
80
81
91.3k
        if (mempool_parents.size()) {
82
86.8k
            if (pool.GetAncestorCount(mempool_parents[0]) + in_package_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
83
0
                return strprintf("tx %s (wtxid=%s) would have too many ancestors",
Line
Count
Source
1172
0
#define strprintf tfm::format
84
0
                                 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
85
0
            }
86
86.8k
        }
87
88
91.3k
        const bool has_parent{mempool_parents.size() + in_package_parents.size() > 0};
89
91.3k
        if (has_parent) {
90
            // A TRUC child cannot be too large.
91
90.6k
            if (vsize > TRUC_CHILD_MAX_VSIZE) {
92
0
                return strprintf("version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
Line
Count
Source
1172
0
#define strprintf tfm::format
93
0
                                 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
94
0
                                 vsize, TRUC_CHILD_MAX_VSIZE);
95
0
            }
96
97
            // Exactly 1 parent exists, either in mempool or package. Find it.
98
90.6k
            const auto parent_info = [&] {
99
90.6k
                if (mempool_parents.size() > 0) {
100
86.8k
                    const auto& mempool_parent = &mempool_parents[0].get();
101
86.8k
                    return ParentInfo{mempool_parent->GetTx().GetHash(),
102
86.8k
                                      mempool_parent->GetTx().GetWitnessHash(),
103
86.8k
                                      mempool_parent->GetTx().version,
104
86.8k
                                      /*has_mempool_descendant=*/pool.GetDescendantCount(*mempool_parent) > 1};
105
86.8k
                } else {
106
3.76k
                    auto& parent_index = in_package_parents.front();
107
3.76k
                    auto& package_parent = package.at(parent_index);
108
3.76k
                    return ParentInfo{package_parent->GetHash(),
109
3.76k
                                      package_parent->GetWitnessHash(),
110
3.76k
                                      package_parent->version,
111
3.76k
                                      /*has_mempool_descendant=*/false};
112
3.76k
                }
113
90.6k
            }();
114
115
            // If there is a parent, it must have the right version.
116
90.6k
            if (parent_info.m_version != TRUC_VERSION) {
117
3.27k
                return strprintf("version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
Line
Count
Source
1172
3.27k
#define strprintf tfm::format
118
3.27k
                                 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
119
3.27k
                                 parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
120
3.27k
            }
121
122
174k
            
for (const auto& package_tx : package)87.3k
{
123
                // Skip same tx.
124
174k
                if (&(*package_tx) == &(*ptx)) 
continue87.3k
;
125
126
87.3k
                for (auto& input : package_tx->vin) {
127
                    // Fail if we find another tx with the same parent. We don't check whether the
128
                    // sibling is to-be-replaced (done in SingleTRUCChecks) because these transactions
129
                    // are within the same package.
130
87.3k
                    if (input.prevout.hash == parent_info.m_txid) {
131
0
                        return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
Line
Count
Source
1172
0
#define strprintf tfm::format
132
0
                                         parent_info.m_txid.ToString(),
133
0
                                         parent_info.m_wtxid.ToString());
134
0
                    }
135
136
                    // This tx can't have both a parent and an in-package child.
137
87.3k
                    if (input.prevout.hash == ptx->GetHash()) {
138
86.8k
                        return strprintf("tx %s (wtxid=%s) would have too many ancestors",
Line
Count
Source
1172
86.8k
#define strprintf tfm::format
139
86.8k
                                         package_tx->GetHash().ToString(), package_tx->GetWitnessHash().ToString());
140
86.8k
                    }
141
87.3k
                }
142
87.3k
            }
143
144
495
            if (parent_info.m_has_mempool_descendant) {
145
0
                return strprintf("tx %s (wtxid=%s) would exceed descendant count limit",
Line
Count
Source
1172
0
#define strprintf tfm::format
146
0
                                parent_info.m_txid.ToString(), parent_info.m_wtxid.ToString());
147
0
            }
148
495
        }
149
91.3k
    } else {
150
        // Non-TRUC transactions cannot have TRUC parents.
151
56.7k
        for (auto it : mempool_parents) {
152
29.3k
            if (it.get().GetTx().version == TRUC_VERSION) {
153
0
                return strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
Line
Count
Source
1172
0
#define strprintf tfm::format
154
0
                                 ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
155
0
                                 it.get().GetSharedTx()->GetHash().ToString(), it.get().GetSharedTx()->GetWitnessHash().ToString());
156
0
            }
157
29.3k
        }
158
56.7k
        for (const auto& index: in_package_parents) {
159
26.8k
            if (package.at(index)->version == TRUC_VERSION) {
160
240
                return strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
Line
Count
Source
1172
240
#define strprintf tfm::format
161
240
                                 ptx->GetHash().ToString(),
162
240
                                 ptx->GetWitnessHash().ToString(),
163
240
                                 package.at(index)->GetHash().ToString(),
164
240
                                 package.at(index)->GetWitnessHash().ToString());
165
240
            }
166
26.8k
        }
167
56.7k
    }
168
57.7k
    return std::nullopt;
169
148k
}
170
171
std::optional<std::pair<std::string, CTransactionRef>> SingleTRUCChecks(const CTxMemPool& pool, const CTransactionRef& ptx,
172
                                          const std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& mempool_parents,
173
                                          const std::set<Txid>& direct_conflicts,
174
                                          int64_t vsize)
175
3.17M
{
176
3.17M
    AssertLockHeld(pool.cs);
Line
Count
Source
144
3.17M
#define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
177
    // Check TRUC and non-TRUC inheritance.
178
3.17M
    for (const auto& entry_ref : mempool_parents) {
179
2.74M
        const auto& entry = &entry_ref.get();
180
2.74M
        if (ptx->version != TRUC_VERSION && 
entry->GetTx().version == TRUC_VERSION2.04M
) {
181
363k
            return std::make_pair(strprintf("non-version=3 tx %s (wtxid=%s) cannot spend from version=3 tx %s (wtxid=%s)",
Line
Count
Source
1172
363k
#define strprintf tfm::format
182
363k
                             ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
183
363k
                             entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
184
363k
                nullptr);
185
2.37M
        } else if (ptx->version == TRUC_VERSION && 
entry->GetTx().version != TRUC_VERSION700k
) {
186
286k
            return std::make_pair(strprintf("version=3 tx %s (wtxid=%s) cannot spend from non-version=3 tx %s (wtxid=%s)",
Line
Count
Source
1172
286k
#define strprintf tfm::format
187
286k
                             ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(),
188
286k
                             entry->GetSharedTx()->GetHash().ToString(), entry->GetSharedTx()->GetWitnessHash().ToString()),
189
286k
                nullptr);
190
286k
        }
191
2.74M
    }
192
193
    // This function is specialized for these limits, and must be reimplemented if they ever change.
194
2.52M
    static_assert(TRUC_ANCESTOR_LIMIT == 2);
195
2.52M
    static_assert(TRUC_DESCENDANT_LIMIT == 2);
196
197
    // The rest of the rules only apply to transactions with version=3.
198
2.52M
    if (ptx->version != TRUC_VERSION) 
return std::nullopt2.06M
;
199
200
451k
    if (vsize > TRUC_MAX_VSIZE) {
201
0
        return std::make_pair(strprintf("version=3 tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
Line
Count
Source
1172
0
#define strprintf tfm::format
202
0
                         ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_MAX_VSIZE),
203
0
            nullptr);
204
0
    }
205
206
    // Check that TRUC_ANCESTOR_LIMIT would not be violated.
207
451k
    if (mempool_parents.size() + 1 > TRUC_ANCESTOR_LIMIT) {
208
0
        return std::make_pair(strprintf("tx %s (wtxid=%s) would have too many ancestors",
Line
Count
Source
1172
0
#define strprintf tfm::format
209
0
                         ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
210
0
            nullptr);
211
0
    }
212
213
    // Remaining checks only pertain to transactions with unconfirmed ancestors.
214
451k
    if (mempool_parents.size() > 0) {
215
        // Ensure that the in-mempool parent doesn't have any additional
216
        // ancestors, as that would also be a violation.
217
414k
        if (pool.GetAncestorCount(mempool_parents[0]) + 1 > TRUC_ANCESTOR_LIMIT) {
218
64.2k
            return std::make_pair(strprintf("tx %s (wtxid=%s) would have too many ancestors",
Line
Count
Source
1172
64.2k
#define strprintf tfm::format
219
64.2k
                             ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString()),
220
64.2k
                nullptr);
221
64.2k
        }
222
        // If this transaction spends TRUC parents, it cannot be too large.
223
349k
        if (vsize > TRUC_CHILD_MAX_VSIZE) {
224
0
            return std::make_pair(strprintf("version=3 child tx %s (wtxid=%s) is too big: %u > %u virtual bytes",
Line
Count
Source
1172
0
#define strprintf tfm::format
225
0
                             ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString(), vsize, TRUC_CHILD_MAX_VSIZE),
226
0
                nullptr);
227
0
        }
228
229
        // Check the descendant counts of in-mempool parents.
230
349k
        const auto& parent_entry = mempool_parents[0].get();
231
        // If there are any parents, this is the only child allowed. The parent cannot have any
232
        // other descendants. We handle the possibility of multiple children as that case is
233
        // possible through a reorg.
234
349k
        CTxMemPool::setEntries descendants;
235
349k
        auto parent_it = pool.CalculateDescendants(parent_entry, descendants);
236
349k
        descendants.erase(parent_it);
237
        // Don't double-count a transaction that is going to be replaced. This logic assumes that
238
        // any descendant of the TRUC transaction is a direct child, which makes sense because a
239
        // TRUC transaction can only have 1 descendant.
240
349k
        const bool child_will_be_replaced = !descendants.empty() &&
241
349k
            std::any_of(descendants.cbegin(), descendants.cend(),
242
339k
                [&direct_conflicts](const CTxMemPool::txiter& child){return direct_conflicts.contains(child->GetTx().GetHash());});
243
349k
        if (pool.GetDescendantCount(parent_entry) + 1 > TRUC_DESCENDANT_LIMIT && 
!child_will_be_replaced339k
) {
244
            // Allow sibling eviction for TRUC transaction: if another child already exists, even if
245
            // we don't conflict inputs with it, consider evicting it under RBF rules. We rely on TRUC rules
246
            // only permitting 1 descendant, as otherwise we would need to have logic for deciding
247
            // which descendant to evict. Skip if this isn't true, e.g. if the transaction has
248
            // multiple children or the sibling also has descendants due to a reorg.
249
0
            const bool consider_sibling_eviction{pool.GetDescendantCount(parent_entry) == 2 &&
250
0
                pool.GetAncestorCount(**descendants.begin()) == 2};
251
252
            // Return the sibling if its eviction can be considered. Provide the "descendant count
253
            // limit" string either way, as the caller may decide not to do sibling eviction.
254
0
            return std::make_pair(strprintf("tx %u (wtxid=%s) would exceed descendant count limit",
Line
Count
Source
1172
0
#define strprintf tfm::format
255
0
                                            parent_entry.GetSharedTx()->GetHash().ToString(),
256
0
                                            parent_entry.GetSharedTx()->GetWitnessHash().ToString()),
257
0
                                  consider_sibling_eviction ? (*descendants.begin())->GetSharedTx() : nullptr);
258
0
        }
259
349k
    }
260
387k
    return std::nullopt;
261
451k
}