Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/wallet/receive.cpp
Line
Count
Source
1
// Copyright (c) 2021-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/amount.h>
6
#include <consensus/consensus.h>
7
#include <util/check.h>
8
#include <wallet/receive.h>
9
#include <wallet/transaction.h>
10
#include <wallet/wallet.h>
11
12
namespace wallet {
13
bool InputIsMine(const CWallet& wallet, const CTxIn& txin)
14
473
{
15
473
    AssertLockHeld(wallet.cs_wallet);
16
473
    const CWalletTx* prev = wallet.GetWalletTx(txin.prevout.hash);
17
473
    if (prev && txin.prevout.n < prev->tx->vout.size()) {
18
269
        return wallet.IsMine(prev->tx->vout[txin.prevout.n]);
19
269
    }
20
204
    return false;
21
473
}
22
23
bool AllInputsMine(const CWallet& wallet, const CTransaction& tx)
24
114
{
25
114
    LOCK(wallet.cs_wallet);
26
267
    for (const CTxIn& txin : tx.vin) {
27
267
        if (!InputIsMine(wallet, txin)) return false;
28
267
    }
29
113
    return true;
30
114
}
31
32
CAmount OutputGetCredit(const CWallet& wallet, const CTxOut& txout)
33
831
{
34
831
    if (!MoneyRange(txout.nValue))
35
0
        throw std::runtime_error(std::string(__func__) + ": value out of range");
36
831
    LOCK(wallet.cs_wallet);
37
831
    return (wallet.IsMine(txout) ? txout.nValue : 0);
38
831
}
39
40
CAmount TxGetCredit(const CWallet& wallet, const CTransaction& tx)
41
436
{
42
436
    CAmount nCredit = 0;
43
436
    for (const CTxOut& txout : tx.vout)
44
831
    {
45
831
        nCredit += OutputGetCredit(wallet, txout);
46
831
        if (!MoneyRange(nCredit))
47
0
            throw std::runtime_error(std::string(__func__) + ": value out of range");
48
831
    }
49
436
    return nCredit;
50
436
}
51
52
bool ScriptIsChange(const CWallet& wallet, const CScript& script)
53
3.25k
{
54
    // TODO: fix handling of 'change' outputs. The assumption is that any
55
    // payment to a script that is ours, but is not in the address book
56
    // is change. That assumption is likely to break when we implement multisignature
57
    // wallets that return change back into a multi-signature-protected address;
58
    // a better way of identifying which outputs are 'the send' and which are
59
    // 'the change' will need to be implemented (maybe extend CWalletTx to remember
60
    // which output, if any, was change).
61
3.25k
    AssertLockHeld(wallet.cs_wallet);
62
3.25k
    if (wallet.IsMine(script))
63
1.91k
    {
64
1.91k
        CTxDestination address;
65
1.91k
        if (!ExtractDestination(script, address))
66
5
            return true;
67
1.91k
        if (!wallet.FindAddressBookEntry(address)) {
68
917
            return true;
69
917
        }
70
1.91k
    }
71
2.32k
    return false;
72
3.25k
}
73
74
bool OutputIsChange(const CWallet& wallet, const CTxOut& txout)
75
2.50k
{
76
2.50k
    return ScriptIsChange(wallet, txout.scriptPubKey);
77
2.50k
}
78
79
CAmount OutputGetChange(const CWallet& wallet, const CTxOut& txout)
80
0
{
81
0
    AssertLockHeld(wallet.cs_wallet);
82
0
    if (!MoneyRange(txout.nValue))
83
0
        throw std::runtime_error(std::string(__func__) + ": value out of range");
84
0
    return (OutputIsChange(wallet, txout) ? txout.nValue : 0);
85
0
}
86
87
CAmount TxGetChange(const CWallet& wallet, const CTransaction& tx)
88
0
{
89
0
    LOCK(wallet.cs_wallet);
90
0
    CAmount nChange = 0;
91
0
    for (const CTxOut& txout : tx.vout)
92
0
    {
93
0
        nChange += OutputGetChange(wallet, txout);
94
0
        if (!MoneyRange(nChange))
95
0
            throw std::runtime_error(std::string(__func__) + ": value out of range");
96
0
    }
97
0
    return nChange;
98
0
}
99
100
static CAmount GetCachableAmount(const CWallet& wallet, const CWalletTx& wtx, CWalletTx::AmountType type, bool avoid_reuse)
101
4.06k
{
102
4.06k
    auto& amount = wtx.m_amounts[type];
103
4.06k
    if (!amount.IsCached(avoid_reuse)) {
104
1.78k
        amount.Set(avoid_reuse, type == CWalletTx::DEBIT ? wallet.GetDebit(*wtx.tx) : TxGetCredit(wallet, *wtx.tx));
105
1.78k
        wtx.m_is_cache_empty = false;
106
1.78k
    }
107
4.06k
    return amount.Get(avoid_reuse);
108
4.06k
}
109
110
CAmount CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, bool avoid_reuse)
111
467
{
112
467
    AssertLockHeld(wallet.cs_wallet);
113
114
    // Must wait until coinbase is safely deep enough in the chain before valuing it
115
467
    if (wallet.IsTxImmatureCoinBase(wtx))
116
10
        return 0;
117
118
    // GetBalance can assume transactions in mapWallet won't change
119
457
    return GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, avoid_reuse);
120
467
}
121
122
CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, bool avoid_reuse)
123
3.60k
{
124
3.60k
    if (wtx.tx->vin.empty())
125
0
        return 0;
126
127
3.60k
    return GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, avoid_reuse);
128
3.60k
}
129
130
CAmount CachedTxGetChange(const CWallet& wallet, const CWalletTx& wtx)
131
0
{
132
0
    if (wtx.fChangeCached)
133
0
        return wtx.nChangeCached;
134
0
    wtx.nChangeCached = TxGetChange(wallet, *wtx.tx);
135
0
    wtx.fChangeCached = true;
136
0
    return wtx.nChangeCached;
137
0
}
138
139
void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx,
140
                  std::list<COutputEntry>& listReceived,
141
                  std::list<COutputEntry>& listSent, CAmount& nFee,
142
                  bool include_change)
143
3.14k
{
144
3.14k
    nFee = 0;
145
3.14k
    listReceived.clear();
146
3.14k
    listSent.clear();
147
148
    // Compute fee:
149
3.14k
    CAmount nDebit = CachedTxGetDebit(wallet, wtx, /*avoid_reuse=*/false);
150
3.14k
    if (nDebit > 0) // debit>0 means we signed/sent this transaction
151
1.34k
    {
152
1.34k
        CAmount nValueOut = wtx.tx->GetValueOut();
153
1.34k
        nFee = nDebit - nValueOut;
154
1.34k
    }
155
156
3.14k
    LOCK(wallet.cs_wallet);
157
    // Sent/received.
158
8.92k
    for (unsigned int i = 0; i < wtx.tx->vout.size(); ++i)
159
5.78k
    {
160
5.78k
        const CTxOut& txout = wtx.tx->vout[i];
161
5.78k
        bool ismine = wallet.IsMine(txout);
162
        // Only need to handle txouts if AT LEAST one of these is true:
163
        //   1) they debit from us (sent)
164
        //   2) the output is to us (received)
165
5.78k
        if (nDebit > 0)
166
2.04k
        {
167
2.04k
            if (!include_change && OutputIsChange(wallet, txout))
168
566
                continue;
169
2.04k
        }
170
3.74k
        else if (!ismine)
171
1.91k
            continue;
172
173
        // In either case, we need to get the destination address
174
3.30k
        CTxDestination address;
175
176
3.30k
        if (!ExtractDestination(txout.scriptPubKey, address) && !txout.scriptPubKey.IsUnspendable())
177
0
        {
178
0
            wallet.WalletLogPrintf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
179
0
                                    wtx.GetHash().ToString());
180
0
            address = CNoDestination();
181
0
        }
182
183
3.30k
        COutputEntry output = {address, txout.nValue, (int)i};
184
185
        // If we are debited by the transaction, add the output as a "sent" entry
186
3.30k
        if (nDebit > 0)
187
1.47k
            listSent.push_back(output);
188
189
        // If we are receiving the output, add it as a "received" entry
190
3.30k
        if (ismine)
191
2.24k
            listReceived.push_back(output);
192
3.30k
    }
193
194
3.14k
}
195
196
bool CachedTxIsFromMe(const CWallet& wallet, const CWalletTx& wtx)
197
257k
{
198
257k
    if (!wtx.m_cached_from_me.has_value()) {
199
10.3k
        wtx.m_cached_from_me = wallet.IsFromMe(*wtx.tx);
200
10.3k
    }
201
257k
    return wtx.m_cached_from_me.value();
202
257k
}
203
204
// NOLINTNEXTLINE(misc-no-recursion)
205
bool CachedTxIsTrusted(const CWallet& wallet, const CWalletTx& wtx, std::set<Txid>& trusted_parents)
206
591k
{
207
591k
    AssertLockHeld(wallet.cs_wallet);
208
209
    // This wtx is already trusted
210
591k
    if (trusted_parents.contains(wtx.GetHash())) return true;
211
212
541k
    if (wtx.isConfirmed()) return true;
213
76.5k
    if (wtx.isBlockConflicted()) return false;
214
    // using wtx's cached debit
215
76.4k
    if (!wallet.m_spend_zero_conf_change || !CachedTxIsFromMe(wallet, wtx)) return false;
216
217
    // Don't trust unconfirmed transactions from us unless they are in the mempool.
218
73.5k
    if (!wtx.InMempool()) return false;
219
220
    // Trusted if all inputs are from us and are in the mempool:
221
73.3k
    for (const CTxIn& txin : wtx.tx->vin)
222
79.1k
    {
223
        // Transactions not sent by us: not trusted
224
79.1k
        const CWalletTx* parent = wallet.GetWalletTx(txin.prevout.hash);
225
79.1k
        if (parent == nullptr) return false;
226
79.1k
        const CTxOut& parentOut = parent->tx->vout[txin.prevout.n];
227
        // Check that this specific input being spent is trusted
228
79.1k
        if (!wallet.IsMine(parentOut)) return false;
229
        // If we've already trusted this parent, continue
230
79.1k
        if (trusted_parents.contains(parent->GetHash())) continue;
231
        // Recurse to check that the parent is also trusted
232
72.8k
        if (!CachedTxIsTrusted(wallet, *parent, trusted_parents)) return false;
233
72.4k
        trusted_parents.insert(parent->GetHash());
234
72.4k
    }
235
72.9k
    return true;
236
73.3k
}
237
238
bool CachedTxIsTrusted(const CWallet& wallet, const CWalletTx& wtx)
239
748
{
240
748
    std::set<Txid> trusted_parents;
241
748
    LOCK(wallet.cs_wallet);
242
748
    return CachedTxIsTrusted(wallet, wtx, trusted_parents);
243
748
}
244
245
Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse, bool include_nonmempool)
246
1.15k
{
247
1.15k
    Balance ret;
248
1.15k
    bool allow_used_addresses = !avoid_reuse || !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
249
1.15k
    {
250
1.15k
        LOCK(wallet.cs_wallet);
251
1.15k
        std::set<Txid> trusted_parents;
252
82.8k
        for (const auto& [outpoint, txo] : wallet.GetTXOs()) {
253
82.8k
            const CWalletTx& wtx = txo.GetWalletTx();
254
255
82.8k
            const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)};
256
82.8k
            const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
257
258
82.8k
            bool nonmempool_spent = false;
259
82.8k
            switch (wallet.HowSpent(outpoint)) {
260
47.3k
            case CWallet::SpendType::CONFIRMED:
261
49.0k
            case CWallet::SpendType::MEMPOOL:
262
                // treat as spent; ignore
263
49.0k
                break;
264
154
            case CWallet::SpendType::NONMEMPOOL:
265
154
                if (!include_nonmempool) break;
266
127
                nonmempool_spent = true;
267
127
                [[fallthrough]];
268
33.7k
            case CWallet::SpendType::UNSPENT:
269
33.7k
                CAmount* bucket = nullptr;
270
271
                // Set the amounts in the return object
272
33.7k
                if (wallet.IsTxImmatureCoinBase(wtx) && wtx.isConfirmed()) {
273
20.9k
                    bucket = &ret.m_mine_immature;
274
20.9k
                } else if (is_trusted && tx_depth >= min_depth) {
275
12.0k
                    bucket = &ret.m_mine_trusted;
276
12.0k
                } else if (!is_trusted && wtx.InMempool()) {
277
395
                    bucket = &ret.m_mine_untrusted_pending;
278
395
                }
279
33.7k
                if (bucket) {
280
                    // Get the amounts for mine
281
33.4k
                    CAmount credit_mine = txo.GetTxOut().nValue;
282
283
33.4k
                    if (!allow_used_addresses && wallet.IsSpentKey(txo.GetTxOut().scriptPubKey)) {
284
2
                        bucket = &ret.m_mine_used;
285
2
                    }
286
33.4k
                    *bucket += credit_mine;
287
33.4k
                    if (nonmempool_spent) {
288
124
                        ret.m_mine_nonmempool -= credit_mine;
289
124
                    }
290
33.4k
                }
291
82.8k
            }
292
82.8k
        }
293
1.15k
    }
294
1.15k
    return ret;
295
1.15k
}
296
297
std::map<CTxDestination, CAmount> GetAddressBalances(const CWallet& wallet)
298
2
{
299
2
    std::map<CTxDestination, CAmount> balances;
300
301
2
    {
302
2
        LOCK(wallet.cs_wallet);
303
2
        std::set<Txid> trusted_parents;
304
204
        for (const auto& [outpoint, txo] : wallet.GetTXOs()) {
305
204
            const CWalletTx& wtx = txo.GetWalletTx();
306
307
204
            if (!CachedTxIsTrusted(wallet, wtx, trusted_parents)) continue;
308
204
            if (wallet.IsTxImmatureCoinBase(wtx)) continue;
309
310
4
            int nDepth = wallet.GetTxDepthInMainChain(wtx);
311
4
            if (nDepth < (CachedTxIsFromMe(wallet, wtx) ? 0 : 1)) continue;
312
313
4
            CTxDestination addr;
314
4
            Assume(wallet.IsMine(txo.GetTxOut()));
315
4
            if(!ExtractDestination(txo.GetTxOut().scriptPubKey, addr)) continue;
316
317
4
            CAmount n = wallet.IsSpent(outpoint) ? 0 : txo.GetTxOut().nValue;
318
4
            balances[addr] += n;
319
4
        }
320
2
    }
321
322
2
    return balances;
323
2
}
324
325
std::set< std::set<CTxDestination> > GetAddressGroupings(const CWallet& wallet)
326
2
{
327
2
    AssertLockHeld(wallet.cs_wallet);
328
2
    std::set< std::set<CTxDestination> > groupings;
329
2
    std::set<CTxDestination> grouping;
330
331
2
    for (const auto& walletEntry : wallet.mapWallet)
332
205
    {
333
205
        const CWalletTx& wtx = walletEntry.second;
334
335
205
        if (wtx.tx->vin.size() > 0)
336
205
        {
337
205
            bool any_mine = false;
338
            // group all input addresses with each other
339
205
            for (const CTxIn& txin : wtx.tx->vin)
340
206
            {
341
206
                CTxDestination address;
342
206
                if(!InputIsMine(wallet, txin)) /* If this input isn't mine, ignore it */
343
204
                    continue;
344
2
                if(!ExtractDestination(wallet.mapWallet.at(txin.prevout.hash).tx->vout[txin.prevout.n].scriptPubKey, address))
345
0
                    continue;
346
2
                grouping.insert(address);
347
2
                any_mine = true;
348
2
            }
349
350
            // group change with input addresses
351
205
            if (any_mine)
352
1
            {
353
1
               for (const CTxOut& txout : wtx.tx->vout)
354
1
                   if (OutputIsChange(wallet, txout))
355
0
                   {
356
0
                       CTxDestination txoutAddr;
357
0
                       if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
358
0
                           continue;
359
0
                       grouping.insert(txoutAddr);
360
0
                   }
361
1
            }
362
205
            if (grouping.size() > 0)
363
1
            {
364
1
                groupings.insert(grouping);
365
1
                grouping.clear();
366
1
            }
367
205
        }
368
369
        // group lone addrs by themselves
370
205
        for (const auto& txout : wtx.tx->vout)
371
409
            if (wallet.IsMine(txout))
372
204
            {
373
204
                CTxDestination address;
374
204
                if(!ExtractDestination(txout.scriptPubKey, address))
375
0
                    continue;
376
204
                grouping.insert(address);
377
204
                groupings.insert(grouping);
378
204
                grouping.clear();
379
204
            }
380
205
    }
381
382
2
    std::set< std::set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
383
2
    std::map< CTxDestination, std::set<CTxDestination>* > setmap;  // map addresses to the unique group containing it
384
2
    for (const std::set<CTxDestination>& _grouping : groupings)
385
5
    {
386
        // make a set of all the groups hit by this new group
387
5
        std::set< std::set<CTxDestination>* > hits;
388
5
        std::map< CTxDestination, std::set<CTxDestination>* >::iterator it;
389
5
        for (const CTxDestination& address : _grouping)
390
6
            if ((it = setmap.find(address)) != setmap.end())
391
2
                hits.insert((*it).second);
392
393
        // merge all hit groups into a new single group and delete old groups
394
5
        std::set<CTxDestination>* merged = new std::set<CTxDestination>(_grouping);
395
5
        for (std::set<CTxDestination>* hit : hits)
396
2
        {
397
2
            merged->insert(hit->begin(), hit->end());
398
2
            uniqueGroupings.erase(hit);
399
2
            delete hit;
400
2
        }
401
5
        uniqueGroupings.insert(merged);
402
403
        // update setmap
404
5
        for (const CTxDestination& element : *merged)
405
7
            setmap[element] = merged;
406
5
    }
407
408
2
    std::set< std::set<CTxDestination> > ret;
409
2
    for (const std::set<CTxDestination>* uniqueGrouping : uniqueGroupings)
410
3
    {
411
3
        ret.insert(*uniqueGrouping);
412
3
        delete uniqueGrouping;
413
3
    }
414
415
2
    return ret;
416
2
}
417
} // namespace wallet