@@ -310,12 +310,12 @@ type MockChainBridge struct {
310
310
311
311
NewBlocks chan int32
312
312
313
- ReqCount int
313
+ ReqCount atomic. Int32
314
314
ConfReqs map [int ]* chainntnfs.ConfirmationEvent
315
315
316
316
failFeeEstimates atomic.Bool
317
- emptyConf bool
318
- errConf bool
317
+ errConf atomic. Int32
318
+ emptyConf atomic. Int32
319
319
confErr chan error
320
320
}
321
321
@@ -334,19 +334,30 @@ func (m *MockChainBridge) FailFeeEstimatesOnce() {
334
334
m .failFeeEstimates .Store (true )
335
335
}
336
336
337
- func (m * MockChainBridge ) FailConf (enable bool ) {
338
- m .errConf = enable
337
+ // FailConfOnce updates the ChainBridge such that the next call to
338
+ // RegisterConfirmationNtfn will fail by returning an error on the error channel
339
+ // returned from RegisterConfirmationNtfn.
340
+ func (m * MockChainBridge ) FailConfOnce () {
341
+ // Store the incremented request count so we never store 0 as a value.
342
+ m .errConf .Store (m .ReqCount .Load () + 1 )
339
343
}
340
- func (m * MockChainBridge ) EmptyConf (enable bool ) {
341
- m .emptyConf = enable
344
+
345
+ // EmptyConfOnce updates the ChainBridge such that the next confirmation event
346
+ // sent via SendConfNtfn will have an empty confirmation.
347
+ func (m * MockChainBridge ) EmptyConfOnce () {
348
+ // Store the incremented request count so we never store 0 as a value.
349
+ m .emptyConf .Store (m .ReqCount .Load () + 1 )
342
350
}
343
351
344
352
func (m * MockChainBridge ) SendConfNtfn (reqNo int , blockHash * chainhash.Hash ,
345
353
blockHeight , blockIndex int , block * wire.MsgBlock ,
346
354
tx * wire.MsgTx ) {
347
355
356
+ // Compare to the incremented request count since we incremented it
357
+ // when storing the request number.
348
358
req := m .ConfReqs [reqNo ]
349
- if m .emptyConf {
359
+ if m .emptyConf .Load () == int32 (reqNo )+ 1 {
360
+ m .emptyConf .Store (0 )
350
361
req .Confirmed <- nil
351
362
return
352
363
}
@@ -371,7 +382,7 @@ func (m *MockChainBridge) RegisterConfirmationsNtfn(ctx context.Context,
371
382
}
372
383
373
384
defer func () {
374
- m .ReqCount ++
385
+ m .ReqCount . Add ( 1 )
375
386
}()
376
387
377
388
req := & chainntnfs.ConfirmationEvent {
@@ -380,15 +391,18 @@ func (m *MockChainBridge) RegisterConfirmationsNtfn(ctx context.Context,
380
391
}
381
392
m .confErr = make (chan error , 1 )
382
393
383
- m .ConfReqs [m .ReqCount ] = req
394
+ currentReqCount := m .ReqCount .Load ()
395
+ m .ConfReqs [int (currentReqCount )] = req
384
396
385
397
select {
386
- case m .ConfReqSignal <- m . ReqCount :
398
+ case m .ConfReqSignal <- int ( currentReqCount ) :
387
399
case <- ctx .Done ():
388
400
}
389
401
390
- if m .errConf {
391
- m .confErr <- fmt .Errorf ("confirmation error" )
402
+ // Compare to the incremented request count since we incremented it
403
+ // when storing the request number.
404
+ if m .errConf .CompareAndSwap (currentReqCount + 1 , 0 ) {
405
+ m .confErr <- fmt .Errorf ("confirmation registration error" )
392
406
}
393
407
394
408
return req , m .confErr , nil
@@ -661,7 +675,7 @@ func (m *MockKeyRing) IsLocalKey(context.Context, keychain.KeyDescriptor) bool {
661
675
662
676
type MockGenSigner struct {
663
677
KeyRing * MockKeyRing
664
- FailSigning bool
678
+ failSigning atomic. Bool
665
679
}
666
680
667
681
func NewMockGenSigner (keyRing * MockKeyRing ) * MockGenSigner {
@@ -670,11 +684,17 @@ func NewMockGenSigner(keyRing *MockKeyRing) *MockGenSigner {
670
684
}
671
685
}
672
686
687
+ // FailSigningOnce updates the GenSigner such that the next call to
688
+ // SignVirtualTx will fail by returning an error.
689
+ func (m * MockGenSigner ) FailSigningOnce () {
690
+ m .failSigning .Store (true )
691
+ }
692
+
673
693
func (m * MockGenSigner ) SignVirtualTx (signDesc * lndclient.SignDescriptor ,
674
694
virtualTx * wire.MsgTx , prevOut * wire.TxOut ) (* schnorr.Signature ,
675
695
error ) {
676
696
677
- if m .FailSigning {
697
+ if m .failSigning . CompareAndSwap ( true , false ) {
678
698
return nil , fmt .Errorf ("failed to sign virtual tx" )
679
699
}
680
700
0 commit comments