loopout+sweepbatcher: calculate the per sweep onchain fees correctly

Previously we'd report the fees per sweep as the total sweep cost of a
batch. With this change the reported cost will be the proportional fee
which should be equal for all sweeps except if there's any rounding
difference in which case that is paid by the sweep belonging to the
first input of the batch tx.
This commit is contained in:
Andras Banki-Horvath 2024-02-02 22:37:54 +01:00
parent b4ebb19a77
commit e1ddb50dfe
No known key found for this signature in database
GPG key ID: 80E5375C094198D8
4 changed files with 103 additions and 17 deletions

View file

@ -514,7 +514,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
}
// Try to spend htlc and continue (rbf) until a spend has confirmed.
spendTx, err := s.waitForHtlcSpendConfirmedV2(
spend, err := s.waitForHtlcSpendConfirmedV2(
globalCtx, *htlcOutpoint, htlcValue,
)
if err != nil {
@ -523,7 +523,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
// If spend details are nil, we resolved the swap without waiting for
// its spend, so we can exit.
if spendTx == nil {
if spend == nil {
return nil
}
@ -531,7 +531,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
// don't just try to match with the hash of our sweep tx, because it
// may be swept by a different (fee) sweep tx from a previous run.
htlcInput, err := swap.GetTxInputByOutpoint(
spendTx, htlcOutpoint,
spend.Tx, htlcOutpoint,
)
if err != nil {
return err
@ -540,9 +540,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
sweepSuccessful := s.htlc.IsSuccessWitness(htlcInput.Witness)
if sweepSuccessful {
s.cost.Server -= htlcValue
s.cost.Onchain = htlcValue -
btcutil.Amount(spendTx.TxOut[0].Value)
s.cost.Onchain = spend.OnChainFeePortion
s.state = loopdb.StateSuccess
} else {
@ -1005,9 +1003,9 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
// sweep or a server revocation tx.
func (s *loopOutSwap) waitForHtlcSpendConfirmedV2(globalCtx context.Context,
htlcOutpoint wire.OutPoint, htlcValue btcutil.Amount) (
*wire.MsgTx, error) {
*sweepbatcher.SpendDetail, error) {
spendChan := make(chan *wire.MsgTx)
spendChan := make(chan *sweepbatcher.SpendDetail)
spendErrChan := make(chan error, 1)
quitChan := make(chan bool, 1)
@ -1054,10 +1052,10 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmedV2(globalCtx context.Context,
for {
select {
// Htlc spend, break loop.
case spendTx := <-spendChan:
s.log.Infof("Htlc spend by tx: %v", spendTx.TxHash())
case spend := <-spendChan:
s.log.Infof("Htlc spend by tx: %v", spend.Tx.TxHash())
return spendTx, nil
return spend, nil
// Spend notification error.
case err := <-spendErrChan: