Skip to content

Commit 875be57

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 fb81d3e commit 875be57

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
@@ -465,13 +465,14 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent( //nolint:funlen
465465
defer s.wg.Done()
466466
for {
467467
select {
468-
case <-spendEvent.Spend:
468+
case spend := <-spendEvent.Spend:
469469
// If there's a post-send event, then
470470
// we'll send that into the current
471471
// state now.
472472
postSpend := daemonEvent.PostSpendEvent
473-
postSpend.WhenSome(func(e Event) {
474-
s.SendEvent(e)
473+
postSpend.WhenSome(func(f SpendMapper[Event]) { //nolint:lll
474+
customEvent := f(spend)
475+
s.SendEvent(customEvent)
475476
})
476477

477478
return

0 commit comments

Comments
 (0)