sweepbatcher: fix fee rate calculation (publish)

We forgot to account for change outputs when checking the feerate of
signed transaction. The bug resulted in fee rate overestimation in the
log message.
This commit is contained in:
Boris Nagaev 2025-10-11 22:11:56 -03:00
parent a5871d6f3e
commit 1f15c604ac
No known key found for this signature in database
2 changed files with 14 additions and 2 deletions

View file

@ -506,7 +506,10 @@ func (b *batch) publishPresigned(ctx context.Context) (btcutil.Amount, error,
// Find actual fee rate of the signed transaction. It may differ from
// the desired fee rate, because SignTx may return a presigned tx.
output := btcutil.Amount(tx.TxOut[0].Value)
var output btcutil.Amount
for _, txOut := range tx.TxOut {
output += btcutil.Amount(txOut.Value)
}
fee = batchAmt - output
signedFeeRate := chainfee.NewSatPerKWeight(fee, realWeight)

View file

@ -1270,7 +1270,8 @@ func testPresigned_presigned_group_with_change(t *testing.T,
}
// testPresigned_fee_portion_with_change ensures that the fee portion reported
// to clients accounts for change outputs in the presigned transaction.
// to clients accounts for change outputs in the presigned transaction. It also
// is a regression test for feerate overestimation when tx is published.
func testPresigned_fee_portion_with_change(t *testing.T,
batcherStore testBatcherStore) {
@ -1350,6 +1351,14 @@ func testPresigned_fee_portion_with_change(t *testing.T,
require.Len(t, tx.TxIn, 1)
require.Len(t, tx.TxOut, 2)
// Mine a blocks to trigger republishing.
require.NoError(t, lnd.NotifyHeight(601))
// Make sure it is the same tx.
tx2 := <-lnd.TxPublishChannel
require.Len(t, tx2.TxOut, len(tx.TxOut))
require.Equal(t, tx.TxOut[0].Value, tx2.TxOut[0].Value)
var (
outputSum int64
foundChange bool