Bitcoin ABC 0.32.4
P2P Digital Currency
spend.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 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 <wallet/spend.h>
6
7#include <common/args.h>
8#include <common/system.h>
10#include <interfaces/chain.h>
11#include <policy/policy.h>
12#include <util/check.h>
13#include <util/insert.h>
14#include <util/moneystr.h>
15#include <util/translation.h>
16#include <wallet/coincontrol.h>
17#include <wallet/fees.h>
18#include <wallet/receive.h>
19#include <wallet/transaction.h>
20#include <wallet/wallet.h>
21
22static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10;
23
24int GetTxSpendSize(const CWallet &wallet, const CWalletTx &wtx,
25 unsigned int out, bool use_max_sig) {
26 return CalculateMaximumSignedInputSize(wtx.tx->vout[out], &wallet,
27 use_max_sig);
28}
29std::string COutput::ToString() const {
30 return strprintf("COutput(%s, %d, %d) [%s]", tx->GetId().ToString(), i,
31 nDepth, FormatMoney(tx->tx->vout[i].nValue));
32}
33
35 bool use_max_sig) {
37 txn.vin.push_back(CTxIn(COutPoint()));
38 if (!wallet->DummySignInput(txn.vin[0], txout, use_max_sig)) {
39 return -1;
40 }
42}
43
44// txouts needs to be in the order of tx.vin
45int64_t CalculateMaximumSignedTxSize(const CTransaction &tx,
46 const CWallet *wallet,
47 const std::vector<CTxOut> &txouts,
48 bool use_max_sig) {
49 CMutableTransaction txNew(tx);
50 if (!wallet->DummySignTx(txNew, txouts, use_max_sig)) {
51 return -1;
52 }
54}
55
56int64_t CalculateMaximumSignedTxSize(const CTransaction &tx,
57 const CWallet *wallet, bool use_max_sig) {
58 std::vector<CTxOut> txouts;
59 for (auto &input : tx.vin) {
60 const auto mi = wallet->mapWallet.find(input.prevout.GetTxId());
61 // Can not estimate size without knowing the input details
62 if (mi == wallet->mapWallet.end()) {
63 return -1;
64 }
65 assert(input.prevout.GetN() < mi->second.tx->vout.size());
66 txouts.emplace_back(mi->second.tx->vout[input.prevout.GetN()]);
67 }
68 return CalculateMaximumSignedTxSize(tx, wallet, txouts, use_max_sig);
69}
70
71void AvailableCoins(const CWallet &wallet, std::vector<COutput> &vCoins,
72 const CCoinControl *coinControl,
73 const Amount nMinimumAmount, const Amount nMaximumAmount,
74 const Amount nMinimumSumAmount,
75 const uint64_t nMaximumCount) {
76 AssertLockHeld(wallet.cs_wallet);
77
78 vCoins.clear();
79 Amount nTotal = Amount::zero();
80 // Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we
81 // always allow), or we default to avoiding, and only in the case where a
82 // coin control object is provided, and has the avoid address reuse flag set
83 // to false, do we allow already used addresses
84 bool allow_used_addresses =
85 !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE) ||
86 (coinControl && !coinControl->m_avoid_address_reuse);
87 const int min_depth = {coinControl ? coinControl->m_min_depth
89 const int max_depth = {coinControl ? coinControl->m_max_depth
91 const bool only_safe = {coinControl ? !coinControl->m_include_unsafe_inputs
92 : true};
93
94 std::set<TxId> trusted_parents;
95 for (const auto &entry : wallet.mapWallet) {
96 const TxId &wtxid = entry.first;
97 const CWalletTx &wtx = entry.second;
98
99 if (wallet.IsTxImmatureCoinBase(wtx)) {
100 continue;
101 }
102
103 int nDepth = wallet.GetTxDepthInMainChain(wtx);
104 if (nDepth < 0) {
105 continue;
106 }
107
108 // We should not consider coins which aren't at least in our mempool.
109 // It's possible for these to be conflicted via ancestors which we may
110 // never be able to detect.
111 if (nDepth == 0 && !wtx.InMempool()) {
112 continue;
113 }
114
115 bool safeTx = CachedTxIsTrusted(wallet, wtx, trusted_parents);
116
117 // Bitcoin-ABC: Removed check that prevents consideration of coins from
118 // transactions that are replacing other transactions. This check based
119 // on wtx.mapValue.count("replaces_txid") which was not being set
120 // anywhere.
121
122 // Similarly, we should not consider coins from transactions that have
123 // been replaced. In the example above, we would want to prevent
124 // creation of a transaction A' spending an output of A, because if
125 // transaction B were initially confirmed, conflicting with A and A', we
126 // wouldn't want to the user to create a transaction D intending to
127 // replace A', but potentially resulting in a scenario where A, A', and
128 // D could all be accepted (instead of just B and D, or just A and A'
129 // like the user would want).
130
131 // Bitcoin-ABC: retained this check as 'replaced_by_txid' is still set
132 // in the wallet code.
133 if (nDepth == 0 && wtx.mapValue.count("replaced_by_txid")) {
134 safeTx = false;
135 }
136
137 if (only_safe && !safeTx) {
138 continue;
139 }
140
141 if (nDepth < min_depth || nDepth > max_depth) {
142 continue;
143 }
144
145 for (uint32_t i = 0; i < wtx.tx->vout.size(); i++) {
146 // Only consider selected coins if add_inputs is false
147 if (coinControl && !coinControl->m_add_inputs &&
148 !coinControl->IsSelected(COutPoint(entry.first, i))) {
149 continue;
150 }
151
152 if (wtx.tx->vout[i].nValue < nMinimumAmount ||
153 wtx.tx->vout[i].nValue > nMaximumAmount) {
154 continue;
155 }
156
157 const COutPoint outpoint(wtxid, i);
158
159 if (coinControl && coinControl->HasSelected() &&
160 !coinControl->fAllowOtherInputs &&
161 !coinControl->IsSelected(outpoint)) {
162 continue;
163 }
164
165 if (wallet.IsLockedCoin(outpoint)) {
166 continue;
167 }
168
169 if (wallet.IsSpent(outpoint)) {
170 continue;
171 }
172
173 isminetype mine = wallet.IsMine(wtx.tx->vout[i]);
174
175 if (mine == ISMINE_NO) {
176 continue;
177 }
178
179 if (!allow_used_addresses && wallet.IsSpentKey(wtxid, i)) {
180 continue;
181 }
182
183 std::unique_ptr<SigningProvider> provider =
184 wallet.GetSolvingProvider(wtx.tx->vout[i].scriptPubKey);
185
186 bool solvable =
187 provider ? IsSolvable(*provider, wtx.tx->vout[i].scriptPubKey)
188 : false;
189 bool spendable =
190 ((mine & ISMINE_SPENDABLE) != ISMINE_NO) ||
191 (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) &&
192 (coinControl && coinControl->fAllowWatchOnly && solvable));
193
194 vCoins.push_back(
195 COutput(wallet, wtx, i, nDepth, spendable, solvable, safeTx,
196 (coinControl && coinControl->fAllowWatchOnly)));
197
198 // Checks the sum amount of all UTXO's.
199 if (nMinimumSumAmount != MAX_MONEY) {
200 nTotal += wtx.tx->vout[i].nValue;
201
202 if (nTotal >= nMinimumSumAmount) {
203 return;
204 }
205 }
206
207 // Checks the maximum number of UTXO's.
208 if (nMaximumCount > 0 && vCoins.size() >= nMaximumCount) {
209 return;
210 }
211 }
212 }
213}
214
216 const CCoinControl *coinControl) {
217 LOCK(wallet.cs_wallet);
218
220 std::vector<COutput> vCoins;
221 AvailableCoins(wallet, vCoins, coinControl);
222 for (const COutput &out : vCoins) {
223 if (out.fSpendable) {
224 balance += out.tx->tx->vout[out.i].nValue;
225 }
226 }
227 return balance;
228}
229
231 const CTransaction &tx, int output) {
232 AssertLockHeld(wallet.cs_wallet);
233 const CTransaction *ptx = &tx;
234 int n = output;
235 while (OutputIsChange(wallet, ptx->vout[n]) && ptx->vin.size() > 0) {
236 const COutPoint &prevout = ptx->vin[0].prevout;
237 auto it = wallet.mapWallet.find(prevout.GetTxId());
238 if (it == wallet.mapWallet.end() ||
239 it->second.tx->vout.size() <= prevout.GetN() ||
240 !wallet.IsMine(it->second.tx->vout[prevout.GetN()])) {
241 break;
242 }
243 ptx = it->second.tx.get();
244 n = prevout.GetN();
245 }
246 return ptx->vout[n];
247}
248
249std::map<CTxDestination, std::vector<COutput>>
251 AssertLockHeld(wallet.cs_wallet);
252
253 std::map<CTxDestination, std::vector<COutput>> result;
254 std::vector<COutput> availableCoins;
255
256 AvailableCoins(wallet, availableCoins);
257
258 for (COutput &coin : availableCoins) {
259 CTxDestination address;
260 if ((coin.fSpendable ||
261 (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) &&
262 coin.fSolvable)) &&
264 FindNonChangeParentOutput(wallet, *coin.tx->tx, coin.i)
266 address)) {
267 result[address].emplace_back(std::move(coin));
268 }
269 }
270
271 std::vector<COutPoint> lockedCoins;
272 wallet.ListLockedCoins(lockedCoins);
273 // Include watch-only for LegacyScriptPubKeyMan wallets without private keys
274 const bool include_watch_only =
275 wallet.GetLegacyScriptPubKeyMan() &&
277 const isminetype is_mine_filter =
278 include_watch_only ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
279 for (const auto &output : lockedCoins) {
280 auto it = wallet.mapWallet.find(output.GetTxId());
281 if (it != wallet.mapWallet.end()) {
282 int depth = wallet.GetTxDepthInMainChain(it->second);
283 if (depth >= 0 && output.GetN() < it->second.tx->vout.size() &&
284 wallet.IsMine(it->second.tx->vout[output.GetN()]) ==
285 is_mine_filter) {
286 CTxDestination address;
288 *it->second.tx,
289 output.GetN())
291 address)) {
292 result[address].emplace_back(
293 wallet, it->second, output.GetN(), depth,
294 true /* spendable */, true /* solvable */,
295 false /* safe */);
296 }
297 }
298 }
299 }
300
301 return result;
302}
303
304std::vector<OutputGroup>
305GroupOutputs(const CWallet &wallet, const std::vector<COutput> &outputs,
306 bool separate_coins, const CFeeRate &effective_feerate,
307 const CFeeRate &long_term_feerate,
308 const CoinEligibilityFilter &filter, bool positive_only) {
309 std::vector<OutputGroup> groups_out;
310
311 if (separate_coins) {
312 // Single coin means no grouping. Each COutput gets its own OutputGroup.
313 for (const COutput &output : outputs) {
314 // Skip outputs we cannot spend
315 if (!output.fSpendable) {
316 continue;
317 }
318
319 CInputCoin input_coin = output.GetInputCoin();
320
321 // Make an OutputGroup containing just this output
322 OutputGroup group{effective_feerate, long_term_feerate};
323 group.Insert(input_coin, output.nDepth,
324 CachedTxIsFromMe(wallet, *output.tx, ISMINE_ALL),
325 positive_only);
326
327 // Check the OutputGroup's eligibility. Only add the eligible ones.
328 if (positive_only && group.effective_value <= Amount::zero()) {
329 continue;
330 }
331 if (group.m_outputs.size() > 0 &&
332 group.EligibleForSpending(filter)) {
333 groups_out.push_back(group);
334 }
335 }
336 return groups_out;
337 }
338
339 // We want to combine COutputs that have the same scriptPubKey into single
340 // OutputGroups except when there are more than OUTPUT_GROUP_MAX_ENTRIES
341 // COutputs grouped in an OutputGroup.
342 // To do this, we maintain a map where the key is the scriptPubKey and the
343 // value is a vector of OutputGroups.
344 // For each COutput, we check if the scriptPubKey is in the map, and if it
345 // is, the COutput's CInputCoin is added to the last OutputGroup in the
346 // vector for the scriptPubKey. When the last OutputGroup has
347 // OUTPUT_GROUP_MAX_ENTRIES CInputCoins, a new OutputGroup is added to the
348 // end of the vector.
349 std::map<CScript, std::vector<OutputGroup>> spk_to_groups_map;
350 for (const auto &output : outputs) {
351 // Skip outputs we cannot spend
352 if (!output.fSpendable) {
353 continue;
354 }
355
356 CInputCoin input_coin = output.GetInputCoin();
357 CScript spk = input_coin.txout.scriptPubKey;
358
359 std::vector<OutputGroup> &groups = spk_to_groups_map[spk];
360
361 if (groups.size() == 0) {
362 // No OutputGroups for this scriptPubKey yet, add one
363 groups.emplace_back(effective_feerate, long_term_feerate);
364 }
365
366 // Get the last OutputGroup in the vector so that we can add the
367 // CInputCoin to it.
368 // A pointer is used here so that group can be reassigned later if it
369 // is full.
370 OutputGroup *group = &groups.back();
371
372 // Check if this OutputGroup is full. We limit to
373 // OUTPUT_GROUP_MAX_ENTRIES when using -avoidpartialspends to avoid
374 // surprising users with very high fees.
375 if (group->m_outputs.size() >= OUTPUT_GROUP_MAX_ENTRIES) {
376 // The last output group is full, add a new group to the vector and
377 // use that group for the insertion
378 groups.emplace_back(effective_feerate, long_term_feerate);
379 group = &groups.back();
380 }
381
382 // Add the input_coin to group
383 group->Insert(input_coin, output.nDepth,
384 CachedTxIsFromMe(wallet, *output.tx, ISMINE_ALL),
385 positive_only);
386 }
387
388 // Now we go through the entire map and pull out the OutputGroups
389 for (const auto &spk_and_groups_pair : spk_to_groups_map) {
390 const std::vector<OutputGroup> &groups_per_spk =
391 spk_and_groups_pair.second;
392
393 // Go through the vector backwards. This allows for the first item we
394 // deal with being the partial group.
395 for (auto group_it = groups_per_spk.rbegin();
396 group_it != groups_per_spk.rend(); group_it++) {
397 const OutputGroup &group = *group_it;
398
399 // Don't include partial groups if there are full groups too and we
400 // don't want partial groups
401 if (group_it == groups_per_spk.rbegin() &&
402 groups_per_spk.size() > 1 && !filter.m_include_partial_groups) {
403 continue;
404 }
405
406 // Check the OutputGroup's eligibility. Only add the eligible ones.
407 if (positive_only && group.effective_value <= Amount::zero()) {
408 continue;
409 }
410 if (group.m_outputs.size() > 0 &&
411 group.EligibleForSpending(filter)) {
412 groups_out.push_back(group);
413 }
414 }
415 }
416
417 return groups_out;
418}
419
420bool SelectCoinsMinConf(const CWallet &wallet, const Amount nTargetValue,
421 const CoinEligibilityFilter &eligibility_filter,
422 std::vector<COutput> coins,
423 std::set<CInputCoin> &setCoinsRet, Amount &nValueRet,
425 bool &bnb_used) {
426 setCoinsRet.clear();
427 nValueRet = Amount::zero();
428
430 // Get long term estimate
431 CCoinControl temp;
432 temp.m_confirm_target = 1008;
433 CFeeRate long_term_feerate = GetMinimumFeeRate(wallet, temp);
434
435 // Get the feerate for effective value.
436 // When subtracting the fee from the outputs, we want the effective
437 // feerate to be 0
438 CFeeRate effective_feerate{Amount::zero()};
440 effective_feerate = coin_selection_params.effective_fee;
441 }
442
443 std::vector<OutputGroup> groups = GroupOutputs(
445 effective_feerate, long_term_feerate, eligibility_filter,
446 /*positive_only=*/true);
447
448 // Calculate cost of change
449 Amount cost_of_change = wallet.chain().relayDustFee().GetFee(
453
454 // Calculate the fees for things that aren't inputs
457 bnb_used = true;
458 return SelectCoinsBnB(groups, nTargetValue, cost_of_change, setCoinsRet,
459 nValueRet, not_input_fees);
460 } else {
461 std::vector<OutputGroup> groups = GroupOutputs(
464 eligibility_filter,
465 /*positive_only=*/false);
466
467 bnb_used = false;
468 return KnapsackSolver(nTargetValue, groups, setCoinsRet, nValueRet);
469 }
470}
471
473 const std::vector<COutput> &vAvailableCoins,
474 const Amount nTargetValue, std::set<CInputCoin> &setCoinsRet,
475 Amount &nValueRet, const CCoinControl &coin_control,
477 std::vector<COutput> vCoins(vAvailableCoins);
478 Amount value_to_select = nTargetValue;
479
480 // Default to bnb was not used. If we use it, we set it later
481 bnb_used = false;
482
483 // coin control -> return all selected outputs (we want all selected to go
484 // into the transaction for sure)
485 if (coin_control.HasSelected() && !coin_control.fAllowOtherInputs) {
486 for (const COutput &out : vCoins) {
487 if (!out.fSpendable) {
488 continue;
489 }
490
491 nValueRet += out.tx->tx->vout[out.i].nValue;
492 setCoinsRet.insert(out.GetInputCoin());
493 }
494
495 return (nValueRet >= nTargetValue);
496 }
497
498 // Calculate value from preset inputs and store them.
499 std::set<CInputCoin> setPresetCoins;
500 Amount nValueFromPresetInputs = Amount::zero();
501
502 std::vector<COutPoint> vPresetInputs;
503 coin_control.ListSelected(vPresetInputs);
504
505 for (const COutPoint &outpoint : vPresetInputs) {
506 std::map<TxId, CWalletTx>::const_iterator it =
507 wallet.mapWallet.find(outpoint.GetTxId());
508 if (it != wallet.mapWallet.end()) {
509 const CWalletTx &wtx = it->second;
510 // Clearly invalid input, fail
511 if (wtx.tx->vout.size() <= outpoint.GetN()) {
512 return false;
513 }
514 // Just to calculate the marginal byte size
515 CInputCoin coin(
516 wtx.tx, outpoint.GetN(),
517 GetTxSpendSize(wallet, wtx, outpoint.GetN(), false));
518 nValueFromPresetInputs += coin.txout.nValue;
519 if (coin.m_input_bytes <= 0) {
520 // Not solvable, can't estimate size for fee
521 return false;
522 }
523 coin.effective_value =
524 coin.txout.nValue -
527 value_to_select -= coin.effective_value;
528 } else {
529 value_to_select -= coin.txout.nValue;
530 }
531 setPresetCoins.insert(coin);
532 } else {
533 return false; // TODO: Allow non-wallet inputs
534 }
535 }
536
537 // Remove preset inputs from vCoins
538 for (std::vector<COutput>::iterator it = vCoins.begin();
539 it != vCoins.end() && coin_control.HasSelected();) {
540 if (setPresetCoins.count(it->GetInputCoin())) {
541 it = vCoins.erase(it);
542 } else {
543 ++it;
544 }
545 }
546
547 // form groups from remaining coins; note that preset coins will not
548 // automatically have their associated (same address) coins included
549 if (coin_control.m_avoid_partial_spends &&
551 // Cases where we have 11+ outputs all pointing to the same destination
552 // may result in privacy leaks as they will potentially be
553 // deterministically sorted. We solve that by explicitly shuffling the
554 // outputs before processing
555 Shuffle(vCoins.begin(), vCoins.end(), FastRandomContext());
556 }
557
558 bool res =
559 value_to_select <= Amount::zero() ||
560 SelectCoinsMinConf(wallet, value_to_select, CoinEligibilityFilter(1, 6),
561 vCoins, setCoinsRet, nValueRet,
562 coin_selection_params, bnb_used) ||
563 SelectCoinsMinConf(wallet, value_to_select, CoinEligibilityFilter(1, 1),
564 vCoins, setCoinsRet, nValueRet,
565 coin_selection_params, bnb_used) ||
566 (wallet.m_spend_zero_conf_change &&
567 SelectCoinsMinConf(wallet, value_to_select,
568 CoinEligibilityFilter(0, 1), vCoins, setCoinsRet,
569 nValueRet, coin_selection_params, bnb_used)) ||
570 (wallet.m_spend_zero_conf_change &&
571 SelectCoinsMinConf(wallet, value_to_select,
572 CoinEligibilityFilter(0, 1), vCoins, setCoinsRet,
573 nValueRet, coin_selection_params, bnb_used)) ||
574 (wallet.m_spend_zero_conf_change &&
575 SelectCoinsMinConf(wallet, value_to_select,
576 CoinEligibilityFilter(0, 1), vCoins, setCoinsRet,
577 nValueRet, coin_selection_params, bnb_used)) ||
578 (wallet.m_spend_zero_conf_change &&
580 wallet, value_to_select,
581 CoinEligibilityFilter(0, 1, /*include_partial_groups=*/true),
582 vCoins, setCoinsRet, nValueRet, coin_selection_params,
583 bnb_used)) ||
584 (wallet.m_spend_zero_conf_change &&
586 wallet, value_to_select,
587 CoinEligibilityFilter(0, 1, /*include_partial_groups=*/true),
588 vCoins, setCoinsRet, nValueRet, coin_selection_params,
589 bnb_used)) ||
590 // Try with unsafe inputs if they are allowed. This may spend
591 // unconfirmed outputs received from other wallets.
592 (coin_control.m_include_unsafe_inputs &&
594 wallet, value_to_select,
595 CoinEligibilityFilter(/*conf_mine=*/0, /*conf_theirs=*/0,
596 /*include_partial_groups=*/true),
597 vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used));
598
599 // Because SelectCoinsMinConf clears the setCoinsRet, we now add the
600 // possible inputs to the coinset.
601 util::insert(setCoinsRet, setPresetCoins);
602
603 // Add preset inputs to the total value selected.
604 nValueRet += nValueFromPresetInputs;
605
606 return res;
607}
608
610 CWallet &wallet, const std::vector<CRecipient> &vecSend, int change_pos,
611 const CCoinControl &coin_control, bool sign) {
612 // TODO: remember to add the lock annotation when adding AssertLockHeld.
613 // The lock annotation was added by core in PR22100, but due to
614 // other missing backports it was not possible to add it during that
615 // backport.
616
617 // out variables, to be packed into returned result structure
618 Amount nFeeRet;
619 int nChangePosInOut = change_pos;
620
621 Amount nValue = Amount::zero();
622 const OutputType change_type = wallet.TransactionChangeType(
623 coin_control.m_change_type ? *coin_control.m_change_type
624 : wallet.m_default_change_type,
625 vecSend);
626 ReserveDestination reservedest(&wallet, change_type);
627 int nChangePosRequest = nChangePosInOut;
628 unsigned int nSubtractFeeFromAmount = 0;
629 for (const auto &recipient : vecSend) {
630 if (nValue < Amount::zero() || recipient.nAmount < Amount::zero()) {
631 return util::Error{_("Transaction amounts must not be negative")};
632 }
633
634 nValue += recipient.nAmount;
635
636 if (recipient.fSubtractFeeFromAmount) {
637 nSubtractFeeFromAmount++;
638 }
639 }
640
641 if (vecSend.empty()) {
642 return util::Error{_("Transaction must have at least one recipient")};
643 }
644
646 {
648 std::set<CInputCoin> setCoins;
649 LOCK(wallet.cs_wallet);
650 // Previously the locktime was set to the current block height, to
651 // prevent fee-sniping. This is now disabled as fee-sniping is mitigated
652 // by avalanche post-consensus. Consistently Using a locktime of 0 for
653 // most wallets in the ecosystem improves privacy, as this is the
654 // easiest solution to implement for light wallets which are not aware
655 // of the current block height.
656 txNew.nLockTime = 0;
657 std::vector<COutput> vAvailableCoins;
658 AvailableCoins(wallet, vAvailableCoins, &coin_control);
659 // Parameters for coin selection, init with dummy
662 coin_control.m_avoid_partial_spends;
663
664 // Create change script that will be used if we need change
665 // TODO: pass in scriptChange instead of reservedest so
666 // change transaction isn't always pay-to-bitcoin-address
667 CScript scriptChange;
668 bilingual_str error; // possible error str
669
670 // coin control: send change to custom address
671 if (!std::get_if<CNoDestination>(&coin_control.destChange)) {
672 scriptChange = GetScriptForDestination(coin_control.destChange);
673
674 // no coin control: send change to newly generated address
675 } else {
676 // Note: We use a new key here to keep it from being obvious
677 // which side is the change.
678 // The drawback is that by not reusing a previous key, the
679 // change may be lost if a backup is restored, if the backup
680 // doesn't have the new private key for the change. If we
681 // reused the old key, it would be possible to add code to look
682 // for and rediscover unknown transactions that were written
683 // with keys of ours to recover post-backup change.
684
685 // Reserve a new key pair from key pool. If it fails, provide a
686 // dummy destination in case we don't need change.
687 CTxDestination dest;
688 if (!reservedest.GetReservedDestination(dest, true)) {
689 error = _("Transaction needs a change address, but we can't "
690 "generate it. Please call keypoolrefill first.");
691 }
692
693 scriptChange = GetScriptForDestination(dest);
694 // A valid destination implies a change script (and
695 // vice-versa). An empty change script will abort later, if the
696 // change keypool ran out, but change is required.
697 CHECK_NONFATAL(IsValidDestination(dest) != scriptChange.empty());
698 }
699 CTxOut change_prototype_txout(Amount::zero(), scriptChange);
701 GetSerializeSize(change_prototype_txout);
702
703 // Get the fee rate to use effective values in coin selection
704 CFeeRate nFeeRateNeeded = GetMinimumFeeRate(wallet, coin_control);
705 // Do not, ever, assume that it's fine to change the fee rate if the
706 // user has explicitly provided one
707 if (coin_control.m_feerate &&
708 nFeeRateNeeded > *coin_control.m_feerate) {
709 return util::Error{strprintf(
710 _("Fee rate (%s) is lower than the minimum fee "
711 "rate setting (%s)"),
712 coin_control.m_feerate->ToString(), nFeeRateNeeded.ToString())};
713 }
714
715 nFeeRet = Amount::zero();
716 bool pick_new_inputs = true;
717 Amount nValueIn = Amount::zero();
718
719 // BnB selector is the only selector used when this is true.
720 // That should only happen on the first pass through the loop.
722 // If we are doing subtract fee from recipient, don't use effective
723 // values
725 nSubtractFeeFromAmount != 0;
726 // Start with no fee and loop until there is enough fee
727 while (true) {
728 nChangePosInOut = nChangePosRequest;
729 txNew.vin.clear();
730 txNew.vout.clear();
731 bool fFirst = true;
732
733 Amount nValueToSelect = nValue;
734 if (nSubtractFeeFromAmount == 0) {
735 nValueToSelect += nFeeRet;
736 }
737
738 // vouts to the payees
740 // Static size overhead + outputs vsize. 4 nVersion, 4
741 // nLocktime, 1 input count, 1 output count
743 }
744 // vouts to the payees
745 for (const auto &recipient : vecSend) {
746 CTxOut txout(recipient.nAmount, recipient.scriptPubKey);
747
748 if (recipient.fSubtractFeeFromAmount) {
749 assert(nSubtractFeeFromAmount != 0);
750 // Subtract fee equally from each selected recipient.
751 txout.nValue -= nFeeRet / int(nSubtractFeeFromAmount);
752
753 // First receiver pays the remainder not divisible by output
754 // count.
755 if (fFirst) {
756 fFirst = false;
757 txout.nValue -= nFeeRet % int(nSubtractFeeFromAmount);
758 }
759 }
760
761 // Include the fee cost for outputs. Note this is only used for
762 // BnB right now
766 }
767
768 if (IsDust(txout, wallet.chain().relayDustFee())) {
769 if (recipient.fSubtractFeeFromAmount &&
770 nFeeRet > Amount::zero()) {
771 if (txout.nValue < Amount::zero()) {
772 error = _("The transaction amount is too small to "
773 "pay the fee");
774 } else {
775 error = _("The transaction amount is too small to "
776 "send after the fee has been deducted");
777 }
778 } else {
779 error = _("Transaction amount too small");
780 }
781
782 return util::Error{error};
783 }
784
785 txNew.vout.push_back(txout);
786 }
787
788 // Choose coins to use
789 bool bnb_used = false;
790 if (pick_new_inputs) {
791 nValueIn = Amount::zero();
792 setCoins.clear();
793 int change_spend_size = CalculateMaximumSignedInputSize(
794 change_prototype_txout, &wallet);
795 // If the wallet doesn't know how to sign change output, assume
796 // p2pkh as lower-bound to allow BnB to do it's thing
797 if (change_spend_size == -1) {
800 } else {
802 size_t(change_spend_size);
803 }
804 coin_selection_params.effective_fee = nFeeRateNeeded;
805 if (!SelectCoins(wallet, vAvailableCoins, nValueToSelect,
806 setCoins, nValueIn, coin_control,
807 coin_selection_params, bnb_used)) {
808 // If BnB was used, it was the first pass. No longer the
809 // first pass and continue loop with knapsack.
810 if (bnb_used) {
812 continue;
813 } else {
814 return util::Error{_("Insufficient funds")};
815 }
816 }
817 } else {
818 bnb_used = false;
819 }
820
821 const Amount nChange = nValueIn - nValueToSelect;
822 if (nChange > Amount::zero()) {
823 // Fill a vout to ourself.
824 CTxOut newTxOut(nChange, scriptChange);
825
826 // Never create dust outputs; if we would, just add the dust to
827 // the fee.
828 // The nChange when BnB is used is always going to go to fees.
829 if (IsDust(newTxOut, wallet.chain().relayDustFee()) ||
830 bnb_used) {
831 nChangePosInOut = -1;
832 nFeeRet += nChange;
833 } else {
834 if (nChangePosInOut == -1) {
835 // Insert change txn at random position:
836 nChangePosInOut = GetRand<int>(txNew.vout.size() + 1);
837 } else if ((unsigned int)nChangePosInOut >
838 txNew.vout.size()) {
839 return util::Error{_("Change index out of range")};
840 }
841
842 std::vector<CTxOut>::iterator position =
843 txNew.vout.begin() + nChangePosInOut;
844 txNew.vout.insert(position, newTxOut);
845 }
846 } else {
847 nChangePosInOut = -1;
848 }
849
850 // Dummy fill vin for maximum size estimation
851 //
852 for (const auto &coin : setCoins) {
853 txNew.vin.push_back(CTxIn(coin.outpoint, CScript()));
854 }
855
856 CTransaction txNewConst(txNew);
857 int nBytes = CalculateMaximumSignedTxSize(
858 txNewConst, &wallet, coin_control.fAllowWatchOnly);
859 if (nBytes < 0) {
860 return util::Error{_("Signing transaction failed")};
861 }
862
863 Amount nFeeNeeded = GetMinimumFee(wallet, nBytes, coin_control);
864
865 if (nFeeRet >= nFeeNeeded) {
866 // Reduce fee to only the needed amount if possible. This
867 // prevents potential overpayment in fees if the coins selected
868 // to meet nFeeNeeded result in a transaction that requires less
869 // fee than the prior iteration.
870
871 // If we have no change and a big enough excess fee, then try to
872 // construct transaction again only without picking new inputs.
873 // We now know we only need the smaller fee (because of reduced
874 // tx size) and so we should add a change output. Only try this
875 // once.
876 if (nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 &&
877 pick_new_inputs) {
878 // Add 2 as a buffer in case increasing # of outputs changes
879 // compact size
880 unsigned int tx_size_with_change =
882 Amount fee_needed_with_change = GetMinimumFee(
883 wallet, tx_size_with_change, coin_control);
884 Amount minimum_value_for_change = GetDustThreshold(
885 change_prototype_txout, wallet.chain().relayDustFee());
886 if (nFeeRet >=
887 fee_needed_with_change + minimum_value_for_change) {
888 pick_new_inputs = false;
889 nFeeRet = fee_needed_with_change;
890 continue;
891 }
892 }
893
894 // If we have change output already, just increase it
895 if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 &&
896 nSubtractFeeFromAmount == 0) {
897 Amount extraFeePaid = nFeeRet - nFeeNeeded;
898 std::vector<CTxOut>::iterator change_position =
899 txNew.vout.begin() + nChangePosInOut;
900 change_position->nValue += extraFeePaid;
901 nFeeRet -= extraFeePaid;
902 }
903
904 // Done, enough fee included.
905 break;
906 } else if (!pick_new_inputs) {
907 // This shouldn't happen, we should have had enough excess fee
908 // to pay for the new output and still meet nFeeNeeded.
909 // Or we should have just subtracted fee from recipients and
910 // nFeeNeeded should not have changed.
911 return util::Error{
912 _("Transaction fee and change calculation failed")};
913 }
914
915 // Try to reduce change to include necessary fee.
916 if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
917 Amount additionalFeeNeeded = nFeeNeeded - nFeeRet;
918 std::vector<CTxOut>::iterator change_position =
919 txNew.vout.begin() + nChangePosInOut;
920 // Only reduce change if remaining amount is still a large
921 // enough output.
922 if (change_position->nValue >=
923 MIN_FINAL_CHANGE + additionalFeeNeeded) {
924 change_position->nValue -= additionalFeeNeeded;
925 nFeeRet += additionalFeeNeeded;
926 // Done, able to increase fee from change.
927 break;
928 }
929 }
930
931 // If subtracting fee from recipients, we now know what fee we
932 // need to subtract, we have no reason to reselect inputs.
933 if (nSubtractFeeFromAmount > 0) {
934 pick_new_inputs = false;
935 }
936
937 // Include more fee and try again.
938 nFeeRet = nFeeNeeded;
940 continue;
941 }
942
943 // Give up if change keypool ran out and change is required
944 if (scriptChange.empty() && nChangePosInOut != -1) {
945 return util::Error{error};
946 }
947
948 // Shuffle selected coins and fill in final vin
949 txNew.vin.clear();
950 std::vector<CInputCoin> selected_coins(setCoins.begin(),
951 setCoins.end());
952 Shuffle(selected_coins.begin(), selected_coins.end(),
954
955 // Note how the sequence number is set to non-maxint so that
956 // the nLockTime set above actually works.
957 for (const auto &coin : selected_coins) {
958 txNew.vin.push_back(
959 CTxIn(coin.outpoint, CScript(),
960 std::numeric_limits<uint32_t>::max() - 1));
961 }
962
963 if (sign && !wallet.SignTransaction(txNew)) {
964 return util::Error{_("Signing transaction failed")};
965 }
966
967 // Return the constructed transaction data.
968 tx = MakeTransactionRef(std::move(txNew));
969
970 // Limit size.
971 if (tx->GetTotalSize() > MAX_STANDARD_TX_SIZE) {
972 return util::Error{_("Transaction too large")};
973 }
974 }
975
976 if (nFeeRet > wallet.m_default_max_tx_fee) {
977 return util::Error{
979 }
980
981 // Before we return success, we assume any change key will be used to
982 // prevent accidental re-use.
983 reservedest.KeepDestination();
984
985 return CreatedTransactionResult(tx, nFeeRet, nChangePosInOut);
986}
987
989CreateTransaction(CWallet &wallet, const std::vector<CRecipient> &vecSend,
990 int change_pos, const CCoinControl &coin_control, bool sign) {
991 auto res = CreateTransactionInternal(wallet, vecSend, change_pos,
992 coin_control, sign);
993 if (!res) {
994 return res;
995 }
996 const auto &txr_ungrouped = *res;
997 // try with avoidpartialspends unless it's enabled already
998 // 0 as fee means non-functional fee rate estimation
999 if (txr_ungrouped.fee > Amount::zero() &&
1000 wallet.m_max_aps_fee > (-1 * SATOSHI) &&
1001 !coin_control.m_avoid_partial_spends) {
1002 CCoinControl tmp_cc = coin_control;
1003 tmp_cc.m_avoid_partial_spends = true;
1004 // fired and forgotten; if an error occurs, we discard the results
1005 bilingual_str error2;
1006 auto txr_grouped = CreateTransactionInternal(wallet, vecSend,
1007 change_pos, tmp_cc, sign);
1008 if (txr_grouped) {
1009 // if fee of this alternative one is within the range of the max
1010 // fee, we use this one
1011 const bool use_aps =
1012 txr_grouped->fee <= txr_ungrouped.fee + wallet.m_max_aps_fee;
1013 wallet.WalletLogPrintf(
1014 "Fee non-grouped = %lld, grouped = %lld, using %s\n",
1015 txr_ungrouped.fee, txr_grouped->fee,
1016 use_aps ? "grouped" : "non-grouped");
1017 if (use_aps) {
1018 return txr_grouped;
1019 }
1020 }
1021 }
1022 return res;
1023}
1024
1026 int &nChangePosInOut, bilingual_str &error,
1027 bool lockUnspents,
1028 const std::set<int> &setSubtractFeeFromOutputs,
1029 CCoinControl coinControl) {
1030 std::vector<CRecipient> vecSend;
1031
1032 // Turn the txout set into a CRecipient vector.
1033 for (size_t idx = 0; idx < tx.vout.size(); idx++) {
1034 const CTxOut &txOut = tx.vout[idx];
1035 CRecipient recipient = {txOut.scriptPubKey, txOut.nValue,
1036 setSubtractFeeFromOutputs.count(idx) == 1};
1037 vecSend.push_back(recipient);
1038 }
1039
1040 coinControl.fAllowOtherInputs = true;
1041
1042 for (const CTxIn &txin : tx.vin) {
1043 coinControl.Select(txin.prevout);
1044 }
1045
1046 // Acquire the locks to prevent races to the new locked unspents between the
1047 // CreateTransaction call and LockCoin calls (when lockUnspents is true).
1048 LOCK(wallet.cs_wallet);
1049
1050 auto res =
1051 CreateTransaction(wallet, vecSend, nChangePosInOut, coinControl, false);
1052 if (!res) {
1053 error = util::ErrorString(res);
1054 return false;
1055 }
1056 const auto &txr = *res;
1057 CTransactionRef tx_new = txr.tx;
1058 nFeeRet = txr.fee;
1059 nChangePosInOut = txr.change_pos;
1060
1061 if (nChangePosInOut != -1) {
1062 tx.vout.insert(tx.vout.begin() + nChangePosInOut,
1063 tx_new->vout[nChangePosInOut]);
1064 }
1065
1066 // Copy output sizes from new transaction; they may have had the fee
1067 // subtracted from them.
1068 for (size_t idx = 0; idx < tx.vout.size(); idx++) {
1069 tx.vout[idx].nValue = tx_new->vout[idx].nValue;
1070 }
1071
1072 // Add new txins (keeping original txin scriptSig/order)
1073 for (const CTxIn &txin : tx_new->vin) {
1074 if (!coinControl.IsSelected(txin.prevout)) {
1075 tx.vin.push_back(txin);
1076 }
1077 if (lockUnspents) {
1078 wallet.LockCoin(txin.prevout);
1079 }
1080 }
1081
1082 return true;
1083}
static constexpr Amount SATOSHI
Definition: amount.h:143
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:165
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:53
Coin Control Features.
Definition: coincontrol.h:21
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition: coincontrol.h:25
bool HasSelected() const
Definition: coincontrol.h:54
std::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition: coincontrol.h:40
bool IsSelected(const COutPoint &output) const
Definition: coincontrol.h:56
int m_max_depth
Maximum chain depth value for coin availability.
Definition: coincontrol.h:48
void Select(const COutPoint &output)
Definition: coincontrol.h:60
bool fAllowWatchOnly
Includes watch only addresses which are solvable.
Definition: coincontrol.h:34
int m_min_depth
Minimum chain depth value for coin availability.
Definition: coincontrol.h:46
std::optional< CFeeRate > m_feerate
Override the wallet's m_pay_tx_fee if set.
Definition: coincontrol.h:38
bool m_add_inputs
If false, only selected inputs are used.
Definition: coincontrol.h:27
CTxDestination destChange
Definition: coincontrol.h:23
bool m_avoid_address_reuse
Forbids inclusion of dirty (previously used) addresses.
Definition: coincontrol.h:44
bool m_include_unsafe_inputs
If false, only safe inputs will be used (confirmed or self transfers)
Definition: coincontrol.h:29
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
Definition: coincontrol.h:42
bool fAllowOtherInputs
If false, allows unselected inputs, but requires all selected inputs be used.
Definition: coincontrol.h:32
void ListSelected(std::vector< COutPoint > &vOutpoints) const
Definition: coincontrol.h:66
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
std::string ToString() const
Definition: feerate.cpp:57
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:49
int m_input_bytes
Pre-computed estimated size of this output as a fully-signed input in a transaction.
Definition: coinselection.h:47
CTxOut txout
Definition: coinselection.h:38
Amount effective_value
Definition: coinselection.h:39
A mutable version of CTransaction.
Definition: transaction.h:274
std::vector< CTxOut > vout
Definition: transaction.h:277
std::vector< CTxIn > vin
Definition: transaction.h:276
Definition: spend.h:22
int nDepth
Definition: spend.h:26
const CWalletTx * tx
Definition: spend.h:24
std::string ToString() const
Definition: spend.cpp:29
int i
Definition: spend.h:25
An output of a transaction.
Definition: transaction.h:128
CScript scriptPubKey
Definition: transaction.h:131
Amount nValue
Definition: transaction.h:130
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:269
A transaction with a bunch of additional info that only the owner cares about.
Definition: transaction.h:65
mapValue_t mapValue
Key/value map with information about the transaction.
Definition: transaction.h:99
CTransactionRef tx
Definition: transaction.h:160
TxId GetId() const
Definition: transaction.h:301
bool InMempool() const
Definition: transaction.cpp:21
Fast randomness source.
Definition: random.h:156
A wrapper to reserve an address from a wallet.
Definition: wallet.h:176
std::string ToString() const
Definition: uint256.h:80
const int DEFAULT_MAX_DEPTH
Definition: coincontrol.h:15
const int DEFAULT_MIN_DEPTH
Definition: coincontrol.h:14
bool KnapsackSolver(const Amount nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet)
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const Amount &target_value, const Amount &cost_of_change, std::set< CInputCoin > &out_set, Amount &value_ret, const Amount not_input_fees)
This is the Branch and Bound Coin Selection algorithm designed by Murch.
static const Amount MIN_FINAL_CHANGE
final minimum change amount after paying for fees
Definition: coinselection.h:15
static std::vector< COutput > vCoins
CoinSelectionParams coin_selection_params(false, 0, 0, CFeeRate(Amount::zero()), 0, false)
static Amount balance
bilingual_str TransactionErrorString(const TransactionError error)
Definition: error.cpp:11
void KeepDestination()
Keep the address.
Definition: wallet.cpp:2499
bool GetReservedDestination(CTxDestination &pubkey, bool internal)
Reserve an address.
Definition: wallet.cpp:2478
isminetype
IsMine() return codes.
Definition: ismine.h:18
@ ISMINE_ALL
Definition: ismine.h:23
@ ISMINE_SPENDABLE
Definition: ismine.h:21
@ ISMINE_NO
Definition: ismine.h:19
@ ISMINE_WATCH_ONLY
Definition: ismine.h:20
std::string FormatMoney(const Amount amt)
Do not use these functions to represent or parse monetary amounts to or from JSON but use AmountFromV...
Definition: moneystr.cpp:13
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:90
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
Definition: insert.h:14
OutputType
Definition: outputtype.h:16
Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:14
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:34
static constexpr unsigned int MAX_STANDARD_TX_SIZE
The maximum size for transactions we're willing to relay/mine.
Definition: policy.h:34
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:316
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
Definition: random.h:297
bool CachedTxIsFromMe(const CWallet &wallet, const CWalletTx &wtx, const isminefilter &filter)
Definition: receive.cpp:321
bool OutputIsChange(const CWallet &wallet, const CTxOut &txout)
Definition: receive.cpp:98
bool CachedTxIsTrusted(const CWallet &wallet, const CWalletTx &wtx, std::set< TxId > &trusted_parents)
Definition: receive.cpp:326
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1207
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Check whether we know how to sign for an output like this, assuming we have all private keys.
Definition: sign.cpp:424
int CalculateMaximumSignedInputSize(const CTxOut &txout, const CWallet *wallet, bool use_max_sig)
Get the marginal bytes of spending the specified output.
Definition: spend.cpp:34
bool SelectCoinsMinConf(const CWallet &wallet, const Amount nTargetValue, const CoinEligibilityFilter &eligibility_filter, std::vector< COutput > coins, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet, const CoinSelectionParams &coin_selection_params, bool &bnb_used)
Shuffle and select coins until nTargetValue is reached while avoiding small change; This method is st...
Definition: spend.cpp:420
bool FundTransaction(CWallet &wallet, CMutableTransaction &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, bool lockUnspents, const std::set< int > &setSubtractFeeFromOutputs, CCoinControl coinControl)
Insert additional inputs into the transaction by calling CreateTransaction();.
Definition: spend.cpp:1025
std::map< CTxDestination, std::vector< COutput > > ListCoins(const CWallet &wallet)
Return list of available coins and locked coins grouped by non-change output address.
Definition: spend.cpp:250
int64_t CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector< CTxOut > &txouts, bool use_max_sig)
Calculate the size of the transaction assuming all signatures are max size Use DummySignatureCreator,...
Definition: spend.cpp:45
const CTxOut & FindNonChangeParentOutput(const CWallet &wallet, const CTransaction &tx, int output)
Find non-change parent output.
Definition: spend.cpp:230
std::vector< OutputGroup > GroupOutputs(const CWallet &wallet, const std::vector< COutput > &outputs, bool separate_coins, const CFeeRate &effective_feerate, const CFeeRate &long_term_feerate, const CoinEligibilityFilter &filter, bool positive_only)
Definition: spend.cpp:305
static const size_t OUTPUT_GROUP_MAX_ENTRIES
Definition: spend.cpp:22
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, int change_pos, const CCoinControl &coin_control, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: spend.cpp:989
void AvailableCoins(const CWallet &wallet, std::vector< COutput > &vCoins, const CCoinControl *coinControl, const Amount nMinimumAmount, const Amount nMaximumAmount, const Amount nMinimumSumAmount, const uint64_t nMaximumCount)
populate vCoins with vector of available COutputs.
Definition: spend.cpp:71
static util::Result< CreatedTransactionResult > CreateTransactionInternal(CWallet &wallet, const std::vector< CRecipient > &vecSend, int change_pos, const CCoinControl &coin_control, bool sign)
Definition: spend.cpp:609
int GetTxSpendSize(const CWallet &wallet, const CWalletTx &wtx, unsigned int out, bool use_max_sig)
Get the marginal bytes if spending the specified output from this transaction.
Definition: spend.cpp:24
Amount GetAvailableBalance(const CWallet &wallet, const CCoinControl *coinControl)
Definition: spend.cpp:215
bool SelectCoins(const CWallet &wallet, const std::vector< COutput > &vAvailableCoins, const Amount nTargetValue, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet, const CCoinControl &coin_control, CoinSelectionParams &coin_selection_params, bool &bnb_used)
Select a set of coins such that nValueRet >= nTargetValue and at least all coins from coin_control ar...
Definition: spend.cpp:472
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:158
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:260
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:240
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
const bool m_include_partial_groups
Include partial destination groups when avoid_reuse and there are full groups.
Definition: coinselection.h:71
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
Definition: wallet.h:248
size_t change_spend_size
Definition: wallet.h:244
size_t tx_noinputs_size
Definition: wallet.h:246
bool m_avoid_partial_spends
Definition: wallet.h:249
CFeeRate effective_fee
Definition: wallet.h:245
size_t change_output_size
Definition: wallet.h:243
std::vector< CInputCoin > m_outputs
Definition: coinselection.h:82
void Insert(const CInputCoin &output, int depth, bool from_me, bool positive_only)
Amount effective_value
Definition: coinselection.h:86
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const
A TxId is the identifier of a transaction.
Definition: txid.h:14
Bilingual messages:
Definition: translation.h:17
#define LOCK(cs)
Definition: sync.h:306
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
Amount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_control)
Estimate the minimum fee considering user set parameters and the required fee.
Definition: fees.cpp:17
CFeeRate GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:26
static constexpr size_t DUMMY_P2PKH_INPUT_SIZE
Pre-calculated constants for input size estimation.
Definition: wallet.h:130
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
Definition: walletutil.h:55
@ WALLET_FLAG_AVOID_REUSE
Definition: walletutil.h:47