From adc6ab25bba42ce9e7ed6bd7599aabb6dead6987 Mon Sep 17 00:00:00 2001 From: josibake Date: Sun, 5 May 2024 14:16:08 +0200 Subject: [PATCH] wallet: use CRecipient instead of CTxOut Now that a CRecipient holds a CTxDestination, we can get the serialized size and determine if the output is dust using the CRecipient directly. This does not change any current behavior, but provides a nice generalization that can be used to apply special logic to a CTxDestination serialization and dust calculations in the future. Specifically, in a later PR when support for `V0SilentPayment` destinations is added, we need to use `WitnessV1Taproot` as the scriptPubKey for serialized size calcuations whenever the `CRecipient` destination is a `V0SilentPayment` destination. --- src/wallet/spend.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 4cbcfdb60f0..39744f714ad 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -976,6 +976,16 @@ static void DiscourageFeeSniping(CMutableTransaction& tx, FastRandomContext& rng } } +size_t GetSerializeSizeForRecipient(const CRecipient& recipient) +{ + return ::GetSerializeSize(CTxOut(recipient.nAmount, GetScriptForDestination(recipient.dest))); +} + +bool IsDust(const CRecipient& recipient, const CFeeRate& dustRelayFee) +{ + return ::IsDust(CTxOut(recipient.nAmount, GetScriptForDestination(recipient.dest)), dustRelayFee); +} + static util::Result CreateTransactionInternal( CWallet& wallet, const std::vector& vecSend, @@ -1097,9 +1107,9 @@ static util::Result CreateTransactionInternal( CTxOut txout(recipient.nAmount, GetScriptForDestination(recipient.dest)); // Include the fee cost for outputs. - coin_selection_params.tx_noinputs_size += ::GetSerializeSize(txout); + coin_selection_params.tx_noinputs_size += GetSerializeSizeForRecipient(recipient); - if (IsDust(txout, wallet.chain().relayDustFee())) { + if (IsDust(recipient, wallet.chain().relayDustFee())) { return util::Error{_("Transaction amount too small")}; } txNew.vout.push_back(txout);