@@ -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,18 @@ 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+ default :
85+ states = make (fsm.States )
86+ }
87+
6288 reservationFsm .StateMachine = fsm .NewStateMachineWithState (
63- reservationFsm .GetReservationStates (), reservation .State ,
64- defaultObserverSize ,
89+ states , reservation .State , defaultObserverSize ,
6590 )
91+
6692 reservationFsm .ActionEntryFunc = reservationFsm .updateReservation
6793
6894 return reservationFsm
@@ -133,9 +159,9 @@ var (
133159 OnUnlocked = fsm .EventType ("OnUnlocked" )
134160)
135161
136- // GetReservationStates returns the statemap that defines the reservation
137- // state machine.
138- func (f * FSM ) GetReservationStates () fsm.States {
162+ // GetServerInitiatedReservationStates returns the statemap that defines the
163+ // reservation state machine, where the server initiates the reservation .
164+ func (f * FSM ) GetServerInitiatedReservationStates () fsm.States {
139165 return fsm.States {
140166 fsm .EmptyState : fsm.State {
141167 Transitions : fsm.Transitions {
@@ -234,22 +260,25 @@ func (r *FSM) updateReservation(ctx context.Context,
234260
235261func (r * FSM ) Infof (format string , args ... interface {}) {
236262 log .Infof (
237- "Reservation %x: " + format ,
238- append ([]interface {}{r .reservation .ID }, args ... )... ,
263+ "Reservation %v %x: " + format ,
264+ append ([]interface {}{r .reservation .ProtocolVersion , r .reservation .ID },
265+ args ... )... ,
239266 )
240267}
241268
242269func (r * FSM ) Debugf (format string , args ... interface {}) {
243270 log .Debugf (
244- "Reservation %x: " + format ,
245- append ([]interface {}{r .reservation .ID }, args ... )... ,
271+ "Reservation %v %x: " + format ,
272+ append ([]interface {}{r .reservation .ProtocolVersion , r .reservation .ID },
273+ args ... )... ,
246274 )
247275}
248276
249277func (r * FSM ) Errorf (format string , args ... interface {}) {
250278 log .Errorf (
251- "Reservation %x: " + format ,
252- append ([]interface {}{r .reservation .ID }, args ... )... ,
279+ "Reservation %v %x: " + format ,
280+ append ([]interface {}{r .reservation .ProtocolVersion , r .reservation .ID },
281+ args ... )... ,
253282 )
254283}
255284
0 commit comments