Skip to content

Commit

Permalink
protofsm: add SpendMapper to craft custom spend events
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Roasbeef committed Aug 2, 2024
1 parent b1af67f commit 3cf619a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
14 changes: 9 additions & 5 deletions protofsm/daemon_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/lnwire"
)
Expand Down Expand Up @@ -67,8 +68,12 @@ type BroadcastTxn struct {
// daemonSealed indicates that this struct is a DaemonEvent instance.
func (b *BroadcastTxn) daemonSealed() {}

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

// RegisterSpend is used to request that a certain event is sent into the state
// machien once the specified outpoint has been spent.
// machine once the specified outpoint has been spent.
type RegisterSpend[Event any] struct {
// OutPoint is the outpoint on chain to watch.
OutPoint wire.OutPoint
Expand All @@ -81,10 +86,9 @@ type RegisterSpend[Event any] struct {
// far back it needs to start its search.
HeightHint uint32

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

// daemonSealed indicates that this struct is a DaemonEvent instance.
Expand Down
7 changes: 4 additions & 3 deletions protofsm/state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,14 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent( //nolint:funlen
defer s.wg.Done()
for {
select {
case <-spendEvent.Spend:
case spend := <-spendEvent.Spend:
// If there's a post-send event, then
// we'll send that into the current
// state now.
postSpend := daemonEvent.PostSpendEvent
postSpend.WhenSome(func(e Event) {
s.SendEvent(e)
postSpend.WhenSome(func(f SpendMapper[Event]) { //nolint:lll
customEvent := f(spend)
s.SendEvent(customEvent)
})

return
Expand Down

0 comments on commit 3cf619a

Please sign in to comment.