From c97273169d22aba26d2a109d0dfa567c2c15c9c0 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Tue, 13 Dec 2022 13:50:26 -0500 Subject: [PATCH 01/10] Merge bitcoin/bitcoin#26643: wallet: Move fee underpayment check to after all fee has been set 798430d127521d088c081ee625912a704f415990 wallet: Sanity check fee paid cannot be negative (Andrew Chow) c1a84f108e320bd44c172a4dd3bb486ab777ff69 wallet: Move fee underpayment check to after fee setting (Andrew Chow) e5daf976d5b064b585029d4bb38d68a8153ea13b wallet: Rename nFeeRet in CreateTransactionInternal to current_fee (Andrew Chow) Pull request description: Currently the fee underpayment check occurs right after we calculate what the transaction's fee should be. However the fee paid by the transaction at that time does not always match. Notably, when doing SFFO, the fee paid at that time will almost always be less than the fee required, which then required having a bypass of the underpayment check that results in SFFO payments going through when they should not. This PR moves the underpayment check to after fees have been finalized so that we always check whether the fee is being underpaid. This removes the exception for SFFO and unifies this behavior for both SFFO and non-SFFO txs. ACKs for top commit: S3RK: Code review ACK 798430d127521d088c081ee625912a704f415990 furszy: Code review ACK 798430d glozow: utACK 798430d127, code looks correct to me Tree-SHA512: 720e8a3dbdc9937b12ee7881eb2ad58332c9584520da87ef3080e6f9d6220ce8d3bd8b9317b4877e56a229113437340852976db8f64df0d5cc50723fa04b02f0 Co-authored-by: Andrew Chow --- src/primitives/transaction.h | 8 +++++++ src/wallet/spend.cpp | 45 +++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index b45e81fa6e88..2a1f4a11de63 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -205,6 +206,13 @@ class CTxOut struct CMutableTransaction; +template +inline CAmount CalculateOutputValue(const TxType& tx) +{ + return std::accumulate(tx.vout.cbegin(), tx.vout.cend(), CAmount{0}, [](CAmount sum, const auto& txout) { return sum + txout.nValue; }); +} + + /** The basic transaction that is broadcasted on the network and contained in * blocks. A transaction can contain multiple inputs and outputs. */ diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 9c8132804272..b76873551a4f 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include