Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tapchannel: add awareness of 1st and 2nd level HTLC sweeps to the AuxSweeper #1154

Open
wants to merge 20 commits into
base: script-key-upsert
Choose a base branch
from

Commits on Nov 13, 2024

  1. Configuration menu
    Copy the full SHA
    379dfe2 View commit details
    Browse the repository at this point in the history
  2. tapchannelmsg: expand fields of ContractResolution for 2nd level sweeps

    In this commit, we expand the fields of the `ContractResolution` struct
    to account for second level sweeps. When we have an HTLC that needs to
    go to the second level, we'll need to create 2 vPkts: one for a direct
    spend from the commitment txns with the second level txn, and the other
    for the spend from the sweeping transaction.
    
    We can construct that transaction early, but can only sign for it once
    we know the sweeping transaction. Therefore, we need to keep some extra
    data for a given resolution, namely the tapTweak and the control block.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    86887c6 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    9c8a7a0 View commit details
    Browse the repository at this point in the history
  4. tapchannel: extract allocation creation from CreateSecondLevelHtlcPac…

    …kets
    
    This'll be useful later when we need to generate the vPkt for second
    level spends.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    6877f4c View commit details
    Browse the repository at this point in the history
  5. tapchannel: add 2nd level information to tapscriptSweepDesc

    In this commit, we add two fields to the tapscriptSweepDesc struct
    related to 2nd level HTLCs. First, we add the second level sig index,
    which is needed to be able to know where to insert our signature after
    we generate it. We also add an optional lnwallet.AuxSigDesc, which
    contains the remote party's signature, and also the sighash needed for
    it and a sign desc to generate it with.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    571e532 View commit details
    Browse the repository at this point in the history
  6. tapchannel: add sweep desc gen for remote HTLC sweeps

    In this commit, we add sweep desc generation for remote HTLCs sweeps.
    This is needed when the remote party force closes, and we can spend the
    HTLC directly from their commitment transaction.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    56f51af View commit details
    Browse the repository at this point in the history
  7. tapchannel: add blobWithWitnessInfo struct

    In this commit, we add `blobWithWitnessInfo` which couples the raw
    resolution TLV blob with some additional information such as the input
    that carried the blob, and also preimage information. Preimage
    information is needed to know where to insert the preimage in the
    witness once we learn about it on chain.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    9fc1a00 View commit details
    Browse the repository at this point in the history
  8. tapchannel: update createSweepVpackets to be second level aware

    In this commit, we update `createSweepVpackets` to be second level
    aware. This means that if we detect an auxSigInfo, then we know the vPkt
    we need to create is actually just the second level vPkt. We also don't
    need to generate a new script key either.
    
    Finally, we'll properly set the absolute delay if the sweep desc
    indicates as such.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    66ea173 View commit details
    Browse the repository at this point in the history
  9. tapchannel: update signSweepVpackets to be 2nd level HTLC aware

    In this commit, we update `signSweepVpackets` to be 2nd level HTLC
    aware. If we have an auxSigDesc, then that means we're spending a 2nd
    level HTLC txn. To spend this properly, we'll need to insert the remote
    party's signature at the proper location, as it's a 2-of-2 multi-sig.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    768c343 View commit details
    Browse the repository at this point in the history
  10. tapchannel: update createAndSignSweepVpackets to be 2nd level aware

    In this commit, we make sure to pass in the correct signDesc when we go
    to sign for a second level txn. For a 2nd level txn, we'll actually use
    the signDesc that's needed to generate the 2-of-2 multi-sig, instead of
    the one that we'd normally use to sweep.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    6826f2e View commit details
    Browse the repository at this point in the history
  11. tapchannel: create 2nd lvl vPkts in resolveContract

    With all the prior commits in place, we can now create the new contract
    resolution that includes the 1st and 2nd level packets, and the
    information that we'll need to re-sign the second level packets later.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    1805fde View commit details
    Browse the repository at this point in the history
  12. tapchannel: update sweepContracts for HTLC sweeps

    In this commit, we add some intermediate functions and types needed to
    properly handle HTLC sweeps.
    
    We now use the preimage info added in a prior commit to make sure that
    once we learn of the preimage (after vPkt creation, but before publish),
    we'll properly insert it in the correct location.
    
    In sweepContracts, when we go to create the new anchor change output,
    we'll make sure to omit any second level transactions, as they already
    have a destination location specified.
    
    Direct spends are spends directly from the commitment transaction. If we
    don't have any direct spends, then we don't need to make a change addr,
    as second level spends are already bound to an anchor output.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    9fdac50 View commit details
    Browse the repository at this point in the history
  13. tapchannel: update registerAndBroadcastSweep for HTLC sweeps

    In this commit, we update `registerAndBroadcastSweep` to be able to
    handle HTLC sweeps.
    
    First, we only need to set the anchor for first level sweeps.
    
    We weren't able to sign the 2nd level sweeps earlier as we didn't know
    the txid of the transaction that swept them. Now that we're about to
    broadcast, we know the sweeping transaction so we can set the prevID
    properly, then sign the sweeping transaction.
    
    If the sweeper is broadcasting _just_ a second level txn with an asset,
    then we won't have an extra change output. This transaction also already
    has its internal key bound.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    a05313d View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    da4df4c View commit details
    Browse the repository at this point in the history
  15. tapchannel: properly set RelativeLockTime in DistributeCoins

    If we don't do this here, then we'll sign a second level success
    transaction that doesn't have the sequence set properly. This will then
    propagate all the way up to anchor output, leading to an incorrect
    inclusion proof later.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    ac98848 View commit details
    Browse the repository at this point in the history
  16. tapchannel: properly thread thru lock time for 2nd level HTLCs

    In this commit, we fix an existing logic gap related to 2nd level HTLCs.
    Before if we were signing a timeout for the remote party, neither of us
    would properly apply the lock time to the vPkt.
    
    In this commit, we fix it by first gaining a new `whoseCommit` param in
    `FetchLeavesFromCommit`. We then use that to decide when to use a non
    zero CLTV timeout.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    1672d2a View commit details
    Browse the repository at this point in the history
  17. tapchannel: ensure the root commit asset has a valid witness

    In this commit, we fix a very subtle issue related to split witnesses
    and root assets.
    
    Before this commit, when we went to sign a second level txn, which is a
    split, we would create a virtual txn, which will commit to the _entire_
    witness of the root asset in the vIn. Note that this bypasses the normal
    segwit encoding logic, as that currently doesn't apply to the root asset
    encoded in the split commit proof.
    
    In the future, we may want to expand the segwit encoding semantics to
    root assets after careful consideration.
    
    In this commit, we fix a bug that would cause an invalid 2nd level sig
    by ensuring that before we create the output commitments, we update the
    root asset witness in each split, to the funding witness script, which
    is just OP_TRUE.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    9233d13 View commit details
    Browse the repository at this point in the history
  18. tapchannel: add an extra budget for each input w/ a blob

    In this commit, we fix an issue that would cause lnd to not broadcast
    sweeps of HTLCs due to their small value. Before we didn't add enough
    budget to actually convince lnd to init the fee function and broadcast
    the sweep.
    
    In the future, we should do a more careful calculation here based on the
    current BTC value of the asset, to make an economical decision. For now,
    we just increase the amt based on the amt of inputs we have. This gives
    lnd a fee budget to use for bumping.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    e079cb3 View commit details
    Browse the repository at this point in the history
  19. tapchannel: utilize new outpointToTxIndex map in NotifyBroadcast

    In this commit, we update the logic of `notifyBroadcast` to use the new
    `outpointToTxIndex` map. We'll use this to make sure that the vOut has
    the correct anchor output index, as the sweeper applies a sorting
    routine before broadcast.
    
    Otherwise, we'll have invalid inclusion proofs.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    92f83fa View commit details
    Browse the repository at this point in the history
  20. tapchannel: import script keys of all HTLC outputs, as known

    If we don't import these script keys, spends will fail later as we
    didn't credit the balance, so we can't spend them.
    Roasbeef committed Nov 13, 2024
    Configuration menu
    Copy the full SHA
    cb4f1f1 View commit details
    Browse the repository at this point in the history