instantout

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: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyFamily = int32(42069)
)
View Source
const Subsystem = "INST"

Subsystem defines the sub system name of this package.

Variables

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

	// SendPaymentAndPollAccepted is the state where the payment is sent
	// and the server is polled for the accepted state.
	SendPaymentAndPollAccepted = fsm.StateType("SendPaymentAndPollAccepted")

	// BuildHtlc is the state where the htlc transaction is built.
	BuildHtlc = fsm.StateType("BuildHtlc")

	// PushPreimage is the state where the preimage is pushed to the server.
	PushPreimage = fsm.StateType("PushPreimage")

	// WaitForSweeplessSweepConfirmed is the state where we wait for the
	// sweepless sweep to be confirmed.
	WaitForSweeplessSweepConfirmed = fsm.StateType(
		"WaitForSweeplessSweepConfirmed")

	// FinishedSweeplessSweep is the state where the swap is finished by
	// publishing the sweepless sweep.
	FinishedSweeplessSweep = fsm.StateType("FinishedSweeplessSweep")

	// PublishHtlc is the state where the htlc transaction is published.
	PublishHtlc = fsm.StateType("PublishHtlc")

	// PublishHtlcSweep is the state where the htlc sweep transaction is
	// published.
	PublishHtlcSweep = fsm.StateType("PublishHtlcSweep")

	// FinishedHtlcPreimageSweep is the state where the swap is finished by
	// publishing the htlc preimage sweep.
	FinishedHtlcPreimageSweep = fsm.StateType("FinishedHtlcPreimageSweep")

	// WaitForHtlcSweepConfirmed is the state where we wait for the htlc
	// sweep to be confirmed.
	WaitForHtlcSweepConfirmed = fsm.StateType("WaitForHtlcSweepConfirmed")

	// FailedHtlcSweep is the state where the htlc sweep failed.
	FailedHtlcSweep = fsm.StateType("FailedHtlcSweep")

	// Failed is the state where the swap failed.
	Failed = fsm.StateType("InstantOutFailed")
)

States.

View Source
var (
	// OnStart is the event that is sent when the FSM is started.
	OnStart = fsm.EventType("OnStart")

	// OnInit is the event that is triggered when the FSM is initialized.
	OnInit = fsm.EventType("OnInit")

	// OnPaymentAccepted is the event that is triggered when the payment
	// is accepted by the server.
	OnPaymentAccepted = fsm.EventType("OnPaymentAccepted")

	// OnHtlcSigReceived is the event that is triggered when the htlc sig
	// is received.
	OnHtlcSigReceived = fsm.EventType("OnHtlcSigReceived")

	// OnPreimagePushed is the event that is triggered when the preimage
	// is pushed to the server.
	OnPreimagePushed = fsm.EventType("OnPreimagePushed")

	// OnSweeplessSweepPublished is the event that is triggered when the
	// sweepless sweep is published.
	OnSweeplessSweepPublished = fsm.EventType("OnSweeplessSweepPublished")

	// OnSweeplessSweepConfirmed is the event that is triggered when the
	// sweepless sweep is confirmed.
	OnSweeplessSweepConfirmed = fsm.EventType("OnSweeplessSweepConfirmed")

	// OnErrorPublishHtlc is the event that is triggered when the htlc
	// sweep is published after an error.
	OnErrorPublishHtlc = fsm.EventType("OnErrorPublishHtlc")

	// OnInvalidCoopSweep is the event that is triggered when the coop
	// sweep is invalid.
	OnInvalidCoopSweep = fsm.EventType("OnInvalidCoopSweep")

	// OnHtlcPublished is the event that is triggered when the htlc
	// transaction is published.
	OnHtlcPublished = fsm.EventType("OnHtlcPublished")

	// OnHtlcSweepPublished is the event that is triggered when the htlc
	// sweep is published.
	OnHtlcSweepPublished = fsm.EventType("OnHtlcSweepPublished")

	// OnHtlcSwept is the event that is triggered when the htlc sweep is
	// confirmed.
	OnHtlcSwept = fsm.EventType("OnHtlcSwept")

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

Events.

View Source
var (
	ErrProtocolVersionNotSupported = errors.New(
		"protocol version not supported",
	)
)
View Source
var (
	ErrSwapDoesNotExist = errors.New("swap does not exist")
)

Functions

func CurrentRpcProtocolVersion

func CurrentRpcProtocolVersion() loop_rpc.InstantOutProtocolVersion

CurrentRpcProtocolVersion returns the current rpc protocol version.

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 Config

type Config struct {
	// Store is used to store the instant out.
	Store InstantLoopOutStore

	// LndClient is used to decode the swap invoice.
	LndClient lndclient.LightningClient

	// RouterClient is used to send the offchain payment to the server.
	RouterClient lndclient.RouterClient

	// ChainNotifier is used to be notified of on-chain events.
	ChainNotifier lndclient.ChainNotifierClient

	// Signer is used to sign transactions.
	Signer lndclient.SignerClient

	// Wallet is used to derive keys.
	Wallet lndclient.WalletKitClient

	// InstantOutClient is used to communicate with the swap server.
	InstantOutClient loop_rpc.InstantSwapServerClient

	// ReservationManager is used to get the reservations and lock them.
	ReservationManager ReservationManager

	// Network is the network that is used for the swap.
	Network *chaincfg.Params
}

Config contains the services required for the instant out FSM.

type FSM

type FSM struct {
	*fsm.StateMachine

	// InstantOut contains all the information about the instant out.
	InstantOut *InstantOut
	// contains filtered or unexported fields
}

FSM is the state machine that handles the instant out.

func NewFSM

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

NewFSM creates a new instant out FSM.

func NewFSMFromInstantOut

func NewFSMFromInstantOut(ctx context.Context, cfg *Config,
	instantOut *InstantOut) (*FSM, error)

NewFSMFromInstantOut creates a new instantout FSM from an existing instantout recovered from the database.

func (*FSM) BuildHTLCAction

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

BuildHTLCAction creates the htlc transaction, exchanges nonces with the server and sends the htlc signatures to the server.

func (*FSM) Debugf

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

Debugf logs a debug message with the reservation hash as prefix.

func (*FSM) Errorf

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

Errorf logs an error message with the reservation hash as prefix.

func (*FSM) GetV1ReservationStates

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

GetV1ReservationStates returns the states for the v1 reservation.

func (*FSM) Infof

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

Infof logs an info message with the reservation hash as prefix.

func (*FSM) InitInstantOutAction

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

InitInstantOutAction is the first action that is executed when the instant out FSM is started. It will send the instant out request to the server.

func (*FSM) PollPaymentAcceptedAction

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

PollPaymentAcceptedAction locks the reservations, sends the payment to the server and polls the server for the payment status.

func (*FSM) PublishHtlcAction

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

PublishHtlcAction publishes the htlc transaction and the htlc sweep transaction.

func (*FSM) PublishHtlcSweepAction

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

PublishHtlcSweepAction publishes the htlc sweep transaction.

func (*FSM) PushPreimageAction

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

PushPreimageAction pushes the preimage to the server. It also creates the sweepless sweep transaction and sends the signatures to the server. Finally, it publishes the sweepless sweep transaction. If any of the steps after pushing the preimage fail, the htlc timeout transaction will be published.

func (*FSM) WaitForHtlcSweepConfirmedAction

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

WaitForHtlcSweepConfirmedAction waits for the htlc sweep transaction to be confirmed.

func (*FSM) WaitForSweeplessSweepConfirmedAction

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

WaitForSweeplessSweepConfirmedAction waits for the sweepless sweep transaction to be confirmed.

type InitInstantOutCtx

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

InitInstantOutCtx contains the context for the InitInstantOutAction.

type InputReservation

type InputReservation struct {
	Outpoint wire.OutPoint
	Value    btcutil.Amount
	PkScript []byte
}

InputReservation is a helper struct for the input reservation.

func (InputReservation) Output

func (r InputReservation) Output() *wire.TxOut

Output returns the output for the input reservation.

type InputReservations

type InputReservations []InputReservation

InputReservations is a helper struct for the input reservations.

func (InputReservations) GetPrevoutFetcher

func (i InputReservations) GetPrevoutFetcher() txscript.PrevOutputFetcher

GetPrevoutFetcher returns a prevout fetcher for the input reservations.

type InstantLoopOutStore

type InstantLoopOutStore interface {
	// CreateInstantLoopOut adds a new instant loop out to the store.
	CreateInstantLoopOut(ctx context.Context, instantOut *InstantOut) error

	// UpdateInstantLoopOut updates an existing instant loop out in the
	// store.
	UpdateInstantLoopOut(ctx context.Context, instantOut *InstantOut) error

	// GetInstantLoopOut returns the instant loop out for the given swap
	// hash.
	GetInstantLoopOut(ctx context.Context, swapHash []byte) (*InstantOut, error)

	// ListInstantLoopOuts returns all instant loop outs that are in the
	// store.
	ListInstantLoopOuts(ctx context.Context) ([]*InstantOut, error)
}

InstantLoopOutStore is the interface that needs to be implemented by a store that wants to be used by the instant loop out manager.

type InstantOut

type InstantOut struct {
	// SwapHash is the hash of the swap.
	SwapHash lntypes.Hash

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

	// CltvExpiry is the expiry of the swap.
	CltvExpiry int32

	// Reservations are the Reservations that are used in as inputs for the
	// instant out swap.
	Reservations []*reservation.Reservation

	// Value is the amount that is swapped.
	Value btcutil.Amount

	// SweepTxHash is the hash of the sweep transaction.
	SweepTxHash *chainhash.Hash

	// FinalizedSweeplessSweepTx is the transaction that is used to sweep
	// the funds in the cooperative path.
	FinalizedSweeplessSweepTx *wire.MsgTx
	// contains filtered or unexported fields
}

InstantOut holds the necessary information to execute an instant out swap.

type InstantOutBaseDB

type InstantOutBaseDB interface {
	// InsertSwap inserts a new base swap.
	InsertSwap(ctx context.Context, arg sqlc.InsertSwapParams) error

	// InsertHtlcKeys inserts the htlc keys for a swap.
	InsertHtlcKeys(ctx context.Context, arg sqlc.InsertHtlcKeysParams) error

	// InsertInstantOut inserts a new instant out swap.
	InsertInstantOut(ctx context.Context,
		arg sqlc.InsertInstantOutParams) error

	// InsertInstantOutUpdate inserts a new instant out update.
	InsertInstantOutUpdate(ctx context.Context,
		arg sqlc.InsertInstantOutUpdateParams) error

	// UpdateInstantOut updates an instant out swap.
	UpdateInstantOut(ctx context.Context,
		arg sqlc.UpdateInstantOutParams) error

	// GetInstantOutSwap retrieves an instant out swap.
	GetInstantOutSwap(ctx context.Context,
		swapHash []byte) (sqlc.GetInstantOutSwapRow, error)

	// GetInstantOutSwapUpdates retrieves all instant out swap updates.
	GetInstantOutSwapUpdates(ctx context.Context,
		swapHash []byte) ([]sqlc.InstantoutUpdate, error)

	// GetInstantOutSwaps retrieves all instant out swaps.
	GetInstantOutSwaps(ctx context.Context) ([]sqlc.GetInstantOutSwapsRow,
		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
}

InstantOutBaseDB is the interface that contains all the queries generated by sqlc for the instantout table.

type Manager

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

Manager manages the instantout state machines.

func NewInstantOutManager

func NewInstantOutManager(cfg *Config) *Manager

NewInstantOutManager creates a new instantout manager.

func (*Manager) GetActiveInstantOut

func (m *Manager) GetActiveInstantOut(swapHash lntypes.Hash) (*FSM, error)

GetActiveInstantOut returns an active instant out.

func (*Manager) GetInstantOutQuote

func (m *Manager) GetInstantOutQuote(ctx context.Context,
	amt btcutil.Amount, numReservations int) (Quote, error)

GetInstantOutQuote returns a quote for an instant out.

func (*Manager) ListInstantOuts

func (m *Manager) ListInstantOuts(ctx context.Context) ([]*InstantOut, error)

ListInstantOuts returns all instant outs from the database.

func (*Manager) NewInstantOut

func (m *Manager) NewInstantOut(ctx context.Context,
	reservations []reservation.ID, sweepAddress string) (*FSM, error)

NewInstantOut creates a new instantout.

func (*Manager) Run

func (m *Manager) Run(ctx context.Context, initChan chan struct{},
	height int32) error

Run runs the instantout manager.

type ProtocolVersion

type ProtocolVersion uint32
const (
	// ProtocolVersionUndefined is the undefined protocol version.
	ProtocolVersionUndefined ProtocolVersion = 0

	// ProtocolVersionFullReservation is the protocol version that uses
	// the full reservation amount without change.
	ProtocolVersionFullReservation ProtocolVersion = 1
)

func CurrentProtocolVersion

func CurrentProtocolVersion() ProtocolVersion

CurrentProtocolVersion returns the current protocol version.

type Quote

type Quote struct {
	// ServiceFee is the fee in sat that is paid to the loop service.
	ServiceFee btcutil.Amount

	// OnChainFee is the estimated on chain fee in sat.
	OnChainFee btcutil.Amount
}

type ReservationManager

type ReservationManager interface {
	// GetReservation returns the reservation for the given id.
	GetReservation(ctx context.Context, id reservation.ID) (
		*reservation.Reservation, error)

	// LockReservation locks the reservation for the given id.
	LockReservation(ctx context.Context, id reservation.ID) error

	// UnlockReservation unlocks the reservation for the given id.
	UnlockReservation(ctx context.Context, id reservation.ID) error
}

ReservationManager handles fetching and locking of reservations.

type ReservationStore

type ReservationStore interface {
	// GetReservation returns the reservation for the given id.
	GetReservation(ctx context.Context, id reservation.ID) (
		*reservation.Reservation, error)
}

ReservationStore is the interface that is required to load the reservations based on the stored reservation ids.

type SQLStore

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

func NewSQLStore

func NewSQLStore(db InstantOutBaseDB, clock clock.Clock,
	reservationStore ReservationStore, network *chaincfg.Params) *SQLStore

NewSQLStore creates a new SQLStore.

func (*SQLStore) CreateInstantLoopOut

func (s *SQLStore) CreateInstantLoopOut(ctx context.Context,
	instantOut *InstantOut) error

CreateInstantLoopOut adds a new instant loop out to the store.

func (*SQLStore) GetInstantLoopOut

func (s *SQLStore) GetInstantLoopOut(ctx context.Context, swapHash []byte) (
	*InstantOut, error)

GetInstantLoopOut returns the instant loop out for the given swap hash.

func (*SQLStore) ListInstantLoopOuts

func (s *SQLStore) ListInstantLoopOuts(ctx context.Context) ([]*InstantOut,
	error)

ListInstantLoopOuts returns all instant loop outs that are in the store.

func (*SQLStore) UpdateInstantLoopOut

func (s *SQLStore) UpdateInstantLoopOut(ctx context.Context,
	instantOut *InstantOut) error

UpdateInstantLoopOut updates an existing instant loop out in the store.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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