Skip to content

Commit a893077

Browse files
authored
Merge pull request #9725 from Roasbeef/protofsm-conf-mapper
protofsm: add ConfMapper to allow conf attribute projection for new events
2 parents 239aab8 + 946ae4c commit a893077

File tree

3 files changed

+345
-15
lines changed

3 files changed

+345
-15
lines changed

protofsm/daemon_events.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func (b *BroadcastTxn) daemonSealed() {}
7272
// custom state machine event.
7373
type SpendMapper[Event any] func(*chainntnfs.SpendDetail) Event
7474

75+
// ConfMapper is a function that's used to map a confirmation notification to a
76+
// custom state machine event.
77+
type ConfMapper[Event any] func(*chainntnfs.TxConfirmation) Event
78+
7579
// RegisterSpend is used to request that a certain event is sent into the state
7680
// machine once the specified outpoint has been spent.
7781
type RegisterSpend[Event any] struct {
@@ -112,10 +116,9 @@ type RegisterConf[Event any] struct {
112116
// transaction needs to dispatch an event.
113117
NumConfs fn.Option[uint32]
114118

115-
// PostConfEvent is an event that's sent back to the requester once the
116-
// transaction specified above has confirmed in the chain with
117-
// sufficient depth.
118-
PostConfEvent fn.Option[Event]
119+
// PostConfMapper is a special conf mapper, that if present, will be
120+
// used to map the protofsm confirmation event to a custom event.
121+
PostConfMapper fn.Option[ConfMapper[Event]]
119122
}
120123

121124
// daemonSealed indicates that this struct is a DaemonEvent instance.

protofsm/state_machine.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,19 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
522522
launched := s.gm.Go(ctx, func(ctx context.Context) {
523523
for {
524524
select {
525-
case <-confEvent.Confirmed:
526-
// If there's a post-conf event, then
525+
//nolint:ll
526+
case conf, ok := <-confEvent.Confirmed:
527+
if !ok {
528+
return
529+
}
530+
531+
// If there's a post-conf mapper, then
527532
// we'll send that into the current
528533
// state now.
529-
//
530-
// TODO(roasbeef): refactor to
531-
// dispatchAfterRecv w/ above
532-
postConf := daemonEvent.PostConfEvent
533-
postConf.WhenSome(func(e Event) {
534-
s.SendEvent(ctx, e)
534+
postConfMapper := daemonEvent.PostConfMapper
535+
postConfMapper.WhenSome(func(f ConfMapper[Event]) {
536+
customEvent := f(conf)
537+
s.SendEvent(ctx, customEvent)
535538
})
536539

537540
return

0 commit comments

Comments
 (0)