Skip to content

Commit

Permalink
protofsm: eliminate outer option layer in EmmittedEvent
Browse files Browse the repository at this point in the history
We'll have the empty slice tuple represent the None case instead.
  • Loading branch information
Roasbeef committed Dec 9, 2024
1 parent a1969fd commit 98c4d02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
59 changes: 22 additions & 37 deletions protofsm/state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ type EmittedEvent[Event any] struct {
// InternalEvent is an optional internal event that is to be routed
// back to the target state. This enables state to trigger one or many
// state transitions without a new external event.
InternalEvent fn.Option[[]Event]
InternalEvent []Event

// ExternalEvent is an optional external event that is to be sent to
// the daemon for dispatch. Usually, this is some form of I/O.
ExternalEvents fn.Option[DaemonEventSet]
ExternalEvents DaemonEventSet
}

// StateTransition is a state transition type. It denotes the next state to go
Expand Down Expand Up @@ -541,46 +541,31 @@ func (s *StateMachine[Event, Env]) applyEvents(currentState State[Event, Env],
// With the event processed, we'll process any
// new daemon events that were emitted as part
// of this new state transition.
//
//nolint:ll
err := fn.MapOptionZ(events.ExternalEvents, func(dEvents DaemonEventSet) error {
log.Debugf("FSM(%v): processing "+
"daemon %v daemon events",
s.cfg.Env.Name(), len(dEvents))

for _, dEvent := range dEvents {
err := s.executeDaemonEvent(
dEvent,
)
if err != nil {
return err
}
for _, dEvent := range events.ExternalEvents {
err := s.executeDaemonEvent(
dEvent,
)
if err != nil {
return err
}

return nil
})
if err != nil {
return err
}

// Next, we'll add any new emitted events to
// our event queue.
// Next, we'll add any new emitted events to our
// event queue.
//
//nolint:ll
events.InternalEvent.WhenSome(func(es []Event) {
for _, inEvent := range es {
log.Debugf("FSM(%v): adding "+
"new internal event "+
"to queue: %v",
s.cfg.Env.Name(),
lnutils.SpewLogClosure(
inEvent,
),
)

eventQueue.Enqueue(inEvent)
}
})
for _, inEvent := range events.InternalEvent {
log.Debugf("FSM(%v): adding "+
"new internal event "+
"to queue: %v",
s.cfg.Env.Name(),
lnutils.SpewLogClosure(
inEvent,
),
)

eventQueue.Enqueue(inEvent)
}

return nil
})
Expand Down
8 changes: 3 additions & 5 deletions protofsm/state_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func (d *dummyStateStart) ProcessEvent(event dummyEvents, env *dummyEnv,
return &StateTransition[dummyEvents, *dummyEnv]{
NextState: &dummyStateStart{},
NewEvents: fn.Some(EmittedEvent[dummyEvents]{
InternalEvent: fn.Some(
[]dummyEvents{&goToFin{}},
),
InternalEvent: []dummyEvents{&goToFin{}},
}),
}, nil

Expand Down Expand Up @@ -114,13 +112,13 @@ func (d *dummyStateStart) ProcessEvent(event dummyEvents, env *dummyEnv,
canSend: d.canSend,
},
NewEvents: fn.Some(EmittedEvent[dummyEvents]{
ExternalEvents: fn.Some(DaemonEventSet{
ExternalEvents: DaemonEventSet{
sendEvent, sendEvent2,
&BroadcastTxn{
Tx: &wire.MsgTx{},
Label: "test",
},
}),
},
}),
}, nil
}
Expand Down

0 comments on commit 98c4d02

Please sign in to comment.