mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 09:48:19 +01:00
protofsm: add String() method to ProtocolState
This commit is contained in:
parent
683aa61a6f
commit
71c3127c85
3 changed files with 65 additions and 3 deletions
|
@ -361,6 +361,9 @@ type ProtocolState interface {
|
||||||
// ProcessEvent takes a protocol event, and implements a state
|
// ProcessEvent takes a protocol event, and implements a state
|
||||||
// transition for the state.
|
// transition for the state.
|
||||||
ProcessEvent(ProtocolEvent, *Environment) (*CloseStateTransition, error)
|
ProcessEvent(ProtocolEvent, *Environment) (*CloseStateTransition, error)
|
||||||
|
|
||||||
|
// String returns the name of the state.
|
||||||
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsymmetricPeerState is an extension of the normal ProtocolState interface
|
// AsymmetricPeerState is an extension of the normal ProtocolState interface
|
||||||
|
@ -401,6 +404,11 @@ type ProtocolStates interface {
|
||||||
type ChannelActive struct {
|
type ChannelActive struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for ChannelActive.
|
||||||
|
func (c *ChannelActive) String() string {
|
||||||
|
return "ChannelActive"
|
||||||
|
}
|
||||||
|
|
||||||
// IsTerminal returns true if the target state is a terminal state.
|
// IsTerminal returns true if the target state is a terminal state.
|
||||||
func (c *ChannelActive) IsTerminal() bool {
|
func (c *ChannelActive) IsTerminal() bool {
|
||||||
return false
|
return false
|
||||||
|
@ -443,6 +451,11 @@ type ShutdownPending struct {
|
||||||
IdealFeeRate fn.Option[chainfee.SatPerVByte]
|
IdealFeeRate fn.Option[chainfee.SatPerVByte]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for ShutdownPending.
|
||||||
|
func (s *ShutdownPending) String() string {
|
||||||
|
return "ShutdownPending"
|
||||||
|
}
|
||||||
|
|
||||||
// IsTerminal returns true if the target state is a terminal state.
|
// IsTerminal returns true if the target state is a terminal state.
|
||||||
func (s *ShutdownPending) IsTerminal() bool {
|
func (s *ShutdownPending) IsTerminal() bool {
|
||||||
return false
|
return false
|
||||||
|
@ -478,6 +491,11 @@ type ChannelFlushing struct {
|
||||||
IdealFeeRate fn.Option[chainfee.SatPerVByte]
|
IdealFeeRate fn.Option[chainfee.SatPerVByte]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for ChannelFlushing.
|
||||||
|
func (c *ChannelFlushing) String() string {
|
||||||
|
return "ChannelFlushing"
|
||||||
|
}
|
||||||
|
|
||||||
// protocolStateSealed indicates that this struct is a ProtocolEvent instance.
|
// protocolStateSealed indicates that this struct is a ProtocolEvent instance.
|
||||||
func (c *ChannelFlushing) protocolStateSealed() {}
|
func (c *ChannelFlushing) protocolStateSealed() {}
|
||||||
|
|
||||||
|
@ -507,6 +525,15 @@ type ClosingNegotiation struct {
|
||||||
PeerState lntypes.Dual[AsymmetricPeerState]
|
PeerState lntypes.Dual[AsymmetricPeerState]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for ClosingNegotiation.
|
||||||
|
func (c *ClosingNegotiation) String() string {
|
||||||
|
localState := c.PeerState.GetForParty(lntypes.Local)
|
||||||
|
remoteState := c.PeerState.GetForParty(lntypes.Remote)
|
||||||
|
|
||||||
|
return fmt.Sprintf("ClosingNegotiation(local=%v, remote=%v)",
|
||||||
|
localState, remoteState)
|
||||||
|
}
|
||||||
|
|
||||||
// IsTerminal returns true if the target state is a terminal state.
|
// IsTerminal returns true if the target state is a terminal state.
|
||||||
func (c *ClosingNegotiation) IsTerminal() bool {
|
func (c *ClosingNegotiation) IsTerminal() bool {
|
||||||
return false
|
return false
|
||||||
|
@ -594,6 +621,12 @@ type LocalCloseStart struct {
|
||||||
CloseChannelTerms
|
CloseChannelTerms
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for LocalCloseStart, including proposed
|
||||||
|
// fee details.
|
||||||
|
func (l *LocalCloseStart) String() string {
|
||||||
|
return "LocalCloseStart"
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldRouteTo returns true if the target state should process the target
|
// ShouldRouteTo returns true if the target state should process the target
|
||||||
// event.
|
// event.
|
||||||
func (l *LocalCloseStart) ShouldRouteTo(event ProtocolEvent) bool {
|
func (l *LocalCloseStart) ShouldRouteTo(event ProtocolEvent) bool {
|
||||||
|
@ -634,6 +667,11 @@ type LocalOfferSent struct {
|
||||||
LocalSig lnwire.Sig
|
LocalSig lnwire.Sig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for LocalOfferSent, including proposed.
|
||||||
|
func (l *LocalOfferSent) String() string {
|
||||||
|
return fmt.Sprintf("LocalOfferSent(proposed_fee=%v)", l.ProposedFee)
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldRouteTo returns true if the target state should process the target
|
// ShouldRouteTo returns true if the target state should process the target
|
||||||
// event.
|
// event.
|
||||||
func (l *LocalOfferSent) ShouldRouteTo(event ProtocolEvent) bool {
|
func (l *LocalOfferSent) ShouldRouteTo(event ProtocolEvent) bool {
|
||||||
|
@ -670,6 +708,11 @@ type ClosePending struct {
|
||||||
CloseTx *wire.MsgTx
|
CloseTx *wire.MsgTx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for ClosePending.
|
||||||
|
func (c *ClosePending) String() string {
|
||||||
|
return fmt.Sprintf("ClosePending(txid=%v)", c.CloseTx.TxHash())
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldRouteTo returns true if the target state should process the target
|
// ShouldRouteTo returns true if the target state should process the target
|
||||||
// event.
|
// event.
|
||||||
func (c *ClosePending) ShouldRouteTo(event ProtocolEvent) bool {
|
func (c *ClosePending) ShouldRouteTo(event ProtocolEvent) bool {
|
||||||
|
@ -696,6 +739,11 @@ type CloseFin struct {
|
||||||
ConfirmedTx *wire.MsgTx
|
ConfirmedTx *wire.MsgTx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for CloseFin.
|
||||||
|
func (c *CloseFin) String() string {
|
||||||
|
return "CloseFin"
|
||||||
|
}
|
||||||
|
|
||||||
// protocolStateSealed indicates that this struct is a ProtocolEvent instance.
|
// protocolStateSealed indicates that this struct is a ProtocolEvent instance.
|
||||||
func (c *CloseFin) protocolStateSealed() {}
|
func (c *CloseFin) protocolStateSealed() {}
|
||||||
|
|
||||||
|
@ -714,6 +762,11 @@ type RemoteCloseStart struct {
|
||||||
CloseChannelTerms
|
CloseChannelTerms
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the name of the state for RemoteCloseStart.
|
||||||
|
func (r *RemoteCloseStart) String() string {
|
||||||
|
return "RemoteCloseStart"
|
||||||
|
}
|
||||||
|
|
||||||
// ShouldRouteTo returns true if the target state should process the target
|
// ShouldRouteTo returns true if the target state should process the target
|
||||||
// event.
|
// event.
|
||||||
func (l *RemoteCloseStart) ShouldRouteTo(event ProtocolEvent) bool {
|
func (l *RemoteCloseStart) ShouldRouteTo(event ProtocolEvent) bool {
|
||||||
|
|
|
@ -77,7 +77,8 @@ type State[Event any, Env Environment] interface {
|
||||||
// otherwise.
|
// otherwise.
|
||||||
IsTerminal() bool
|
IsTerminal() bool
|
||||||
|
|
||||||
// TODO(roasbeef): also add state serialization?
|
// String returns a human readable string that represents the state.
|
||||||
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DaemonAdapters is a set of methods that server as adapters to bridge the
|
// DaemonAdapters is a set of methods that server as adapters to bridge the
|
||||||
|
@ -597,8 +598,8 @@ func (s *StateMachine[Event, Env]) applyEvents(ctx context.Context,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.log.InfoS(ctx, "State transition",
|
s.log.InfoS(ctx, "State transition",
|
||||||
btclog.Fmt("from_state", "%T", currentState),
|
btclog.Fmt("from_state", "%v", currentState),
|
||||||
btclog.Fmt("to_state", "%T", transition.NextState))
|
btclog.Fmt("to_state", "%v", transition.NextState))
|
||||||
|
|
||||||
// With our events processed, we'll now update our
|
// With our events processed, we'll now update our
|
||||||
// internal state.
|
// internal state.
|
||||||
|
|
|
@ -51,6 +51,10 @@ type dummyStateStart struct {
|
||||||
canSend *atomic.Bool
|
canSend *atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dummyStateStart) String() string {
|
||||||
|
return "dummyStateStart"
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
hexDecode = func(keyStr string) []byte {
|
hexDecode = func(keyStr string) []byte {
|
||||||
keyBytes, _ := hex.DecodeString(keyStr)
|
keyBytes, _ := hex.DecodeString(keyStr)
|
||||||
|
@ -134,6 +138,10 @@ func (d *dummyStateStart) IsTerminal() bool {
|
||||||
type dummyStateFin struct {
|
type dummyStateFin struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dummyStateFin) String() string {
|
||||||
|
return "dummyStateFin"
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dummyStateFin) ProcessEvent(event dummyEvents, env *dummyEnv,
|
func (d *dummyStateFin) ProcessEvent(event dummyEvents, env *dummyEnv,
|
||||||
) (*StateTransition[dummyEvents, *dummyEnv], error) {
|
) (*StateTransition[dummyEvents, *dummyEnv], error) {
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue