Skip to content

Commit 188e567

Browse files
authored
Merge pull request #871 from starius/sweepbatcher-fixes
sweepbatcher: fix some minor bugs
2 parents 7cfceb7 + 6efd010 commit 188e567

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

sweepbatcher/sweep_batch.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ type batch struct {
214214
// currentHeight is the current block height.
215215
currentHeight int32
216216

217-
// blockEpochChan is the channel over which block epoch notifications
218-
// are received.
219-
blockEpochChan chan int32
220-
221217
// spendChan is the channel over which spend notifications are received.
222218
spendChan chan *chainntnfs.SpendDetail
223219

@@ -362,7 +358,6 @@ func NewBatch(cfg batchConfig, bk batchKit) *batch {
362358
id: -1,
363359
state: Open,
364360
sweeps: make(map[lntypes.Hash]sweep),
365-
blockEpochChan: make(chan int32),
366361
spendChan: make(chan *chainntnfs.SpendDetail),
367362
confChan: make(chan *chainntnfs.TxConfirmation, 1),
368363
reorgChan: make(chan struct{}, 1),
@@ -407,7 +402,6 @@ func NewBatchFromDB(cfg batchConfig, bk batchKit) (*batch, error) {
407402
state: bk.state,
408403
primarySweepID: bk.primaryID,
409404
sweeps: bk.sweeps,
410-
blockEpochChan: make(chan int32),
411405
spendChan: make(chan *chainntnfs.SpendDetail),
412406
confChan: make(chan *chainntnfs.TxConfirmation, 1),
413407
reorgChan: make(chan struct{}, 1),
@@ -626,6 +620,16 @@ func (b *batch) Run(ctx context.Context) error {
626620
return err
627621
}
628622

623+
// Set currentHeight here, because it may be needed in monitorSpend.
624+
select {
625+
case b.currentHeight = <-blockChan:
626+
b.log.Debugf("initial height for the batch is %v",
627+
b.currentHeight)
628+
629+
case <-runCtx.Done():
630+
return runCtx.Err()
631+
}
632+
629633
// If a primary sweep exists we immediately start monitoring for its
630634
// spend.
631635
if b.primarySweepID != lntypes.ZeroHash {
@@ -642,9 +646,9 @@ func (b *batch) Run(ctx context.Context) error {
642646
skipBefore := clock.Now().Add(b.cfg.initialDelay)
643647

644648
// initialDelayChan is a timer which fires upon initial delay end.
645-
// If initialDelay is 0, it does not fire to prevent race with
646-
// blockChan which also fires immediately with current tip. Such a race
647-
// may result in double publishing if batchPublishDelay is also 0.
649+
// If initialDelay is set to 0, it will not trigger to avoid setting up
650+
// timerChan twice, which could lead to double publishing if
651+
// batchPublishDelay is also 0.
648652
var initialDelayChan <-chan time.Time
649653
if b.cfg.initialDelay > 0 {
650654
initialDelayChan = clock.TickAfter(b.cfg.initialDelay)
@@ -653,9 +657,10 @@ func (b *batch) Run(ctx context.Context) error {
653657
// We use a timer in order to not publish new transactions at the same
654658
// time as the block epoch notification. This is done to prevent
655659
// unnecessary transaction publishments when a spend is detected on that
656-
// block. This timer starts after new block arrives or initialDelay
660+
// block. This timer starts after new block arrives (including the
661+
// current tip which we read from blockChan above) or when initialDelay
657662
// completes.
658-
var timerChan <-chan time.Time
663+
timerChan := clock.TickAfter(b.cfg.batchPublishDelay)
659664

660665
b.log.Infof("started, primary %x, total sweeps %v",
661666
b.primarySweepID[0:6], len(b.sweeps))
@@ -1872,7 +1877,10 @@ func (b *batch) monitorConfirmations(ctx context.Context) error {
18721877
func getFeePortionForSweep(spendTx *wire.MsgTx, numSweeps int,
18731878
totalSweptAmt btcutil.Amount) (btcutil.Amount, btcutil.Amount) {
18741879

1875-
totalFee := int64(totalSweptAmt) - spendTx.TxOut[0].Value
1880+
totalFee := int64(totalSweptAmt)
1881+
if len(spendTx.TxOut) > 0 {
1882+
totalFee -= spendTx.TxOut[0].Value
1883+
}
18761884
feePortionPerSweep := totalFee / int64(numSweeps)
18771885
roundingDiff := totalFee - (int64(numSweeps) * feePortionPerSweep)
18781886

@@ -1900,7 +1908,11 @@ func (b *batch) handleSpend(ctx context.Context, spendTx *wire.MsgTx) error {
19001908
notifyList = make([]sweep, 0, len(b.sweeps))
19011909
)
19021910
b.batchTxid = &txHash
1903-
b.batchPkScript = spendTx.TxOut[0].PkScript
1911+
if len(spendTx.TxOut) > 0 {
1912+
b.batchPkScript = spendTx.TxOut[0].PkScript
1913+
} else {
1914+
b.log.Warnf("transaction %v has no outputs", txHash)
1915+
}
19041916

19051917
// As a previous version of the batch transaction may get confirmed,
19061918
// which does not contain the latest sweeps, we need to detect the

0 commit comments

Comments
 (0)