aboutsummaryrefslogtreecommitdiff
path: root/src/policy/ephemeral_policy.cpp
blob: b7d254dd630459f528ac103dd5580c34ec00a597 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2024-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <consensus/validation.h>
#include <policy/ephemeral_policy.h>
#include <policy/feerate.h>
#include <policy/packages.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <txmempool.h>
#include <util/check.h>
#include <util/hasher.h>

#include <algorithm>
#include <cstdint>
#include <map>
#include <memory>
#include <unordered_set>
#include <utility>
#include <vector>

bool PreCheckEphemeralTx(const CTransaction& tx, CFeeRate dust_relay_rate, CAmount base_fee, CAmount mod_fee, TxValidationState& state)
{
    // We never want to give incentives to mine this transaction alone
    if ((base_fee != 0 || mod_fee != 0) && !GetDust(tx, dust_relay_rate).empty()) {
        return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "dust", "tx with dust output must be 0-fee");
    }

    return true;
}

bool CheckEphemeralSpends(const Package& package, CFeeRate dust_relay_rate, const CTxMemPool& tx_pool, TxValidationState& out_child_state, Txid& out_child_txid)
{
    if (!Assume(std::ranges::all_of(package, [](const auto& tx){return tx != nullptr;}))) {
        // Bail out of spend checks if caller gave us an invalid package
        return true;
    }

    std::map<Txid, CTransactionRef> map_txid_ref;
    for (const auto& tx : package) {
        map_txid_ref[tx->GetHash()] = tx;
    }

    for (const auto& tx : package) {
        std::unordered_set<Txid, SaltedTxidHasher> processed_parent_set;
        std::unordered_set<COutPoint, SaltedOutpointHasher> unspent_parent_dust;

        for (const auto& tx_input : tx->vin) {
            const Txid& parent_txid{tx_input.prevout.hash};
            // Skip parents we've already checked dust for
            if (processed_parent_set.contains(parent_txid)) continue;

            // We look for an in-package or in-mempool dependency
            CTransactionRef parent_ref = nullptr;
            if (auto it = map_txid_ref.find(parent_txid); it != map_txid_ref.end()) {
                parent_ref = it->second;
            } else {
                parent_ref = tx_pool.get(parent_txid);
            }

            // Check for dust on parents
            if (parent_ref) {
                for (uint32_t out_index = 0; out_index < parent_ref->vout.size(); out_index++) {
                    const auto& tx_output = parent_ref->vout[out_index];
                    if (IsDust(tx_output, dust_relay_rate)) {
                        unspent_parent_dust.insert(COutPoint(parent_txid, out_index));
                    }
                }
            }

            processed_parent_set.insert(parent_txid);
        }

        if (unspent_parent_dust.empty()) {
            continue;
        }

        // Now that we have gathered parents' dust, make sure it's spent
        // by the child
        for (const auto& tx_input : tx->vin) {
            unspent_parent_dust.erase(tx_input.prevout);
        }

        if (!unspent_parent_dust.empty()) {
            out_child_txid = tx->GetHash();
            out_child_state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "missing-ephemeral-spends",
                                strprintf("tx %s did not spend parent's ephemeral dust", out_child_txid.ToString()));
            return false;
        }
    }

    return true;
}