|
| 1 | +package protofsm |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/btcsuite/btcd/btcec/v2" |
| 5 | + "github.com/btcsuite/btcd/wire" |
| 6 | + "github.com/lightningnetwork/lnd/fn" |
| 7 | + "github.com/lightningnetwork/lnd/lnwire" |
| 8 | +) |
| 9 | + |
| 10 | +// DaemonEvent is a special event that can be emmitted by a state transition |
| 11 | +// function. A state machine can use this to perform side effects, such as |
| 12 | +// sending a message to a peer, or broadcasting a transaction. |
| 13 | +type DaemonEvent interface { |
| 14 | + daemonSealed() |
| 15 | +} |
| 16 | + |
| 17 | +// DaemonEventSet is a set of daemon events that can be emitted by a state |
| 18 | +// transition. |
| 19 | +type DaemonEventSet []DaemonEvent |
| 20 | + |
| 21 | +// DaemonEvents is a special type constraint that enumerates all the possible |
| 22 | +// types of daemon events. |
| 23 | +type DaemonEvents interface { |
| 24 | + SendMsgEvent[any] | BroadcastTxn |
| 25 | +} |
| 26 | + |
| 27 | +// SendPredicate is a function that returns true if the target message should |
| 28 | +// sent. |
| 29 | +type SendPredicate = func() bool |
| 30 | + |
| 31 | +// SendMsgEvent is a special event that can be emitted by a state transition |
| 32 | +// that instructs the daemon to send the contained message to the target peer. |
| 33 | +type SendMsgEvent[Event any] struct { |
| 34 | + // TargetPeer is the peer to send the message to. |
| 35 | + TargetPeer btcec.PublicKey |
| 36 | + |
| 37 | + // Msgs is the set of messages to send to the target peer. |
| 38 | + Msgs []lnwire.Message |
| 39 | + |
| 40 | + // SendWhen implements a system for a conditional send once a special |
| 41 | + // send predicate has been met. |
| 42 | + // |
| 43 | + // TODO(roasbeef): contrast with usage of OnCommitFlush, etc |
| 44 | + SendWhen fn.Option[SendPredicate] |
| 45 | + |
| 46 | + // PostSendEvent is an optional event that is to be emitted after the |
| 47 | + // message has been sent. If a SendWhen is specified, then this will |
| 48 | + // only be executed after that returns true to unblock the send. |
| 49 | + PostSendEvent fn.Option[Event] |
| 50 | +} |
| 51 | + |
| 52 | +// daemonSealed indicates that this struct is a DaemonEvent instance. |
| 53 | +func (s *SendMsgEvent[E]) daemonSealed() {} |
| 54 | + |
| 55 | +// BroadcastTxn indicates the target transaction should be broadcast to the |
| 56 | +// network. |
| 57 | +type BroadcastTxn struct { |
| 58 | + // Tx is the transaction to broadcast. |
| 59 | + Tx *wire.MsgTx |
| 60 | + |
| 61 | + // Label is an optional label to attach to the transaction. |
| 62 | + Label string |
| 63 | +} |
| 64 | + |
| 65 | +// daemonSealed indicates that this struct is a DaemonEvent instance. |
| 66 | +func (b *BroadcastTxn) daemonSealed() {} |
0 commit comments