@@ -214,10 +214,6 @@ type batch struct {
214
214
// currentHeight is the current block height.
215
215
currentHeight int32
216
216
217
- // blockEpochChan is the channel over which block epoch notifications
218
- // are received.
219
- blockEpochChan chan int32
220
-
221
217
// spendChan is the channel over which spend notifications are received.
222
218
spendChan chan * chainntnfs.SpendDetail
223
219
@@ -362,7 +358,6 @@ func NewBatch(cfg batchConfig, bk batchKit) *batch {
362
358
id : - 1 ,
363
359
state : Open ,
364
360
sweeps : make (map [lntypes.Hash ]sweep ),
365
- blockEpochChan : make (chan int32 ),
366
361
spendChan : make (chan * chainntnfs.SpendDetail ),
367
362
confChan : make (chan * chainntnfs.TxConfirmation , 1 ),
368
363
reorgChan : make (chan struct {}, 1 ),
@@ -407,7 +402,6 @@ func NewBatchFromDB(cfg batchConfig, bk batchKit) (*batch, error) {
407
402
state : bk .state ,
408
403
primarySweepID : bk .primaryID ,
409
404
sweeps : bk .sweeps ,
410
- blockEpochChan : make (chan int32 ),
411
405
spendChan : make (chan * chainntnfs.SpendDetail ),
412
406
confChan : make (chan * chainntnfs.TxConfirmation , 1 ),
413
407
reorgChan : make (chan struct {}, 1 ),
@@ -626,6 +620,16 @@ func (b *batch) Run(ctx context.Context) error {
626
620
return err
627
621
}
628
622
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
+
629
633
// If a primary sweep exists we immediately start monitoring for its
630
634
// spend.
631
635
if b .primarySweepID != lntypes .ZeroHash {
@@ -642,9 +646,9 @@ func (b *batch) Run(ctx context.Context) error {
642
646
skipBefore := clock .Now ().Add (b .cfg .initialDelay )
643
647
644
648
// 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.
648
652
var initialDelayChan <- chan time.Time
649
653
if b .cfg .initialDelay > 0 {
650
654
initialDelayChan = clock .TickAfter (b .cfg .initialDelay )
@@ -653,9 +657,10 @@ func (b *batch) Run(ctx context.Context) error {
653
657
// We use a timer in order to not publish new transactions at the same
654
658
// time as the block epoch notification. This is done to prevent
655
659
// 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
657
662
// completes.
658
- var timerChan <- chan time. Time
663
+ timerChan := clock . TickAfter ( b . cfg . batchPublishDelay )
659
664
660
665
b .log .Infof ("started, primary %x, total sweeps %v" ,
661
666
b .primarySweepID [0 :6 ], len (b .sweeps ))
@@ -1872,7 +1877,10 @@ func (b *batch) monitorConfirmations(ctx context.Context) error {
1872
1877
func getFeePortionForSweep (spendTx * wire.MsgTx , numSweeps int ,
1873
1878
totalSweptAmt btcutil.Amount ) (btcutil.Amount , btcutil.Amount ) {
1874
1879
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
+ }
1876
1884
feePortionPerSweep := totalFee / int64 (numSweeps )
1877
1885
roundingDiff := totalFee - (int64 (numSweeps ) * feePortionPerSweep )
1878
1886
@@ -1900,7 +1908,11 @@ func (b *batch) handleSpend(ctx context.Context, spendTx *wire.MsgTx) error {
1900
1908
notifyList = make ([]sweep , 0 , len (b .sweeps ))
1901
1909
)
1902
1910
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
+ }
1904
1916
1905
1917
// As a previous version of the batch transaction may get confirmed,
1906
1918
// which does not contain the latest sweeps, we need to detect the
0 commit comments