* This covers: * 1. to-us output spend (` 0`) * 2. the their-commitment, our HTLC timeout case (` 0`), * 3. the their-commitment, our HTLC redeem case (` `) * 4. the their-revoked-commitment, to-local (` 1`) * 5. the their-revoked-commitment, htlc (` `) * * Overrides *tx_type if
| 624 | * Overrides *tx_type if it all turns to dust. |
| 625 | */ |
| 626 | static struct bitcoin_tx *tx_to_us(const tal_t *ctx, |
| 627 | u8 *(*hsm_sign_msg)(const tal_t *ctx, |
| 628 | struct bitcoin_tx *tx, |
| 629 | const u8 *wscript), |
| 630 | struct tracked_output *out, |
| 631 | u32 to_self_delay, |
| 632 | u32 locktime, |
| 633 | const void *elem, size_t elemsize, |
| 634 | const u8 *wscript, |
| 635 | enum tx_type *tx_type, |
| 636 | u32 feerate) |
| 637 | { |
| 638 | struct bitcoin_tx *tx; |
| 639 | struct amount_sat fee, min_out, amt; |
| 640 | struct bitcoin_signature sig; |
| 641 | size_t weight; |
| 642 | u8 *msg; |
| 643 | u8 **witness; |
| 644 | |
| 645 | tx = bitcoin_tx(ctx, chainparams, 1, 1, locktime); |
| 646 | bitcoin_tx_add_input(tx, &out->outpoint, to_self_delay, |
| 647 | NULL, out->sat, NULL, wscript); |
| 648 | |
| 649 | bitcoin_tx_add_output( |
| 650 | tx, scriptpubkey_p2wpkh(tmpctx, &our_wallet_pubkey), NULL, out->sat); |
| 651 | psbt_add_keypath_to_last_output(tx, our_wallet_index, &our_wallet_ext_key); |
| 652 | |
| 653 | /* Worst-case sig is 73 bytes */ |
| 654 | weight = bitcoin_tx_weight(tx) + 1 + 3 + 73 + 0 + tal_count(wscript); |
| 655 | weight += elements_tx_overhead(chainparams, 1, 1); |
| 656 | fee = amount_tx_fee(feerate, weight); |
| 657 | |
| 658 | /* Result is trivial? Spend with small feerate, but don't wait |
| 659 | * around for it as it might not confirm. */ |
| 660 | if (!amount_sat_add(&min_out, dust_limit, fee)) |
| 661 | status_failed(STATUS_FAIL_INTERNAL_ERROR, |
| 662 | "Cannot add dust_limit %s and fee %s", |
| 663 | type_to_string(tmpctx, struct amount_sat, &dust_limit), |
| 664 | type_to_string(tmpctx, struct amount_sat, &fee)); |
| 665 | |
| 666 | if (amount_sat_less(out->sat, min_out)) { |
| 667 | /* FIXME: We should use SIGHASH_NONE so others can take it */ |
| 668 | fee = amount_tx_fee(feerate_floor(), weight); |
| 669 | status_unusual("TX %s amount %s too small to" |
| 670 | " pay reasonable fee, using minimal fee" |
| 671 | " and ignoring", |
| 672 | tx_type_name(*tx_type), |
| 673 | type_to_string(tmpctx, struct amount_sat, &out->sat)); |
| 674 | *tx_type = IGNORING_TINY_PAYMENT; |
| 675 | } |
| 676 | |
| 677 | /* This can only happen if feerate_floor() is still too high; shouldn't |
| 678 | * happen! */ |
| 679 | if (!amount_sat_sub(&amt, out->sat, fee)) { |
| 680 | amt = dust_limit; |
| 681 | status_broken("TX %s can't afford minimal feerate" |
| 682 | "; setting output to %s", |
| 683 | tx_type_name(*tx_type), |
no test coverage detected