loopdb

package
v0.11.2-beta Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2020 License: MIT Imports: 22 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// StateTypePending indicates that the swap is still pending.
	StateTypePending SwapStateType = 0

	// StateTypeSuccess indicates that the swap has completed successfully.
	StateTypeSuccess = 1

	// StateTypeFail indicates that the swap has failed.
	StateTypeFail = 2
)
View Source
const DefaultLoopOutHtlcConfirmations uint32 = 1

DefaultLoopOutHtlcConfirmations is the default number of confirmations we set for a loop out htlc.

Variables

View Source
var (

	// ErrDBReversion is returned when detecting an attempt to revert to a
	// prior database version.
	ErrDBReversion = fmt.Errorf("channel db cannot revert to prior version")
)

Functions

func MarshalProtocolVersion

func MarshalProtocolVersion(version ProtocolVersion) []byte

MarshalProtocolVersion marshals a ProtocolVersion value to a byte slice.

func NewBoltSwapStore

func NewBoltSwapStore(dbPath string, chainParams *chaincfg.Params) (
	*boltSwapStore, error)

NewBoltSwapStore creates a new client swap store.

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 ChannelSet

type ChannelSet []uint64

ChannelSet stores a set of channels.

func NewChannelSet

func NewChannelSet(set []uint64) (ChannelSet, error)

NewChannelSet instantiates a new channel set and verifies that there are no duplicates present.

func (ChannelSet) String

func (c ChannelSet) String() string

String returns the human-readable representation of a channel set.

type Loop

type Loop struct {
	Hash   lntypes.Hash
	Events []*LoopEvent
}

Loop contains fields shared between LoopIn and LoopOut

func (*Loop) LastUpdate

func (s *Loop) LastUpdate() *LoopEvent

LastUpdate returns the most recent update of this swap.

func (*Loop) State

func (s *Loop) State() SwapStateData

State returns the most recent state of this swap.

type LoopEvent

type LoopEvent struct {
	SwapStateData

	// Time is the time that this swap had its state changed.
	Time time.Time
}

LoopEvent contains the dynamic data of a swap.

type LoopIn

type LoopIn struct {
	Loop

	Contract *LoopInContract
}

LoopIn is a combination of the contract and the updates.

func (*LoopIn) LastUpdateTime

func (s *LoopIn) LastUpdateTime() time.Time

LastUpdateTime returns the last update time of this swap.

type LoopInContract

type LoopInContract struct {
	SwapContract

	// SweepConfTarget specifies the targeted confirmation target for the
	// client sweep tx.
	HtlcConfTarget int32

	// LastHop is the last hop to use for the loop in swap (optional).
	LastHop *route.Vertex

	// ExternalHtlc specifies whether the htlc is published by an external
	// source.
	ExternalHtlc bool

	// Label contains an optional label for the swap. Note that this field
	// is stored separately to the rest of the contract on disk.
	Label string
}

LoopInContract contains the data that is serialized to persistent storage for pending loop in swaps.

type LoopOut

type LoopOut struct {
	Loop

	// Contract is the active contract for this swap. It describes the
	// precise details of the swap including the final fee, CLTV value,
	// etc.
	Contract *LoopOutContract
}

LoopOut is a combination of the contract and the updates.

func (*LoopOut) LastUpdateTime

func (s *LoopOut) LastUpdateTime() time.Time

LastUpdateTime returns the last update time of this swap.

type LoopOutContract

type LoopOutContract struct {
	// SwapContract contains basic information pertaining to this swap.
	// Each swap type has a base contract, then swap specific information
	// on top of it.
	SwapContract

	// DestAddr is the destination address of the loop out swap.
	DestAddr btcutil.Address

	// SwapInvoice is the invoice that is to be paid by the client to
	// initiate the loop out swap.
	SwapInvoice string

	// MaxSwapRoutingFee is the maximum off-chain fee in msat that may be
	// paid for the swap payment to the server.
	MaxSwapRoutingFee btcutil.Amount

	// SweepConfTarget specifies the targeted confirmation target for the
	// client sweep tx.
	SweepConfTarget int32

	// HtlcConfirmations is the number of confirmations we require the on
	// chain htlc to have before proceeding with the swap.
	HtlcConfirmations uint32

	// OutgoingChanSet is the set of short ids of channels that may be used.
	// If empty, any channel may be used.
	OutgoingChanSet ChannelSet

	// PrepayInvoice is the invoice that the client should pay to the
	// server that will be returned if the swap is complete.
	PrepayInvoice string

	// MaxPrepayRoutingFee is the maximum off-chain fee in msat that may be
	// paid for the prepayment to the server.
	MaxPrepayRoutingFee btcutil.Amount

	// SwapPublicationDeadline is a timestamp that the server commits to
	// have the on-chain swap published by. It is set by the client to
	// allow the server to delay the publication in exchange for possibly
	// lower fees.
	SwapPublicationDeadline time.Time
}

LoopOutContract contains the data that is serialized to persistent storage for pending swaps.

type ProtocolVersion

type ProtocolVersion uint32

ProtocolVersion represents the protocol version (declared on rpc level) that the client declared to us.

const (
	// ProtocolVersionLegacy indicates that the client is a legacy version
	// that did not report its protocol version.
	ProtocolVersionLegacy ProtocolVersion = 0

	// ProtocolVersionMultiLoopOut indicates that the client supports multi
	// loop out.
	ProtocolVersionMultiLoopOut ProtocolVersion = 1

	// ProtocolVersionSegwitLoopIn indicates that the client supports segwit
	// loop in.
	ProtocolVersionSegwitLoopIn ProtocolVersion = 2

	// ProtocolVersionPreimagePush indicates that the client will push loop
	// out preimages to the sever to speed up claim.
	ProtocolVersionPreimagePush ProtocolVersion = 3

	// ProtocolVersionUserExpiryLoopOut indicates that the client will
	// propose a cltv expiry height for loop out.
	ProtocolVersionUserExpiryLoopOut ProtocolVersion = 4

	// ProtocolVersionHtlcV2 indicates that the client will use the new
	// HTLC v2 scrips for swaps.
	ProtocolVersionHtlcV2 ProtocolVersion = 5

	// ProtocolVersionMultiLoopIn indicates that the client creates a probe
	// invoice so that the server can perform a multi-path probe.
	ProtocolVersionMultiLoopIn ProtocolVersion = 6

	// ProtocolVersionUnrecorded is set for swaps were created before we
	// started saving protocol version with swaps.
	ProtocolVersionUnrecorded ProtocolVersion = math.MaxUint32

	// CurrentRPCProtocolVersion defines the version of the RPC protocol
	// that is currently supported by the loop client.
	CurrentRPCProtocolVersion = looprpc.ProtocolVersion_MULTI_LOOP_IN

	// CurrentInternalProtocolVersion defines the RPC current protocol in
	// the internal representation.
	CurrentInternalProtocolVersion = ProtocolVersion(CurrentRPCProtocolVersion)
)

func UnmarshalProtocolVersion

func UnmarshalProtocolVersion(b []byte) (ProtocolVersion, error)

UnmarshalProtocolVersion attempts to unmarshal a byte slice to a ProtocolVersion value. If the unmarshal fails, ProtocolVersionUnrecorded is returned along with an error.

func (ProtocolVersion) String

func (p ProtocolVersion) String() string

String returns the string representation of a protocol version.

func (ProtocolVersion) Valid

func (p ProtocolVersion) Valid() bool

Valid returns true if the value of the ProtocolVersion is valid.

type SwapContract

type SwapContract struct {
	// Preimage is the preimage for the swap.
	Preimage lntypes.Preimage

	// AmountRequested is the total amount of the swap.
	AmountRequested btcutil.Amount

	// SenderKey is the key of the sender that will be used in the on-chain
	// HTLC.
	SenderKey [33]byte

	// ReceiverKey is the of the receiver that will be used in the on-chain
	// HTLC.
	ReceiverKey [33]byte

	// CltvExpiry is the total absolute CLTV expiry of the swap.
	CltvExpiry int32

	// MaxSwapFee is the maximum we are willing to pay the server for the
	// swap.
	MaxSwapFee btcutil.Amount

	// MaxMinerFee is the maximum in on-chain fees that we are willing to
	// spend.
	MaxMinerFee btcutil.Amount

	// InitiationHeight is the block height at which the swap was
	// initiated.
	InitiationHeight int32

	// InitiationTime is the time at which the swap was initiated.
	InitiationTime time.Time

	// Label contains an optional label for the swap.
	Label string

	// ProtocolVersion stores the protocol version when the swap was
	// created.
	ProtocolVersion ProtocolVersion
}

SwapContract contains the base data that is serialized to persistent storage for pending swaps.

type SwapCost

type SwapCost struct {
	// Swap is the amount paid to the server.
	Server btcutil.Amount

	// Onchain is the amount paid to miners for the onchain tx.
	Onchain btcutil.Amount

	// Offchain is the amount paid in routing fees.
	Offchain btcutil.Amount
}

SwapCost is a breakdown of the final swap costs.

func (SwapCost) Total

func (s SwapCost) Total() btcutil.Amount

Total returns the total costs represented by swap costs.

type SwapState

type SwapState uint8

SwapState indicates the current state of a swap. This enumeration is the union of loop in and loop out states. A single type is used for both swap types to be able to reduce code duplication that would otherwise be required.

const (
	// StateInitiated is the initial state of a swap. At that point, the
	// initiation call to the server has been made and the payment process
	// has been started for the swap and prepayment invoices.
	StateInitiated SwapState = 0

	// StatePreimageRevealed is reached when the sweep tx publication is
	// first attempted. From that point on, we should consider the preimage
	// to no longer be secret and we need to do all we can to get the sweep
	// confirmed. This state will mostly coalesce with StateHtlcConfirmed,
	// except in the case where we wait for fees to come down before we
	// sweep.
	StatePreimageRevealed SwapState = 1

	// StateSuccess is the final swap state that is reached when the sweep
	// tx has the required confirmation depth (SweepConfDepth) and the
	// server pulled the off-chain htlc.
	StateSuccess SwapState = 2

	// StateFailOffchainPayments indicates that it wasn't possible to find
	// routes for one or both of the off-chain payments to the server that
	// satisfied the payment restrictions (fee and timelock limits).
	StateFailOffchainPayments SwapState = 3

	// StateFailTimeout indicates that the on-chain htlc wasn't confirmed
	// before its expiry or confirmed too late (MinPreimageRevealDelta
	// violated).
	StateFailTimeout SwapState = 4

	// StateFailSweepTimeout indicates that the on-chain htlc wasn't swept
	// before the server revoked the htlc. The server didn't pull the
	// off-chain htlc (even though it could have) and we timed out the
	// off-chain htlc ourselves. No funds lost.
	StateFailSweepTimeout SwapState = 5

	// StateFailInsufficientValue indicates that the published on-chain htlc
	// had a value lower than the requested amount.
	StateFailInsufficientValue SwapState = 6

	// StateFailTemporary indicates that the swap cannot progress because
	// of an internal error. This is not a final state. Manual intervention
	// (like a restart) is required to solve this problem.
	StateFailTemporary SwapState = 7

	// StateHtlcPublished means that the client published the on-chain htlc.
	StateHtlcPublished SwapState = 8

	// StateInvoiceSettled means that the swap invoice has been paid by the
	// server.
	StateInvoiceSettled SwapState = 9

	// StateFailIncorrectHtlcAmt indicates that the amount of an externally
	// published loop in htlc didn't match the swap amount.
	StateFailIncorrectHtlcAmt SwapState = 10
)

func (SwapState) String

func (s SwapState) String() string

String returns a string representation of the swap's state.

func (SwapState) Type

func (s SwapState) Type() SwapStateType

Type returns the type of the SwapState it is called on.

type SwapStateData

type SwapStateData struct {
	// SwapState is the state the swap is in.
	State SwapState

	// Cost are the accrued (final) costs so far.
	Cost SwapCost

	// HtlcTxHash is the tx id of the confirmed htlc.
	HtlcTxHash *chainhash.Hash
}

SwapStateData is all persistent data to describe the current swap state.

type SwapStateType

type SwapStateType uint8

SwapStateType defines the types of swap states that exist. Every swap state defined as type SwapState above, falls into one of these SwapStateType categories.

type SwapStore

type SwapStore interface {
	// FetchLoopOutSwaps returns all swaps currently in the store.
	FetchLoopOutSwaps() ([]*LoopOut, error)

	// CreateLoopOut adds an initiated swap to the store.
	CreateLoopOut(hash lntypes.Hash, swap *LoopOutContract) error

	// UpdateLoopOut stores a new event for a target loop out swap. This
	// appends to the event log for a particular swap as it goes through
	// the various stages in its lifetime.
	UpdateLoopOut(hash lntypes.Hash, time time.Time,
		state SwapStateData) error

	// FetchLoopInSwaps returns all swaps currently in the store.
	FetchLoopInSwaps() ([]*LoopIn, error)

	// CreateLoopIn adds an initiated swap to the store.
	CreateLoopIn(hash lntypes.Hash, swap *LoopInContract) error

	// UpdateLoopIn stores a new event for a target loop in swap. This
	// appends to the event log for a particular swap as it goes through
	// the various stages in its lifetime.
	UpdateLoopIn(hash lntypes.Hash, time time.Time,
		state SwapStateData) error

	// Close closes the underlying database.
	Close() error
}

SwapStore is the primary database interface used by the loopd system. It houses information for all pending completed/failed swaps.

Jump to

Keyboard shortcuts

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