Documentation
¶
Index ¶
- Constants
- Variables
- func CurrentRpcProtocolVersion() swapserverrpc.InstantOutProtocolVersion
- func UseLogger(logger btclog.Logger)
- type Config
- type FSM
- func (f *FSM) BuildHTLCAction(eventCtx fsm.EventContext) fsm.EventType
- func (f *FSM) Debugf(format string, args ...interface{})
- func (f *FSM) Errorf(format string, args ...interface{})
- func (f *FSM) GetV1ReservationStates() fsm.States
- func (f *FSM) Infof(format string, args ...interface{})
- func (f *FSM) InitInstantOutAction(eventCtx fsm.EventContext) fsm.EventType
- func (f *FSM) PollPaymentAcceptedAction(_ fsm.EventContext) fsm.EventType
- func (f *FSM) PublishHtlcAction(eventCtx fsm.EventContext) fsm.EventType
- func (f *FSM) PublishHtlcSweepAction(eventCtx fsm.EventContext) fsm.EventType
- func (f *FSM) PushPreimageAction(eventCtx fsm.EventContext) fsm.EventType
- func (f *FSM) WaitForHtlcSweepConfirmedAction(eventCtx fsm.EventContext) fsm.EventType
- func (f *FSM) WaitForSweeplessSweepConfirmedAction(eventCtx fsm.EventContext) fsm.EventType
- type InitInstantOutCtx
- type InputReservation
- type InputReservations
- type InstantLoopOutStore
- type InstantOut
- type InstantOutBaseDB
- type Manager
- func (m *Manager) GetActiveInstantOut(swapHash lntypes.Hash) (*FSM, error)
- func (m *Manager) GetInstantOutQuote(ctx context.Context, amt btcutil.Amount, numReservations int) (Quote, error)
- func (m *Manager) ListInstantOuts(ctx context.Context) ([]*InstantOut, error)
- func (m *Manager) NewInstantOut(ctx context.Context, reservations []reservation.ID, sweepAddress string) (*FSM, error)
- func (m *Manager) Run(ctx context.Context, initChan chan struct{}, height int32) error
- type ProtocolVersion
- type Querier
- type Quote
- type ReservationManager
- type ReservationStore
- type SQLStore
- func (s *SQLStore) CreateInstantLoopOut(ctx context.Context, instantOut *InstantOut) error
- func (s *SQLStore) GetInstantLoopOut(ctx context.Context, swapHash []byte) (*InstantOut, error)
- func (s *SQLStore) ListInstantLoopOuts(ctx context.Context) ([]*InstantOut, error)
- func (s *SQLStore) UpdateInstantLoopOut(ctx context.Context, instantOut *InstantOut) error
Constants ¶
const (
KeyFamily = int32(42069)
)
const Subsystem = "INST"
Subsystem defines the sub system name of this package.
Variables ¶
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.
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.
var (
ErrProtocolVersionNotSupported = errors.New(
"protocol version not supported",
)
)
var (
ErrSwapDoesNotExist = errors.New("swap does not exist")
)
Functions ¶
func CurrentRpcProtocolVersion ¶
func CurrentRpcProtocolVersion() swapserverrpc.InstantOutProtocolVersion
CurrentRpcProtocolVersion returns the current rpc protocol version.
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 swapserverrpc.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 NewFSMFromInstantOut ¶
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) GetV1ReservationStates ¶
GetV1ReservationStates returns the states for the v1 reservation.
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 ¶
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 { Querier // ExecTx allows for executing a function in the context of a database // transaction. ExecTx(ctx context.Context, txOptions loopdb.TxOptions, txBody func(Querier) error) error }
InstantOutBaseDB is the interface that contains all the queries generated by sqlc for the instantout table and transaction functionality.
type Manager ¶
Manager manages the instantout state machines.
func NewInstantOutManager ¶
NewInstantOutManager creates a new instantout manager.
func (*Manager) GetActiveInstantOut ¶
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.
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 Querier ¶
type Querier 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) }
Querier is the interface that contains all the queries generated by sqlc for the instantout table.
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 ¶
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.