Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fsm/stateparser/stateparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func run() error {

case "reservation":
reservationFSM := &reservation.FSM{}
err = writeMermaidFile(fp, reservationFSM.GetReservationStates())
err = writeMermaidFile(fp, reservationFSM.GetServerInitiatedReservationStates())
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions instantout/reservation/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (f *FSM) InitAction(ctx context.Context,
reservationRequest.expiry,
reservationRequest.heightHint,
keyRes.KeyLocator,
ProtocolVersionServerInitiated,
)
if err != nil {
return f.HandleError(err)
Expand Down
54 changes: 42 additions & 12 deletions instantout/reservation/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@ package reservation

import (
"context"
"fmt"

"github.com/lightninglabs/lndclient"
"github.com/lightninglabs/loop/fsm"
"github.com/lightninglabs/loop/swapserverrpc"
)

type ProtocolVersion uint32

// String returns the string representation of the protocol version.
func (v ProtocolVersion) String() string {
return fmt.Sprintf("ProtocolVersion(%d)", v)
}

const (
// CurrentProtocolVersion is the current protocol version.
CurrentProtocolVersion ProtocolVersion = ProtocolVersionServerInitiated

// ProtocolVersionServerInitiated is the protocol version where the
// server initiates the reservation.
ProtocolVersionServerInitiated ProtocolVersion = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could have a CurrentProtocolVersion function that returns the last one, so we don't need to pass it in when creating a new reservation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no current as we will still use both simultaneously

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, so the reason to not have is because there will be two current versions, one for the client requested one for the server requested.

)

const (
// defaultObserverSize is the size of the fsm observer channel.
defaultObserverSize = 15
Expand Down Expand Up @@ -45,7 +62,8 @@ type FSM struct {
// NewFSM creates a new reservation FSM.
func NewFSM(cfg *Config) *FSM {
reservation := &Reservation{
State: fsm.EmptyState,
State: fsm.EmptyState,
ProtocolVersion: CurrentProtocolVersion,
}

return NewFSMFromReservation(cfg, reservation)
Expand All @@ -59,10 +77,19 @@ func NewFSMFromReservation(cfg *Config, reservation *Reservation) *FSM {
reservation: reservation,
}

var states fsm.States
switch reservation.ProtocolVersion {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to log here which version of the state machine is running. Perhaps also consider adding a String() function to the ProtocolVersion type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, will do that for the static state machine as well.

case ProtocolVersionServerInitiated:
states = reservationFsm.GetServerInitiatedReservationStates()

default:
states = make(fsm.States)
}

reservationFsm.StateMachine = fsm.NewStateMachineWithState(
reservationFsm.GetReservationStates(), reservation.State,
defaultObserverSize,
states, reservation.State, defaultObserverSize,
)

reservationFsm.ActionEntryFunc = reservationFsm.updateReservation

return reservationFsm
Expand Down Expand Up @@ -133,9 +160,9 @@ var (
OnUnlocked = fsm.EventType("OnUnlocked")
)

// GetReservationStates returns the statemap that defines the reservation
// state machine.
func (f *FSM) GetReservationStates() fsm.States {
// GetServerInitiatedReservationStates returns the statemap that defines the
// reservation state machine, where the server initiates the reservation.
func (f *FSM) GetServerInitiatedReservationStates() fsm.States {
return fsm.States{
fsm.EmptyState: fsm.State{
Transitions: fsm.Transitions{
Expand Down Expand Up @@ -234,22 +261,25 @@ func (r *FSM) updateReservation(ctx context.Context,

func (r *FSM) Infof(format string, args ...interface{}) {
log.Infof(
"Reservation %x: "+format,
append([]interface{}{r.reservation.ID}, args...)...,
"Reservation %v %x: "+format,
append([]interface{}{r.reservation.ProtocolVersion, r.reservation.ID},
args...)...,
)
}

func (r *FSM) Debugf(format string, args ...interface{}) {
log.Debugf(
"Reservation %x: "+format,
append([]interface{}{r.reservation.ID}, args...)...,
"Reservation %v %x: "+format,
append([]interface{}{r.reservation.ProtocolVersion, r.reservation.ID},
args...)...,
)
}

func (r *FSM) Errorf(format string, args ...interface{}) {
log.Errorf(
"Reservation %x: "+format,
append([]interface{}{r.reservation.ID}, args...)...,
"Reservation %v %x: "+format,
append([]interface{}{r.reservation.ProtocolVersion, r.reservation.ID},
args...)...,
)
}

Expand Down
9 changes: 7 additions & 2 deletions instantout/reservation/reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type Reservation struct {
// ID is the unique identifier of the reservation.
ID ID

// ProtocolVersion is the version of the protocol used for the
// reservation.
ProtocolVersion ProtocolVersion

// State is the current state of the reservation.
State fsm.StateType

Expand Down Expand Up @@ -69,8 +73,8 @@ type Reservation struct {

func NewReservation(id ID, serverPubkey, clientPubkey *btcec.PublicKey,
value btcutil.Amount, expiry, heightHint uint32,
keyLocator keychain.KeyLocator) (*Reservation,
error) {
keyLocator keychain.KeyLocator, protocolVersion ProtocolVersion) (
*Reservation, error) {

if id == [32]byte{} {
return nil, errors.New("id is empty")
Expand Down Expand Up @@ -103,6 +107,7 @@ func NewReservation(id ID, serverPubkey, clientPubkey *btcec.PublicKey,
KeyLocator: keyLocator,
Expiry: expiry,
InitiationHeight: int32(heightHint),
ProtocolVersion: protocolVersion,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions instantout/reservation/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (r *SQLStore) CreateReservation(ctx context.Context,
ClientKeyFamily: int32(reservation.KeyLocator.Family),
ClientKeyIndex: int32(reservation.KeyLocator.Index),
InitiationHeight: reservation.InitiationHeight,
ProtocolVersion: int32(reservation.ProtocolVersion),
}

updateArgs := sqlc.InsertReservationUpdateParams{
Expand Down Expand Up @@ -287,6 +288,7 @@ func sqlReservationToReservation(row sqlc.Reservation,
),
InitiationHeight: row.InitiationHeight,
State: fsm.StateType(lastUpdate.UpdateState),
ProtocolVersion: ProtocolVersion(row.ProtocolVersion),
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions instantout/reservation/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestSqlStore(t *testing.T) {
Family: 1,
Index: 1,
},
ProtocolVersion: ProtocolVersionServerInitiated,
}

err := store.CreateReservation(ctxb, reservation)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- protocol_version is used to determine the version of the reservation protocol
-- that was used to create the reservation.
ALTER TABLE reservations DROP COLUMN protocol_Version;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- protocol_version is used to determine the version of the reservation protocol
-- that was used to create the reservation.
ALTER TABLE reservations ADD COLUMN protocol_Version INTEGER NOT NULL DEFAULT 0;
1 change: 1 addition & 0 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions loopdb/sqlc/queries/reservations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ INSERT INTO reservations (
value,
client_key_family,
client_key_index,
initiation_height
initiation_height,
protocol_version
) VALUES (
$1,
$2,
Expand All @@ -16,7 +17,8 @@ INSERT INTO reservations (
$5,
$6,
$7,
$8
$8,
$9
);

-- name: UpdateReservation :exec
Expand Down
14 changes: 10 additions & 4 deletions loopdb/sqlc/reservations.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.