Coverage Report

Created: 2026-07-14 18:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/policy/ephemeral_policy.cpp
Line
Count
Source
1
// Copyright (c) 2024-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 <consensus/validation.h>
6
#include <policy/ephemeral_policy.h>
7
#include <policy/feerate.h>
8
#include <policy/packages.h>
9
#include <policy/policy.h>
10
#include <primitives/transaction.h>
11
#include <txmempool.h>
12
#include <util/check.h>
13
#include <util/hasher.h>
14
15
#include <algorithm>
16
#include <cstdint>
17
#include <map>
18
#include <memory>
19
#include <unordered_set>
20
#include <utility>
21
#include <vector>
22
23
bool PreCheckEphemeralTx(const CTransaction& tx, CFeeRate dust_relay_rate, CAmount base_fee, CAmount mod_fee, TxValidationState& state)
24
521k
{
25
    // We never want to give incentives to mine this transaction alone
26
521k
    if ((base_fee != 0 || mod_fee != 0) && !GetDust(tx, dust_relay_rate).empty()) {
  Branch (26:9): [True: 2.02k, False: 519k]
  Branch (26:10): [True: 446k, False: 75.0k]
  Branch (26:27): [True: 0, False: 75.0k]
  Branch (26:44): [True: 2.02k, False: 444k]
27
2.02k
        return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "dust", "tx with dust output must be 0-fee");
28
2.02k
    }
29
30
519k
    return true;
31
521k
}
32
33
bool CheckEphemeralSpends(const Package& package, CFeeRate dust_relay_rate, const CTxMemPool& tx_pool, TxValidationState& out_child_state, Wtxid& out_child_wtxid)
34
382k
{
35
382k
    if (!Assume(std::ranges::all_of(package, [](const auto& tx){return tx != nullptr;}))) {
  Branch (35:9): [True: 0, False: 382k]
36
        // Bail out of spend checks if caller gave us an invalid package
37
0
        return true;
38
0
    }
39
40
382k
    std::map<Txid, CTransactionRef> map_txid_ref;
41
388k
    for (const auto& tx : package) {
  Branch (41:25): [True: 388k, False: 382k]
42
388k
        map_txid_ref[tx->GetHash()] = tx;
43
388k
    }
44
45
388k
    for (const auto& tx : package) {
  Branch (45:25): [True: 388k, False: 382k]
46
388k
        std::unordered_set<Txid, SaltedTxidHasher> processed_parent_set;
47
388k
        std::unordered_set<COutPoint, SaltedOutpointHasher> unspent_parent_dust;
48
49
410k
        for (const auto& tx_input : tx->vin) {
  Branch (49:35): [True: 410k, False: 388k]
50
410k
            const Txid& parent_txid{tx_input.prevout.hash};
51
            // Skip parents we've already checked dust for
52
410k
            if (processed_parent_set.contains(parent_txid)) continue;
  Branch (52:17): [True: 11.5k, False: 398k]
53
54
            // We look for an in-package or in-mempool dependency
55
398k
            CTransactionRef parent_ref = nullptr;
56
398k
            if (auto it = map_txid_ref.find(parent_txid); it != map_txid_ref.end()) {
  Branch (56:59): [True: 5.20k, False: 393k]
57
5.20k
                parent_ref = it->second;
58
393k
            } else {
59
393k
                parent_ref = tx_pool.get(parent_txid);
60
393k
            }
61
62
            // Check for dust on parents
63
398k
            if (parent_ref) {
  Branch (63:17): [True: 329k, False: 69.4k]
64
698k
                for (uint32_t out_index = 0; out_index < parent_ref->vout.size(); out_index++) {
  Branch (64:46): [True: 369k, False: 329k]
65
369k
                    const auto& tx_output = parent_ref->vout[out_index];
66
369k
                    if (IsDust(tx_output, dust_relay_rate)) {
  Branch (66:25): [True: 1.18k, False: 368k]
67
1.18k
                        unspent_parent_dust.insert(COutPoint(parent_txid, out_index));
68
1.18k
                    }
69
369k
                }
70
329k
            }
71
72
398k
            processed_parent_set.insert(parent_txid);
73
398k
        }
74
75
388k
        if (unspent_parent_dust.empty()) {
  Branch (75:13): [True: 386k, False: 1.12k]
76
386k
            continue;
77
386k
        }
78
79
        // Now that we have gathered parents' dust, make sure it's spent
80
        // by the child
81
2.07k
        for (const auto& tx_input : tx->vin) {
  Branch (81:35): [True: 2.07k, False: 1.12k]
82
2.07k
            unspent_parent_dust.erase(tx_input.prevout);
83
2.07k
        }
84
85
1.12k
        if (!unspent_parent_dust.empty()) {
  Branch (85:13): [True: 752, False: 372]
86
752
            const Txid& out_child_txid = tx->GetHash();
87
752
            out_child_wtxid = tx->GetWitnessHash();
88
752
            out_child_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "missing-ephemeral-spends",
89
752
                                strprintf("tx %s (wtxid=%s) did not spend parent's ephemeral dust", out_child_txid.ToString(), out_child_wtxid.ToString()));
90
752
            return false;
91
752
        }
92
1.12k
    }
93
94
382k
    return true;
95
382k
}