protofsm: add String() method to ProtocolState

This commit is contained in:
Olaoluwa Osuntokun 2025-02-07 18:17:27 -08:00
parent 683aa61a6f
commit 71c3127c85
3 changed files with 65 additions and 3 deletions

View file

@ -361,6 +361,9 @@ type ProtocolState interface {
// ProcessEvent takes a protocol event, and implements a state
// transition for the state.
ProcessEvent(ProtocolEvent, *Environment) (*CloseStateTransition, error)
// String returns the name of the state.
String() string
}
// AsymmetricPeerState is an extension of the normal ProtocolState interface
@ -401,6 +404,11 @@ type ProtocolStates interface {
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.
func (c *ChannelActive) IsTerminal() bool {
return false
@ -443,6 +451,11 @@ type ShutdownPending struct {
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.
func (s *ShutdownPending) IsTerminal() bool {
return false
@ -478,6 +491,11 @@ type ChannelFlushing struct {
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.
func (c *ChannelFlushing) protocolStateSealed() {}
@ -507,6 +525,15 @@ type ClosingNegotiation struct {
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.
func (c *ClosingNegotiation) IsTerminal() bool {
return false
@ -594,6 +621,12 @@ type LocalCloseStart struct {
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
// event.
func (l *LocalCloseStart) ShouldRouteTo(event ProtocolEvent) bool {
@ -634,6 +667,11 @@ type LocalOfferSent struct {
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
// event.
func (l *LocalOfferSent) ShouldRouteTo(event ProtocolEvent) bool {
@ -670,6 +708,11 @@ type ClosePending struct {
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
// event.
func (c *ClosePending) ShouldRouteTo(event ProtocolEvent) bool {
@ -696,6 +739,11 @@ type CloseFin struct {
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.
func (c *CloseFin) protocolStateSealed() {}
@ -714,6 +762,11 @@ type RemoteCloseStart struct {
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
// event.
func (l *RemoteCloseStart) ShouldRouteTo(event ProtocolEvent) bool {

View file

@ -77,7 +77,8 @@ type State[Event any, Env Environment] interface {
// otherwise.
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
@ -597,8 +598,8 @@ func (s *StateMachine[Event, Env]) applyEvents(ctx context.Context,
}
s.log.InfoS(ctx, "State transition",
btclog.Fmt("from_state", "%T", currentState),
btclog.Fmt("to_state", "%T", transition.NextState))
btclog.Fmt("from_state", "%v", currentState),
btclog.Fmt("to_state", "%v", transition.NextState))
// With our events processed, we'll now update our
// internal state.

View file

@ -51,6 +51,10 @@ type dummyStateStart struct {
canSend *atomic.Bool
}
func (d *dummyStateStart) String() string {
return "dummyStateStart"
}
var (
hexDecode = func(keyStr string) []byte {
keyBytes, _ := hex.DecodeString(keyStr)
@ -134,6 +138,10 @@ func (d *dummyStateStart) IsTerminal() bool {
type dummyStateFin struct {
}
func (d *dummyStateFin) String() string {
return "dummyStateFin"
}
func (d *dummyStateFin) ProcessEvent(event dummyEvents, env *dummyEnv,
) (*StateTransition[dummyEvents, *dummyEnv], error) {