Skip to content

Commit 0276ea5

Browse files
committed
protofsm: add test for new full block conf behavior
1 parent 34c1978 commit 0276ea5

File tree

1 file changed

+69
-18
lines changed

1 file changed

+69
-18
lines changed

protofsm/state_machine_test.go

+69-18
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (c *confDetailsEvent) dummy() {
4949
}
5050

5151
type registerConf struct {
52+
fullBlock bool
5253
}
5354

5455
func (r *registerConf) dummy() {
@@ -173,6 +174,7 @@ func (d *dummyStateStart) ProcessEvent(event dummyEvents, env *dummyEnv,
173174
Txid: chainhash.Hash{1},
174175
PkScript: []byte{0x01},
175176
HeightHint: 100,
177+
FullBlock: newEvent.fullBlock,
176178
PostConfMapper: fn.Some[ConfMapper[dummyEvents]](
177179
confMapper,
178180
),
@@ -380,10 +382,10 @@ func (d *dummyAdapters) BroadcastTransaction(tx *wire.MsgTx,
380382

381383
func (d *dummyAdapters) RegisterConfirmationsNtfn(txid *chainhash.Hash,
382384
pkScript []byte, numConfs, heightHint uint32,
383-
opts ...chainntnfs.NotifierOption,
384-
) (*chainntnfs.ConfirmationEvent, error) {
385+
opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
385386

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)
387389

388390
err := args.Error(0)
389391

@@ -576,11 +578,12 @@ func TestStateMachineDaemonEvents(t *testing.T) {
576578
env.AssertExpectations(t)
577579
}
578580

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) {
584587
ctx := context.Background()
585588

586589
// Create the state machine.
@@ -601,35 +604,66 @@ func TestStateMachineConfMapper(t *testing.T) {
601604
stateMachine.Start(ctx)
602605
defer stateMachine.Stop()
603606

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+
}
610644

611645
// Send the event that triggers RegisterConf emission.
612-
stateMachine.SendEvent(ctx, &registerConf{})
646+
stateMachine.SendEvent(ctx, regConfEvent)
613647

614648
// We should transition back to the starting state initially.
615649
expectedStates := []State[dummyEvents, *dummyEnv]{
616650
&dummyStateStart{}, &dummyStateStart{},
617651
}
618652
assertStateTransitions(t, stateSub, expectedStates)
619653

620-
// Assert the registration call was made.
654+
// Assert the registration call was made with the correct arguments
655+
// (including options).
621656
adapters.AssertExpectations(t)
622657

623658
// Now, simulate the confirmation event coming back from the notifier.
624-
// Populate it with some data to be mapped.
625659
simulatedConf := &chainntnfs.TxConfirmation{
626660
BlockHash: &chainhash.Hash{2},
627661
BlockHeight: 123,
628662
}
629663
adapters.confChan <- simulatedConf
630664

631665
// This should trigger the mapper and send the confDetailsEvent,
632-
// transitioning us to the final state.
666+
// transitioning us to the confirmed state.
633667
expectedStates = []State[dummyEvents, *dummyEnv]{&dummyStateConfirmed{}}
634668
assertStateTransitions(t, stateSub, expectedStates)
635669

@@ -649,6 +683,23 @@ func TestStateMachineConfMapper(t *testing.T) {
649683
env.AssertExpectations(t)
650684
}
651685

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+
652703
// TestStateMachineSpendMapper tests that the state machine is able to properly
653704
// map the spend event into a custom event that can be used to trigger a state
654705
// transition.

0 commit comments

Comments
 (0)