Skip to content

Commit 3eab968

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

File tree

12 files changed

+58
-17
lines changed

12 files changed

+58
-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: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import (
88
"github.com/lightninglabs/loop/swapserverrpc"
99
)
1010

11+
type ProtocolVersion uint32
12+
13+
const (
14+
// ProtocolVersionServerInitiated is the protocol version where the
15+
// server initiates the reservation.
16+
ProtocolVersionServerInitiated ProtocolVersion = 0
17+
)
18+
1119
const (
1220
// defaultObserverSize is the size of the fsm observer channel.
1321
defaultObserverSize = 15
@@ -43,9 +51,10 @@ type FSM struct {
4351
}
4452

4553
// NewFSM creates a new reservation FSM.
46-
func NewFSM(cfg *Config) *FSM {
54+
func NewFSM(cfg *Config, protocolVersion ProtocolVersion) *FSM {
4755
reservation := &Reservation{
48-
State: fsm.EmptyState,
56+
State: fsm.EmptyState,
57+
ProtocolVersion: protocolVersion,
4958
}
5059

5160
return NewFSMFromReservation(cfg, reservation)
@@ -59,10 +68,18 @@ func NewFSMFromReservation(cfg *Config, reservation *Reservation) *FSM {
5968
reservation: reservation,
6069
}
6170

71+
var states fsm.States
72+
switch reservation.ProtocolVersion {
73+
case ProtocolVersionServerInitiated:
74+
states = reservationFsm.GetServerInitiatedReservationStates()
75+
default:
76+
states = make(fsm.States)
77+
}
78+
6279
reservationFsm.StateMachine = fsm.NewStateMachineWithState(
63-
reservationFsm.GetReservationStates(), reservation.State,
64-
defaultObserverSize,
80+
states, reservation.State, defaultObserverSize,
6581
)
82+
6683
reservationFsm.ActionEntryFunc = reservationFsm.updateReservation
6784

6885
return reservationFsm
@@ -133,9 +150,9 @@ var (
133150
OnUnlocked = fsm.EventType("OnUnlocked")
134151
)
135152

136-
// GetReservationStates returns the statemap that defines the reservation
137-
// state machine.
138-
func (f *FSM) GetReservationStates() fsm.States {
153+
// GetServerInitiatedReservationStates returns the statemap that defines the
154+
// reservation state machine, where the server initiates the reservation.
155+
func (f *FSM) GetServerInitiatedReservationStates() fsm.States {
139156
return fsm.States{
140157
fsm.EmptyState: fsm.State{
141158
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 0;

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)