From 56848d0cfaed38a9887a55ba1043c16824610bd2 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Mon, 3 Feb 2025 14:00:10 +0100 Subject: [PATCH] reservation: add protocol version --- fsm/stateparser/stateparser.go | 2 +- instantout/reservation/actions.go | 1 + instantout/reservation/fsm.go | 54 ++++++++++++++----- instantout/reservation/reservation.go | 9 +++- instantout/reservation/store.go | 2 + instantout/reservation/store_test.go | 1 + ...0014_reservation_protocol_version.down.sql | 3 ++ ...000014_reservation_protocol_version.up.sql | 3 ++ loopdb/sqlc/models.go | 1 + loopdb/sqlc/queries/reservations.sql | 6 ++- loopdb/sqlc/reservations.sql.go | 14 +++-- 11 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 loopdb/sqlc/migrations/000014_reservation_protocol_version.down.sql create mode 100644 loopdb/sqlc/migrations/000014_reservation_protocol_version.up.sql diff --git a/fsm/stateparser/stateparser.go b/fsm/stateparser/stateparser.go index d70645230..cafedd7d3 100644 --- a/fsm/stateparser/stateparser.go +++ b/fsm/stateparser/stateparser.go @@ -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 } diff --git a/instantout/reservation/actions.go b/instantout/reservation/actions.go index 1d58cd744..c8fbedfd7 100644 --- a/instantout/reservation/actions.go +++ b/instantout/reservation/actions.go @@ -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) diff --git a/instantout/reservation/fsm.go b/instantout/reservation/fsm.go index 6bf567d28..620c78a0f 100644 --- a/instantout/reservation/fsm.go +++ b/instantout/reservation/fsm.go @@ -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 +) + const ( // defaultObserverSize is the size of the fsm observer channel. defaultObserverSize = 15 @@ -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) @@ -59,10 +77,19 @@ func NewFSMFromReservation(cfg *Config, reservation *Reservation) *FSM { reservation: reservation, } + var states fsm.States + switch reservation.ProtocolVersion { + 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 @@ -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{ @@ -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...)..., ) } diff --git a/instantout/reservation/reservation.go b/instantout/reservation/reservation.go index f9d5b66e4..5a167d2e1 100644 --- a/instantout/reservation/reservation.go +++ b/instantout/reservation/reservation.go @@ -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 @@ -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") @@ -103,6 +107,7 @@ func NewReservation(id ID, serverPubkey, clientPubkey *btcec.PublicKey, KeyLocator: keyLocator, Expiry: expiry, InitiationHeight: int32(heightHint), + ProtocolVersion: protocolVersion, }, nil } diff --git a/instantout/reservation/store.go b/instantout/reservation/store.go index b009d0a4c..117d02c69 100644 --- a/instantout/reservation/store.go +++ b/instantout/reservation/store.go @@ -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{ @@ -287,6 +288,7 @@ func sqlReservationToReservation(row sqlc.Reservation, ), InitiationHeight: row.InitiationHeight, State: fsm.StateType(lastUpdate.UpdateState), + ProtocolVersion: ProtocolVersion(row.ProtocolVersion), }, nil } diff --git a/instantout/reservation/store_test.go b/instantout/reservation/store_test.go index feb43257b..e5ed49636 100644 --- a/instantout/reservation/store_test.go +++ b/instantout/reservation/store_test.go @@ -33,6 +33,7 @@ func TestSqlStore(t *testing.T) { Family: 1, Index: 1, }, + ProtocolVersion: ProtocolVersionServerInitiated, } err := store.CreateReservation(ctxb, reservation) diff --git a/loopdb/sqlc/migrations/000014_reservation_protocol_version.down.sql b/loopdb/sqlc/migrations/000014_reservation_protocol_version.down.sql new file mode 100644 index 000000000..d23af75ec --- /dev/null +++ b/loopdb/sqlc/migrations/000014_reservation_protocol_version.down.sql @@ -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; diff --git a/loopdb/sqlc/migrations/000014_reservation_protocol_version.up.sql b/loopdb/sqlc/migrations/000014_reservation_protocol_version.up.sql new file mode 100644 index 000000000..29da580a0 --- /dev/null +++ b/loopdb/sqlc/migrations/000014_reservation_protocol_version.up.sql @@ -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; \ No newline at end of file diff --git a/loopdb/sqlc/models.go b/loopdb/sqlc/models.go index 0f2a166b7..c2e617aab 100644 --- a/loopdb/sqlc/models.go +++ b/loopdb/sqlc/models.go @@ -113,6 +113,7 @@ type Reservation struct { TxHash []byte OutIndex sql.NullInt32 ConfirmationHeight sql.NullInt32 + ProtocolVersion int32 } type ReservationUpdate struct { diff --git a/loopdb/sqlc/queries/reservations.sql b/loopdb/sqlc/queries/reservations.sql index c96d664c9..ba95f53a7 100644 --- a/loopdb/sqlc/queries/reservations.sql +++ b/loopdb/sqlc/queries/reservations.sql @@ -7,7 +7,8 @@ INSERT INTO reservations ( value, client_key_family, client_key_index, - initiation_height + initiation_height, + protocol_version ) VALUES ( $1, $2, @@ -16,7 +17,8 @@ INSERT INTO reservations ( $5, $6, $7, - $8 + $8, + $9 ); -- name: UpdateReservation :exec diff --git a/loopdb/sqlc/reservations.sql.go b/loopdb/sqlc/reservations.sql.go index b26ccbcf8..3a1ba8987 100644 --- a/loopdb/sqlc/reservations.sql.go +++ b/loopdb/sqlc/reservations.sql.go @@ -20,7 +20,8 @@ INSERT INTO reservations ( value, client_key_family, client_key_index, - initiation_height + initiation_height, + protocol_version ) VALUES ( $1, $2, @@ -29,7 +30,8 @@ INSERT INTO reservations ( $5, $6, $7, - $8 + $8, + $9 ) ` @@ -42,6 +44,7 @@ type CreateReservationParams struct { ClientKeyFamily int32 ClientKeyIndex int32 InitiationHeight int32 + ProtocolVersion int32 } func (q *Queries) CreateReservation(ctx context.Context, arg CreateReservationParams) error { @@ -54,13 +57,14 @@ func (q *Queries) CreateReservation(ctx context.Context, arg CreateReservationPa arg.ClientKeyFamily, arg.ClientKeyIndex, arg.InitiationHeight, + arg.ProtocolVersion, ) return err } const getReservation = `-- name: GetReservation :one SELECT - id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height + id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height, protocol_version FROM reservations WHERE @@ -83,6 +87,7 @@ func (q *Queries) GetReservation(ctx context.Context, reservationID []byte) (Res &i.TxHash, &i.OutIndex, &i.ConfirmationHeight, + &i.ProtocolVersion, ) return i, err } @@ -128,7 +133,7 @@ func (q *Queries) GetReservationUpdates(ctx context.Context, reservationID []byt const getReservations = `-- name: GetReservations :many SELECT - id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height + id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height, protocol_version FROM reservations ORDER BY @@ -157,6 +162,7 @@ func (q *Queries) GetReservations(ctx context.Context) ([]Reservation, error) { &i.TxHash, &i.OutIndex, &i.ConfirmationHeight, + &i.ProtocolVersion, ); err != nil { return nil, err }