2017-05-01 19:58:08 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
2018-10-30 12:36:27 +03:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2017-05-03 17:02:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-02-20 14:11:15 +03:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
2018-06-08 06:10:26 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnpeer"
|
2019-01-15 13:31:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2017-05-01 19:58:08 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2017-05-03 17:02:22 +03:00
|
|
|
// InvoiceDatabase is an interface which represents the persistent subsystem
|
|
|
|
// which may search, lookup and settle invoices.
|
|
|
|
type InvoiceDatabase interface {
|
2018-02-07 06:13:07 +03:00
|
|
|
// LookupInvoice attempts to look up an invoice according to its 32
|
2018-06-30 02:02:59 +03:00
|
|
|
// byte payment hash. This method should also reutrn the min final CLTV
|
|
|
|
// delta for this invoice. We'll use this to ensure that the HTLC
|
|
|
|
// extended to us gives us enough time to settle as we prescribe.
|
2019-01-15 13:31:22 +03:00
|
|
|
LookupInvoice(lntypes.Hash) (channeldb.Invoice, uint32, error)
|
2017-05-03 17:02:22 +03:00
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
// NotifyExitHopHtlc attempts to mark an invoice as settled. If the
|
|
|
|
// invoice is a debug invoice, then this method is a noop as debug
|
|
|
|
// invoices are never fully settled. The return value describes how the
|
2019-02-11 14:01:05 +03:00
|
|
|
// htlc should be resolved. If the htlc cannot be resolved immediately,
|
|
|
|
// the resolution is sent on the passed in hodlChan later.
|
|
|
|
NotifyExitHopHtlc(payHash lntypes.Hash, paidAmount lnwire.MilliSatoshi,
|
2019-04-16 13:11:20 +03:00
|
|
|
expiry uint32, currentHeight int32,
|
2019-02-11 14:01:05 +03:00
|
|
|
hodlChan chan<- interface{}) (*invoices.HodlEvent, error)
|
2019-01-11 13:19:16 +03:00
|
|
|
|
|
|
|
// CancelInvoice attempts to cancel the invoice corresponding to the
|
|
|
|
// passed payment hash.
|
|
|
|
CancelInvoice(payHash lntypes.Hash) error
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
// SettleHodlInvoice settles a hold invoice.
|
|
|
|
SettleHodlInvoice(preimage lntypes.Preimage) error
|
|
|
|
|
|
|
|
// HodlUnsubscribeAll unsubscribes from all hodl events.
|
|
|
|
HodlUnsubscribeAll(subscriber chan<- interface{})
|
2017-05-03 17:02:22 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:32:41 +03:00
|
|
|
// ChannelLink is an interface which represents the subsystem for managing the
|
|
|
|
// incoming htlc requests, applying the changes to the channel, and also
|
2017-05-01 19:58:08 +03:00
|
|
|
// propagating/forwarding it to htlc switch.
|
|
|
|
//
|
|
|
|
// abstraction level
|
|
|
|
// ^
|
|
|
|
// |
|
|
|
|
// | - - - - - - - - - - - - Lightning - - - - - - - - - - - - -
|
|
|
|
// |
|
|
|
|
// | (Switch) (Switch) (Switch)
|
|
|
|
// | Alice <-- channel link --> Bob <-- channel link --> Carol
|
2018-04-05 19:52:35 +03:00
|
|
|
// |
|
2017-05-01 19:58:08 +03:00
|
|
|
// | - - - - - - - - - - - - - TCP - - - - - - - - - - - - - - -
|
|
|
|
// |
|
|
|
|
// | (Peer) (Peer) (Peer)
|
|
|
|
// | Alice <----- tcp conn --> Bob <---- tcp conn -----> Carol
|
|
|
|
// |
|
|
|
|
//
|
|
|
|
type ChannelLink interface {
|
2017-11-11 01:48:23 +03:00
|
|
|
// TODO(roasbeef): modify interface to embed mail boxes?
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// HandleSwitchPacket handles the switch packets. This packets might be
|
|
|
|
// forwarded to us from another channel link in case the htlc update
|
|
|
|
// came from another peer or if the update was created by user
|
|
|
|
// initially.
|
2017-11-11 01:48:23 +03:00
|
|
|
//
|
|
|
|
// NOTE: This function MUST be non-blocking (or block as little as
|
|
|
|
// possible).
|
2018-01-16 23:55:41 +03:00
|
|
|
HandleSwitchPacket(*htlcPacket) error
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
// HandleChannelUpdate handles the htlc requests as settle/add/fail
|
|
|
|
// which sent to us from remote peer we have a channel with.
|
2017-11-11 01:48:23 +03:00
|
|
|
//
|
|
|
|
// NOTE: This function MUST be non-blocking (or block as little as
|
|
|
|
// possible).
|
2017-05-01 19:58:08 +03:00
|
|
|
HandleChannelUpdate(lnwire.Message)
|
|
|
|
|
2018-10-30 12:36:27 +03:00
|
|
|
// ChannelPoint returns the channel outpoint for the channel link.
|
|
|
|
ChannelPoint() *wire.OutPoint
|
|
|
|
|
2017-06-17 00:32:41 +03:00
|
|
|
// ChanID returns the channel ID for the channel link. The channel ID
|
|
|
|
// is a more compact representation of a channel's full outpoint.
|
2017-05-01 19:58:08 +03:00
|
|
|
ChanID() lnwire.ChannelID
|
|
|
|
|
2017-06-17 00:32:41 +03:00
|
|
|
// ShortChanID returns the short channel ID for the channel link. The
|
|
|
|
// short channel ID encodes the exact location in the main chain that
|
|
|
|
// the original funding output can be found.
|
|
|
|
ShortChanID() lnwire.ShortChannelID
|
2017-06-17 01:01:00 +03:00
|
|
|
|
2018-02-04 05:14:09 +03:00
|
|
|
// UpdateShortChanID updates the short channel ID for a link. This may
|
|
|
|
// be required in the event that a link is created before the short
|
|
|
|
// chan ID for it is known, or a re-org occurs, and the funding
|
2018-02-07 11:30:09 +03:00
|
|
|
// transaction changes location within the chain.
|
2018-05-02 02:29:24 +03:00
|
|
|
UpdateShortChanID() (lnwire.ShortChannelID, error)
|
2018-02-04 05:14:09 +03:00
|
|
|
|
2017-06-17 01:01:00 +03:00
|
|
|
// UpdateForwardingPolicy updates the forwarding policy for the target
|
|
|
|
// ChannelLink. Once updated, the link will use the new forwarding
|
|
|
|
// policy to govern if it an incoming HTLC should be forwarded or not.
|
|
|
|
UpdateForwardingPolicy(ForwardingPolicy)
|
|
|
|
|
htlcswitch: perform fee related checks at forwarding time
In this commit, we fix a very old, lingering bug within the link. When
accepting an HTLC we are meant to validate the fee against the
constraints of the *outgoing* link. This is due to the fact that we're
offering a payment transit service on our outgoing link. Before this
commit, we would use the policies of the *incoming* link. This would at
times lead to odd routing errors as we would go to route, get an error
update and then route again, repeating the process.
With this commit, we'll properly use the incoming link for timelock
related constraints, and the outgoing link for fee related constraints.
We do this by introducing a new HtlcSatisfiesPolicy method in the link.
This method should return a non-nil error if the link can carry the HTLC
as it satisfies its current forwarding policy. We'll use this method now
at *forwarding* time to ensure that we only forward to links that
actually accept the policy. This fixes a number of bugs that existed
before that could result in a link accepting an HTLC that actually
violated its policy. In the case that the policy is violated for *all*
links, we take care to return the error returned by the *target* link so
the caller can update their sending accordingly.
In this commit, we also remove the prior linkControl channel in the
channelLink. Instead, of sending a message to update the internal link
policy, we'll use a mutex in place. This simplifies the code, and also
adds some necessary refactoring in anticipation of the next follow up
commit.
2018-04-04 05:51:40 +03:00
|
|
|
// HtlcSatifiesPolicy should return a nil error if the passed HTLC
|
|
|
|
// details satisfy the current forwarding policy fo the target link.
|
|
|
|
// Otherwise, a valid protocol failure message should be returned in
|
|
|
|
// order to signal to the source of the HTLC, the policy consistency
|
|
|
|
// issue.
|
2018-06-26 06:15:46 +03:00
|
|
|
HtlcSatifiesPolicy(payHash [32]byte, incomingAmt lnwire.MilliSatoshi,
|
|
|
|
amtToForward lnwire.MilliSatoshi,
|
|
|
|
incomingTimeout, outgoingTimeout uint32,
|
|
|
|
heightNow uint32) lnwire.FailureMessage
|
htlcswitch: perform fee related checks at forwarding time
In this commit, we fix a very old, lingering bug within the link. When
accepting an HTLC we are meant to validate the fee against the
constraints of the *outgoing* link. This is due to the fact that we're
offering a payment transit service on our outgoing link. Before this
commit, we would use the policies of the *incoming* link. This would at
times lead to odd routing errors as we would go to route, get an error
update and then route again, repeating the process.
With this commit, we'll properly use the incoming link for timelock
related constraints, and the outgoing link for fee related constraints.
We do this by introducing a new HtlcSatisfiesPolicy method in the link.
This method should return a non-nil error if the link can carry the HTLC
as it satisfies its current forwarding policy. We'll use this method now
at *forwarding* time to ensure that we only forward to links that
actually accept the policy. This fixes a number of bugs that existed
before that could result in a link accepting an HTLC that actually
violated its policy. In the case that the policy is violated for *all*
links, we take care to return the error returned by the *target* link so
the caller can update their sending accordingly.
In this commit, we also remove the prior linkControl channel in the
channelLink. Instead, of sending a message to update the internal link
policy, we'll use a mutex in place. This simplifies the code, and also
adds some necessary refactoring in anticipation of the next follow up
commit.
2018-04-04 05:51:40 +03:00
|
|
|
|
2019-04-19 12:11:16 +03:00
|
|
|
// HtlcSatifiesPolicyLocal should return a nil error if the passed HTLC
|
|
|
|
// details satisfy the current channel policy. Otherwise, a valid
|
|
|
|
// protocol failure message should be returned in order to signal the
|
|
|
|
// violation. This call is intended to be used for locally initiated
|
|
|
|
// payments for which there is no corresponding incoming htlc.
|
|
|
|
HtlcSatifiesPolicyLocal(payHash [32]byte, amt lnwire.MilliSatoshi,
|
|
|
|
timeout uint32, heightNow uint32) lnwire.FailureMessage
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
// Bandwidth returns the amount of milli-satoshis which current link
|
|
|
|
// might pass through channel link. The value returned from this method
|
2017-06-17 01:01:00 +03:00
|
|
|
// represents the up to date available flow through the channel. This
|
|
|
|
// takes into account any forwarded but un-cleared HTLC's, and any
|
|
|
|
// HTLC's which have been set to the over flow queue.
|
2017-08-22 09:36:43 +03:00
|
|
|
Bandwidth() lnwire.MilliSatoshi
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
// Stats return the statistics of channel link. Number of updates,
|
2017-08-22 09:36:43 +03:00
|
|
|
// total sent/received milli-satoshis.
|
|
|
|
Stats() (uint64, lnwire.MilliSatoshi, lnwire.MilliSatoshi)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-06-17 00:32:41 +03:00
|
|
|
// Peer returns the representation of remote peer with which we have
|
|
|
|
// the channel link opened.
|
2018-06-08 06:10:26 +03:00
|
|
|
Peer() lnpeer.Peer
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-12-06 04:48:28 +03:00
|
|
|
// EligibleToForward returns a bool indicating if the channel is able
|
|
|
|
// to actively accept requests to forward HTLC's. A channel may be
|
|
|
|
// active, but not able to forward HTLC's if it hasn't yet finalized
|
|
|
|
// the pre-channel operation protocol with the remote peer. The switch
|
|
|
|
// will use this function in forwarding decisions accordingly.
|
|
|
|
EligibleToForward() bool
|
|
|
|
|
2018-01-16 23:55:41 +03:00
|
|
|
// AttachMailBox delivers an active MailBox to the link. The MailBox may
|
|
|
|
// have buffered messages.
|
|
|
|
AttachMailBox(MailBox)
|
|
|
|
|
2017-06-17 00:32:41 +03:00
|
|
|
// Start/Stop are used to initiate the start/stop of the channel link
|
|
|
|
// functioning.
|
2017-05-01 19:58:08 +03:00
|
|
|
Start() error
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
// ForwardingLog is an interface that represents a time series database which
|
|
|
|
// keep track of all successfully completed payment circuits. Every few
|
|
|
|
// seconds, the switch will collate and flush out all the successful payment
|
|
|
|
// circuits during the last interval.
|
|
|
|
type ForwardingLog interface {
|
|
|
|
// AddForwardingEvents is a method that should write out the set of
|
|
|
|
// forwarding events in a batch to persistent storage. Outside
|
|
|
|
// sub-systems can then query the contents of the log for analysis,
|
|
|
|
// visualizations, etc.
|
|
|
|
AddForwardingEvents([]channeldb.ForwardingEvent) error
|
|
|
|
}
|