Skip to content

Commit cb18240

Browse files
authored
Merge pull request #920 from sputn1ck/reservation_protocol_version
Reservations add protocol version
2 parents a889d62 + 56848d0 commit cb18240

11 files changed

+75
-21
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: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,29 @@ package reservation
22

33
import (
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+
1128
const (
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.
4663
func 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

235262
func (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

242270
func (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

249278
func (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

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.

loopdb/sqlc/queries/reservations.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ INSERT INTO reservations (
77
value,
88
client_key_family,
99
client_key_index,
10-
initiation_height
10+
initiation_height,
11+
protocol_version
1112
) VALUES (
1213
$1,
1314
$2,
@@ -16,7 +17,8 @@ INSERT INTO reservations (
1617
$5,
1718
$6,
1819
$7,
19-
$8
20+
$8,
21+
$9
2022
);
2123

2224
-- name: UpdateReservation :exec

loopdb/sqlc/reservations.sql.go

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)