Skip to content

Commit 1f15c60

Browse files
committed
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.
1 parent a5871d6 commit 1f15c60

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

sweepbatcher/presigned.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,10 @@ func (b *batch) publishPresigned(ctx context.Context) (btcutil.Amount, error,
506506

507507
// Find actual fee rate of the signed transaction. It may differ from
508508
// the desired fee rate, because SignTx may return a presigned tx.
509-
output := btcutil.Amount(tx.TxOut[0].Value)
509+
var output btcutil.Amount
510+
for _, txOut := range tx.TxOut {
511+
output += btcutil.Amount(txOut.Value)
512+
}
510513
fee = batchAmt - output
511514
signedFeeRate := chainfee.NewSatPerKWeight(fee, realWeight)
512515

sweepbatcher/sweep_batcher_presigned_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,8 @@ func testPresigned_presigned_group_with_change(t *testing.T,
12701270
}
12711271

12721272
// testPresigned_fee_portion_with_change ensures that the fee portion reported
1273-
// to clients accounts for change outputs in the presigned transaction.
1273+
// to clients accounts for change outputs in the presigned transaction. It also
1274+
// is a regression test for feerate overestimation when tx is published.
12741275
func testPresigned_fee_portion_with_change(t *testing.T,
12751276
batcherStore testBatcherStore) {
12761277

@@ -1350,6 +1351,14 @@ func testPresigned_fee_portion_with_change(t *testing.T,
13501351
require.Len(t, tx.TxIn, 1)
13511352
require.Len(t, tx.TxOut, 2)
13521353

1354+
// Mine a blocks to trigger republishing.
1355+
require.NoError(t, lnd.NotifyHeight(601))
1356+
1357+
// Make sure it is the same tx.
1358+
tx2 := <-lnd.TxPublishChannel
1359+
require.Len(t, tx2.TxOut, len(tx.TxOut))
1360+
require.Equal(t, tx.TxOut[0].Value, tx2.TxOut[0].Value)
1361+
13531362
var (
13541363
outputSum int64
13551364
foundChange bool

0 commit comments

Comments
 (0)