reservation: add protocol version

This commit is contained in:
sputn1ck 2025-02-03 14:00:10 +01:00
parent a889d6226b
commit 56848d0cfa
No known key found for this signature in database
GPG key ID: 671103D881A5F0E4
11 changed files with 75 additions and 21 deletions

View file

@ -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)

View file

@ -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...)...,
)
}

View file

@ -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
}

View file

@ -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
}

View file

@ -33,6 +33,7 @@ func TestSqlStore(t *testing.T) {
Family: 1,
Index: 1,
},
ProtocolVersion: ProtocolVersionServerInitiated,
}
err := store.CreateReservation(ctxb, reservation)