Skip to content

Commit 847c1a7

Browse files
committed
protofsm: add SpendMapper to craft custom spend events
In this commit, we add the SpendMapper which allows callers to create custom spent events. Before this commit, the caller would be able to have an event sent to them in the case a spend happens, but that event wouldn't have any of the relevant spend details. With this new addition, the caller can specify how to take a generic spend event, and transform it into the state machine specific spend event.
1 parent d805c0f commit 847c1a7

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

protofsm/daemon_events.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/btcsuite/btcd/btcec/v2"
55
"github.com/btcsuite/btcd/chaincfg/chainhash"
66
"github.com/btcsuite/btcd/wire"
7+
"github.com/lightningnetwork/lnd/chainntnfs"
78
"github.com/lightningnetwork/lnd/fn"
89
"github.com/lightningnetwork/lnd/lnwire"
910
)
@@ -67,8 +68,12 @@ type BroadcastTxn struct {
6768
// daemonSealed indicates that this struct is a DaemonEvent instance.
6869
func (b *BroadcastTxn) daemonSealed() {}
6970

71+
// SpendMapper is a function that's used to map a spend notification to a
72+
// custom state machine event.
73+
type SpendMapper[Event any] func(*chainntnfs.SpendDetail) Event
74+
7075
// RegisterSpend is used to request that a certain event is sent into the state
71-
// machien once the specified outpoint has been spent.
76+
// machine once the specified outpoint has been spent.
7277
type RegisterSpend[Event any] struct {
7378
// OutPoint is the outpoint on chain to watch.
7479
OutPoint wire.OutPoint
@@ -81,10 +86,9 @@ type RegisterSpend[Event any] struct {
8186
// far back it needs to start its search.
8287
HeightHint uint32
8388

84-
// PostSpendEvent is an event that's sent back to the requester once a
85-
// transaction spending the outpoint has been confirmed in the main
86-
// chain.
87-
PostSpendEvent fn.Option[Event]
89+
// PostSpendEvent is a special spend mapper, that if present, will be
90+
// used to map the protofsm spend event to a custom event.
91+
PostSpendEvent fn.Option[SpendMapper[Event]]
8892
}
8993

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

protofsm/state_machine.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,14 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent( //nolint:funlen
451451
defer s.wg.Done()
452452
for {
453453
select {
454-
case <-spendEvent.Spend:
454+
case spend := <-spendEvent.Spend:
455455
// If there's a post-send event, then
456456
// we'll send that into the current
457457
// state now.
458458
postSpend := daemonEvent.PostSpendEvent
459-
postSpend.WhenSome(func(e Event) {
460-
s.SendEvent(e)
459+
postSpend.WhenSome(func(f SpendMapper[Event]) { //nolint:lll
460+
customEvent := f(spend)
461+
s.SendEvent(customEvent)
461462
})
462463

463464
return

0 commit comments

Comments
 (0)