mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
reservation: add protocol version
This commit is contained in:
parent
a889d6226b
commit
56848d0cfa
11 changed files with 75 additions and 21 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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...)...,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ func TestSqlStore(t *testing.T) {
|
|||
Family: 1,
|
||||
Index: 1,
|
||||
},
|
||||
ProtocolVersion: ProtocolVersionServerInitiated,
|
||||
}
|
||||
|
||||
err := store.CreateReservation(ctxb, reservation)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
@ -113,6 +113,7 @@ type Reservation struct {
|
|||
TxHash []byte
|
||||
OutIndex sql.NullInt32
|
||||
ConfirmationHeight sql.NullInt32
|
||||
ProtocolVersion int32
|
||||
}
|
||||
|
||||
type ReservationUpdate struct {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue