Skip to content

Commit 4085771

Browse files
committed
elements: Add removal of fee output if empty
Needed for zero-fee anchor outputs
1 parent 1557918 commit 4085771

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

bitcoin/test/run-tx-bitcoin_tx_2of2_input_witness_weight.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ struct amount_sat amount_asset_to_sat(struct amount_asset *asset UNNEEDED)
2727
struct amount_sat a UNNEEDED,
2828
struct amount_sat b UNNEEDED)
2929
{ fprintf(stderr, "amount_sat_add called!\n"); abort(); }
30+
/* Generated stub for amount_sat_eq */
31+
bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
32+
{ fprintf(stderr, "amount_sat_eq called!\n"); abort(); }
3033
/* Generated stub for amount_sat_greater_eq */
3134
bool amount_sat_greater_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
3235
{ fprintf(stderr, "amount_sat_greater_eq called!\n"); abort(); }

bitcoin/tx.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,34 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx,
542542
return tx;
543543
}
544544

545+
static void elements_maybe_remove_fee_output(struct bitcoin_tx *tx)
546+
{
547+
struct amount_sat fee = bitcoin_tx_compute_fee(tx);
548+
int pos;
549+
550+
/* If we aren't using elements, we don't add explicit fee outputs */
551+
if (!chainparams->is_elements)
552+
return;
553+
554+
/* If we have a fee we must keep the fee output. */
555+
if (!amount_sat_eq(fee, AMOUNT_SAT(0)))
556+
return;
557+
558+
/* Try to find any existing fee output */
559+
for (pos = 0; pos < tx->wtx->num_outputs; pos++) {
560+
if (elements_tx_output_is_fee(tx, pos))
561+
break;
562+
}
563+
564+
if (pos != tx->wtx->num_outputs) {
565+
wally_tx_remove_output(tx->wtx, pos);
566+
}
567+
}
568+
545569
void bitcoin_tx_finalize(struct bitcoin_tx *tx)
546570
{
547571
elements_tx_add_fee_output(tx);
572+
elements_maybe_remove_fee_output(tx);
548573
assert(bitcoin_tx_check(tx));
549574
}
550575

0 commit comments

Comments
 (0)