Skip to content

protofsm: add ConfMapper to allow conf attribute projection for new events #9725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions protofsm/daemon_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (b *BroadcastTxn) daemonSealed() {}
// custom state machine event.
type SpendMapper[Event any] func(*chainntnfs.SpendDetail) Event

// ConfMapper is a function that's used to map a confirmation notification to a
// custom state machine event.
type ConfMapper[Event any] func(*chainntnfs.TxConfirmation) Event

// RegisterSpend is used to request that a certain event is sent into the state
// machine once the specified outpoint has been spent.
type RegisterSpend[Event any] struct {
Expand Down Expand Up @@ -112,10 +116,9 @@ type RegisterConf[Event any] struct {
// transaction needs to dispatch an event.
NumConfs fn.Option[uint32]

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

// daemonSealed indicates that this struct is a DaemonEvent instance.
Expand Down
18 changes: 10 additions & 8 deletions protofsm/state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,18 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent(ctx context.Context,
launched := s.gm.Go(ctx, func(ctx context.Context) {
for {
select {
case <-confEvent.Confirmed:
// If there's a post-conf event, then
case conf, ok := <-confEvent.Confirmed:
if !ok {
return
}

// If there's a post-conf mapper, then
// we'll send that into the current
// state now.
//
// TODO(roasbeef): refactor to
// dispatchAfterRecv w/ above
postConf := daemonEvent.PostConfEvent
postConf.WhenSome(func(e Event) {
s.SendEvent(ctx, e)
postConfMapper := daemonEvent.PostConfMapper //nolint:ll
postConfMapper.WhenSome(func(f ConfMapper[Event]) { //nolint:ll
customEvent := f(conf)
s.SendEvent(ctx, customEvent)
})

return
Expand Down
Loading
Loading