reservation

package
v0.28.5-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyFamily         = int32(42068)
	DefaultConfTarget = int32(3)
	IdLength          = 32
)
View Source
const Subsystem = "RSRV"

Subsystem defines the sub system name of this package.

Variables

View Source
var (
	// Init is the initial state of the reservation.
	Init = fsm.StateType("Init")

	// WaitForConfirmation is the state where we wait for the reservation
	// tx to be confirmed.
	WaitForConfirmation = fsm.StateType("WaitForConfirmation")

	// Confirmed is the state where the reservation tx has been confirmed.
	Confirmed = fsm.StateType("Confirmed")

	// TimedOut is the state where the reservation has timed out.
	TimedOut = fsm.StateType("TimedOut")

	// Failed is the state where the reservation has failed.
	Failed = fsm.StateType("Failed")

	// Spent is the state where a spend tx has been confirmed.
	Spent = fsm.StateType("Spent")

	// Locked is the state where the reservation is locked and can't be
	// used for instant out swaps.
	Locked = fsm.StateType("Locked")
)

States.

View Source
var (
	// OnServerRequest is the event that is triggered when the server
	// requests a new reservation.
	OnServerRequest = fsm.EventType("OnServerRequest")

	// OnBroadcast is the event that is triggered when the reservation tx
	// has been broadcast.
	OnBroadcast = fsm.EventType("OnBroadcast")

	// OnConfirmed is the event that is triggered when the reservation tx
	// has been confirmed.
	OnConfirmed = fsm.EventType("OnConfirmed")

	// OnTimedOut is the event that is triggered when the reservation has
	// timed out.
	OnTimedOut = fsm.EventType("OnTimedOut")

	// OnSwept is the event that is triggered when the reservation has been
	// swept by the server.
	OnSwept = fsm.EventType("OnSwept")

	// OnRecover is the event that is triggered when the reservation FSM
	// recovers from a restart.
	OnRecover = fsm.EventType("OnRecover")

	// OnSpent is the event that is triggered when the reservation has been
	// spent.
	OnSpent = fsm.EventType("OnSpent")

	// OnLocked is the event that is triggered when the reservation has
	// been locked.
	OnLocked = fsm.EventType("OnLocked")

	// OnUnlocked is the event that is triggered when the reservation has
	// been unlocked.
	OnUnlocked = fsm.EventType("OnUnlocked")
)

Events.

View Source
var (
	ErrReservationAlreadyExists = fmt.Errorf("reservation already exists")
	ErrReservationNotFound      = fmt.Errorf("reservation not found")
)

Functions

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type BaseDB

type BaseDB interface {
	// CreateReservation stores the reservation in the database.
	CreateReservation(ctx context.Context,
		arg sqlc.CreateReservationParams) error

	// GetReservation retrieves the reservation from the database.
	GetReservation(ctx context.Context,
		reservationID []byte) (sqlc.Reservation, error)

	// GetReservationUpdates fetches all updates for a reservation.
	GetReservationUpdates(ctx context.Context,
		reservationID []byte) ([]sqlc.ReservationUpdate, error)

	// GetReservations lists all existing reservations the client has ever
	// made.
	GetReservations(ctx context.Context) ([]sqlc.Reservation, error)

	// UpdateReservation inserts a new reservation update.
	UpdateReservation(ctx context.Context,
		arg sqlc.UpdateReservationParams) error

	// ExecTx allows for executing a function in the context of a database
	// transaction.
	ExecTx(ctx context.Context, txOptions loopdb.TxOptions,
		txBody func(*sqlc.Queries) error) error
}

BaseDB is the interface that contains all the queries generated by sqlc for the reservation table.

type Config

type Config struct {
	// Store is the database store for the reservations.
	Store Store

	// Wallet handles the key derivation for the reservation.
	Wallet lndclient.WalletKitClient

	// ChainNotifier is used to subscribe to block notifications.
	ChainNotifier lndclient.ChainNotifierClient

	// ReservationClient is the client used to communicate with the
	// swap server.
	ReservationClient looprpc.ReservationServiceClient

	// FetchL402 is the function used to fetch the l402 token.
	FetchL402 func(context.Context) error
}

Config contains all the services that the reservation FSM needs to operate.

type FSM

type FSM struct {
	*fsm.StateMachine
	// contains filtered or unexported fields
}

FSM is the state machine that manages the reservation lifecycle.

func NewFSM

func NewFSM(ctx context.Context, cfg *Config) *FSM

NewFSM creates a new reservation FSM.

func NewFSMFromReservation

func NewFSMFromReservation(ctx context.Context, cfg *Config,
	reservation *Reservation) *FSM

NewFSMFromReservation creates a new reservation FSM from an existing reservation recovered from the database.

func (*FSM) AsyncWaitForExpiredOrSweptAction

func (f *FSM) AsyncWaitForExpiredOrSweptAction(_ fsm.EventContext,
) fsm.EventType

AsyncWaitForExpiredOrSweptAction waits for the reservation to be either expired or swept. This is non-blocking and can be used to wait for the reservation to expire while expecting other events.

func (*FSM) Debugf

func (r *FSM) Debugf(format string, args ...interface{})

func (*FSM) Errorf

func (r *FSM) Errorf(format string, args ...interface{})

func (*FSM) GetReservationStates

func (f *FSM) GetReservationStates() fsm.States

GetReservationStates returns the statemap that defines the reservation state machine.

func (*FSM) Infof

func (r *FSM) Infof(format string, args ...interface{})

func (*FSM) InitAction

func (f *FSM) InitAction(eventCtx fsm.EventContext) fsm.EventType

InitAction is the action that is executed when the reservation state machine is initialized. It creates the reservation in the database and dispatches the payment to the server.

func (*FSM) SubscribeToConfirmationAction

func (f *FSM) SubscribeToConfirmationAction(_ fsm.EventContext) fsm.EventType

SubscribeToConfirmationAction is the action that is executed when the reservation is waiting for confirmation. It subscribes to the confirmation of the reservation transaction.

type ID

type ID [IdLength]byte

ID is a unique identifier for a reservation.

func (*ID) FromByteSlice

func (r *ID) FromByteSlice(b []byte) error

FromByteSlice creates a reservation id from a byte slice.

type InitReservationContext

type InitReservationContext struct {
	// contains filtered or unexported fields
}

InitReservationContext contains the request parameters for a reservation.

type Manager

type Manager struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Manager manages the reservation state machines.

func NewManager

func NewManager(cfg *Config) *Manager

NewManager creates a new reservation manager.

func (*Manager) GetReservation

func (m *Manager) GetReservation(ctx context.Context, id ID) (*Reservation,
	error)

GetReservation returns the reservation for the given id.

func (*Manager) GetReservations

func (m *Manager) GetReservations(ctx context.Context) ([]*Reservation, error)

GetReservations retrieves all reservations from the database.

func (*Manager) LockReservation

func (m *Manager) LockReservation(ctx context.Context, id ID) error

LockReservation locks the reservation with the given ID.

func (*Manager) RecoverReservations

func (m *Manager) RecoverReservations(ctx context.Context) error

RecoverReservations tries to recover all reservations that are still active from the database.

func (*Manager) RegisterReservationNotifications

func (m *Manager) RegisterReservationNotifications(
	reservationChan chan *reservationrpc.ServerReservationNotification) error

RegisterReservationNotifications registers a new reservation notification stream.

func (*Manager) Run

func (m *Manager) Run(ctx context.Context, height int32) error

Run runs the reservation manager.

func (*Manager) UnlockReservation

func (m *Manager) UnlockReservation(ctx context.Context, id ID) error

UnlockReservation unlocks the reservation with the given ID.

type Reservation

type Reservation struct {
	// ID is the unique identifier of the reservation.
	ID ID

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

	// ClientPubkey is the client's pubkey.
	ClientPubkey *btcec.PublicKey

	// ServerPubkey is the server's pubkey.
	ServerPubkey *btcec.PublicKey

	// Value is the amount of the reservation.
	Value btcutil.Amount

	// Expiry is the absolute block height at which the reservation expires.
	Expiry uint32

	// KeyLocator is the key locator of the client's key.
	KeyLocator keychain.KeyLocator

	// Outpoint is the outpoint of the reservation.
	Outpoint *wire.OutPoint

	// InitiationHeight is the height at which the reservation was
	// initiated.
	InitiationHeight int32

	// ConfirmationHeight is the height at which the reservation was
	// confirmed.
	ConfirmationHeight uint32
}

Reservation holds all the necessary information for the 2-of-2 multisig reservation utxo.

func NewReservation

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

func (*Reservation) GetPkScript

func (r *Reservation) GetPkScript() ([]byte, error)

GetPkScript returns the pk script of the reservation.

func (*Reservation) Musig2CreateSession

func (r *Reservation) Musig2CreateSession(ctx context.Context,
	signer lndclient.SignerClient) (*input.MuSig2SessionInfo, error)

Musig2CreateSession creates a musig2 session for the reservation.

func (*Reservation) Output

func (r *Reservation) Output() (*wire.TxOut, error)

Output returns the reservation output.

type SQLStore

type SQLStore struct {
	// contains filtered or unexported fields
}

SQLStore manages the reservations in the database.

func NewSQLStore

func NewSQLStore(db BaseDB) *SQLStore

NewSQLStore creates a new SQLStore.

func (*SQLStore) CreateReservation

func (r *SQLStore) CreateReservation(ctx context.Context,
	reservation *Reservation) error

CreateReservation stores the reservation in the database.

func (*SQLStore) GetReservation

func (r *SQLStore) GetReservation(ctx context.Context,
	reservationId ID) (*Reservation, error)

GetReservation retrieves the reservation from the database.

func (*SQLStore) ListReservations

func (r *SQLStore) ListReservations(ctx context.Context) ([]*Reservation,
	error)

ListReservations lists all existing reservations the client has ever made.

func (*SQLStore) UpdateReservation

func (r *SQLStore) UpdateReservation(ctx context.Context,
	reservation *Reservation) error

UpdateReservation updates the reservation in the database.

type Store

type Store interface {
	// CreateReservation stores the reservation in the database.
	CreateReservation(ctx context.Context, reservation *Reservation) error

	// UpdateReservation updates the reservation in the database.
	UpdateReservation(ctx context.Context, reservation *Reservation) error

	// GetReservation retrieves the reservation from the database.
	GetReservation(ctx context.Context, id ID) (*Reservation, error)

	// ListReservations lists all existing reservations the client has ever
	// made.
	ListReservations(ctx context.Context) ([]*Reservation, error)
}

Store is the interface that stores the reservations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL