@@ -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
),
@@ -383,7 +385,8 @@ func (d *dummyAdapters) RegisterConfirmationsNtfn(txid *chainhash.Hash,
383
385
opts ... chainntnfs.NotifierOption ,
384
386
) (* chainntnfs.ConfirmationEvent , error ) {
385
387
386
- args := d .Called (txid , pkScript , numConfs )
388
+ // Pass opts as the last argument to the mock call checker.
389
+ args := d .Called (txid , pkScript , numConfs , heightHint , opts )
387
390
388
391
err := args .Error (0 )
389
392
@@ -589,11 +592,12 @@ func TestStateMachineDaemonEvents(t *testing.T) {
589
592
env .AssertExpectations (t )
590
593
}
591
594
592
- // TestStateMachineConfMapper tests that the state machine is able to properly
593
- // map the confirmation event into a custom event that can be used to trigger a
594
- // state transition.
595
- func TestStateMachineConfMapper (t * testing.T ) {
596
- t .Parallel ()
595
+ // testStateMachineConfMapperImpl is a helper function that encapsulates the
596
+ // core logic for testing the confirmation mapping functionality of the state
597
+ // machine. It takes a boolean flag `fullBlock` to determine whether to test the
598
+ // scenario where full block details are requested in the confirmation
599
+ // notification.
600
+ func testStateMachineConfMapperImpl (t * testing.T , fullBlock bool ) {
597
601
ctx := context .Background ()
598
602
599
603
// Create the state machine.
@@ -614,35 +618,69 @@ func TestStateMachineConfMapper(t *testing.T) {
614
618
stateMachine .Start (ctx )
615
619
defer stateMachine .Stop ()
616
620
617
- // Expect the RegisterConfirmationsNtfn call when we send the event.
618
- // We use NumConfs=1 as the default.
619
- adapters .On (
620
- "RegisterConfirmationsNtfn" , & chainhash.Hash {1 }, []byte {0x01 },
621
- uint32 (1 ),
622
- ).Return (nil )
621
+ // Define the expected arguments for the mock call.
622
+ expectedTxid := & chainhash.Hash {1 }
623
+ expectedPkScript := []byte {0x01 }
624
+ expectedNumConfs := uint32 (1 )
625
+ expectedHeightHint := uint32 (100 )
626
+
627
+ // Set up the mock expectation based on the FullBlock flag. We use
628
+ // mock.MatchedBy to assert the options passed.
629
+ if fullBlock {
630
+ // Expect WithIncludeBlock() option when FullBlock is true.
631
+ adapters .On (
632
+ "RegisterConfirmationsNtfn" ,
633
+ expectedTxid , expectedPkScript ,
634
+ expectedNumConfs , expectedHeightHint ,
635
+ mock .MatchedBy (
636
+ func (opts []chainntnfs.NotifierOption ) bool {
637
+ // Check if exactly one option is passed
638
+ // and it's the correct type. Unless we
639
+ // use reflect, we can introspect into
640
+ // the private fields.
641
+ return len (opts ) == 1
642
+ },
643
+ ),
644
+ ).Return (nil )
645
+ } else {
646
+ // Expect no options when FullBlock is false.
647
+ adapters .On (
648
+ "RegisterConfirmationsNtfn" ,
649
+ expectedTxid , expectedPkScript ,
650
+ expectedNumConfs , expectedHeightHint ,
651
+ mock .MatchedBy (func (opts []chainntnfs.NotifierOption ) bool { //nolint:ll
652
+ return len (opts ) == 0
653
+ }),
654
+ ).Return (nil )
655
+ }
656
+
657
+ // Create the registerConf event with the specified FullBlock value.
658
+ regConfEvent := & registerConf {
659
+ fullBlock : fullBlock ,
660
+ }
623
661
624
662
// Send the event that triggers RegisterConf emission.
625
- stateMachine .SendEvent (ctx , & registerConf {} )
663
+ stateMachine .SendEvent (ctx , regConfEvent )
626
664
627
665
// We should transition back to the starting state initially.
628
666
expectedStates := []State [dummyEvents , * dummyEnv ]{
629
667
& dummyStateStart {}, & dummyStateStart {},
630
668
}
631
669
assertStateTransitions (t , stateSub , expectedStates )
632
670
633
- // Assert the registration call was made.
671
+ // Assert the registration call was made with the correct arguments
672
+ // (including options).
634
673
adapters .AssertExpectations (t )
635
674
636
675
// Now, simulate the confirmation event coming back from the notifier.
637
- // Populate it with some data to be mapped.
638
676
simulatedConf := & chainntnfs.TxConfirmation {
639
677
BlockHash : & chainhash.Hash {2 },
640
678
BlockHeight : 123 ,
641
679
}
642
680
adapters .confChan <- simulatedConf
643
681
644
682
// This should trigger the mapper and send the confDetailsEvent,
645
- // transitioning us to the final state.
683
+ // transitioning us to the confirmed state.
646
684
expectedStates = []State [dummyEvents , * dummyEnv ]{& dummyStateConfirmed {}}
647
685
assertStateTransitions (t , stateSub , expectedStates )
648
686
@@ -662,6 +700,23 @@ func TestStateMachineConfMapper(t *testing.T) {
662
700
env .AssertExpectations (t )
663
701
}
664
702
703
+ // TestStateMachineConfMapper tests the confirmation mapping functionality using
704
+ // subtests driven by the testStateMachineConfMapperImpl helper function. It
705
+ // covers scenarios both with and without requesting the full block details.
706
+ func TestStateMachineConfMapper (t * testing.T ) {
707
+ t .Parallel ()
708
+
709
+ t .Run ("full block false" , func (t * testing.T ) {
710
+ t .Parallel ()
711
+ testStateMachineConfMapperImpl (t , false )
712
+ })
713
+
714
+ t .Run ("full block true" , func (t * testing.T ) {
715
+ t .Parallel ()
716
+ testStateMachineConfMapperImpl (t , true )
717
+ })
718
+ }
719
+
665
720
// TestStateMachineSpendMapper tests that the state machine is able to properly
666
721
// map the spend event into a custom event that can be used to trigger a state
667
722
// transition.
0 commit comments