Skip to content

Commit c2d199b

Browse files
committed
reservation: add protocol version
1 parent 84820f2 commit c2d199b

File tree

12 files changed

+62
-17
lines changed

12 files changed

+62
-17
lines changed

fsm/stateparser/stateparser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func run() error {
4545

4646
case "reservation":
4747
reservationFSM := &reservation.FSM{}
48-
err = writeMermaidFile(fp, reservationFSM.GetReservationStates())
48+
err = writeMermaidFile(fp, reservationFSM.GetServerInitiatedReservationStates())
4949
if err != nil {
5050
return err
5151
}

instantout/reservation/actions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (f *FSM) InitAction(ctx context.Context,
5858
reservationRequest.expiry,
5959
reservationRequest.heightHint,
6060
keyRes.KeyLocator,
61+
ProtocolVersionServerInitiated,
6162
)
6263
if err != nil {
6364
return f.HandleError(err)

instantout/reservation/fsm.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1122
const (
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.
5668
func 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{

instantout/reservation/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (m *Manager) newReservation(ctx context.Context, currentHeight uint32,
113113
// Create the reservation state machine. We need to pass in the runCtx
114114
// of the reservation manager so that the state machine will keep on
115115
// running even if the grpc conte
116-
reservationFSM := NewFSM(m.cfg)
116+
reservationFSM := NewFSM(m.cfg, ProtocolVersionServerInitiated)
117117

118118
// Add the reservation to the active reservations map.
119119
m.Lock()

instantout/reservation/reservation.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ type Reservation struct {
3737
// ID is the unique identifier of the reservation.
3838
ID ID
3939

40+
// ProtocolVersion is the version of the protocol used for the
41+
// reservation.
42+
ProtocolVersion ProtocolVersion
43+
4044
// State is the current state of the reservation.
4145
State fsm.StateType
4246

@@ -69,8 +73,8 @@ type Reservation struct {
6973

7074
func NewReservation(id ID, serverPubkey, clientPubkey *btcec.PublicKey,
7175
value btcutil.Amount, expiry, heightHint uint32,
72-
keyLocator keychain.KeyLocator) (*Reservation,
73-
error) {
76+
keyLocator keychain.KeyLocator, protocolVersion ProtocolVersion) (
77+
*Reservation, error) {
7478

7579
if id == [32]byte{} {
7680
return nil, errors.New("id is empty")
@@ -103,6 +107,7 @@ func NewReservation(id ID, serverPubkey, clientPubkey *btcec.PublicKey,
103107
KeyLocator: keyLocator,
104108
Expiry: expiry,
105109
InitiationHeight: int32(heightHint),
110+
ProtocolVersion: protocolVersion,
106111
}, nil
107112
}
108113

instantout/reservation/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func (r *SQLStore) CreateReservation(ctx context.Context,
8383
ClientKeyFamily: int32(reservation.KeyLocator.Family),
8484
ClientKeyIndex: int32(reservation.KeyLocator.Index),
8585
InitiationHeight: reservation.InitiationHeight,
86+
ProtocolVersion: int32(reservation.ProtocolVersion),
8687
}
8788

8889
updateArgs := sqlc.InsertReservationUpdateParams{
@@ -287,6 +288,7 @@ func sqlReservationToReservation(row sqlc.Reservation,
287288
),
288289
InitiationHeight: row.InitiationHeight,
289290
State: fsm.StateType(lastUpdate.UpdateState),
291+
ProtocolVersion: ProtocolVersion(row.ProtocolVersion),
290292
}, nil
291293
}
292294

instantout/reservation/store_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestSqlStore(t *testing.T) {
3333
Family: 1,
3434
Index: 1,
3535
},
36+
ProtocolVersion: ProtocolVersionServerInitiated,
3637
}
3738

3839
err := store.CreateReservation(ctxb, reservation)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- protocol_version is used to determine the version of the reservation protocol
2+
-- that was used to create the reservation.
3+
ALTER TABLE reservations DROP COLUMN protocol_Version;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- protocol_version is used to determine the version of the reservation protocol
2+
-- that was used to create the reservation.
3+
ALTER TABLE reservations ADD COLUMN protocol_Version INTEGER NOT NULL DEFAULT 1;

loopdb/sqlc/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)