protofsm: implement the actor.ActorBehavior interface for StateMachine
What changed, and why it matters
This commit is a routine feature addition: it lets an existing internal state machine receive messages through a new 'actor' messaging interface. There is no indication of a security bug, fix, or vulnerability in the changes.
No security action required; review as normal feature code.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds an ActorMessage wrapper type and implements actor.ActorBehavior.Receive on StateMachine. Receive simply forwards the wrapped Event into the state machine’s existing events channel, respecting actor shutdown context and the state machine’s quit channel. It is pure plumbing/adapter code with no parsing, authorization, cryptographic, network, or resource-handling changes that would introduce or fix a security issue.
Changed components
protofsm/actor_wrapper.goprotofsm/state_machine.goInspect captured patch +43 / −0
diff --git a/protofsm/actor_wrapper.go b/protofsm/actor_wrapper.go
new file mode 100644
index 0000000..b0be9a0
--- /dev/null
+++ b/protofsm/actor_wrapper.go
@@ -0,0 +1,23 @@
+package protofsm
+
+import (
+ "fmt"
+
+ "github.com/lightningnetwork/lnd/actor"
+)
+
+// ActorMessage wraps an Event, in order to create a new message that can be
+// used with the actor package.
+type ActorMessage[Event any] struct {
+ actor.BaseMessage
+
+ // Event is the event that is being sent to the actor.
+ Event Event
+}
+
+// MessageType returns the type of the message.
+//
+// NOTE: This implements the actor.Message interface.
+func (a ActorMessage[Event]) MessageType() string {
+ return fmt.Sprintf("ActorMessage(%T)", a.Event)
+}
diff --git a/protofsm/state_machine.go b/protofsm/state_machine.go
index b3e16f5..b0c376c 100644
--- a/protofsm/state_machine.go
+++ b/protofsm/state_machine.go
@@ -259,6 +259,26 @@ func (s *StateMachine[Event, Env]) SendEvent(ctx context.Context, event Event) {
}
}
+// Receive processes a message and returns a Result. The provided context is the
+// actor's internal context, which can be used to detect actor shutdown
+// requests.
+//
+// NOTE: This implements the actor.ActorBehavior interface.
+func (s *StateMachine[Event, Env]) Receive(ctx context.Context,
+ e ActorMessage[Event]) fn.Result[bool] {
+
+ select {
+ case s.events <- e.Event:
+ return fn.Ok(true)
+
+ case <-ctx.Done():
+ return fn.Err[bool](ctx.Err())
+
+ case <-s.quit:
+ return fn.Err[bool](ErrStateMachineShutdown)
+ }
+}
+
// CanHandle returns true if the target message can be routed to the state
// machine.
func (s *StateMachine[Event, Env]) CanHandle(msg msgmux.PeerMsg) bool {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.