@@ -49,6 +49,7 @@ func (c *confDetailsEvent) dummy() {
49
49
}
50
50
51
51
type registerConf struct {
52
+ fullBlock bool
52
53
}
53
54
54
55
func (r * registerConf ) dummy () {
@@ -173,6 +174,7 @@ func (d *dummyStateStart) ProcessEvent(event dummyEvents, env *dummyEnv,
173
174
Txid : chainhash.Hash {1 },
174
175
PkScript : []byte {0x01 },
175
176
HeightHint : 100 ,
177
+ FullBlock : newEvent .fullBlock ,
176
178
PostConfMapper : fn.Some [ConfMapper [dummyEvents ]](
177
179
confMapper ,
178
180
),
@@ -380,10 +382,10 @@ func (d *dummyAdapters) BroadcastTransaction(tx *wire.MsgTx,
380
382
381
383
func (d * dummyAdapters ) RegisterConfirmationsNtfn (txid * chainhash.Hash ,
382
384
pkScript []byte , numConfs , heightHint uint32 ,
383
- opts ... chainntnfs.NotifierOption ,
384
- ) (* chainntnfs.ConfirmationEvent , error ) {
385
+ opts ... chainntnfs.NotifierOption ) (* chainntnfs.ConfirmationEvent , error ) {
385
386
386
- args := d .Called (txid , pkScript , numConfs )
387
+ // Pass opts as the last argument to the mock call checker.
388
+ args := d .Called (txid , pkScript , numConfs , heightHint , opts )
387
389
388
390
err := args .Error (0 )
389
391
@@ -576,11 +578,12 @@ func TestStateMachineDaemonEvents(t *testing.T) {
576
578
env .AssertExpectations (t )
577
579
}
578
580
579
- // TestStateMachineConfMapper tests that the state machine is able to properly
580
- // map the confirmation event into a custom event that can be used to trigger a
581
- // state transition.
582
- func TestStateMachineConfMapper (t * testing.T ) {
583
- t .Parallel ()
581
+ // testStateMachineConfMapperImpl is a helper function that encapsulates the
582
+ // core logic for testing the confirmation mapping functionality of the state
583
+ // machine. It takes a boolean flag `fullBlock` to determine whether to test the
584
+ // scenario where full block details are requested in the confirmation
585
+ // notification.
586
+ func testStateMachineConfMapperImpl (t * testing.T , fullBlock bool ) {
584
587
ctx := context .Background ()
585
588
586
589
// Create the state machine.
@@ -601,35 +604,66 @@ func TestStateMachineConfMapper(t *testing.T) {
601
604
stateMachine .Start (ctx )
602
605
defer stateMachine .Stop ()
603
606
604
- // Expect the RegisterConfirmationsNtfn call when we send the event.
605
- // We use NumConfs=1 as the default.
606
- adapters .On (
607
- "RegisterConfirmationsNtfn" , & chainhash.Hash {1 }, []byte {0x01 },
608
- uint32 (1 ),
609
- ).Return (nil )
607
+ // Define the expected arguments for the mock call.
608
+ expectedTxid := & chainhash.Hash {1 }
609
+ expectedPkScript := []byte {0x01 }
610
+ expectedNumConfs := uint32 (1 )
611
+ expectedHeightHint := uint32 (100 )
612
+
613
+ // Set up the mock expectation based on the FullBlock flag. We use
614
+ // mock.MatchedBy to assert the options passed.
615
+ if fullBlock {
616
+ // Expect WithIncludeBlock() option when FullBlock is true.
617
+ adapters .On (
618
+ "RegisterConfirmationsNtfn" ,
619
+ expectedTxid , expectedPkScript ,
620
+ expectedNumConfs , expectedHeightHint ,
621
+ mock .MatchedBy (func (opts []chainntnfs.NotifierOption ) bool {
622
+ // Check if exactly one option is passed and
623
+ // it's the correct type. Unless we use reflect,
624
+ // we can introspect into the private fields.
625
+ return len (opts ) == 1
626
+ }),
627
+ ).Return (nil )
628
+ } else {
629
+ // Expect no options when FullBlock is false.
630
+ adapters .On (
631
+ "RegisterConfirmationsNtfn" ,
632
+ expectedTxid , expectedPkScript ,
633
+ expectedNumConfs , expectedHeightHint ,
634
+ mock .MatchedBy (func (opts []chainntnfs.NotifierOption ) bool { //nolint:ll
635
+ return len (opts ) == 0
636
+ }),
637
+ ).Return (nil )
638
+ }
639
+
640
+ // Create the registerConf event with the specified FullBlock value.
641
+ regConfEvent := & registerConf {
642
+ fullBlock : fullBlock ,
643
+ }
610
644
611
645
// Send the event that triggers RegisterConf emission.
612
- stateMachine .SendEvent (ctx , & registerConf {} )
646
+ stateMachine .SendEvent (ctx , regConfEvent )
613
647
614
648
// We should transition back to the starting state initially.
615
649
expectedStates := []State [dummyEvents , * dummyEnv ]{
616
650
& dummyStateStart {}, & dummyStateStart {},
617
651
}
618
652
assertStateTransitions (t , stateSub , expectedStates )
619
653
620
- // Assert the registration call was made.
654
+ // Assert the registration call was made with the correct arguments
655
+ // (including options).
621
656
adapters .AssertExpectations (t )
622
657
623
658
// Now, simulate the confirmation event coming back from the notifier.
624
- // Populate it with some data to be mapped.
625
659
simulatedConf := & chainntnfs.TxConfirmation {
626
660
BlockHash : & chainhash.Hash {2 },
627
661
BlockHeight : 123 ,
628
662
}
629
663
adapters .confChan <- simulatedConf
630
664
631
665
// This should trigger the mapper and send the confDetailsEvent,
632
- // transitioning us to the final state.
666
+ // transitioning us to the confirmed state.
633
667
expectedStates = []State [dummyEvents , * dummyEnv ]{& dummyStateConfirmed {}}
634
668
assertStateTransitions (t , stateSub , expectedStates )
635
669
@@ -649,6 +683,23 @@ func TestStateMachineConfMapper(t *testing.T) {
649
683
env .AssertExpectations (t )
650
684
}
651
685
686
+ // TestStateMachineConfMapper tests the confirmation mapping functionality using
687
+ // subtests driven by the testStateMachineConfMapperImpl helper function. It
688
+ // covers scenarios both with and without requesting the full block details.
689
+ func TestStateMachineConfMapper (t * testing.T ) {
690
+ t .Parallel ()
691
+
692
+ t .Run ("full block false" , func (t * testing.T ) {
693
+ t .Parallel ()
694
+ testStateMachineConfMapperImpl (t , false )
695
+ })
696
+
697
+ t .Run ("full block true" , func (t * testing.T ) {
698
+ t .Parallel ()
699
+ testStateMachineConfMapperImpl (t , true )
700
+ })
701
+ }
702
+
652
703
// TestStateMachineSpendMapper tests that the state machine is able to properly
653
704
// map the spend event into a custom event that can be used to trigger a state
654
705
// transition.
0 commit comments