@@ -2,12 +2,29 @@ package reservation
22
33import (
44 "context"
5+ "fmt"
56
67 "github.com/lightninglabs/lndclient"
78 "github.com/lightninglabs/loop/fsm"
89 "github.com/lightninglabs/loop/swapserverrpc"
910)
1011
12+ type ProtocolVersion uint32
13+
14+ // String returns the string representation of the protocol version.
15+ func (v ProtocolVersion ) String () string {
16+ return fmt .Sprintf ("ProtocolVersion(%d)" , v )
17+ }
18+
19+ const (
20+ // CurrentProtocolVersion is the current protocol version.
21+ CurrentProtocolVersion ProtocolVersion = ProtocolVersionServerInitiated
22+
23+ // ProtocolVersionServerInitiated is the protocol version where the
24+ // server initiates the reservation.
25+ ProtocolVersionServerInitiated ProtocolVersion = 0
26+ )
27+
1128const (
1229 // defaultObserverSize is the size of the fsm observer channel.
1330 defaultObserverSize = 15
@@ -45,7 +62,8 @@ type FSM struct {
4562// NewFSM creates a new reservation FSM.
4663func NewFSM (cfg * Config ) * FSM {
4764 reservation := & Reservation {
48- State : fsm .EmptyState ,
65+ State : fsm .EmptyState ,
66+ ProtocolVersion : CurrentProtocolVersion ,
4967 }
5068
5169 return NewFSMFromReservation (cfg , reservation )
@@ -59,10 +77,19 @@ func NewFSMFromReservation(cfg *Config, reservation *Reservation) *FSM {
5977 reservation : reservation ,
6078 }
6179
80+ var states fsm.States
81+ switch reservation .ProtocolVersion {
82+ case ProtocolVersionServerInitiated :
83+ states = reservationFsm .GetServerInitiatedReservationStates ()
84+
85+ default :
86+ states = make (fsm.States )
87+ }
88+
6289 reservationFsm .StateMachine = fsm .NewStateMachineWithState (
63- reservationFsm .GetReservationStates (), reservation .State ,
64- defaultObserverSize ,
90+ states , reservation .State , defaultObserverSize ,
6591 )
92+
6693 reservationFsm .ActionEntryFunc = reservationFsm .updateReservation
6794
6895 return reservationFsm
@@ -133,9 +160,9 @@ var (
133160 OnUnlocked = fsm .EventType ("OnUnlocked" )
134161)
135162
136- // GetReservationStates returns the statemap that defines the reservation
137- // state machine.
138- func (f * FSM ) GetReservationStates () fsm.States {
163+ // GetServerInitiatedReservationStates returns the statemap that defines the
164+ // reservation state machine, where the server initiates the reservation .
165+ func (f * FSM ) GetServerInitiatedReservationStates () fsm.States {
139166 return fsm.States {
140167 fsm .EmptyState : fsm.State {
141168 Transitions : fsm.Transitions {
@@ -234,22 +261,25 @@ func (r *FSM) updateReservation(ctx context.Context,
234261
235262func (r * FSM ) Infof (format string , args ... interface {}) {
236263 log .Infof (
237- "Reservation %x: " + format ,
238- append ([]interface {}{r .reservation .ID }, args ... )... ,
264+ "Reservation %v %x: " + format ,
265+ append ([]interface {}{r .reservation .ProtocolVersion , r .reservation .ID },
266+ args ... )... ,
239267 )
240268}
241269
242270func (r * FSM ) Debugf (format string , args ... interface {}) {
243271 log .Debugf (
244- "Reservation %x: " + format ,
245- append ([]interface {}{r .reservation .ID }, args ... )... ,
272+ "Reservation %v %x: " + format ,
273+ append ([]interface {}{r .reservation .ProtocolVersion , r .reservation .ID },
274+ args ... )... ,
246275 )
247276}
248277
249278func (r * FSM ) Errorf (format string , args ... interface {}) {
250279 log .Errorf (
251- "Reservation %x: " + format ,
252- append ([]interface {}{r .reservation .ID }, args ... )... ,
280+ "Reservation %v %x: " + format ,
281+ append ([]interface {}{r .reservation .ProtocolVersion , r .reservation .ID },
282+ args ... )... ,
253283 )
254284}
255285
0 commit comments