Bitcoin ABC  0.26.3
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>
9 #include <consensus/validation.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 
23 
24 static const size_t OUTPUT_GROUP_MAX_ENTRIES = 10;
25 
26 int GetTxSpendSize(const CWallet &wallet, const CWalletTx &wtx,
27  unsigned int out, bool use_max_sig) {
28  return CalculateMaximumSignedInputSize(wtx.tx->vout[out], &wallet,
29  use_max_sig);
30 }
31 std::string COutput::ToString() const {
32  return strprintf("COutput(%s, %d, %d) [%s]", tx->GetId().ToString(), i,
33  nDepth, FormatMoney(tx->tx->vout[i].nValue));
34 }
35 
37  bool use_max_sig) {
39  txn.vin.push_back(CTxIn(COutPoint()));
40  if (!wallet->DummySignInput(txn.vin[0], txout, use_max_sig)) {
41  return -1;
42  }
43  return GetSerializeSize(txn.vin[0], PROTOCOL_VERSION);
44 }
45 
46 // txouts needs to be in the order of tx.vin
48  const CWallet *wallet,
49  const std::vector<CTxOut> &txouts,
50  bool use_max_sig) {
51  CMutableTransaction txNew(tx);
52  if (!wallet->DummySignTx(txNew, txouts, use_max_sig)) {
53  return -1;
54  }
55  return GetSerializeSize(txNew, PROTOCOL_VERSION);
56 }
57 
59  const CWallet *wallet, bool use_max_sig) {
60  std::vector<CTxOut> txouts;
61  for (auto &input : tx.vin) {
62  const auto mi = wallet->mapWallet.find(input.prevout.GetTxId());
63  // Can not estimate size without knowing the input details
64  if (mi == wallet->mapWallet.end()) {
65  return -1;
66  }
67  assert(input.prevout.GetN() < mi->second.tx->vout.size());
68  txouts.emplace_back(mi->second.tx->vout[input.prevout.GetN()]);
69  }
70  return CalculateMaximumSignedTxSize(tx, wallet, txouts, use_max_sig);
71 }
72 
73 void AvailableCoins(const CWallet &wallet, std::vector<COutput> &vCoins,
74  const CCoinControl *coinControl,
75  const Amount nMinimumAmount, const Amount nMaximumAmount,
76  const Amount nMinimumSumAmount,
77  const uint64_t nMaximumCount) {
78  AssertLockHeld(wallet.cs_wallet);
79 
80  vCoins.clear();
81  Amount nTotal = Amount::zero();
82  // Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we
83  // always allow), or we default to avoiding, and only in the case where a
84  // coin control object is provided, and has the avoid address reuse flag set
85  // to false, do we allow already used addresses
86  bool allow_used_addresses =
87  !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE) ||
88  (coinControl && !coinControl->m_avoid_address_reuse);
89  const int min_depth = {coinControl ? coinControl->m_min_depth
91  const int max_depth = {coinControl ? coinControl->m_max_depth
93  const bool only_safe = {coinControl ? !coinControl->m_include_unsafe_inputs
94  : true};
95 
96  std::set<TxId> trusted_parents;
97  for (const auto &entry : wallet.mapWallet) {
98  const TxId &wtxid = entry.first;
99  const CWalletTx &wtx = entry.second;
100 
101  if (wallet.IsTxImmatureCoinBase(wtx)) {
102  continue;
103  }
104 
105  int nDepth = wallet.GetTxDepthInMainChain(wtx);
106  if (nDepth < 0) {
107  continue;
108  }
109 
110  // We should not consider coins which aren't at least in our mempool.
111  // It's possible for these to be conflicted via ancestors which we may
112  // never be able to detect.
113  if (nDepth == 0 && !wtx.InMempool()) {
114  continue;
115  }
116 
117  bool safeTx = CachedTxIsTrusted(wallet, wtx, trusted_parents);
118 
119  // Bitcoin-ABC: Removed check that prevents consideration of coins from
120  // transactions that are replacing other transactions. This check based
121  // on wtx.mapValue.count("replaces_txid") which was not being set
122  // anywhere.
123 
124  // Similarly, we should not consider coins from transactions that have
125  // been replaced. In the example above, we would want to prevent
126  // creation of a transaction A' spending an output of A, because if
127  // transaction B were initially confirmed, conflicting with A and A', we
128  // wouldn't want to the user to create a transaction D intending to
129  // replace A', but potentially resulting in a scenario where A, A', and
130  // D could all be accepted (instead of just B and D, or just A and A'
131  // like the user would want).
132 
133  // Bitcoin-ABC: retained this check as 'replaced_by_txid' is still set
134  // in the wallet code.
135  if (nDepth == 0 && wtx.mapValue.count("replaced_by_txid")) {
136  safeTx = false;
137  }
138 
139  if (only_safe && !safeTx) {
140  continue;
141  }
142 
143  if (nDepth < min_depth || nDepth > max_depth) {
144  continue;
145  }
146 
147  for (uint32_t i = 0; i < wtx.tx->vout.size(); i++) {
148  // Only consider selected coins if add_inputs is false
149  if (coinControl && !coinControl->m_add_inputs &&
150  !coinControl->IsSelected(COutPoint(entry.first, i))) {
151  continue;
152  }
153 
154  if (wtx.tx->vout[i].nValue < nMinimumAmount ||
155  wtx.tx->vout[i].nValue > nMaximumAmount) {
156  continue;
157  }
158 
159  const COutPoint outpoint(wtxid, i);
160 
161  if (coinControl && coinControl->HasSelected() &&
162  !coinControl->fAllowOtherInputs &&
163  !coinControl->IsSelected(outpoint)) {
164  continue;
165  }
166 
167  if (wallet.IsLockedCoin(outpoint)) {
168  continue;
169  }
170 
171  if (wallet.IsSpent(outpoint)) {
172  continue;
173  }
174 
175  isminetype mine = wallet.IsMine(wtx.tx->vout[i]);
176 
177  if (mine == ISMINE_NO) {
178  continue;
179  }
180 
181  if (!allow_used_addresses && wallet.IsSpentKey(wtxid, i)) {
182  continue;
183  }
184 
185  std::unique_ptr<SigningProvider> provider =
186  wallet.GetSolvingProvider(wtx.tx->vout[i].scriptPubKey);
187 
188  bool solvable =
189  provider ? IsSolvable(*provider, wtx.tx->vout[i].scriptPubKey)
190  : false;
191  bool spendable =
192  ((mine & ISMINE_SPENDABLE) != ISMINE_NO) ||
193  (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) &&
194  (coinControl && coinControl->fAllowWatchOnly && solvable));
195 
196  vCoins.push_back(
197  COutput(wallet, wtx, i, nDepth, spendable, solvable, safeTx,
198  (coinControl && coinControl->fAllowWatchOnly)));
199 
200  // Checks the sum amount of all UTXO's.
201  if (nMinimumSumAmount != MAX_MONEY) {
202  nTotal += wtx.tx->vout[i].nValue;
203 
204  if (nTotal >= nMinimumSumAmount) {
205  return;
206  }
207  }
208 
209  // Checks the maximum number of UTXO's.
210  if (nMaximumCount > 0 && vCoins.size() >= nMaximumCount) {
211  return;
212  }
213  }
214  }
215 }
216 
218  const CCoinControl *coinControl) {
219  LOCK(wallet.cs_wallet);
220 
222  std::vector<COutput> vCoins;
223  AvailableCoins(wallet, vCoins, coinControl);
224  for (const COutput &out : vCoins) {
225  if (out.fSpendable) {
226  balance += out.tx->tx->vout[out.i].nValue;
227  }
228  }
229  return balance;
230 }
231 
233  const CTransaction &tx, int output) {
234  AssertLockHeld(wallet.cs_wallet);
235  const CTransaction *ptx = &tx;
236  int n = output;
237  while (OutputIsChange(wallet, ptx->vout[n]) && ptx->vin.size() > 0) {
238  const COutPoint &prevout = ptx->vin[0].prevout;
239  auto it = wallet.mapWallet.find(prevout.GetTxId());
240  if (it == wallet.mapWallet.end() ||
241  it->second.tx->vout.size() <= prevout.GetN() ||
242  !wallet.IsMine(it->second.tx->vout[prevout.GetN()])) {
243  break;
244  }
245  ptx = it->second.tx.get();
246  n = prevout.GetN();
247  }
248  return ptx->vout[n];
249 }
250 
251 std::map<CTxDestination, std::vector<COutput>>
253  AssertLockHeld(wallet.cs_wallet);
254 
255  std::map<CTxDestination, std::vector<COutput>> result;
256  std::vector<COutput> availableCoins;
257 
258  AvailableCoins(wallet, availableCoins);
259 
260  for (COutput &coin : availableCoins) {
261  CTxDestination address;
262  if ((coin.fSpendable ||
263  (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) &&
264  coin.fSolvable)) &&
266  FindNonChangeParentOutput(wallet, *coin.tx->tx, coin.i)
267  .scriptPubKey,
268  address)) {
269  result[address].emplace_back(std::move(coin));
270  }
271  }
272 
273  std::vector<COutPoint> lockedCoins;
274  wallet.ListLockedCoins(lockedCoins);
275  // Include watch-only for LegacyScriptPubKeyMan wallets without private keys
276  const bool include_watch_only =
277  wallet.GetLegacyScriptPubKeyMan() &&
278  wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
279  const isminetype is_mine_filter =
280  include_watch_only ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
281  for (const auto &output : lockedCoins) {
282  auto it = wallet.mapWallet.find(output.GetTxId());
283  if (it != wallet.mapWallet.end()) {
284  int depth = wallet.GetTxDepthInMainChain(it->second);
285  if (depth >= 0 && output.GetN() < it->second.tx->vout.size() &&
286  wallet.IsMine(it->second.tx->vout[output.GetN()]) ==
287  is_mine_filter) {
288  CTxDestination address;
290  *it->second.tx,
291  output.GetN())
292  .scriptPubKey,
293  address)) {
294  result[address].emplace_back(
295  wallet, it->second, output.GetN(), depth,
296  true /* spendable */, true /* solvable */,
297  false /* safe */);
298  }
299  }
300  }
301  }
302 
303  return result;
304 }
305 
306 std::vector<OutputGroup>
307 GroupOutputs(const CWallet &wallet, const std::vector<COutput> &outputs,
308  bool separate_coins, const CFeeRate &effective_feerate,
309  const CFeeRate &long_term_feerate,
310  const CoinEligibilityFilter &filter, bool positive_only) {
311  std::vector<OutputGroup> groups_out;
312 
313  if (separate_coins) {
314  // Single coin means no grouping. Each COutput gets its own OutputGroup.
315  for (const COutput &output : outputs) {
316  // Skip outputs we cannot spend
317  if (!output.fSpendable) {
318  continue;
319  }
320 
321  CInputCoin input_coin = output.GetInputCoin();
322 
323  // Make an OutputGroup containing just this output
324  OutputGroup group{effective_feerate, long_term_feerate};
325  group.Insert(input_coin, output.nDepth,
326  CachedTxIsFromMe(wallet, *output.tx, ISMINE_ALL),
327  positive_only);
328 
329  // Check the OutputGroup's eligibility. Only add the eligible ones.
330  if (positive_only && group.effective_value <= Amount::zero()) {
331  continue;
332  }
333  if (group.m_outputs.size() > 0 &&
334  group.EligibleForSpending(filter)) {
335  groups_out.push_back(group);
336  }
337  }
338  return groups_out;
339  }
340 
341  // We want to combine COutputs that have the same scriptPubKey into single
342  // OutputGroups except when there are more than OUTPUT_GROUP_MAX_ENTRIES
343  // COutputs grouped in an OutputGroup.
344  // To do this, we maintain a map where the key is the scriptPubKey and the
345  // value is a vector of OutputGroups.
346  // For each COutput, we check if the scriptPubKey is in the map, and if it
347  // is, the COutput's CInputCoin is added to the last OutputGroup in the
348  // vector for the scriptPubKey. When the last OutputGroup has
349  // OUTPUT_GROUP_MAX_ENTRIES CInputCoins, a new OutputGroup is added to the
350  // end of the vector.
351  std::map<CScript, std::vector<OutputGroup>> spk_to_groups_map;
352  for (const auto &output : outputs) {
353  // Skip outputs we cannot spend
354  if (!output.fSpendable) {
355  continue;
356  }
357 
358  CInputCoin input_coin = output.GetInputCoin();
359  CScript spk = input_coin.txout.scriptPubKey;
360 
361  std::vector<OutputGroup> &groups = spk_to_groups_map[spk];
362 
363  if (groups.size() == 0) {
364  // No OutputGroups for this scriptPubKey yet, add one
365  groups.emplace_back(effective_feerate, long_term_feerate);
366  }
367 
368  // Get the last OutputGroup in the vector so that we can add the
369  // CInputCoin to it.
370  // A pointer is used here so that group can be reassigned later if it
371  // is full.
372  OutputGroup *group = &groups.back();
373 
374  // Check if this OutputGroup is full. We limit to
375  // OUTPUT_GROUP_MAX_ENTRIES when using -avoidpartialspends to avoid
376  // surprising users with very high fees.
377  if (group->m_outputs.size() >= OUTPUT_GROUP_MAX_ENTRIES) {
378  // The last output group is full, add a new group to the vector and
379  // use that group for the insertion
380  groups.emplace_back(effective_feerate, long_term_feerate);
381  group = &groups.back();
382  }
383 
384  // Add the input_coin to group
385  group->Insert(input_coin, output.nDepth,
386  CachedTxIsFromMe(wallet, *output.tx, ISMINE_ALL),
387  positive_only);
388  }
389 
390  // Now we go through the entire map and pull out the OutputGroups
391  for (const auto &spk_and_groups_pair : spk_to_groups_map) {
392  const std::vector<OutputGroup> &groups_per_spk =
393  spk_and_groups_pair.second;
394 
395  // Go through the vector backwards. This allows for the first item we
396  // deal with being the partial group.
397  for (auto group_it = groups_per_spk.rbegin();
398  group_it != groups_per_spk.rend(); group_it++) {
399  const OutputGroup &group = *group_it;
400 
401  // Don't include partial groups if there are full groups too and we
402  // don't want partial groups
403  if (group_it == groups_per_spk.rbegin() &&
404  groups_per_spk.size() > 1 && !filter.m_include_partial_groups) {
405  continue;
406  }
407 
408  // Check the OutputGroup's eligibility. Only add the eligible ones.
409  if (positive_only && group.effective_value <= Amount::zero()) {
410  continue;
411  }
412  if (group.m_outputs.size() > 0 &&
413  group.EligibleForSpending(filter)) {
414  groups_out.push_back(group);
415  }
416  }
417  }
418 
419  return groups_out;
420 }
421 
422 bool SelectCoinsMinConf(const CWallet &wallet, const Amount nTargetValue,
423  const CoinEligibilityFilter &eligibility_filter,
424  std::vector<COutput> coins,
425  std::set<CInputCoin> &setCoinsRet, Amount &nValueRet,
427  bool &bnb_used) {
428  setCoinsRet.clear();
429  nValueRet = Amount::zero();
430 
432  // Get long term estimate
433  CCoinControl temp;
434  temp.m_confirm_target = 1008;
435  CFeeRate long_term_feerate = GetMinimumFeeRate(wallet, temp);
436 
437  // Get the feerate for effective value.
438  // When subtracting the fee from the outputs, we want the effective
439  // feerate to be 0
440  CFeeRate effective_feerate{Amount::zero()};
442  effective_feerate = coin_selection_params.effective_fee;
443  }
444 
445  std::vector<OutputGroup> groups = GroupOutputs(
447  effective_feerate, long_term_feerate, eligibility_filter,
448  /*positive_only=*/true);
449 
450  // Calculate cost of change
451  Amount cost_of_change = wallet.chain().relayDustFee().GetFee(
455 
456  // Calculate the fees for things that aren't inputs
459  bnb_used = true;
460  return SelectCoinsBnB(groups, nTargetValue, cost_of_change, setCoinsRet,
461  nValueRet, not_input_fees);
462  } else {
463  std::vector<OutputGroup> groups = GroupOutputs(
466  eligibility_filter,
467  /*positive_only=*/false);
468 
469  bnb_used = false;
470  return KnapsackSolver(nTargetValue, groups, setCoinsRet, nValueRet);
471  }
472 }
473 
475  const std::vector<COutput> &vAvailableCoins,
476  const Amount nTargetValue, std::set<CInputCoin> &setCoinsRet,
477  Amount &nValueRet, const CCoinControl &coin_control,
478  CoinSelectionParams &coin_selection_params, bool &bnb_used) {
479  std::vector<COutput> vCoins(vAvailableCoins);
480  Amount value_to_select = nTargetValue;
481 
482  // Default to bnb was not used. If we use it, we set it later
483  bnb_used = false;
484 
485  // coin control -> return all selected outputs (we want all selected to go
486  // into the transaction for sure)
487  if (coin_control.HasSelected() && !coin_control.fAllowOtherInputs) {
488  for (const COutput &out : vCoins) {
489  if (!out.fSpendable) {
490  continue;
491  }
492 
493  nValueRet += out.tx->tx->vout[out.i].nValue;
494  setCoinsRet.insert(out.GetInputCoin());
495  }
496 
497  return (nValueRet >= nTargetValue);
498  }
499 
500  // Calculate value from preset inputs and store them.
501  std::set<CInputCoin> setPresetCoins;
502  Amount nValueFromPresetInputs = Amount::zero();
503 
504  std::vector<COutPoint> vPresetInputs;
505  coin_control.ListSelected(vPresetInputs);
506 
507  for (const COutPoint &outpoint : vPresetInputs) {
508  std::map<TxId, CWalletTx>::const_iterator it =
509  wallet.mapWallet.find(outpoint.GetTxId());
510  if (it != wallet.mapWallet.end()) {
511  const CWalletTx &wtx = it->second;
512  // Clearly invalid input, fail
513  if (wtx.tx->vout.size() <= outpoint.GetN()) {
514  return false;
515  }
516  // Just to calculate the marginal byte size
517  CInputCoin coin(
518  wtx.tx, outpoint.GetN(),
519  GetTxSpendSize(wallet, wtx, outpoint.GetN(), false));
520  nValueFromPresetInputs += coin.txout.nValue;
521  if (coin.m_input_bytes <= 0) {
522  // Not solvable, can't estimate size for fee
523  return false;
524  }
525  coin.effective_value =
526  coin.txout.nValue -
529  value_to_select -= coin.effective_value;
530  } else {
531  value_to_select -= coin.txout.nValue;
532  }
533  setPresetCoins.insert(coin);
534  } else {
535  return false; // TODO: Allow non-wallet inputs
536  }
537  }
538 
539  // Remove preset inputs from vCoins
540  for (std::vector<COutput>::iterator it = vCoins.begin();
541  it != vCoins.end() && coin_control.HasSelected();) {
542  if (setPresetCoins.count(it->GetInputCoin())) {
543  it = vCoins.erase(it);
544  } else {
545  ++it;
546  }
547  }
548 
549  // form groups from remaining coins; note that preset coins will not
550  // automatically have their associated (same address) coins included
551  if (coin_control.m_avoid_partial_spends &&
552  vCoins.size() > OUTPUT_GROUP_MAX_ENTRIES) {
553  // Cases where we have 11+ outputs all pointing to the same destination
554  // may result in privacy leaks as they will potentially be
555  // deterministically sorted. We solve that by explicitly shuffling the
556  // outputs before processing
557  Shuffle(vCoins.begin(), vCoins.end(), FastRandomContext());
558  }
559 
560  bool res =
561  value_to_select <= Amount::zero() ||
562  SelectCoinsMinConf(wallet, value_to_select, CoinEligibilityFilter(1, 6),
563  vCoins, setCoinsRet, nValueRet,
564  coin_selection_params, bnb_used) ||
565  SelectCoinsMinConf(wallet, value_to_select, CoinEligibilityFilter(1, 1),
566  vCoins, setCoinsRet, nValueRet,
567  coin_selection_params, bnb_used) ||
568  (wallet.m_spend_zero_conf_change &&
569  SelectCoinsMinConf(wallet, value_to_select,
570  CoinEligibilityFilter(0, 1), vCoins, setCoinsRet,
571  nValueRet, coin_selection_params, bnb_used)) ||
572  (wallet.m_spend_zero_conf_change &&
573  SelectCoinsMinConf(wallet, value_to_select,
574  CoinEligibilityFilter(0, 1), vCoins, setCoinsRet,
575  nValueRet, coin_selection_params, bnb_used)) ||
576  (wallet.m_spend_zero_conf_change &&
577  SelectCoinsMinConf(wallet, value_to_select,
578  CoinEligibilityFilter(0, 1), vCoins, setCoinsRet,
579  nValueRet, coin_selection_params, bnb_used)) ||
580  (wallet.m_spend_zero_conf_change &&
582  wallet, value_to_select,
583  CoinEligibilityFilter(0, 1, /*include_partial_groups=*/true),
584  vCoins, setCoinsRet, nValueRet, coin_selection_params,
585  bnb_used)) ||
586  (wallet.m_spend_zero_conf_change &&
588  wallet, value_to_select,
589  CoinEligibilityFilter(0, 1, /*include_partial_groups=*/true),
590  vCoins, setCoinsRet, nValueRet, coin_selection_params,
591  bnb_used)) ||
592  // Try with unsafe inputs if they are allowed. This may spend
593  // unconfirmed outputs received from other wallets.
594  (coin_control.m_include_unsafe_inputs &&
596  wallet, value_to_select,
597  CoinEligibilityFilter(/*conf_mine=*/0, /*conf_theirs=*/0,
598  /*include_partial_groups=*/true),
599  vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used));
600 
601  // Because SelectCoinsMinConf clears the setCoinsRet, we now add the
602  // possible inputs to the coinset.
603  util::insert(setCoinsRet, setPresetCoins);
604 
605  // Add preset inputs to the total value selected.
606  nValueRet += nValueFromPresetInputs;
607 
608  return res;
609 }
610 
612  CWallet &wallet, const std::vector<CRecipient> &vecSend,
613  CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut,
614  bilingual_str &error, const CCoinControl &coin_control, bool sign) {
615  // TODO: remember to add the lock annotation when adding AssertLockHeld.
616  // The lock annotation was added by core in PR22100, but due to
617  // other missing backports it was not possible to add it during that
618  // backport.
619  Amount nValue = Amount::zero();
620  const OutputType change_type = wallet.TransactionChangeType(
621  coin_control.m_change_type ? *coin_control.m_change_type
622  : wallet.m_default_change_type,
623  vecSend);
624  ReserveDestination reservedest(&wallet, change_type);
625  int nChangePosRequest = nChangePosInOut;
626  unsigned int nSubtractFeeFromAmount = 0;
627  for (const auto &recipient : vecSend) {
628  if (nValue < Amount::zero() || recipient.nAmount < Amount::zero()) {
629  error = _("Transaction amounts must not be negative");
630  return false;
631  }
632 
633  nValue += recipient.nAmount;
634 
635  if (recipient.fSubtractFeeFromAmount) {
636  nSubtractFeeFromAmount++;
637  }
638  }
639 
640  if (vecSend.empty()) {
641  error = _("Transaction must have at least one recipient");
642  return false;
643  }
644 
645  CMutableTransaction txNew;
646 
647  {
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 
669  // coin control: send change to custom address
670  if (!std::get_if<CNoDestination>(&coin_control.destChange)) {
671  scriptChange = GetScriptForDestination(coin_control.destChange);
672 
673  // no coin control: send change to newly generated address
674  } else {
675  // Note: We use a new key here to keep it from being obvious
676  // which side is the change.
677  // The drawback is that by not reusing a previous key, the
678  // change may be lost if a backup is restored, if the backup
679  // doesn't have the new private key for the change. If we
680  // reused the old key, it would be possible to add code to look
681  // for and rediscover unknown transactions that were written
682  // with keys of ours to recover post-backup change.
683 
684  // Reserve a new key pair from key pool. If it fails, provide a
685  // dummy destination in case we don't need change.
686  CTxDestination dest;
687  if (!reservedest.GetReservedDestination(dest, true)) {
688  error = _("Transaction needs a change address, but we can't "
689  "generate it. Please call keypoolrefill first.");
690  }
691 
692  scriptChange = GetScriptForDestination(dest);
693  // A valid destination implies a change script (and
694  // vice-versa). An empty change script will abort later, if the
695  // change keypool ran out, but change is required.
696  CHECK_NONFATAL(IsValidDestination(dest) != scriptChange.empty());
697  }
698  CTxOut change_prototype_txout(Amount::zero(), scriptChange);
700  GetSerializeSize(change_prototype_txout);
701 
702  // Get the fee rate to use effective values in coin selection
703  CFeeRate nFeeRateNeeded = GetMinimumFeeRate(wallet, coin_control);
704  // Do not, ever, assume that it's fine to change the fee rate if the
705  // user has explicitly provided one
706  if (coin_control.m_feerate &&
707  nFeeRateNeeded > *coin_control.m_feerate) {
708  error = strprintf(_("Fee rate (%s) is lower than the minimum fee "
709  "rate setting (%s)"),
710  coin_control.m_feerate->ToString(),
711  nFeeRateNeeded.ToString());
712  return false;
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 false;
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  error = _("Insufficient funds");
815  return false;
816  }
817  }
818  } else {
819  bnb_used = false;
820  }
821 
822  const Amount nChange = nValueIn - nValueToSelect;
823  if (nChange > Amount::zero()) {
824  // Fill a vout to ourself.
825  CTxOut newTxOut(nChange, scriptChange);
826 
827  // Never create dust outputs; if we would, just add the dust to
828  // the fee.
829  // The nChange when BnB is used is always going to go to fees.
830  if (IsDust(newTxOut, wallet.chain().relayDustFee()) ||
831  bnb_used) {
832  nChangePosInOut = -1;
833  nFeeRet += nChange;
834  } else {
835  if (nChangePosInOut == -1) {
836  // Insert change txn at random position:
837  nChangePosInOut = GetRand<int>(txNew.vout.size() + 1);
838  } else if ((unsigned int)nChangePosInOut >
839  txNew.vout.size()) {
840  error = _("Change index out of range");
841  return false;
842  }
843 
844  std::vector<CTxOut>::iterator position =
845  txNew.vout.begin() + nChangePosInOut;
846  txNew.vout.insert(position, newTxOut);
847  }
848  } else {
849  nChangePosInOut = -1;
850  }
851 
852  // Dummy fill vin for maximum size estimation
853  //
854  for (const auto &coin : setCoins) {
855  txNew.vin.push_back(CTxIn(coin.outpoint, CScript()));
856  }
857 
858  CTransaction txNewConst(txNew);
859  int nBytes = CalculateMaximumSignedTxSize(
860  txNewConst, &wallet, coin_control.fAllowWatchOnly);
861  if (nBytes < 0) {
862  error = _("Signing transaction failed");
863  return false;
864  }
865 
866  Amount nFeeNeeded = GetMinimumFee(wallet, nBytes, coin_control);
867 
868  if (nFeeRet >= nFeeNeeded) {
869  // Reduce fee to only the needed amount if possible. This
870  // prevents potential overpayment in fees if the coins selected
871  // to meet nFeeNeeded result in a transaction that requires less
872  // fee than the prior iteration.
873 
874  // If we have no change and a big enough excess fee, then try to
875  // construct transaction again only without picking new inputs.
876  // We now know we only need the smaller fee (because of reduced
877  // tx size) and so we should add a change output. Only try this
878  // once.
879  if (nChangePosInOut == -1 && nSubtractFeeFromAmount == 0 &&
880  pick_new_inputs) {
881  // Add 2 as a buffer in case increasing # of outputs changes
882  // compact size
883  unsigned int tx_size_with_change =
885  Amount fee_needed_with_change = GetMinimumFee(
886  wallet, tx_size_with_change, coin_control);
887  Amount minimum_value_for_change = GetDustThreshold(
888  change_prototype_txout, wallet.chain().relayDustFee());
889  if (nFeeRet >=
890  fee_needed_with_change + minimum_value_for_change) {
891  pick_new_inputs = false;
892  nFeeRet = fee_needed_with_change;
893  continue;
894  }
895  }
896 
897  // If we have change output already, just increase it
898  if (nFeeRet > nFeeNeeded && nChangePosInOut != -1 &&
899  nSubtractFeeFromAmount == 0) {
900  Amount extraFeePaid = nFeeRet - nFeeNeeded;
901  std::vector<CTxOut>::iterator change_position =
902  txNew.vout.begin() + nChangePosInOut;
903  change_position->nValue += extraFeePaid;
904  nFeeRet -= extraFeePaid;
905  }
906 
907  // Done, enough fee included.
908  break;
909  } else if (!pick_new_inputs) {
910  // This shouldn't happen, we should have had enough excess fee
911  // to pay for the new output and still meet nFeeNeeded.
912  // Or we should have just subtracted fee from recipients and
913  // nFeeNeeded should not have changed.
914  error = _("Transaction fee and change calculation failed");
915  return false;
916  }
917 
918  // Try to reduce change to include necessary fee.
919  if (nChangePosInOut != -1 && nSubtractFeeFromAmount == 0) {
920  Amount additionalFeeNeeded = nFeeNeeded - nFeeRet;
921  std::vector<CTxOut>::iterator change_position =
922  txNew.vout.begin() + nChangePosInOut;
923  // Only reduce change if remaining amount is still a large
924  // enough output.
925  if (change_position->nValue >=
926  MIN_FINAL_CHANGE + additionalFeeNeeded) {
927  change_position->nValue -= additionalFeeNeeded;
928  nFeeRet += additionalFeeNeeded;
929  // Done, able to increase fee from change.
930  break;
931  }
932  }
933 
934  // If subtracting fee from recipients, we now know what fee we
935  // need to subtract, we have no reason to reselect inputs.
936  if (nSubtractFeeFromAmount > 0) {
937  pick_new_inputs = false;
938  }
939 
940  // Include more fee and try again.
941  nFeeRet = nFeeNeeded;
943  continue;
944  }
945 
946  // Give up if change keypool ran out and change is required
947  if (scriptChange.empty() && nChangePosInOut != -1) {
948  return false;
949  }
950 
951  // Shuffle selected coins and fill in final vin
952  txNew.vin.clear();
953  std::vector<CInputCoin> selected_coins(setCoins.begin(),
954  setCoins.end());
955  Shuffle(selected_coins.begin(), selected_coins.end(),
957 
958  // Note how the sequence number is set to non-maxint so that
959  // the nLockTime set above actually works.
960  for (const auto &coin : selected_coins) {
961  txNew.vin.push_back(
962  CTxIn(coin.outpoint, CScript(),
963  std::numeric_limits<uint32_t>::max() - 1));
964  }
965 
966  if (sign && !wallet.SignTransaction(txNew)) {
967  error = _("Signing transaction failed");
968  return false;
969  }
970 
971  // Return the constructed transaction data.
972  tx = MakeTransactionRef(std::move(txNew));
973 
974  // Limit size.
975  if (tx->GetTotalSize() > MAX_STANDARD_TX_SIZE) {
976  error = _("Transaction too large");
977  return false;
978  }
979  }
980 
981  if (nFeeRet > wallet.m_default_max_tx_fee) {
983  return false;
984  }
985 
986  // Before we return success, we assume any change key will be used to
987  // prevent accidental re-use.
988  reservedest.KeepDestination();
989 
990  return true;
991 }
992 
993 bool CreateTransaction(CWallet &wallet, const std::vector<CRecipient> &vecSend,
994  CTransactionRef &tx, Amount &nFeeRet,
995  int &nChangePosInOut, bilingual_str &error,
996  const CCoinControl &coin_control, bool sign) {
997  int nChangePosIn = nChangePosInOut;
998  CTransactionRef tx2 = tx;
999  bool res =
1000  CreateTransactionInternal(wallet, vecSend, tx, nFeeRet, nChangePosInOut,
1001  error, coin_control, sign);
1002  // try with avoidpartialspends unless it's enabled already
1003  if (res &&
1004  nFeeRet >
1005  Amount::zero() /* 0 means non-functional fee rate estimation */
1006  && wallet.m_max_aps_fee > (-1 * SATOSHI) &&
1007  !coin_control.m_avoid_partial_spends) {
1008  CCoinControl tmp_cc = coin_control;
1009  tmp_cc.m_avoid_partial_spends = true;
1010  Amount nFeeRet2;
1011  int nChangePosInOut2 = nChangePosIn;
1012  // fired and forgotten; if an error occurs, we discard the results
1013  bilingual_str error2;
1014  if (CreateTransactionInternal(wallet, vecSend, tx2, nFeeRet2,
1015  nChangePosInOut2, error2, tmp_cc, sign)) {
1016  // if fee of this alternative one is within the range of the max
1017  // fee, we use this one
1018  const bool use_aps = nFeeRet2 <= nFeeRet + wallet.m_max_aps_fee;
1019  wallet.WalletLogPrintf(
1020  "Fee non-grouped = %lld, grouped = %lld, using %s\n", nFeeRet,
1021  nFeeRet2, use_aps ? "grouped" : "non-grouped");
1022  if (use_aps) {
1023  tx = tx2;
1024  nFeeRet = nFeeRet2;
1025  nChangePosInOut = nChangePosInOut2;
1026  }
1027  }
1028  }
1029  return res;
1030 }
1031 
1033  int &nChangePosInOut, bilingual_str &error,
1034  bool lockUnspents,
1035  const std::set<int> &setSubtractFeeFromOutputs,
1036  CCoinControl coinControl) {
1037  std::vector<CRecipient> vecSend;
1038 
1039  // Turn the txout set into a CRecipient vector.
1040  for (size_t idx = 0; idx < tx.vout.size(); idx++) {
1041  const CTxOut &txOut = tx.vout[idx];
1042  CRecipient recipient = {txOut.scriptPubKey, txOut.nValue,
1043  setSubtractFeeFromOutputs.count(idx) == 1};
1044  vecSend.push_back(recipient);
1045  }
1046 
1047  coinControl.fAllowOtherInputs = true;
1048 
1049  for (const CTxIn &txin : tx.vin) {
1050  coinControl.Select(txin.prevout);
1051  }
1052 
1053  // Acquire the locks to prevent races to the new locked unspents between the
1054  // CreateTransaction call and LockCoin calls (when lockUnspents is true).
1055  LOCK(wallet.cs_wallet);
1056 
1057  CTransactionRef tx_new;
1058  if (!CreateTransaction(wallet, vecSend, tx_new, nFeeRet, nChangePosInOut,
1059  error, coinControl, false)) {
1060  return false;
1061  }
1062 
1063  if (nChangePosInOut != -1) {
1064  tx.vout.insert(tx.vout.begin() + nChangePosInOut,
1065  tx_new->vout[nChangePosInOut]);
1066  }
1067 
1068  // Copy output sizes from new transaction; they may have had the fee
1069  // subtracted from them.
1070  for (size_t idx = 0; idx < tx.vout.size(); idx++) {
1071  tx.vout[idx].nValue = tx_new->vout[idx].nValue;
1072  }
1073 
1074  // Add new txins (keeping original txin scriptSig/order)
1075  for (const CTxIn &txin : tx_new->vin) {
1076  if (!coinControl.IsSelected(txin.prevout)) {
1077  tx.vin.push_back(txin);
1078  }
1079  if (lockUnspents) {
1080  wallet.LockCoin(txin.prevout);
1081  }
1082  }
1083 
1084  return true;
1085 }
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
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
uint32_t GetN() const
Definition: transaction.h:36
const TxId & GetTxId() const
Definition: transaction.h:35
Definition: spend.h:19
int nDepth
Definition: spend.h:23
const CWalletTx * tx
Definition: spend.h:21
std::string ToString() const
Definition: spend.cpp:31
int i
Definition: spend.h:22
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
const std::vector< CTxOut > vout
Definition: transaction.h:207
const std::vector< CTxIn > vin
Definition: transaction.h:206
An input of a transaction.
Definition: transaction.h:59
COutPoint prevout
Definition: transaction.h:61
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:254
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:300
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:161
std::string ToString() const
Definition: uint256.h:80
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:48
bool empty() const
Definition: prevector.h:388
void emplace_back(Args &&...args)
Definition: prevector.h:522
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:2438
bool GetReservedDestination(CTxDestination &pubkey, bool internal)
Reserve an address.
Definition: wallet.cpp:2417
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
bool error(const char *fmt, const Args &...args)
Definition: logging.h:226
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
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:291
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:1258
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:36
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:422
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:1032
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:47
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:252
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:307
bool CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, 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:993
static bool CreateTransactionInternal(CWallet &wallet, const std::vector< CRecipient > &vecSend, CTransactionRef &tx, Amount &nFeeRet, int &nChangePosInOut, bilingual_str &error, const CCoinControl &coin_control, bool sign)
Definition: spend.cpp:611
static const size_t OUTPUT_GROUP_MAX_ENTRIES
Definition: spend.cpp:24
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:73
const CTxOut & FindNonChangeParentOutput(const CWallet &wallet, const CTransaction &tx, int output)
Find non-change parent output.
Definition: spend.cpp:232
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:26
Amount GetAvailableBalance(const CWallet &wallet, const CCoinControl *coinControl)
Definition: spend.cpp:217
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:474
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:233
size_t change_spend_size
Definition: wallet.h:229
size_t tx_noinputs_size
Definition: wallet.h:231
bool m_avoid_partial_spends
Definition: wallet.h:234
CFeeRate effective_fee
Definition: wallet.h:230
size_t change_output_size
Definition: wallet.h:228
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:115
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
Definition: walletutil.h:55
@ WALLET_FLAG_AVOID_REUSE
Definition: walletutil.h:47