68 const CCoinControl* coin_control,
const bool tx_is_segwit,
69 const bool can_grind_r) {
72 const bool is_segwit =
IsSegwit(desc);
80 const int64_t witstack_len = is_segwit ?
GetSizeOfCompactSize(*elems_count) : (tx_is_segwit ? 1 : 0);
92 if (!provider)
return -1;
95 if (
const auto weight =
MaxInputWeight(*desc, {}, coin_control,
true, can_grind_r)) {
105 const std::unique_ptr<SigningProvider> provider =
wallet->GetSolvingProvider(txout.
scriptPubKey);
114 for (
const auto spkman:
wallet->GetScriptPubKeyMans(script_pubkey)) {
115 providers.
AddProvider(spkman->GetSolvingProvider(script_pubkey));
125 const CTxIn& txin,
const CTxOut& txo,
const bool tx_is_segwit,
126 const bool can_grind_r)
129 std::optional<int64_t> weight;
131 return weight.value();
136 if (desc)
return MaxInputWeight(*desc, {txin}, coin_control, tx_is_segwit, can_grind_r);
148 bool is_segwit = std::any_of(txouts.begin(), txouts.end(), [&](
const CTxOut& txo) {
149 std::unique_ptr<Descriptor> desc{GetDescriptor(wallet, coin_control, txo.scriptPubKey)};
154 if (is_segwit) weight += 2;
160 for (uint32_t i = 0; i < txouts.size(); i++) {
162 if (!txin_weight)
return TxSize{-1, -1};
163 assert(*txin_weight > -1);
164 weight += *txin_weight;
173 std::vector<CTxOut> txouts;
178 if (mi !=
wallet->mapWallet.end()) {
180 txouts.emplace_back(mi->second.tx->vout.at(input.
prevout.
n));
181 }
else if (coin_control) {
183 if (!txout)
return TxSize{-1, -1};
184 txouts.emplace_back(*txout);
192size_t CoinsResult::Size()
const
195 for (
const auto& it : coins) {
196 size += it.second.size();
201std::vector<COutput> CoinsResult::All()
const
203 std::vector<COutput> all;
204 all.reserve(coins.size());
205 for (
const auto& it : coins) {
206 all.insert(all.end(), it.second.begin(), it.second.end());
211void CoinsResult::Clear() {
215void CoinsResult::Erase(
const std::unordered_set<COutPoint, SaltedOutpointHasher>& coins_to_remove)
217 for (
auto& [type, vec] : coins) {
218 auto remove_it = std::remove_if(vec.begin(), vec.end(), [&](
const COutput& coin) {
220 if (coins_to_remove.count(coin.outpoint) == 0) return false;
223 total_amount -= coin.txout.nValue;
224 if (coin.HasEffectiveValue()) total_effective_amount = *total_effective_amount - coin.GetEffectiveValue();
227 vec.erase(remove_it, vec.end());
233 for (
auto& it : coins) {
234 std::shuffle(it.second.begin(), it.second.end(), rng_fast);
240 coins[type].emplace_back(
out);
241 total_amount +=
out.txout.nValue;
242 if (
out.HasEffectiveValue()) {
243 total_effective_amount = total_effective_amount.has_value() ?
244 *total_effective_amount +
out.GetEffectiveValue() :
out.GetEffectiveValue();
271 const bool can_grind_r =
wallet.CanGrindR();
274 int64_t input_bytes = coin_control.
GetInputWeight(outpoint).value_or(-1);
275 if (input_bytes != -1) {
279 if (
auto ptr_wtx =
wallet.GetWalletTx(outpoint.hash)) {
281 if (ptr_wtx->tx->vout.size() <= outpoint.n) {
284 txout = ptr_wtx->tx->vout.at(outpoint.n);
285 if (input_bytes == -1) {
298 if (input_bytes == -1) {
302 if (input_bytes == -1) {
316 std::optional<CFeeRate> feerate,
328 const bool can_grind_r =
wallet.CanGrindR();
329 std::vector<COutPoint> outpoints;
331 std::set<uint256> trusted_parents;
332 for (
const auto& entry :
wallet.mapWallet)
334 const uint256& txid = entry.first;
340 int nDepth =
wallet.GetTxDepthInMainChain(wtx);
366 if (nDepth == 0 && wtx.
mapValue.count(
"replaces_txid")) {
378 if (nDepth == 0 && wtx.
mapValue.count(
"replaced_by_txid")) {
382 if (only_safe && !safeTx) {
386 if (nDepth < min_depth || nDepth > max_depth) {
392 for (
unsigned int i = 0; i < wtx.
tx->vout.size(); i++) {
393 const CTxOut& output = wtx.
tx->vout[i];
406 if (
wallet.IsSpent(outpoint))
419 std::unique_ptr<SigningProvider> provider =
wallet.GetSolvingProvider(output.
scriptPubKey);
424 bool solvable = input_bytes > -1;
431 std::vector<std::vector<uint8_t>> script_solutions;
438 bool is_from_p2sh{
false};
447 COutput(outpoint, output, nDepth, input_bytes, spendable, solvable, safeTx, wtx.
GetTxTime(), tx_from_me, feerate));
449 outpoints.push_back(outpoint);
465 if (feerate.has_value()) {
466 std::map<COutPoint, CAmount> map_of_bump_fees =
wallet.chain().calculateIndividualBumpFees(outpoints, feerate.value());
468 for (
auto& [
_, outputs] : result.
coins) {
469 for (
auto& output : outputs) {
470 output.ApplyBumpFee(map_of_bump_fees.at(output.outpoint));
494 if (!it || it->
tx->vout.size() <= prevout.
n ||
495 !
wallet.IsMine(it->
tx->vout[prevout.
n])) {
508 std::map<CTxDestination, std::vector<COutput>> result;
521 if (
auto pk_dest = std::get_if<PubKeyDestination>(&address)) {
522 address =
PKHash(pk_dest->GetPubKey());
527 result[address].emplace_back(coin);
536 const std::vector<SelectionFilter>& filters,
537 std::vector<OutputGroup>& ret_discarded_groups)
543 for (
const auto& [type, outputs] : coins.
coins) {
544 for (
const COutput& output : outputs) {
546 size_t ancestors, descendants;
547 wallet.chain().getTransactionAncestry(output.outpoint.hash, ancestors, descendants);
551 group.Insert(std::make_shared<COutput>(output), ancestors, descendants);
554 bool accepted =
false;
555 for (
const auto& sel_filter : filters) {
556 const auto& filter = sel_filter.filter;
557 if (!
group.EligibleForSpending(filter))
continue;
558 filtered_groups[filter].Push(
group, type,
true,
true);
561 if (!accepted) ret_discarded_groups.emplace_back(
group);
564 return filtered_groups;
573 typedef std::map<std::pair<CScript, OutputType>, std::vector<OutputGroup>> ScriptPubKeyToOutgroup;
574 const auto& insert_output = [&](
575 const std::shared_ptr<COutput>& output,
OutputType type,
size_t ancestors,
size_t descendants,
576 ScriptPubKeyToOutgroup& groups_map) {
577 std::vector<OutputGroup>& groups = groups_map[std::make_pair(output->txout.scriptPubKey,type)];
579 if (groups.size() == 0) {
581 groups.emplace_back(coin_sel_params);
592 groups.emplace_back(coin_sel_params);
593 group = &groups.back();
596 group->Insert(output, ancestors, descendants);
599 ScriptPubKeyToOutgroup spk_to_groups_map;
600 ScriptPubKeyToOutgroup spk_to_positive_groups_map;
601 for (
const auto& [type, outs] : coins.
coins) {
602 for (
const COutput& output : outs) {
603 size_t ancestors, descendants;
604 wallet.chain().getTransactionAncestry(output.outpoint.hash, ancestors, descendants);
606 const auto& shared_output = std::make_shared<COutput>(output);
608 if (output.GetEffectiveValue() > 0) {
609 insert_output(shared_output, type, ancestors, descendants, spk_to_positive_groups_map);
613 insert_output(shared_output, type, ancestors, descendants, spk_to_groups_map);
618 const auto& push_output_groups = [&](
const ScriptPubKeyToOutgroup& groups_map,
bool positive_only) {
619 for (
const auto& [
script, groups] : groups_map) {
621 for (
auto group_it = groups.rbegin(); group_it != groups.rend(); group_it++) {
625 bool accepted =
false;
626 for (
const auto& sel_filter : filters) {
627 const auto& filter = sel_filter.filter;
628 if (!
group.EligibleForSpending(filter))
continue;
631 if (group_it == groups.rbegin() && groups.size() > 1 && !filter.m_include_partial_groups) {
637 filtered_groups[filter].Push(
group, type, positive_only, !positive_only);
640 if (!accepted) ret_discarded_groups.emplace_back(
group);
645 push_output_groups(spk_to_groups_map,
false);
646 push_output_groups(spk_to_positive_groups_map,
true);
648 return filtered_groups;
654 const std::vector<SelectionFilter>& filters)
656 std::vector<OutputGroup> unused;
667 std::vector<SelectionResult> results;
673 if (result) results.push_back(*result);
677 if (results.size() > 0)
return *std::min_element(results.begin(), results.end());
682 if (allow_mixed_output_types && groups.
TypesCount() > 1) {
693 std::vector<SelectionResult> results;
694 std::vector<util::Result<SelectionResult>> errors;
699 errors.emplace_back(std::move(result));
706 int max_selection_weight = max_transaction_weight - tx_weight_no_input;
707 if (max_selection_weight <= 0) {
708 return util::Error{
_(
"Maximum transaction weight is less than transaction weight without inputs")};
714 results.push_back(*bnb_result);
715 }
else append_error(std::move(bnb_result));
720 max_selection_weight -= change_outputs_weight;
721 if (max_selection_weight < 0 && results.empty()) {
722 return util::Error{
_(
"Maximum transaction weight is too low, can not accommodate change output")};
727 results.push_back(*knapsack_result);
728 }
else append_error(std::move(knapsack_result));
733 results.push_back(*cg_result);
735 append_error(std::move(cg_result));
740 results.push_back(*srd_result);
741 }
else append_error(std::move(srd_result));
743 if (results.empty()) {
746 return errors.empty() ?
util::Error() : std::move(errors.front());
750 for (
auto& result : results) {
751 std::vector<COutPoint> outpoints;
752 std::set<std::shared_ptr<COutput>> coins = result.GetInputSet();
754 for (
auto& coin : coins) {
755 if (coin->depth > 0)
continue;
756 outpoints.push_back(coin->outpoint);
757 summed_bump_fees += coin->ancestor_bump_fees;
760 if (!combined_bump_fee.has_value()) {
761 return util::Error{
_(
"Failed to calculate bump fees, because unconfirmed UTXOs depend on enormous cluster of unconfirmed transactions.")};
763 CAmount bump_fee_overestimate = summed_bump_fees - combined_bump_fee.value();
764 if (bump_fee_overestimate) {
765 result.SetBumpFeeDiscount(bump_fee_overestimate);
772 return *std::min_element(results.begin(), results.end());
784 return util::Error{
_(
"The preselected coins total amount does not cover the transaction target. "
785 "Please allow other inputs to be automatically selected or include more coins manually")};
789 if (selection_target <= 0) {
800 if (selection_target > available_coins_total_amount) {
806 if (!op_selection_result)
return op_selection_result;
809 if (!pre_set_inputs.
coins.empty()) {
812 op_selection_result->Merge(preselected);
819 if (op_selection_result->GetWeight() > max_inputs_weight) {
820 return util::Error{
_(
"The combination of the pre-selected inputs and the wallet automatic inputs selection exceeds the transaction maximum weight. "
821 "Please try sending a smaller amount or manually consolidating your wallet's UTXOs")};
824 return op_selection_result;
829 unsigned int limit_ancestor_count = 0;
830 unsigned int limit_descendant_count = 0;
831 wallet.chain().getPackageLimits(limit_ancestor_count, limit_descendant_count);
832 const size_t max_ancestors = (size_t)std::max<int64_t>(1, limit_ancestor_count);
833 const size_t max_descendants = (size_t)std::max<int64_t>(1, limit_descendant_count);
848 std::vector<SelectionFilter> ordered_filters{
856 if (
wallet.m_spend_zero_conf_change) {
858 ordered_filters.push_back({
CoinEligibilityFilter(0, 1, std::min(
size_t{4}, max_ancestors/3), std::min(
size_t{4}, max_descendants/3))});
872 if (!fRejectLongChains) {
874 std::numeric_limits<uint64_t>::max(),
880 std::vector<OutputGroup> discarded_groups;
885 CAmount total_unconf_long_chain = 0;
886 for (
const auto&
group : discarded_groups) {
887 total_discarded +=
group.GetSelectionAmount();
888 if (
group.m_ancestors >= max_ancestors ||
group.m_descendants >= max_descendants) total_unconf_long_chain +=
group.GetSelectionAmount();
893 if (total_amount + total_unconf_long_chain > value_to_select) {
894 return util::Error{
_(
"Unconfirmed UTXOs are available, but spending them creates a chain of transactions that will be rejected by the mempool")};
902 std::vector<util::Result<SelectionResult>> res_detailed_errors;
903 for (
const auto& select_filter : ordered_filters) {
904 auto it = filtered_groups.find(select_filter.filter);
905 if (it == filtered_groups.end())
continue;
907 coin_selection_params, select_filter.allow_mixed_output_types)}) {
913 if (
HasErrorMsg(res)) res_detailed_errors.emplace_back(std::move(res));
918 if (!res_detailed_errors.empty())
return std::move(res_detailed_errors.front());
931 constexpr int64_t MAX_ANTI_FEE_SNIPING_TIP_AGE = 8 * 60 * 60;
934 if (block_time < (
GetTime() - MAX_ANTI_FEE_SNIPING_TIP_AGE)) {
988 for (
const auto& in : tx.
vin) {
1012 const std::vector<CRecipient>& vecSend,
1013 std::optional<unsigned int> change_pos,
1022 if (coin_control.m_version) {
1023 txNew.
version = coin_control.m_version.value();
1027 coin_selection_params.m_avoid_partial_spends = coin_control.m_avoid_partial_spends;
1028 coin_selection_params.m_include_unsafe_inputs = coin_control.m_include_unsafe_inputs;
1031 if (coin_selection_params.m_max_tx_weight.value() < minimum_tx_weight || coin_selection_params.m_max_tx_weight.value() >
MAX_STANDARD_TX_WEIGHT) {
1035 coin_selection_params.m_long_term_feerate =
wallet.m_consolidate_feerate;
1040 const OutputType change_type =
wallet.TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type :
wallet.m_default_change_type, vecSend);
1042 unsigned int outputs_to_subtract_fee_from = 0;
1043 for (
const auto& recipient : vecSend) {
1044 if (
IsDust(recipient,
wallet.chain().relayDustFee())) {
1050 recipients_sum += recipient.nAmount;
1052 if (recipient.fSubtractFeeFromAmount) {
1053 outputs_to_subtract_fee_from++;
1054 coin_selection_params.m_subtract_fee_outputs =
true;
1063 if (!std::get_if<CNoDestination>(&coin_control.destChange)) {
1088 CTxOut change_prototype_txout(0, scriptChange);
1089 coin_selection_params.change_output_size =
GetSerializeSize(change_prototype_txout);
1095 if (change_spend_size == -1) {
1098 coin_selection_params.change_spend_size = change_spend_size;
1109 if (coin_control.m_feerate && coin_selection_params.m_effective_feerate > *coin_control.m_feerate) {
1114 return util::Error{
strprintf(
_(
"Fee estimation failed. Fallbackfee is disabled. Wait a few blocks or enable %s."),
"-fallbackfee")};
1122 coin_selection_params.m_change_fee = coin_selection_params.m_effective_feerate.GetFee(coin_selection_params.change_output_size);
1123 coin_selection_params.m_cost_of_change = coin_selection_params.m_discard_feerate.GetFee(coin_selection_params.change_spend_size) + coin_selection_params.m_change_fee;
1125 coin_selection_params.m_min_change_target =
GenerateChangeTarget(std::floor(recipients_sum / vecSend.size()), coin_selection_params.m_change_fee, rng_fast);
1130 const auto dust =
GetDustThreshold(change_prototype_txout, coin_selection_params.m_discard_feerate);
1131 const auto change_spend_fee = coin_selection_params.m_discard_feerate.GetFee(coin_selection_params.change_spend_size);
1132 coin_selection_params.min_viable_change = std::max(change_spend_fee + 1, dust);
1135 const CAmount not_input_fees = coin_selection_params.m_effective_feerate.GetFee(coin_selection_params.m_subtract_fee_outputs ? 0 : coin_selection_params.tx_noinputs_size);
1136 CAmount selection_target = recipients_sum + not_input_fees;
1140 if (selection_target == 0 && !coin_control.HasSelected()) {
1141 return util::Error{
_(
"Transaction requires one destination of non-0 value, a non-0 feerate, or a pre-selected input")};
1146 if (coin_control.HasSelected()) {
1149 preset_inputs = *res_fetch_inputs;
1155 if (coin_control.m_allow_other_inputs) {
1156 available_coins =
AvailableCoins(
wallet, &coin_control, coin_selection_params.m_effective_feerate);
1160 auto select_coins_res =
SelectCoins(
wallet, available_coins, preset_inputs, selection_target, coin_control, coin_selection_params);
1161 if (!select_coins_res) {
1168 wallet.GetName().c_str(),
1175 txNew.
vout.reserve(vecSend.size() + 1);
1176 for (
const auto& recipient : vecSend)
1180 const CAmount change_amount = result.
GetChange(coin_selection_params.min_viable_change, coin_selection_params.m_change_fee);
1181 if (change_amount > 0) {
1182 CTxOut newTxOut(change_amount, scriptChange);
1186 }
else if ((
unsigned int)*change_pos > txNew.
vout.size()) {
1187 return util::Error{
_(
"Transaction change output index out of range")};
1189 txNew.
vout.insert(txNew.
vout.begin() + *change_pos, newTxOut);
1191 change_pos = std::nullopt;
1197 if (coin_control.HasSelected() && coin_control.HasSelectedOrder()) {
1202 std::stable_sort(selected_coins.begin(), selected_coins.end(),
1203 [&coin_control](
const std::shared_ptr<COutput>& a,
const std::shared_ptr<COutput>& b) {
1204 auto a_pos = coin_control.GetSelectionPos(a->outpoint);
1205 auto b_pos = coin_control.GetSelectionPos(b->outpoint);
1206 if (a_pos.has_value() && b_pos.has_value()) {
1207 return a_pos.value() < b_pos.value();
1208 }
else if (a_pos.has_value() && !b_pos.has_value()) {
1224 bool use_anti_fee_sniping =
true;
1226 txNew.vin.reserve(selected_coins.size());
1227 for (
const auto& coin : selected_coins) {
1228 std::optional<uint32_t>
sequence = coin_control.GetSequence(coin->outpoint);
1231 use_anti_fee_sniping =
false;
1233 txNew.vin.emplace_back(coin->outpoint,
CScript{},
sequence.value_or(default_sequence));
1235 auto scripts = coin_control.GetScripts(coin->outpoint);
1236 if (scripts.first) {
1237 txNew.vin.back().scriptSig = *scripts.first;
1239 if (scripts.second) {
1240 txNew.vin.back().scriptWitness = *scripts.second;
1243 if (coin_control.m_locktime) {
1244 txNew.nLockTime = coin_control.m_locktime.value();
1246 use_anti_fee_sniping =
false;
1248 if (use_anti_fee_sniping) {
1254 int nBytes = tx_sizes.vsize;
1256 return util::Error{
_(
"Missing solving data for estimating transaction size")};
1258 CAmount fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes) + result.GetTotalBumpFees();
1260 Assume(recipients_sum + change_amount == output_value);
1261 CAmount current_fee = result.GetSelectedValue() - output_value;
1264 if (current_fee < 0) {
1269 if (change_pos && fee_needed < current_fee) {
1270 auto& change = txNew.vout.at(*change_pos);
1271 change.nValue += current_fee - fee_needed;
1273 if (fee_needed != current_fee) {
1279 if (coin_selection_params.m_subtract_fee_outputs) {
1280 CAmount to_reduce = fee_needed - current_fee;
1283 for (
const auto& recipient : vecSend)
1285 if (change_pos && i == *change_pos) {
1288 CTxOut& txout = txNew.vout[i];
1290 if (recipient.fSubtractFeeFromAmount)
1292 txout.
nValue -= to_reduce / outputs_to_subtract_fee_from;
1297 txout.
nValue -= to_reduce % outputs_to_subtract_fee_from;
1303 return util::Error{
_(
"The transaction amount is too small to pay the fee")};
1305 return util::Error{
_(
"The transaction amount is too small to send after the fee has been deducted")};
1312 if (fee_needed != current_fee) {
1319 if (fee_needed > current_fee) {
1324 if (scriptChange.empty() && change_pos) {
1328 if (
sign && !
wallet.SignTransaction(txNew)) {
1342 if (current_fee >
wallet.m_default_max_tx_fee) {
1348 auto result =
wallet.chain().checkChainLimits(tx);
1356 reservedest.KeepDestination();
1358 wallet.WalletLogPrintf(
"Coin Selection: Algorithm:%s, Waste Metric Score:%d\n",
GetAlgorithmName(result.GetAlgo()), result.GetWaste());
1359 wallet.WalletLogPrintf(
"Fee Calculation: Fee:%d Bytes:%u Tgt:%d (requested %d) Reason:\"%s\" Decay %.5f: Estimation: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out) Fail: (%g - %g) %.2f%% %.1f/(%.1f %d mem %.1f out)\n",
1360 current_fee, nBytes, feeCalc.returnedTarget, feeCalc.desiredTarget,
StringForFeeReason(feeCalc.reason), feeCalc.est.decay,
1361 feeCalc.est.pass.start, feeCalc.est.pass.end,
1362 (feeCalc.est.pass.totalConfirmed + feeCalc.est.pass.inMempool + feeCalc.est.pass.leftMempool) > 0.0 ? 100 * feeCalc.est.pass.withinTarget / (feeCalc.est.pass.totalConfirmed + feeCalc.est.pass.inMempool + feeCalc.est.pass.leftMempool) : 0.0,
1363 feeCalc.est.pass.withinTarget, feeCalc.est.pass.totalConfirmed, feeCalc.est.pass.inMempool, feeCalc.est.pass.leftMempool,
1364 feeCalc.est.fail.start, feeCalc.est.fail.end,
1365 (feeCalc.est.fail.totalConfirmed + feeCalc.est.fail.inMempool + feeCalc.est.fail.leftMempool) > 0.0 ? 100 * feeCalc.est.fail.withinTarget / (feeCalc.est.fail.totalConfirmed + feeCalc.est.fail.inMempool + feeCalc.est.fail.leftMempool) : 0.0,
1366 feeCalc.est.fail.withinTarget, feeCalc.est.fail.totalConfirmed, feeCalc.est.fail.inMempool, feeCalc.est.fail.leftMempool);
1367 return CreatedTransactionResult(tx, current_fee, change_pos, feeCalc);
1372 const std::vector<CRecipient>& vecSend,
1373 std::optional<unsigned int> change_pos,
1377 if (vecSend.empty()) {
1378 return util::Error{
_(
"Transaction must have at least one recipient")};
1381 if (std::any_of(vecSend.cbegin(), vecSend.cend(), [](
const auto& recipient){ return recipient.nAmount < 0; })) {
1382 return util::Error{
_(
"Transaction amounts must not be negative")};
1388 TRACEPOINT(coin_selection, normal_create_tx_internal,
1389 wallet.GetName().c_str(),
1392 res && res->change_pos.has_value() ? int32_t(*res->change_pos) : -1);
1393 if (!res)
return res;
1394 const auto& txr_ungrouped = *res;
1397 TRACEPOINT(coin_selection, attempting_aps_create_tx,
wallet.GetName().c_str());
1402 if (txr_ungrouped.change_pos) {
1408 const bool use_aps{txr_grouped.has_value() ? (txr_grouped->fee <= txr_ungrouped.fee +
wallet.m_max_aps_fee) :
false};
1409 TRACEPOINT(coin_selection, aps_create_tx_internal,
1410 wallet.GetName().c_str(),
1412 txr_grouped.has_value(),
1413 txr_grouped.has_value() ? txr_grouped->fee : 0,
1414 txr_grouped.has_value() && txr_grouped->change_pos.has_value() ? int32_t(*txr_grouped->change_pos) : -1);
1416 wallet.WalletLogPrintf(
"Fee non-grouped = %lld, grouped = %lld, using %s\n",
1417 txr_ungrouped.fee, txr_grouped->fee, use_aps ?
"grouped" :
"non-grouped");
1418 if (use_aps)
return txr_grouped;
1442 std::map<COutPoint, Coin> coins;
1446 wallet.chain().findCoins(coins);
1449 const auto& outPoint = txin.
prevout;
1451 if (!
wallet.IsMine(outPoint)) {
1452 if (coins[outPoint].
out.IsNull()) {
1453 return util::Error{
_(
"Unable to find UTXO for external input")};
1470 for (
const CTxIn& txin : res->tx->vin) {
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
int64_t CAmount
Amount in satoshis (Can be negative)
#define CHECK_NONFATAL(condition)
Identity function.
#define Assert(val)
Identity function.
#define STR_INTERNAL_BUG(msg)
#define Assume(val)
Assume is the identity function.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
A reference to a CScript: the Hash160 of its serialization.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
const std::vector< CTxIn > vin
An input of a transaction.
static const uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65).
static const uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
CScriptWitness scriptWitness
Only serialized through CTransaction.
An output of a transaction.
A signing provider to be used to interface with multiple signing providers at once.
void AddProvider(std::unique_ptr< SigningProvider > provider)
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
An interface to be implemented by keystores that support signing.
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
virtual bool isInitialBlockDownload()=0
Check if in IBD.
virtual std::optional< CAmount > calculateCombinedBumpFee(const std::vector< COutPoint > &outpoints, const CFeeRate &target_feerate)=0
Calculate the combined bump fee for an input set per the same strategy.
virtual bool findBlock(const uint256 &hash, const FoundBlock &block={})=0
Return whether node has the block and optionally return block metadata or contents.
Helper for findBlock to selectively return pieces of block data.
static transaction_identifier FromUint256(const uint256 &id)
bool IsSelected(const COutPoint &outpoint) const
Returns true if the given output is pre-selected.
std::optional< CTxOut > GetExternalOutput(const COutPoint &outpoint) const
Returns the external output for the given outpoint if it exists.
bool m_avoid_address_reuse
Forbids inclusion of dirty (previously used) addresses.
std::optional< int64_t > GetInputWeight(const COutPoint &outpoint) const
Returns the input weight.
PreselectedInput & Select(const COutPoint &outpoint)
Lock-in the given output for spending.
bool HasSelected() const
Returns true if there are pre-selected inputs.
bool IsExternalSelected(const COutPoint &outpoint) const
Returns true if the given output is selected as an external input.
int m_min_depth
Minimum chain depth value for coin availability.
bool m_allow_other_inputs
If true, the selection process can add extra unselected inputs from the wallet while requires all sel...
int m_max_depth
Maximum chain depth value for coin availability.
std::optional< uint32_t > m_version
Version.
bool m_include_unsafe_inputs
If false, only safe inputs will be used.
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
bool fAllowWatchOnly
Includes watch only addresses which are solvable.
FlatSigningProvider m_external_provider
SigningProvider that has pubkeys and scripts to do spend size estimation for external inputs.
CTxDestination destChange
Custom change destination, if not set an address is generated.
std::vector< COutPoint > ListSelected() const
List the selected inputs.
std::optional< uint32_t > m_locktime
Locktime.
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
A transaction with a bunch of additional info that only the owner cares about.
mapValue_t mapValue
Key/value map with information about the transaction.
int64_t GetTxTime() const
A wrapper to reserve an address from a wallet.
static int32_t GetTransactionWeight(const CTransaction &tx)
static const int WITNESS_SCALE_FACTOR
@ SAT_VB
Use sat/vB fee rate unit.
util::Result< CTxDestination > GetReservedDestination(bool internal)
Reserve an address.
is a home for simple string functions returning descriptive messages that are used in RPC and GUI int...
static int sign(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64)
bilingual_str TransactionErrorString(const TransactionError err)
std::string StringForFeeReason(FeeReason reason)
bilingual_str ErrorString(const Result< T > &result)
static constexpr size_t DUMMY_NESTED_P2WPKH_INPUT_SIZE
Pre-calculated constants for input size estimation in virtual size
const CTxOut & FindNonChangeParentOutput(const CWallet &wallet, const COutPoint &outpoint)
Find non-change parent output.
bool OutputIsChange(const CWallet &wallet, const CTxOut &txout)
util::Result< SelectionResult > AutomaticCoinSelection(const CWallet &wallet, CoinsResult &available_coins, const CAmount &value_to_select, const CoinSelectionParams &coin_selection_params)
Select a set of coins such that nTargetValue is met; never select unconfirmed coins if they are not o...
util::Result< CreatedTransactionResult > FundTransaction(CWallet &wallet, const CMutableTransaction &tx, const std::vector< CRecipient > &vecSend, std::optional< unsigned int > change_pos, bool lockUnspents, CCoinControl coinControl)
Insert additional inputs into the transaction by calling CreateTransaction();.
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, std::optional< unsigned 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...
const int DEFAULT_MIN_DEPTH
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_selection_weight)
CAmount GenerateChangeTarget(const CAmount payment_value, const CAmount change_fee, FastRandomContext &rng)
Choose a random change target for each transaction to make it harder to fingerprint the Core wallet b...
bool CachedTxIsFromMe(const CWallet &wallet, const CWalletTx &wtx, const isminefilter &filter)
static std::unique_ptr< Descriptor > GetDescriptor(const CWallet *wallet, const CCoinControl *coin_control, const CScript script_pubkey)
Infer a descriptor for the given output script.
static bool UseMaxSig(const std::optional< CTxIn > &txin, const CCoinControl *coin_control)
Whether to assume ECDSA signatures' will be high-r.
static std::optional< int64_t > GetSignedTxinWeight(const CWallet *wallet, const CCoinControl *coin_control, const CTxIn &txin, const CTxOut &txo, const bool tx_is_segwit, const bool can_grind_r)
Infer the maximum size of this input after it will be signed.
util::Result< SelectionResult > CoinGrinder(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, CAmount change_target, int max_selection_weight)
util::Result< PreSelectedInputs > FetchSelectedInputs(const CWallet &wallet, const CCoinControl &coin_control, const CoinSelectionParams &coin_selection_params)
Fetch and validate coin control selected inputs.
bool IsDust(const CRecipient &recipient, const CFeeRate &dustRelayFee)
static OutputType GetOutputType(TxoutType type, bool is_from_p2sh)
bool CachedTxIsTrusted(const CWallet &wallet, const CWalletTx &wtx, std::set< uint256 > &trusted_parents)
static bool IsSegwit(const Descriptor &desc)
Whether the descriptor represents, directly or not, a witness program.
const int DEFAULT_MAX_DEPTH
util::Result< SelectionResult > AttemptSelection(interfaces::Chain &chain, const CAmount &nTargetValue, OutputGroupTypeMap &groups, const CoinSelectionParams &coin_selection_params, bool allow_mixed_output_types)
Attempt to find a valid input set that preserves privacy by not mixing OutputTypes.
util::Result< SelectionResult > ChooseSelectionResult(interfaces::Chain &chain, const CAmount &nTargetValue, Groups &groups, const CoinSelectionParams &coin_selection_params)
Attempt to find a valid input set that meets the provided eligibility filter and target.
util::Result< SelectionResult > KnapsackSolver(std::vector< OutputGroup > &groups, const CAmount &nTargetValue, CAmount change_target, FastRandomContext &rng, int max_selection_weight)
util::Result< SelectionResult > SelectCoins(const CWallet &wallet, CoinsResult &available_coins, const PreSelectedInputs &pre_set_inputs, const CAmount &nTargetValue, const CCoinControl &coin_control, const CoinSelectionParams &coin_selection_params)
Select all coins from coin_control, and if coin_control 'm_allow_other_inputs=true',...
CFeeRate GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee rate considering user set parameters and the required fee.
std::map< CTxDestination, std::vector< COutput > > ListCoins(const CWallet &wallet)
Return list of available coins and locked coins grouped by non-change output address.
int CalculateMaximumSignedInputSize(const CTxOut &txout, const CWallet *wallet, const CCoinControl *coin_control)
Get the marginal bytes if spending the specified output from this transaction.
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
CoinsResult AvailableCoinsListUnspent(const CWallet &wallet, const CCoinControl *coinControl, CoinFilterParams params)
Wrapper function for AvailableCoins which skips the feerate and CoinFilterParams::only_spendable para...
std::string GetAlgorithmName(const SelectionAlgorithm algo)
TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const CCoinControl *coin_control)
static bool HasErrorMsg(const util::Result< SelectionResult > &res)
size_t GetSerializeSizeForRecipient(const CRecipient &recipient)
CFeeRate GetDiscardRate(const CWallet &wallet)
Return the maximum feerate for discarding change.
std::map< CoinEligibilityFilter, OutputGroupTypeMap > FilteredOutputGroups
TxSize CalculateMaximumSignedTxSize(const CTransaction &tx, const CWallet *wallet, const std::vector< CTxOut > &txouts, const CCoinControl *coin_control)
Calculate the size of the transaction using CoinControl to determine whether to expect signature grin...
static util::Result< CreatedTransactionResult > CreateTransactionInternal(CWallet &wallet, const std::vector< CRecipient > &vecSend, std::optional< unsigned int > change_pos, const CCoinControl &coin_control, bool sign) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
@ WALLET_FLAG_AVOID_REUSE
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
static bool IsCurrentForAntiFeeSniping(interfaces::Chain &chain, const uint256 &block_hash)
FilteredOutputGroups GroupOutputs(const CWallet &wallet, const CoinsResult &coins, const CoinSelectionParams ¶ms, const std::vector< SelectionFilter > &filters)
Group coins by the provided filters.
static void DiscourageFeeSniping(CMutableTransaction &tx, FastRandomContext &rng_fast, interfaces::Chain &chain, const uint256 &block_hash, int block_height)
Set a height-based locktime for new transactions (uses the height of the current chain tip unless we ...
static constexpr size_t OUTPUT_GROUP_MAX_ENTRIES
int CalculateMaximumSignedInputSize(const CTxOut &txout, const COutPoint outpoint, const SigningProvider *provider, bool can_grind_r, const CCoinControl *coin_control)
static std::optional< int64_t > MaxInputWeight(const Descriptor &desc, const std::optional< CTxIn > &txin, const CCoinControl *coin_control, const bool tx_is_segwit, const bool can_grind_r)
Get the size of an input (in witness units) once it's signed.
static const bool DEFAULT_WALLET_REJECT_LONG_CHAINS
Default for -walletrejectlongchains.
util::Result< SelectionResult > SelectCoinsSRD(const std::vector< OutputGroup > &utxo_pool, CAmount target_value, CAmount change_fee, FastRandomContext &rng, int max_selection_weight)
Select coins by Single Random Draw.
is a home for public enum and struct type definitions that are used by internally by node code,...
CAmount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE
The minimum non-witness size for transactions we're willing to relay/mine: one larger than 64
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
CAmount CalculateOutputValue(const TxType &tx)
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
static const unsigned int LOCKTIME_THRESHOLD
size_t GetSerializeSize(const T &t)
constexpr unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
TRACEPOINT_SEMAPHORE(coin_selection, selected_coins)
A mutable version of CTransaction.
std::vector< CTxOut > vout
Interface for parsed descriptor objects.
virtual std::optional< int64_t > MaxSatisfactionElems() const =0
Get the maximum size number of stack elements for satisfying this descriptor.
virtual std::optional< int64_t > MaxSatisfactionWeight(bool use_max_sig) const =0
Get the maximum size of a satisfaction for this descriptor, in weight units.
virtual std::optional< OutputType > GetOutputType() const =0
A UTXO under consideration for use in funding a new transaction.
COutPoint outpoint
The outpoint identifying this UTXO.
void ApplyBumpFee(CAmount bump_fee)
Parameters for filtering which OutputGroups we may use in coin selection.
bool include_immature_coinbase
Parameters for one iteration of Coin Selection.
FastRandomContext & rng_fast
Randomness to use in the context of coin selection.
CAmount m_min_change_target
Mininmum change to target in Knapsack solver and CoinGrinder: select coins to cover the payment and a...
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
bool m_include_unsafe_inputs
When true, allow unsafe coins to be selected during Coin Selection.
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
CAmount min_viable_change
Minimum amount for creating a change output.
CAmount m_cost_of_change
Cost of creating the change output + cost of spending the change output in the future.
CAmount m_change_fee
Cost of creating the change output.
int change_output_size
Size of a change output in bytes, determined by the output type.
int tx_noinputs_size
Size of the transaction before coin selection, consisting of the header and recipient output(s),...
std::optional< int > m_max_tx_weight
The maximum weight for this transaction.
bool m_avoid_partial_spends
When true, always spend all (up to OUTPUT_GROUP_MAX_ENTRIES) or none of the outputs associated with t...
COutputs available for spending, stored by OutputType.
std::optional< CAmount > GetEffectiveTotalAmount()
void Add(OutputType type, const COutput &out)
size_t Size() const
The following methods are provided so that CoinsResult can mimic a vector, i.e., methods can work wit...
std::map< OutputType, std::vector< COutput > > coins
void Shuffle(FastRandomContext &rng_fast)
std::vector< OutputGroup > positive_group
std::vector< OutputGroup > mixed_group
A group of UTXOs paid to the same output script.
Stores several 'Groups' whose were mapped by output type.
std::map< OutputType, Groups > groups_by_type
CAmount GetChange(const CAmount min_viable_change, const CAmount change_fee) const
Get the amount for the change output after paying needed fees.
CAmount GetSelectedValue() const
Get the sum of the input values.
CAmount GetTarget() const
void RecalculateWaste(const CAmount min_viable_change, const CAmount change_cost, const CAmount change_fee)
Calculates and stores the waste for this result given the cost of change and the opportunity cost of ...
SelectionAlgorithm GetAlgo() const
void AddInputs(const std::set< std::shared_ptr< COutput > > &inputs, bool subtract_fee_outputs)
std::vector< std::shared_ptr< COutput > > GetShuffledInputVector() const
Get the vector of COutputs that will be used to fill in a CTransaction's vin.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
#define TRACEPOINT(context,...)
bilingual_str _(ConstevalStringLiteral str)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
static constexpr uint32_t MAX_BIP125_RBF_SEQUENCE
static void AvailableCoins(benchmark::Bench &bench, const std::vector< OutputType > &output_type)