@@ -8,6 +8,17 @@ import (
88 "github.com/lightninglabs/loop/swapserverrpc"
99)
1010
11+ type ProtocolVersion uint32
12+
13+ const (
14+ // ProtocolVersionUndefined is the default protocol version.
15+ ProtocolVersionUndefined ProtocolVersion = 0
16+
17+ // ProtocolVersionServerInitiated is the protocol version where the
18+ // server initiates the reservation.
19+ ProtocolVersionServerInitiated ProtocolVersion = 1
20+ )
21+
1122const (
1223 // defaultObserverSize is the size of the fsm observer channel.
1324 defaultObserverSize = 15
@@ -43,9 +54,10 @@ type FSM struct {
4354}
4455
4556// NewFSM creates a new reservation FSM.
46- func NewFSM (cfg * Config ) * FSM {
57+ func NewFSM (cfg * Config , protocolVersion ProtocolVersion ) * FSM {
4758 reservation := & Reservation {
48- State : fsm .EmptyState ,
59+ State : fsm .EmptyState ,
60+ ProtocolVersion : protocolVersion ,
4961 }
5062
5163 return NewFSMFromReservation (cfg , reservation )
@@ -54,15 +66,24 @@ func NewFSM(cfg *Config) *FSM {
5466// NewFSMFromReservation creates a new reservation FSM from an existing
5567// reservation recovered from the database.
5668func NewFSMFromReservation (cfg * Config , reservation * Reservation ) * FSM {
69+
5770 reservationFsm := & FSM {
5871 cfg : cfg ,
5972 reservation : reservation ,
6073 }
6174
75+ var states fsm.States
76+ switch reservation .ProtocolVersion {
77+ case ProtocolVersionServerInitiated :
78+ states = reservationFsm .GetServerInitiatedReservationStates ()
79+ default :
80+ states = make (fsm.States )
81+ }
82+
6283 reservationFsm .StateMachine = fsm .NewStateMachineWithState (
63- reservationFsm .GetReservationStates (), reservation .State ,
64- defaultObserverSize ,
84+ states , reservation .State , defaultObserverSize ,
6585 )
86+
6687 reservationFsm .ActionEntryFunc = reservationFsm .updateReservation
6788
6889 return reservationFsm
@@ -133,9 +154,9 @@ var (
133154 OnUnlocked = fsm .EventType ("OnUnlocked" )
134155)
135156
136- // GetReservationStates returns the statemap that defines the reservation
137- // state machine.
138- func (f * FSM ) GetReservationStates () fsm.States {
157+ // GetServerInitiatedReservationStates returns the statemap that defines the
158+ // reservation state machine, where the server initiates the reservation .
159+ func (f * FSM ) GetServerInitiatedReservationStates () fsm.States {
139160 return fsm.States {
140161 fsm .EmptyState : fsm.State {
141162 Transitions : fsm.Transitions {
0 commit comments