Skip to content

Commit 5b510f0

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 2fd1f77 commit 5b510f0

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
)
@@ -79,8 +80,12 @@ type BroadcastTxn struct {
7980
// daemonSealed indicates that this struct is a DaemonEvent instance.
8081
func (b *BroadcastTxn) daemonSealed() {}
8182

83+
// SpendMapper is a function that's used to map a spend notification to a
84+
// custom state machine event.
85+
type SpendMapper[Event any] func(*chainntnfs.SpendDetail) Event
86+
8287
// RegisterSpend is used to request that a certain event is sent into the state
83-
// machien once the specified outpoint has been spent.
88+
// machine once the specified outpoint has been spent.
8489
type RegisterSpend[Event any] struct {
8590
// OutPoint is the outpoint on chain to watch.
8691
OutPoint wire.OutPoint
@@ -93,10 +98,9 @@ type RegisterSpend[Event any] struct {
9398
// far back it needs to start its search.
9499
HeightHint uint32
95100

96-
// PostSpendEvent is an event that's sent back to the requester once a
97-
// transaction spending the outpoint has been confirmed in the main
98-
// chain.
99-
PostSpendEvent fn.Option[Event]
101+
// PostSpendEvent is a special spend mapper, that if present, will be
102+
// used to map the protofsm spend event to a custom event.
103+
PostSpendEvent fn.Option[SpendMapper[Event]]
100104
}
101105

102106
// 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
@@ -481,13 +481,14 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent( //nolint:funlen
481481
defer s.wg.Done()
482482
for {
483483
select {
484-
case <-spendEvent.Spend:
484+
case spend := <-spendEvent.Spend:
485485
// If there's a post-send event, then
486486
// we'll send that into the current
487487
// state now.
488488
postSpend := daemonEvent.PostSpendEvent
489-
postSpend.WhenSome(func(e Event) {
490-
s.SendEvent(e)
489+
postSpend.WhenSome(func(f SpendMapper[Event]) { //nolint:lll
490+
customEvent := f(spend)
491+
s.SendEvent(customEvent)
491492
})
492493

493494
return

0 commit comments

Comments
 (0)