|
8 | 8 | #include <interfaces/chain.h>
|
9 | 9 | #include <numeric>
|
10 | 10 | #include <policy/policy.h>
|
| 11 | +#include <primitives/transaction.h> |
11 | 12 | #include <script/signingprovider.h>
|
12 | 13 | #include <util/check.h>
|
13 | 14 | #include <util/fees.h>
|
@@ -959,19 +960,18 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
|
959 | 960 | return util::Error{_("Missing solving data for estimating transaction size")};
|
960 | 961 | }
|
961 | 962 | CAmount fee_needed = coin_selection_params.m_effective_feerate.GetFee(nBytes);
|
962 |
| - CAmount current_fee = result->GetSelectedValue() - recipients_sum - change_amount; |
963 |
| - |
964 |
| - // The only time that fee_needed should be less than the amount available for fees is when |
965 |
| - // we are subtracting the fee from the outputs. If this occurs at any other time, it is a bug. |
966 |
| - if (!coin_selection_params.m_subtract_fee_outputs && fee_needed > current_fee) { |
967 |
| - return util::Error{Untranslated(STR_INTERNAL_BUG("Fee needed > fee paid"))}; |
968 |
| - } |
| 963 | + const CAmount output_value = CalculateOutputValue(txNew); |
| 964 | + Assume(recipients_sum + change_amount == output_value); |
| 965 | + CAmount current_fee = result->GetSelectedValue() - output_value; |
969 | 966 |
|
970 | 967 | // If there is a change output and we overpay the fees then increase the change to match the fee needed
|
971 | 968 | if (nChangePosInOut != -1 && fee_needed < current_fee) {
|
972 | 969 | auto& change = txNew.vout.at(nChangePosInOut);
|
973 | 970 | change.nValue += current_fee - fee_needed;
|
974 |
| - current_fee = fee_needed; |
| 971 | + current_fee = result->GetSelectedValue() - CalculateOutputValue(txNew); |
| 972 | + if (fee_needed != current_fee) { |
| 973 | + return util::Error{Untranslated(STR_INTERNAL_BUG("Change adjustment: Fee needed != fee paid"))}; |
| 974 | + } |
975 | 975 | }
|
976 | 976 |
|
977 | 977 | // Reduce output values for subtractFeeFromAmount
|
@@ -1007,7 +1007,16 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
|
1007 | 1007 | }
|
1008 | 1008 | ++i;
|
1009 | 1009 | }
|
1010 |
| - current_fee = fee_needed; |
| 1010 | + current_fee = result->GetSelectedValue() - CalculateOutputValue(txNew); |
| 1011 | + if (fee_needed != current_fee) { |
| 1012 | + return util::Error{Untranslated(STR_INTERNAL_BUG("SFFO: Fee needed != fee paid"))}; |
| 1013 | + } |
| 1014 | + } |
| 1015 | + |
| 1016 | + // fee_needed should now always be less than or equal to the current fees that we pay. |
| 1017 | + // If it is not, it is a bug. |
| 1018 | + if (fee_needed > current_fee) { |
| 1019 | + return util::Error{Untranslated(STR_INTERNAL_BUG("Fee needed > fee paid"))}; |
1011 | 1020 | }
|
1012 | 1021 |
|
1013 | 1022 | // Give up if change keypool ran out and change is required
|
|
0 commit comments