IMPL2b.md (3374B)
1 # IMPL2b: HTLC Transaction Builder Abstraction 2 3 Implements the HTLC transaction builder refactoring from ARCH2.md. 4 5 ## Scope 6 7 - Factor common code from `build_htlc_timeout_tx` and `build_htlc_success_tx` 8 - No public API changes 9 10 ## Tasks 11 12 ### 1. Add internal helper function to Tx.hs 13 14 Add a common builder function in `lib/Lightning/Protocol/BOLT3/Tx.hs` 15 (not exported): 16 17 ```haskell 18 -- | Internal helper for HTLC transaction construction. 19 -- 20 -- Both HTLC-timeout and HTLC-success transactions share the same 21 -- structure, differing only in locktime and fee calculation. 22 build_htlc_tx_common 23 :: HTLCContext 24 -> Locktime -- ^ Transaction locktime 25 -> Satoshi -- ^ Fee to subtract from output 26 -> HTLCTx 27 build_htlc_tx_common ctx locktime fee = 28 let !amountSat = msat_to_sat (htlc_amount_msat $ hc_htlc ctx) 29 !outputValue = if unSatoshi amountSat >= unSatoshi fee 30 then Satoshi (unSatoshi amountSat - unSatoshi fee) 31 else Satoshi 0 32 !inputSeq = if has_anchors (hc_features ctx) 33 then Sequence 1 34 else Sequence 0 35 !outpoint = Outpoint (hc_commitment_txid ctx) (hc_output_index ctx) 36 !outputScript = to_p2wsh $ htlc_output_script 37 (hc_revocation_pubkey ctx) 38 (hc_to_self_delay ctx) 39 (hc_local_delayed ctx) 40 in HTLCTx 41 { htx_version = 2 42 , htx_locktime = locktime 43 , htx_input_outpoint = outpoint 44 , htx_input_sequence = inputSeq 45 , htx_output_value = outputValue 46 , htx_output_script = outputScript 47 } 48 {-# INLINE build_htlc_tx_common #-} 49 ``` 50 51 ### 2. Refactor build_htlc_timeout_tx 52 53 Replace the current implementation with: 54 55 ```haskell 56 -- | Build an HTLC-timeout transaction. 57 -- 58 -- * locktime: cltv_expiry 59 -- * sequence: 0 (or 1 with option_anchors) 60 -- * output: to_local style script with revocation and delayed paths 61 build_htlc_timeout_tx :: HTLCContext -> HTLCTx 62 build_htlc_timeout_tx ctx = 63 let !fee = htlc_timeout_fee (hc_feerate ctx) (hc_features ctx) 64 !locktime = Locktime (unCltvExpiry $ htlc_cltv_expiry $ hc_htlc ctx) 65 in build_htlc_tx_common ctx locktime fee 66 {-# INLINE build_htlc_timeout_tx #-} 67 ``` 68 69 ### 3. Refactor build_htlc_success_tx 70 71 Replace the current implementation with: 72 73 ```haskell 74 -- | Build an HTLC-success transaction. 75 -- 76 -- * locktime: 0 77 -- * sequence: 0 (or 1 with option_anchors) 78 -- * output: to_local style script with revocation and delayed paths 79 build_htlc_success_tx :: HTLCContext -> HTLCTx 80 build_htlc_success_tx ctx = 81 let !fee = htlc_success_fee (hc_feerate ctx) (hc_features ctx) 82 in build_htlc_tx_common ctx (Locktime 0) fee 83 {-# INLINE build_htlc_success_tx #-} 84 ``` 85 86 ### 4. Verify build and tests pass 87 88 ```bash 89 nix develop -c cabal build all 90 nix develop -c cabal test 91 ``` 92 93 Existing tests should continue to pass since the public API is unchanged. 94 95 ### 5. Run benchmarks to verify no regression 96 97 ```bash 98 nix develop -c cabal bench bolt3-bench 99 ``` 100 101 ## Files Modified 102 103 - `lib/Lightning/Protocol/BOLT3/Tx.hs` 104 105 ## Commit Message 106 107 ``` 108 Refactor HTLC transaction builders to share common code 109 110 Factor out build_htlc_tx_common helper that handles the shared 111 transaction structure. build_htlc_timeout_tx and build_htlc_success_tx 112 now differ only in their locktime and fee parameters, making the 113 distinction between them explicit. 114 115 No public API changes. 116 ```