From 847c1a789d726f9fb31fb7931b4b1cc2b5045d24 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 29 Feb 2024 16:10:48 -0600 Subject: [PATCH] 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. --- protofsm/daemon_events.go | 14 +++++++++----- protofsm/state_machine.go | 7 ++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/protofsm/daemon_events.go b/protofsm/daemon_events.go index b65adf012..e5de0b695 100644 --- a/protofsm/daemon_events.go +++ b/protofsm/daemon_events.go @@ -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" ) @@ -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 @@ -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. diff --git a/protofsm/state_machine.go b/protofsm/state_machine.go index 71cf54dd7..eb967f2a2 100644 --- a/protofsm/state_machine.go +++ b/protofsm/state_machine.go @@ -451,13 +451,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