wallet: Sanity check fee paid cannot be negative

We need to check that the fee is not negative even before it is
finalized. The setting of fees for SFFO may adjust the fee to be
"correct" and no longer negative, but erroneously reduce the amounts too
far. So we need to check this condition before we do those adjustments.
This commit is contained in:
Andrew Chow 2022-12-05 15:53:18 -05:00
parent c1a84f108e
commit 798430d127

View File

@ -964,6 +964,11 @@ static util::Result<CreatedTransactionResult> CreateTransactionInternal(
Assume(recipients_sum + change_amount == output_value); Assume(recipients_sum + change_amount == output_value);
CAmount current_fee = result->GetSelectedValue() - output_value; CAmount current_fee = result->GetSelectedValue() - output_value;
// Sanity check that the fee cannot be negative as that means we have more output value than input value
if (current_fee < 0) {
return util::Error{Untranslated(STR_INTERNAL_BUG("Fee paid < 0"))};
}
// If there is a change output and we overpay the fees then increase the change to match the fee needed // If there is a change output and we overpay the fees then increase the change to match the fee needed
if (nChangePosInOut != -1 && fee_needed < current_fee) { if (nChangePosInOut != -1 && fee_needed < current_fee) {
auto& change = txNew.vout.at(nChangePosInOut); auto& change = txNew.vout.at(nChangePosInOut);