2017-05-01 19:58:08 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
2017-12-05 04:28:16 +03:00
|
|
|
"bytes"
|
2018-06-01 06:31:40 +03:00
|
|
|
"crypto/sha256"
|
2018-07-27 13:27:11 +03:00
|
|
|
"errors"
|
2017-08-22 09:36:43 +03:00
|
|
|
"fmt"
|
2017-05-01 19:58:08 +03:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2018-08-10 23:59:50 +03:00
|
|
|
"github.com/coreos/bbolt"
|
2018-08-12 16:19:54 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2018-06-01 06:31:40 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2018-02-28 09:18:52 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-01-17 07:11:01 +03:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-05-01 19:58:08 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2018-08-01 03:01:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/ticker"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultFwdEventInterval is the duration between attempts to flush
|
|
|
|
// pending forwarding events to disk.
|
|
|
|
DefaultFwdEventInterval = 15 * time.Second
|
|
|
|
|
|
|
|
// DefaultLogInterval is the duration between attempts to log statistics
|
|
|
|
// about forwarding events.
|
|
|
|
DefaultLogInterval = 10 * time.Second
|
2017-05-01 19:58:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrChannelLinkNotFound is used when channel link hasn't been found.
|
|
|
|
ErrChannelLinkNotFound = errors.New("channel link not found")
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// ErrDuplicateAdd signals that the ADD htlc was already forwarded
|
|
|
|
// through the switch and is locked into another commitment txn.
|
|
|
|
ErrDuplicateAdd = errors.New("duplicate add HTLC detected")
|
|
|
|
|
|
|
|
// ErrIncompleteForward is used when an htlc was already forwarded
|
|
|
|
// through the switch, but did not get locked into another commitment
|
|
|
|
// txn.
|
2018-07-27 13:27:11 +03:00
|
|
|
ErrIncompleteForward = errors.New("incomplete forward detected")
|
2017-12-06 08:23:23 +03:00
|
|
|
|
2018-08-21 14:12:08 +03:00
|
|
|
// ErrUnknownErrorDecryptor signals that we were unable to locate the
|
|
|
|
// error decryptor for this payment. This is likely due to restarting
|
|
|
|
// the daemon.
|
|
|
|
ErrUnknownErrorDecryptor = errors.New("unknown error decryptor")
|
|
|
|
|
2018-07-27 13:29:16 +03:00
|
|
|
// ErrSwitchExiting signaled when the switch has received a shutdown
|
|
|
|
// request.
|
|
|
|
ErrSwitchExiting = errors.New("htlcswitch shutting down")
|
|
|
|
|
2018-08-29 06:10:02 +03:00
|
|
|
// ErrNoLinksFound is an error returned when we attempt to retrieve the
|
|
|
|
// active links in the switch for a specific destination.
|
|
|
|
ErrNoLinksFound = errors.New("no channel links found")
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// zeroPreimage is the empty preimage which is returned when we have
|
|
|
|
// some errors.
|
|
|
|
zeroPreimage [sha256.Size]byte
|
|
|
|
)
|
|
|
|
|
|
|
|
// pendingPayment represents the payment which made by user and waits for
|
|
|
|
// updates to be received whether the payment has been rejected or proceed
|
|
|
|
// successfully.
|
|
|
|
type pendingPayment struct {
|
|
|
|
paymentHash lnwallet.PaymentHash
|
2017-08-22 09:36:43 +03:00
|
|
|
amount lnwire.MilliSatoshi
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
preimage chan [sha256.Size]byte
|
|
|
|
err chan error
|
2017-06-29 16:40:45 +03:00
|
|
|
|
2018-04-18 05:02:04 +03:00
|
|
|
// deobfuscator is a serializable entity which is used if we received
|
2017-06-29 16:40:45 +03:00
|
|
|
// an error, it deobfuscates the onion failure blob, and extracts the
|
|
|
|
// exact error from it.
|
2017-10-11 05:36:52 +03:00
|
|
|
deobfuscator ErrorDecrypter
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// plexPacket encapsulates switch packet and adds error channel to receive
|
|
|
|
// error from request handler.
|
|
|
|
type plexPacket struct {
|
2017-05-01 19:58:08 +03:00
|
|
|
pkt *htlcPacket
|
|
|
|
err chan error
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:02:04 +03:00
|
|
|
// ChannelCloseType is an enum which signals the type of channel closure the
|
2017-05-01 19:58:08 +03:00
|
|
|
// peer should execute.
|
|
|
|
type ChannelCloseType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CloseRegular indicates a regular cooperative channel closure
|
|
|
|
// should be attempted.
|
|
|
|
CloseRegular ChannelCloseType = iota
|
|
|
|
|
2017-11-11 01:48:23 +03:00
|
|
|
// CloseBreach indicates that a channel breach has been detected, and
|
2017-05-01 19:58:08 +03:00
|
|
|
// the link should immediately be marked as unavailable.
|
|
|
|
CloseBreach
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChanClose represents a request which close a particular channel specified by
|
|
|
|
// its id.
|
|
|
|
type ChanClose struct {
|
|
|
|
// CloseType is a variable which signals the type of channel closure the
|
|
|
|
// peer should execute.
|
|
|
|
CloseType ChannelCloseType
|
|
|
|
|
|
|
|
// ChanPoint represent the id of the channel which should be closed.
|
|
|
|
ChanPoint *wire.OutPoint
|
|
|
|
|
2017-11-23 10:28:16 +03:00
|
|
|
// TargetFeePerKw is the ideal fee that was specified by the caller.
|
|
|
|
// This value is only utilized if the closure type is CloseRegular.
|
|
|
|
// This will be the starting offered fee when the fee negotiation
|
|
|
|
// process for the cooperative closure transaction kicks off.
|
2018-02-13 16:54:54 +03:00
|
|
|
TargetFeePerKw lnwallet.SatPerKWeight
|
2017-11-23 10:28:16 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// Updates is used by request creator to receive the notifications about
|
|
|
|
// execution of the close channel request.
|
2018-12-20 20:52:27 +03:00
|
|
|
Updates chan interface{}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
// Err is used by request creator to receive request execution error.
|
|
|
|
Err chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config defines the configuration for the service. ALL elements within the
|
|
|
|
// configuration MUST be non-nil for the service to carry out its duties.
|
|
|
|
type Config struct {
|
2017-10-17 04:39:06 +03:00
|
|
|
// SelfKey is the key of the backing Lightning node. This key is used
|
|
|
|
// to properly craft failure messages, such that the Layer 3 router can
|
|
|
|
// properly route around link./vertex failures.
|
|
|
|
SelfKey *btcec.PublicKey
|
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
// FwdingLog is an interface that will be used by the switch to log
|
|
|
|
// forwarding events. A forwarding event happens each time a payment
|
|
|
|
// circuit is successfully completed. So when we forward an HTLC, and a
|
|
|
|
// settle is eventually received.
|
|
|
|
FwdingLog ForwardingLog
|
|
|
|
|
2017-10-17 04:39:06 +03:00
|
|
|
// LocalChannelClose kicks-off the workflow to execute a cooperative or
|
|
|
|
// forced unilateral closure of the channel initiated by a local
|
2017-05-01 19:58:08 +03:00
|
|
|
// subsystem.
|
|
|
|
LocalChannelClose func(pubKey []byte, request *ChanClose)
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
// DB is the channeldb instance that will be used to back the switch's
|
|
|
|
// persistent circuit map.
|
|
|
|
DB *channeldb.DB
|
|
|
|
|
|
|
|
// SwitchPackager provides access to the forwarding packages of all
|
|
|
|
// active channels. This gives the switch the ability to read arbitrary
|
|
|
|
// forwarding packages, and ack settles and fails contained within them.
|
|
|
|
SwitchPackager channeldb.FwdOperator
|
2018-03-12 23:40:06 +03:00
|
|
|
|
|
|
|
// ExtractErrorEncrypter is an interface allowing switch to reextract
|
|
|
|
// error encrypters stored in the circuit map on restarts, since they
|
|
|
|
// are not stored directly within the database.
|
|
|
|
ExtractErrorEncrypter ErrorEncrypterExtracter
|
2018-05-08 06:00:32 +03:00
|
|
|
|
|
|
|
// FetchLastChannelUpdate retrieves the latest routing policy for a
|
|
|
|
// target channel. This channel will typically be the outgoing channel
|
|
|
|
// specified when we receive an incoming HTLC. This will be used to
|
|
|
|
// provide payment senders our latest policy when sending encrypted
|
|
|
|
// error messages.
|
|
|
|
FetchLastChannelUpdate func(lnwire.ShortChannelID) (*lnwire.ChannelUpdate, error)
|
2018-06-01 06:31:40 +03:00
|
|
|
|
|
|
|
// Notifier is an instance of a chain notifier that we'll use to signal
|
|
|
|
// the switch when a new block has arrived.
|
|
|
|
Notifier chainntnfs.ChainNotifier
|
2018-08-01 03:01:19 +03:00
|
|
|
|
|
|
|
// FwdEventTicker is a signal that instructs the htlcswitch to flush any
|
|
|
|
// pending forwarding events.
|
|
|
|
FwdEventTicker ticker.Ticker
|
|
|
|
|
|
|
|
// LogEventTicker is a signal instructing the htlcswitch to log
|
|
|
|
// aggregate stats about it's forwarding during the last interval.
|
|
|
|
LogEventTicker ticker.Ticker
|
2019-01-23 04:45:51 +03:00
|
|
|
|
|
|
|
// NotifyActiveChannel and NotifyInactiveChannel allow the link to tell
|
|
|
|
// the ChannelNotifier when channels become active and inactive.
|
|
|
|
NotifyActiveChannel func(wire.OutPoint)
|
|
|
|
NotifyInactiveChannel func(wire.OutPoint)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-01 02:48:07 +03:00
|
|
|
// Switch is the central messaging bus for all incoming/outgoing HTLCs.
|
2017-06-01 02:16:52 +03:00
|
|
|
// Connected peers with active channels are treated as named interfaces which
|
|
|
|
// refer to active channels as links. A link is the switch's message
|
|
|
|
// communication point with the goroutine that manages an active channel. New
|
|
|
|
// links are registered each time a channel is created, and unregistered once
|
|
|
|
// the channel is closed. The switch manages the hand-off process for multi-hop
|
|
|
|
// HTLCs, forwarding HTLCs initiated from within the daemon, and finally
|
|
|
|
// notifies users local-systems concerning their outstanding payment requests.
|
2017-05-01 19:58:08 +03:00
|
|
|
type Switch struct {
|
2018-06-01 01:41:41 +03:00
|
|
|
started int32 // To be used atomically.
|
|
|
|
shutdown int32 // To be used atomically.
|
2018-06-01 06:31:40 +03:00
|
|
|
|
|
|
|
// bestHeight is the best known height of the main chain. The links will
|
|
|
|
// be used this information to govern decisions based on HTLC timeouts.
|
|
|
|
// This will be retrieved by the registered links atomically.
|
|
|
|
bestHeight uint32
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
// cfg is a copy of the configuration struct that the htlc switch
|
|
|
|
// service was initialized with.
|
|
|
|
cfg *Config
|
|
|
|
|
2017-10-30 22:57:32 +03:00
|
|
|
// pendingPayments stores payments initiated by the user that are not yet
|
|
|
|
// settled. The map is used to later look up the payments and notify the
|
|
|
|
// user of the result when they are complete. Each payment is given a unique
|
|
|
|
// integer ID when it is created.
|
|
|
|
pendingPayments map[uint64]*pendingPayment
|
2017-05-01 19:58:08 +03:00
|
|
|
pendingMutex sync.RWMutex
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
paymentSequencer Sequencer
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-08-12 16:19:54 +03:00
|
|
|
// control provides verification of sending htlc mesages
|
|
|
|
control ControlTower
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// circuits is storage for payment circuits which are used to
|
|
|
|
// forward the settle/fail htlc updates back to the add htlc initiator.
|
2017-12-06 08:23:23 +03:00
|
|
|
circuits CircuitMap
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// mailOrchestrator manages the lifecycle of mailboxes used throughout
|
|
|
|
// the switch, and facilitates delayed delivery of packets to links that
|
|
|
|
// later come online.
|
|
|
|
mailOrchestrator *mailOrchestrator
|
2017-12-06 08:23:23 +03:00
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
// indexMtx is a read/write mutex that protects the set of indexes
|
|
|
|
// below.
|
|
|
|
indexMtx sync.RWMutex
|
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// pendingLinkIndex holds links that have not had their final, live
|
|
|
|
// short_chan_id assigned. These links can be transitioned into the
|
|
|
|
// primary linkIndex by using UpdateShortChanID to load their live id.
|
|
|
|
pendingLinkIndex map[lnwire.ChannelID]ChannelLink
|
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
// links is a map of channel id and channel link which manages
|
|
|
|
// this channel.
|
|
|
|
linkIndex map[lnwire.ChannelID]ChannelLink
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// forwardingIndex is an index which is consulted by the switch when it
|
|
|
|
// needs to locate the next hop to forward an incoming/outgoing HTLC
|
|
|
|
// update to/from.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): eventually add a NetworkHop mapping before the
|
|
|
|
// ChannelLink
|
|
|
|
forwardingIndex map[lnwire.ShortChannelID]ChannelLink
|
|
|
|
|
|
|
|
// interfaceIndex maps the compressed public key of a peer to all the
|
2018-06-12 09:02:07 +03:00
|
|
|
// channels that the switch maintains with that peer.
|
|
|
|
interfaceIndex map[[33]byte]map[lnwire.ChannelID]ChannelLink
|
2017-06-17 00:49:38 +03:00
|
|
|
|
|
|
|
// htlcPlex is the channel which all connected links use to coordinate
|
|
|
|
// the setup/teardown of Sphinx (onion routing) payment circuits.
|
|
|
|
// Active links forward any add/settle messages over this channel each
|
|
|
|
// state transition, sending new adds/settles which are fully locked
|
|
|
|
// in.
|
|
|
|
htlcPlex chan *plexPacket
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
// chanCloseRequests is used to transfer the channel close request to
|
|
|
|
// the channel close handler.
|
|
|
|
chanCloseRequests chan *ChanClose
|
|
|
|
|
2018-01-17 07:11:01 +03:00
|
|
|
// resolutionMsgs is the channel that all external contract resolution
|
|
|
|
// messages will be sent over.
|
|
|
|
resolutionMsgs chan *resolutionMsg
|
|
|
|
|
2017-08-04 02:57:56 +03:00
|
|
|
// linkControl is a channel used to propagate add/remove/get htlc
|
2017-05-01 19:58:08 +03:00
|
|
|
// switch handler commands.
|
|
|
|
linkControl chan interface{}
|
2018-02-28 09:18:52 +03:00
|
|
|
|
|
|
|
// pendingFwdingEvents is the set of forwarding events which have been
|
|
|
|
// collected during the current interval, but hasn't yet been written
|
|
|
|
// to the forwarding log.
|
|
|
|
fwdEventMtx sync.Mutex
|
|
|
|
pendingFwdingEvents []channeldb.ForwardingEvent
|
2018-06-01 06:31:40 +03:00
|
|
|
|
|
|
|
// blockEpochStream is an active block epoch event stream backed by an
|
|
|
|
// active ChainNotifier instance. This will be used to retrieve the
|
|
|
|
// lastest height of the chain.
|
|
|
|
blockEpochStream *chainntnfs.BlockEpochEvent
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates the new instance of htlc switch.
|
2018-06-01 06:31:40 +03:00
|
|
|
func New(cfg Config, currentHeight uint32) (*Switch, error) {
|
2018-03-12 23:40:06 +03:00
|
|
|
circuitMap, err := NewCircuitMap(&CircuitMapConfig{
|
2018-10-07 05:34:32 +03:00
|
|
|
DB: cfg.DB,
|
2018-03-12 23:40:06 +03:00
|
|
|
ExtractErrorEncrypter: cfg.ExtractErrorEncrypter,
|
|
|
|
})
|
2017-12-06 08:23:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sequencer, err := NewPersistentSequencer(cfg.DB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return &Switch{
|
2018-06-01 06:31:40 +03:00
|
|
|
bestHeight: currentHeight,
|
2017-05-01 19:58:08 +03:00
|
|
|
cfg: &cfg,
|
2017-12-06 08:23:23 +03:00
|
|
|
circuits: circuitMap,
|
|
|
|
paymentSequencer: sequencer,
|
2018-08-14 04:49:22 +03:00
|
|
|
control: NewPaymentControl(false, cfg.DB),
|
2017-06-17 00:49:38 +03:00
|
|
|
linkIndex: make(map[lnwire.ChannelID]ChannelLink),
|
2018-05-02 02:30:30 +03:00
|
|
|
mailOrchestrator: newMailOrchestrator(),
|
2017-06-17 00:49:38 +03:00
|
|
|
forwardingIndex: make(map[lnwire.ShortChannelID]ChannelLink),
|
2018-06-12 09:02:07 +03:00
|
|
|
interfaceIndex: make(map[[33]byte]map[lnwire.ChannelID]ChannelLink),
|
2018-05-02 02:30:30 +03:00
|
|
|
pendingLinkIndex: make(map[lnwire.ChannelID]ChannelLink),
|
2017-10-30 22:57:32 +03:00
|
|
|
pendingPayments: make(map[uint64]*pendingPayment),
|
2017-06-17 00:49:38 +03:00
|
|
|
htlcPlex: make(chan *plexPacket),
|
2017-05-01 19:58:08 +03:00
|
|
|
chanCloseRequests: make(chan *ChanClose),
|
2018-01-17 07:11:01 +03:00
|
|
|
resolutionMsgs: make(chan *resolutionMsg),
|
2017-05-01 19:58:08 +03:00
|
|
|
quit: make(chan struct{}),
|
2017-12-06 08:23:23 +03:00
|
|
|
}, nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2018-01-17 07:11:01 +03:00
|
|
|
// resolutionMsg is a struct that wraps an existing ResolutionMsg with a done
|
|
|
|
// channel. We'll use this channel to synchronize delivery of the message with
|
|
|
|
// the caller.
|
|
|
|
type resolutionMsg struct {
|
|
|
|
contractcourt.ResolutionMsg
|
|
|
|
|
|
|
|
doneChan chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessContractResolution is called by active contract resolvers once a
|
|
|
|
// contract they are watching over has been fully resolved. The message carries
|
|
|
|
// an external signal that *would* have been sent if the outgoing channel
|
|
|
|
// didn't need to go to the chain in order to fulfill a contract. We'll process
|
|
|
|
// this message just as if it came from an active outgoing channel.
|
|
|
|
func (s *Switch) ProcessContractResolution(msg contractcourt.ResolutionMsg) error {
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.resolutionMsgs <- &resolutionMsg{
|
|
|
|
ResolutionMsg: msg,
|
|
|
|
doneChan: done,
|
|
|
|
}:
|
|
|
|
case <-s.quit:
|
2018-07-27 13:29:16 +03:00
|
|
|
return ErrSwitchExiting
|
2018-01-17 07:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-s.quit:
|
2018-07-27 13:29:16 +03:00
|
|
|
return ErrSwitchExiting
|
2018-01-17 07:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// SendHTLC is used by other subsystems which aren't belong to htlc switch
|
|
|
|
// package in order to send the htlc update.
|
2018-07-10 04:11:25 +03:00
|
|
|
func (s *Switch) SendHTLC(firstHop lnwire.ShortChannelID,
|
|
|
|
htlc *lnwire.UpdateAddHTLC,
|
2017-10-11 05:36:52 +03:00
|
|
|
deobfuscator ErrorDecrypter) ([sha256.Size]byte, error) {
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-08-10 23:59:50 +03:00
|
|
|
// Before sending, double check that we don't already have 1) an
|
|
|
|
// in-flight payment to this payment hash, or 2) a complete payment for
|
|
|
|
// the same hash.
|
|
|
|
if err := s.control.ClearForTakeoff(htlc); err != nil {
|
2018-08-12 16:19:54 +03:00
|
|
|
return zeroPreimage, err
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// Create payment and add to the map of payment in order later to be
|
|
|
|
// able to retrieve it and return response to the user.
|
|
|
|
payment := &pendingPayment{
|
2017-06-29 16:40:45 +03:00
|
|
|
err: make(chan error, 1),
|
|
|
|
preimage: make(chan [sha256.Size]byte, 1),
|
|
|
|
paymentHash: htlc.PaymentHash,
|
|
|
|
amount: htlc.Amount,
|
|
|
|
deobfuscator: deobfuscator,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
paymentID, err := s.paymentSequencer.NextID()
|
|
|
|
if err != nil {
|
|
|
|
return zeroPreimage, err
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
s.pendingMutex.Lock()
|
2017-10-30 22:57:32 +03:00
|
|
|
s.pendingPayments[paymentID] = payment
|
2017-05-01 19:58:08 +03:00
|
|
|
s.pendingMutex.Unlock()
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// Generate and send new update packet, if error will be received on
|
|
|
|
// this stage it means that packet haven't left boundaries of our
|
2017-05-01 19:58:08 +03:00
|
|
|
// system and something wrong happened.
|
2017-10-24 09:18:26 +03:00
|
|
|
packet := &htlcPacket{
|
2017-12-06 08:23:23 +03:00
|
|
|
incomingChanID: sourceHop,
|
2017-10-30 22:57:32 +03:00
|
|
|
incomingHTLCID: paymentID,
|
2018-07-10 04:11:25 +03:00
|
|
|
outgoingChanID: firstHop,
|
2017-10-30 22:57:32 +03:00
|
|
|
htlc: htlc,
|
2017-10-24 09:18:26 +03:00
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
if err := s.forward(packet); err != nil {
|
2017-10-30 22:57:32 +03:00
|
|
|
s.removePendingPayment(paymentID)
|
2018-08-12 16:19:54 +03:00
|
|
|
if err := s.control.Fail(htlc.PaymentHash); err != nil {
|
|
|
|
return zeroPreimage, err
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return zeroPreimage, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns channels so that other subsystem might wait/skip the
|
|
|
|
// waiting of handling of payment.
|
|
|
|
var preimage [sha256.Size]byte
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e := <-payment.err:
|
|
|
|
err = e
|
|
|
|
case <-s.quit:
|
2018-07-27 13:29:16 +03:00
|
|
|
return zeroPreimage, ErrSwitchExiting
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case p := <-payment.preimage:
|
|
|
|
preimage = p
|
|
|
|
case <-s.quit:
|
2018-07-27 13:29:16 +03:00
|
|
|
return zeroPreimage, ErrSwitchExiting
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return preimage, err
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:50:12 +03:00
|
|
|
// UpdateForwardingPolicies sends a message to the switch to update the
|
|
|
|
// forwarding policies for the set of target channels. If the set of targeted
|
|
|
|
// channels is nil, then the forwarding policies for all active channels with
|
|
|
|
// be updated.
|
|
|
|
//
|
|
|
|
// NOTE: This function is synchronous and will block until either the
|
|
|
|
// forwarding policies for all links have been updated, or the switch shuts
|
|
|
|
// down.
|
|
|
|
func (s *Switch) UpdateForwardingPolicies(newPolicy ForwardingPolicy,
|
|
|
|
targetChans ...wire.OutPoint) error {
|
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
log.Debugf("Updating link policies: %v", newLogClosure(func() string {
|
|
|
|
return spew.Sdump(newPolicy)
|
|
|
|
}))
|
2017-08-22 09:50:12 +03:00
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
var linksToUpdate []ChannelLink
|
2017-08-22 09:50:12 +03:00
|
|
|
|
2018-04-04 06:18:42 +03:00
|
|
|
s.indexMtx.RLock()
|
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
// If no channels have been targeted, then we'll collect all inks to
|
|
|
|
// update their policies.
|
|
|
|
if len(targetChans) == 0 {
|
2017-08-22 09:50:12 +03:00
|
|
|
for _, link := range s.linkIndex {
|
2018-04-04 06:06:57 +03:00
|
|
|
linksToUpdate = append(linksToUpdate, link)
|
2017-08-22 09:50:12 +03:00
|
|
|
}
|
2018-04-04 06:06:57 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise, we'll only attempt to update the forwarding
|
|
|
|
// policies for the set of targeted links.
|
|
|
|
for _, targetLink := range targetChans {
|
|
|
|
cid := lnwire.NewChanIDFromOutPoint(&targetLink)
|
|
|
|
|
|
|
|
// If we can't locate a link by its converted channel
|
|
|
|
// ID, then we'll return an error back to the caller.
|
|
|
|
link, ok := s.linkIndex[cid]
|
|
|
|
if !ok {
|
|
|
|
s.indexMtx.RUnlock()
|
2017-08-22 09:50:12 +03:00
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
return fmt.Errorf("unable to find "+
|
|
|
|
"ChannelPoint(%v) to update link "+
|
|
|
|
"policy", targetLink)
|
|
|
|
}
|
2017-08-22 09:50:12 +03:00
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
linksToUpdate = append(linksToUpdate, link)
|
2017-08-22 09:50:12 +03:00
|
|
|
}
|
2018-04-04 06:06:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.indexMtx.RUnlock()
|
2017-08-22 09:50:12 +03:00
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
// With all the links we need to update collected, we can release the
|
|
|
|
// mutex then update each link directly.
|
|
|
|
for _, link := range linksToUpdate {
|
|
|
|
link.UpdateForwardingPolicy(newPolicy)
|
2017-08-22 09:50:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-13 04:52:52 +03:00
|
|
|
// forward is used in order to find next channel link and apply htlc update.
|
|
|
|
// Also this function is used by channel links itself in order to forward the
|
|
|
|
// update after it has been included in the channel.
|
2017-05-01 19:58:08 +03:00
|
|
|
func (s *Switch) forward(packet *htlcPacket) error {
|
2017-12-06 08:23:23 +03:00
|
|
|
switch htlc := packet.htlc.(type) {
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
circuit := newPaymentCircuit(&htlc.PaymentHash, packet)
|
|
|
|
actions, err := s.circuits.CommitCircuits(circuit)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to commit circuit in switch: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop duplicate packet if it has already been seen.
|
|
|
|
switch {
|
|
|
|
case len(actions.Drops) == 1:
|
|
|
|
return ErrDuplicateAdd
|
|
|
|
|
|
|
|
case len(actions.Fails) == 1:
|
|
|
|
if packet.incomingChanID == sourceHop {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-08 06:00:32 +03:00
|
|
|
var failure lnwire.FailureMessage
|
|
|
|
update, err := s.cfg.FetchLastChannelUpdate(
|
|
|
|
packet.incomingChanID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
failure = &lnwire.FailTemporaryNodeFailure{}
|
|
|
|
} else {
|
|
|
|
failure = lnwire.NewTemporaryChannelFailure(update)
|
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
addErr := ErrIncompleteForward
|
|
|
|
|
|
|
|
return s.failAddPacket(packet, failure, addErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.circuit = circuit
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.route(packet)
|
|
|
|
}
|
|
|
|
|
2018-03-13 04:52:52 +03:00
|
|
|
// ForwardPackets adds a list of packets to the switch for processing. Fails
|
|
|
|
// and settles are added on a first past, simultaneously constructing circuits
|
|
|
|
// for any adds. After persisting the circuits, another pass of the adds is
|
2018-07-30 23:17:39 +03:00
|
|
|
// given to forward them through the router. The sending link's quit channel is
|
|
|
|
// used to prevent deadlocks when the switch stops a link in the midst of
|
|
|
|
// forwarding.
|
2018-03-13 04:52:52 +03:00
|
|
|
//
|
2017-12-06 08:23:23 +03:00
|
|
|
// NOTE: This method guarantees that the returned err chan will eventually be
|
|
|
|
// closed. The receiver should read on the channel until receiving such a
|
|
|
|
// signal.
|
2018-07-30 23:17:39 +03:00
|
|
|
func (s *Switch) ForwardPackets(linkQuit chan struct{},
|
|
|
|
packets ...*htlcPacket) chan error {
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
var (
|
|
|
|
// fwdChan is a buffered channel used to receive err msgs from
|
|
|
|
// the htlcPlex when forwarding this batch.
|
|
|
|
fwdChan = make(chan error, len(packets))
|
|
|
|
|
|
|
|
// errChan is a buffered channel returned to the caller, that is
|
|
|
|
// proxied by the fwdChan. This method guarantees that errChan
|
|
|
|
// will be closed eventually to alert the receiver that it can
|
|
|
|
// stop reading from the channel.
|
|
|
|
errChan = make(chan error, len(packets))
|
|
|
|
|
|
|
|
// numSent keeps a running count of how many packets are
|
|
|
|
// forwarded to the switch, which determines how many responses
|
|
|
|
// we will wait for on the fwdChan..
|
|
|
|
numSent int
|
|
|
|
)
|
|
|
|
|
|
|
|
// No packets, nothing to do.
|
|
|
|
if len(packets) == 0 {
|
|
|
|
close(errChan)
|
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a barrier to prevent the background tasks from processing
|
|
|
|
// responses until this function returns to the user.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
|
|
|
|
2018-03-12 23:49:22 +03:00
|
|
|
// Before spawning the following goroutine to proxy our error responses,
|
|
|
|
// check to see if we have already been issued a shutdown request. If
|
|
|
|
// so, we exit early to avoid incrementing the switch's waitgroup while
|
|
|
|
// it is already in the process of shutting down.
|
|
|
|
select {
|
2018-07-30 23:17:39 +03:00
|
|
|
case <-linkQuit:
|
|
|
|
close(errChan)
|
|
|
|
return errChan
|
2018-03-12 23:49:22 +03:00
|
|
|
case <-s.quit:
|
|
|
|
close(errChan)
|
|
|
|
return errChan
|
|
|
|
default:
|
|
|
|
// Spawn a goroutine the proxy the errs back to the returned err
|
|
|
|
// chan. This is done to ensure the err chan returned to the
|
|
|
|
// caller closed properly, alerting the receiver of completion
|
|
|
|
// or shutdown.
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.proxyFwdErrs(&numSent, &wg, fwdChan, errChan)
|
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
// Make a first pass over the packets, forwarding any settles or fails.
|
|
|
|
// As adds are found, we create a circuit and append it to our set of
|
|
|
|
// circuits to be written to disk.
|
|
|
|
var circuits []*PaymentCircuit
|
|
|
|
var addBatch []*htlcPacket
|
|
|
|
for _, packet := range packets {
|
|
|
|
switch htlc := packet.htlc.(type) {
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
circuit := newPaymentCircuit(&htlc.PaymentHash, packet)
|
|
|
|
packet.circuit = circuit
|
|
|
|
circuits = append(circuits, circuit)
|
|
|
|
addBatch = append(addBatch, packet)
|
|
|
|
default:
|
2018-07-30 23:17:39 +03:00
|
|
|
err := s.routeAsync(packet, fwdChan, linkQuit)
|
|
|
|
if err != nil {
|
|
|
|
return errChan
|
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
numSent++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this batch did not contain any circuits to commit, we can return
|
|
|
|
// early.
|
|
|
|
if len(circuits) == 0 {
|
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write any circuits that we found to disk.
|
|
|
|
actions, err := s.circuits.CommitCircuits(circuits...)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to commit circuits in switch: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the htlc packets by comparing an in-order seek to the head of
|
|
|
|
// the added, dropped, or failed circuits.
|
|
|
|
//
|
|
|
|
// NOTE: This assumes each list is guaranteed to be a subsequence of the
|
|
|
|
// circuits, and that the union of the sets results in the original set
|
|
|
|
// of circuits.
|
|
|
|
var addedPackets, failedPackets []*htlcPacket
|
|
|
|
for _, packet := range addBatch {
|
|
|
|
switch {
|
|
|
|
case len(actions.Adds) > 0 && packet.circuit == actions.Adds[0]:
|
|
|
|
addedPackets = append(addedPackets, packet)
|
|
|
|
actions.Adds = actions.Adds[1:]
|
|
|
|
|
|
|
|
case len(actions.Drops) > 0 && packet.circuit == actions.Drops[0]:
|
|
|
|
actions.Drops = actions.Drops[1:]
|
|
|
|
|
|
|
|
case len(actions.Fails) > 0 && packet.circuit == actions.Fails[0]:
|
|
|
|
failedPackets = append(failedPackets, packet)
|
|
|
|
actions.Fails = actions.Fails[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, forward any packets for circuits that were successfully added to
|
|
|
|
// the switch's circuit map.
|
|
|
|
for _, packet := range addedPackets {
|
2018-07-30 23:17:39 +03:00
|
|
|
err := s.routeAsync(packet, fwdChan, linkQuit)
|
|
|
|
if err != nil {
|
|
|
|
return errChan
|
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
numSent++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lastly, for any packets that failed, this implies that they were
|
|
|
|
// left in a half added state, which can happen when recovering from
|
|
|
|
// failures.
|
2018-05-08 06:00:32 +03:00
|
|
|
if len(failedPackets) > 0 {
|
|
|
|
var failure lnwire.FailureMessage
|
|
|
|
update, err := s.cfg.FetchLastChannelUpdate(
|
|
|
|
failedPackets[0].incomingChanID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
failure = &lnwire.FailTemporaryNodeFailure{}
|
|
|
|
} else {
|
|
|
|
failure = lnwire.NewTemporaryChannelFailure(update)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, packet := range failedPackets {
|
2018-07-27 13:27:11 +03:00
|
|
|
addErr := errors.New("failing packet after " +
|
2018-05-08 06:00:32 +03:00
|
|
|
"detecting incomplete forward")
|
|
|
|
|
|
|
|
// We don't handle the error here since this method
|
|
|
|
// always returns an error.
|
|
|
|
s.failAddPacket(packet, failure, addErr)
|
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// proxyFwdErrs transmits any errors received on `fwdChan` back to `errChan`,
|
|
|
|
// and guarantees that the `errChan` will be closed after 1) all errors have
|
|
|
|
// been sent, or 2) the switch has received a shutdown. The `errChan` should be
|
|
|
|
// buffered with at least the value of `num` after the barrier has been
|
|
|
|
// released.
|
|
|
|
//
|
|
|
|
// NOTE: The receiver of `errChan` should read until the channel closed, since
|
|
|
|
// this proxying guarantees that the close will happen.
|
|
|
|
func (s *Switch) proxyFwdErrs(num *int, wg *sync.WaitGroup,
|
|
|
|
fwdChan, errChan chan error) {
|
|
|
|
defer s.wg.Done()
|
2018-07-27 13:47:15 +03:00
|
|
|
defer close(errChan)
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
// Wait here until the outer function has finished persisting
|
|
|
|
// and routing the packets. This guarantees we don't read from num until
|
|
|
|
// the value is accurate.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
numSent := *num
|
|
|
|
for i := 0; i < numSent; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-fwdChan:
|
|
|
|
errChan <- err
|
|
|
|
case <-s.quit:
|
|
|
|
log.Errorf("unable to forward htlc packet " +
|
|
|
|
"htlc switch was stopped")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// route sends a single htlcPacket through the switch and synchronously awaits a
|
|
|
|
// response.
|
|
|
|
func (s *Switch) route(packet *htlcPacket) error {
|
2017-06-17 00:49:38 +03:00
|
|
|
command := &plexPacket{
|
2017-05-01 19:58:08 +03:00
|
|
|
pkt: packet,
|
|
|
|
err: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2017-06-17 00:49:38 +03:00
|
|
|
case s.htlcPlex <- command:
|
|
|
|
case <-s.quit:
|
2018-07-27 13:29:16 +03:00
|
|
|
return ErrSwitchExiting
|
2017-06-17 00:49:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-command.err:
|
|
|
|
return err
|
2017-05-01 19:58:08 +03:00
|
|
|
case <-s.quit:
|
2018-07-27 13:29:16 +03:00
|
|
|
return ErrSwitchExiting
|
2017-12-06 08:23:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// routeAsync sends a packet through the htlc switch, using the provided err
|
2018-07-30 23:17:39 +03:00
|
|
|
// chan to propagate errors back to the caller. The link's quit channel is
|
|
|
|
// provided so that the send can be canceled if either the link or the switch
|
|
|
|
// receive a shutdown requuest. This method does not wait for a response from
|
|
|
|
// the htlcForwarder before returning.
|
|
|
|
func (s *Switch) routeAsync(packet *htlcPacket, errChan chan error,
|
|
|
|
linkQuit chan struct{}) error {
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
command := &plexPacket{
|
|
|
|
pkt: packet,
|
|
|
|
err: errChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.htlcPlex <- command:
|
|
|
|
return nil
|
2018-07-30 23:17:39 +03:00
|
|
|
case <-linkQuit:
|
|
|
|
return ErrLinkShuttingDown
|
2017-12-06 08:23:23 +03:00
|
|
|
case <-s.quit:
|
|
|
|
return errors.New("Htlc Switch was stopped")
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 04:42:11 +03:00
|
|
|
// handleLocalDispatch is used at the start/end of the htlc update life cycle.
|
|
|
|
// At the start (1) it is used to send the htlc to the channel link without
|
|
|
|
// creation of circuit. At the end (2) it is used to notify the user about the
|
|
|
|
// result of his payment is it was successful or not.
|
2017-05-01 19:58:08 +03:00
|
|
|
//
|
|
|
|
// Alice Bob Carol
|
|
|
|
// o --add----> o ---add----> o
|
|
|
|
// (1)
|
|
|
|
//
|
|
|
|
// (2)
|
|
|
|
// o <-settle-- o <--settle-- o
|
|
|
|
// Alice Bob Carol
|
|
|
|
//
|
2017-12-06 08:23:23 +03:00
|
|
|
func (s *Switch) handleLocalDispatch(pkt *htlcPacket) error {
|
2017-05-01 19:58:08 +03:00
|
|
|
// User have created the htlc update therefore we should find the
|
|
|
|
// appropriate channel link and send the payment over this link.
|
2018-08-10 23:59:50 +03:00
|
|
|
if htlc, ok := pkt.htlc.(*lnwire.UpdateAddHTLC); ok {
|
|
|
|
// Try to find links by node destination.
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RLock()
|
2018-07-10 04:11:25 +03:00
|
|
|
link, err := s.getLinkByShortID(pkt.outgoingChanID)
|
|
|
|
s.indexMtx.RUnlock()
|
2017-05-01 19:58:08 +03:00
|
|
|
if err != nil {
|
2018-07-10 04:11:25 +03:00
|
|
|
log.Errorf("Link %v not found", pkt.outgoingChanID)
|
2017-10-17 04:42:11 +03:00
|
|
|
return &ForwardingError{
|
|
|
|
ErrorSource: s.cfg.SelfKey,
|
|
|
|
FailureMessage: &lnwire.FailUnknownNextPeer{},
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2017-12-06 04:49:55 +03:00
|
|
|
|
2018-07-10 04:11:25 +03:00
|
|
|
if !link.EligibleToForward() {
|
|
|
|
err := fmt.Errorf("Link %v is not available to forward",
|
|
|
|
pkt.outgoingChanID)
|
|
|
|
log.Error(err)
|
2017-10-03 08:06:02 +03:00
|
|
|
|
2018-07-10 04:11:25 +03:00
|
|
|
// The update does not need to be populated as the error
|
|
|
|
// will be returned back to the router.
|
|
|
|
htlcErr := lnwire.NewTemporaryChannelFailure(nil)
|
|
|
|
return &ForwardingError{
|
|
|
|
ErrorSource: s.cfg.SelfKey,
|
|
|
|
ExtraMsg: err.Error(),
|
|
|
|
FailureMessage: htlcErr,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 04:11:25 +03:00
|
|
|
if link.Bandwidth() < htlc.Amount {
|
|
|
|
err := fmt.Errorf("Link %v has insufficient capacity: "+
|
|
|
|
"need %v, has %v", pkt.outgoingChanID,
|
|
|
|
htlc.Amount, link.Bandwidth())
|
2017-10-03 08:06:02 +03:00
|
|
|
log.Error(err)
|
2017-10-17 04:42:11 +03:00
|
|
|
|
2018-07-10 04:11:25 +03:00
|
|
|
// The update does not need to be populated as the error
|
|
|
|
// will be returned back to the router.
|
2017-10-17 04:42:11 +03:00
|
|
|
htlcErr := lnwire.NewTemporaryChannelFailure(nil)
|
|
|
|
return &ForwardingError{
|
|
|
|
ErrorSource: s.cfg.SelfKey,
|
|
|
|
ExtraMsg: err.Error(),
|
|
|
|
FailureMessage: htlcErr,
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2018-07-10 04:11:25 +03:00
|
|
|
return link.HandleSwitchPacket(pkt)
|
2018-08-10 23:59:50 +03:00
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-08-21 07:21:15 +03:00
|
|
|
s.wg.Add(1)
|
|
|
|
go s.handleLocalResponse(pkt)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleLocalResponse processes a Settle or Fail responding to a
|
|
|
|
// locally-initiated payment. This is handled asynchronously to avoid blocking
|
|
|
|
// the main event loop within the switch, as these operations can require
|
|
|
|
// multiple db transactions. The guarantees of the circuit map are stringent
|
|
|
|
// enough such that we are able to tolerate reordering of these operations
|
|
|
|
// without side effects. The primary operations handled are:
|
|
|
|
// 1. Ack settle/fail references, to avoid resending this response internally
|
|
|
|
// 2. Teardown the closing circuit in the circuit map
|
|
|
|
// 3. Transition the payment status to grounded or completed.
|
|
|
|
// 4. Respond to an in-mem pending payment, if it is found.
|
|
|
|
//
|
|
|
|
// NOTE: This method MUST be spawned as a goroutine.
|
|
|
|
func (s *Switch) handleLocalResponse(pkt *htlcPacket) {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
|
|
|
// First, we'll clean up any fwdpkg references, circuit entries, and
|
|
|
|
// mark in our db that the payment for this payment hash has either
|
|
|
|
// succeeded or failed.
|
2018-08-10 23:59:50 +03:00
|
|
|
//
|
|
|
|
// If this response is contained in a forwarding package, we'll start by
|
|
|
|
// acking the settle/fail so that we don't continue to retransmit the
|
|
|
|
// HTLC internally.
|
|
|
|
if pkt.destRef != nil {
|
|
|
|
if err := s.ackSettleFail(*pkt.destRef); err != nil {
|
|
|
|
log.Warnf("Unable to ack settle/fail reference: %s: %v",
|
|
|
|
*pkt.destRef, err)
|
2018-08-21 07:21:15 +03:00
|
|
|
return
|
2018-08-10 23:59:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll remove the circuit since we are about to complete an
|
|
|
|
// fulfill/fail of this HTLC. Since we've already removed the
|
|
|
|
// settle/fail fwdpkg reference, the response from the peer cannot be
|
|
|
|
// replayed internally if this step fails. If this happens, this logic
|
|
|
|
// will be executed when a provided resolution message comes through.
|
|
|
|
// This can only happen if the circuit is still open, which is why this
|
|
|
|
// ordering is chosen.
|
|
|
|
if err := s.teardownCircuit(pkt); err != nil {
|
|
|
|
log.Warnf("Unable to teardown circuit %s: %v",
|
|
|
|
pkt.inKey(), err)
|
2018-08-21 07:21:15 +03:00
|
|
|
return
|
2018-08-10 23:59:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Locate the pending payment to notify the application that this
|
|
|
|
// payment has failed. If one is not found, it likely means the daemon
|
|
|
|
// has been restarted since sending the payment.
|
|
|
|
payment := s.findPayment(pkt.incomingHTLCID)
|
|
|
|
|
|
|
|
var (
|
|
|
|
preimage [32]byte
|
|
|
|
paymentErr error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch htlc := pkt.htlc.(type) {
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-08-10 23:59:50 +03:00
|
|
|
// We've received a settle update which means we can finalize the user
|
|
|
|
// payment and return successful response.
|
|
|
|
case *lnwire.UpdateFulfillHTLC:
|
|
|
|
// Persistently mark that a payment to this payment hash
|
|
|
|
// succeeded. This will prevent us from ever making another
|
|
|
|
// payment to this hash.
|
2018-08-14 04:49:22 +03:00
|
|
|
err := s.control.Success(pkt.circuit.PaymentHash)
|
|
|
|
if err != nil && err != ErrPaymentAlreadyCompleted {
|
2018-08-21 07:21:15 +03:00
|
|
|
log.Warnf("Unable to mark completed payment %x: %v",
|
|
|
|
pkt.circuit.PaymentHash, err)
|
|
|
|
return
|
2018-08-12 16:19:54 +03:00
|
|
|
}
|
|
|
|
|
2018-08-10 23:59:50 +03:00
|
|
|
preimage = htlc.PaymentPreimage
|
|
|
|
|
|
|
|
// We've received a fail update which means we can finalize the user
|
|
|
|
// payment and return fail response.
|
2017-05-01 19:58:08 +03:00
|
|
|
case *lnwire.UpdateFailHTLC:
|
2018-08-10 23:59:50 +03:00
|
|
|
// Persistently mark that a payment to this payment hash failed.
|
|
|
|
// This will permit us to make another attempt at a successful
|
|
|
|
// payment.
|
2018-08-14 04:49:22 +03:00
|
|
|
err := s.control.Fail(pkt.circuit.PaymentHash)
|
|
|
|
if err != nil && err != ErrPaymentAlreadyCompleted {
|
2018-08-21 07:21:15 +03:00
|
|
|
log.Warnf("Unable to ground payment %x: %v",
|
|
|
|
pkt.circuit.PaymentHash, err)
|
|
|
|
return
|
2018-08-10 23:59:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
paymentErr = s.parseFailedPayment(payment, pkt, htlc)
|
2018-01-20 04:34:52 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
default:
|
2018-08-21 07:21:15 +03:00
|
|
|
log.Warnf("Received unknown response type: %T", pkt.htlc)
|
|
|
|
return
|
2017-12-06 08:23:23 +03:00
|
|
|
}
|
2018-01-20 04:34:52 +03:00
|
|
|
|
2018-08-10 23:59:50 +03:00
|
|
|
// Deliver the payment error and preimage to the application, if it is
|
|
|
|
// waiting for a response.
|
|
|
|
if payment != nil {
|
|
|
|
payment.err <- paymentErr
|
|
|
|
payment.preimage <- preimage
|
|
|
|
s.removePendingPayment(pkt.incomingHTLCID)
|
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
}
|
2018-01-20 04:34:52 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// parseFailedPayment determines the appropriate failure message to return to
|
|
|
|
// a user initiated payment. The three cases handled are:
|
|
|
|
// 1) A local failure, which should already plaintext.
|
|
|
|
// 2) A resolution from the chain arbitrator,
|
|
|
|
// 3) A failure from the remote party, which will need to be decrypted using the
|
|
|
|
// payment deobfuscator.
|
|
|
|
func (s *Switch) parseFailedPayment(payment *pendingPayment, pkt *htlcPacket,
|
|
|
|
htlc *lnwire.UpdateFailHTLC) *ForwardingError {
|
|
|
|
|
|
|
|
var failure *ForwardingError
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
// The payment never cleared the link, so we don't need to
|
|
|
|
// decrypt the error, simply decode it them report back to the
|
|
|
|
// user.
|
|
|
|
case pkt.localFailure:
|
|
|
|
var userErr string
|
|
|
|
r := bytes.NewReader(htlc.Reason)
|
|
|
|
failureMsg, err := lnwire.DecodeFailure(r, 0)
|
|
|
|
if err != nil {
|
|
|
|
userErr = fmt.Sprintf("unable to decode onion failure, "+
|
2018-08-10 23:59:50 +03:00
|
|
|
"htlc with hash(%x): %v",
|
2018-08-21 14:12:08 +03:00
|
|
|
pkt.circuit.PaymentHash[:], err)
|
2017-12-06 08:23:23 +03:00
|
|
|
log.Error(userErr)
|
2018-05-08 06:00:32 +03:00
|
|
|
|
|
|
|
// As this didn't even clear the link, we don't need to
|
|
|
|
// apply an update here since it goes directly to the
|
|
|
|
// router.
|
2017-12-06 08:23:23 +03:00
|
|
|
failureMsg = lnwire.NewTemporaryChannelFailure(nil)
|
|
|
|
}
|
|
|
|
failure = &ForwardingError{
|
|
|
|
ErrorSource: s.cfg.SelfKey,
|
|
|
|
ExtraMsg: userErr,
|
|
|
|
FailureMessage: failureMsg,
|
2017-10-03 08:06:18 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// A payment had to be timed out on chain before it got past
|
|
|
|
// the first hop. In this case, we'll report a permanent
|
|
|
|
// channel failure as this means us, or the remote party had to
|
|
|
|
// go on chain.
|
|
|
|
case pkt.isResolution && htlc.Reason == nil:
|
|
|
|
userErr := fmt.Sprintf("payment was resolved " +
|
|
|
|
"on-chain, then cancelled back")
|
|
|
|
failure = &ForwardingError{
|
|
|
|
ErrorSource: s.cfg.SelfKey,
|
|
|
|
ExtraMsg: userErr,
|
|
|
|
FailureMessage: lnwire.FailPermanentChannelFailure{},
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-08-21 14:12:08 +03:00
|
|
|
// If the provided payment is nil, we have discarded the error decryptor
|
|
|
|
// due to a restart. We'll return a fixed error and signal a temporary
|
|
|
|
// channel failure to the router.
|
|
|
|
case payment == nil:
|
|
|
|
userErr := fmt.Sprintf("error decryptor for payment " +
|
|
|
|
"could not be located, likely due to restart")
|
|
|
|
failure = &ForwardingError{
|
|
|
|
ErrorSource: s.cfg.SelfKey,
|
|
|
|
ExtraMsg: userErr,
|
|
|
|
FailureMessage: lnwire.NewTemporaryChannelFailure(nil),
|
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// A regular multi-hop payment error that we'll need to
|
|
|
|
// decrypt.
|
2017-05-01 19:58:08 +03:00
|
|
|
default:
|
2017-12-06 08:23:23 +03:00
|
|
|
var err error
|
|
|
|
// We'll attempt to fully decrypt the onion encrypted
|
|
|
|
// error. If we're unable to then we'll bail early.
|
|
|
|
failure, err = payment.deobfuscator.DecryptError(htlc.Reason)
|
|
|
|
if err != nil {
|
2018-08-21 14:12:08 +03:00
|
|
|
userErr := fmt.Sprintf("unable to de-obfuscate onion "+
|
|
|
|
"failure, htlc with hash(%x): %v",
|
|
|
|
pkt.circuit.PaymentHash[:], err)
|
2017-12-06 08:23:23 +03:00
|
|
|
log.Error(userErr)
|
|
|
|
failure = &ForwardingError{
|
|
|
|
ErrorSource: s.cfg.SelfKey,
|
|
|
|
ExtraMsg: userErr,
|
|
|
|
FailureMessage: lnwire.NewTemporaryChannelFailure(nil),
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
return failure
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// handlePacketForward is used in cases when we need forward the htlc update
|
|
|
|
// from one channel link to another and be able to propagate the settle/fail
|
|
|
|
// updates back. This behaviour is achieved by creation of payment circuits.
|
2017-05-01 19:58:08 +03:00
|
|
|
func (s *Switch) handlePacketForward(packet *htlcPacket) error {
|
|
|
|
switch htlc := packet.htlc.(type) {
|
|
|
|
|
|
|
|
// Channel link forwarded us a new htlc, therefore we initiate the
|
|
|
|
// payment circuit within our internal state so we can properly forward
|
|
|
|
// the ultimate settle message back latter.
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
2017-12-06 08:23:23 +03:00
|
|
|
if packet.incomingChanID == sourceHop {
|
|
|
|
// A blank incomingChanID indicates that this is
|
|
|
|
// a pending user-initiated payment.
|
2017-10-30 22:57:32 +03:00
|
|
|
return s.handleLocalDispatch(packet)
|
|
|
|
}
|
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RLock()
|
2017-10-30 21:56:51 +03:00
|
|
|
targetLink, err := s.getLinkByShortID(packet.outgoingChanID)
|
2017-05-01 19:58:08 +03:00
|
|
|
if err != nil {
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RUnlock()
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
// If packet was forwarded from another channel link
|
|
|
|
// than we should notify this link that some error
|
|
|
|
// occurred.
|
2017-12-06 08:23:23 +03:00
|
|
|
failure := &lnwire.FailUnknownNextPeer{}
|
2018-07-27 13:27:11 +03:00
|
|
|
addErr := fmt.Errorf("unable to find link with "+
|
2017-10-30 21:56:51 +03:00
|
|
|
"destination %v", packet.outgoingChanID)
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
return s.failAddPacket(packet, failure, addErr)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2017-06-17 00:49:38 +03:00
|
|
|
interfaceLinks, _ := s.getLinks(targetLink.Peer().PubKey())
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RUnlock()
|
2017-05-01 19:58:08 +03:00
|
|
|
|
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
|
|
|
// We'll keep track of any HTLC failures during the link
|
|
|
|
// selection process. This way we can return the error for
|
|
|
|
// precise link that the sender selected, while optimistically
|
|
|
|
// trying all links to utilize our available bandwidth.
|
|
|
|
linkErrs := make(map[lnwire.ShortChannelID]lnwire.FailureMessage)
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// Try to find destination channel link with appropriate
|
|
|
|
// bandwidth.
|
|
|
|
var destination ChannelLink
|
2017-06-17 00:49:38 +03:00
|
|
|
for _, link := range interfaceLinks {
|
2017-12-08 06:03:22 +03:00
|
|
|
// We'll skip any links that aren't yet eligible for
|
|
|
|
// forwarding.
|
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
|
|
|
switch {
|
|
|
|
case !link.EligibleToForward():
|
|
|
|
continue
|
|
|
|
|
|
|
|
// If the link doesn't yet have a source chan ID, then
|
|
|
|
// we'll skip it as well.
|
|
|
|
case link.ShortChanID() == sourceHop:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we check the link's bandwidth, we'll ensure
|
|
|
|
// that the HTLC satisfies the current forwarding
|
|
|
|
// policy of this target link.
|
2018-06-26 06:25:59 +03:00
|
|
|
currentHeight := atomic.LoadUint32(&s.bestHeight)
|
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
|
|
|
err := link.HtlcSatifiesPolicy(
|
|
|
|
htlc.PaymentHash, packet.incomingAmount,
|
2018-06-26 06:25:59 +03:00
|
|
|
packet.amount, packet.incomingTimeout,
|
|
|
|
packet.outgoingTimeout, currentHeight,
|
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
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
linkErrs[link.ShortChanID()] = err
|
2017-12-08 06:03:22 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if link.Bandwidth() >= htlc.Amount {
|
2017-05-01 19:58:08 +03:00
|
|
|
destination = link
|
2018-02-28 09:18:52 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
switch {
|
2017-05-01 19:58:08 +03:00
|
|
|
// If the channel link we're attempting to forward the update
|
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
|
|
|
// over has insufficient capacity, and didn't violate any
|
|
|
|
// forwarding policies, then we'll cancel the htlc as the
|
|
|
|
// payment cannot succeed.
|
|
|
|
case destination == nil && len(linkErrs) == 0:
|
2017-12-06 08:23:23 +03:00
|
|
|
// If packet was forwarded from another channel link
|
|
|
|
// than we should notify this link that some error
|
|
|
|
// occurred.
|
2018-05-08 06:00:32 +03:00
|
|
|
var failure lnwire.FailureMessage
|
|
|
|
update, err := s.cfg.FetchLastChannelUpdate(
|
|
|
|
packet.outgoingChanID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
failure = &lnwire.FailTemporaryNodeFailure{}
|
|
|
|
} else {
|
|
|
|
failure = lnwire.NewTemporaryChannelFailure(update)
|
|
|
|
}
|
|
|
|
|
2018-07-27 13:27:11 +03:00
|
|
|
addErr := fmt.Errorf("unable to find appropriate "+
|
2017-05-01 19:58:08 +03:00
|
|
|
"channel link insufficient capacity, need "+
|
|
|
|
"%v", htlc.Amount)
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
return s.failAddPacket(packet, failure, addErr)
|
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
|
|
|
|
|
|
|
// If we had a forwarding failure due to the HTLC not
|
|
|
|
// satisfying the current policy, then we'll send back an
|
|
|
|
// error, but ensure we send back the error sourced at the
|
|
|
|
// *target* link.
|
|
|
|
case destination == nil && len(linkErrs) != 0:
|
|
|
|
// At this point, some or all of the links rejected the
|
|
|
|
// HTLC so we couldn't forward it. So we'll try to look
|
|
|
|
// up the error that came from the source.
|
|
|
|
linkErr, ok := linkErrs[packet.outgoingChanID]
|
|
|
|
if !ok {
|
|
|
|
// If we can't find the error of the source,
|
|
|
|
// then we'll return an unknown next peer,
|
|
|
|
// though this should never happen.
|
|
|
|
linkErr = &lnwire.FailUnknownNextPeer{}
|
|
|
|
log.Warnf("unable to find err source for "+
|
|
|
|
"outgoing_link=%v, errors=%v",
|
|
|
|
packet.outgoingChanID, newLogClosure(func() string {
|
|
|
|
return spew.Sdump(linkErrs)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
addErr := fmt.Errorf("incoming HTLC(%x) violated "+
|
|
|
|
"target outgoing link (id=%v) policy: %v",
|
|
|
|
htlc.PaymentHash[:], packet.outgoingChanID,
|
|
|
|
linkErr)
|
|
|
|
|
|
|
|
return s.failAddPacket(packet, linkErr, addErr)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the packet to the destination channel link which
|
|
|
|
// manages the channel.
|
2017-12-06 08:23:23 +03:00
|
|
|
packet.outgoingChanID = destination.ShortChanID()
|
|
|
|
return destination.HandleSwitchPacket(packet)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
case *lnwire.UpdateFailHTLC, *lnwire.UpdateFulfillHTLC:
|
|
|
|
// If the source of this packet has not been set, use the
|
|
|
|
// circuit map to lookup the origin.
|
|
|
|
circuit, err := s.closeCircuit(packet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-24 01:50:26 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
fail, isFail := htlc.(*lnwire.UpdateFailHTLC)
|
|
|
|
if isFail && !packet.hasSource {
|
|
|
|
switch {
|
|
|
|
case circuit.ErrorEncrypter == nil:
|
|
|
|
// No message to encrypt, locally sourced
|
|
|
|
// payment.
|
|
|
|
|
|
|
|
case packet.isResolution:
|
|
|
|
// If this is a resolution message, then we'll need to encrypt
|
|
|
|
// it as it's actually internally sourced.
|
|
|
|
var err error
|
|
|
|
// TODO(roasbeef): don't need to pass actually?
|
|
|
|
failure := &lnwire.FailPermanentChannelFailure{}
|
|
|
|
fail.Reason, err = circuit.ErrorEncrypter.EncryptFirstHop(
|
|
|
|
failure,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-07-27 13:27:11 +03:00
|
|
|
err = fmt.Errorf("unable to obfuscate "+
|
2017-12-06 08:23:23 +03:00
|
|
|
"error: %v", err)
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2017-10-30 22:57:32 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
default:
|
|
|
|
// Otherwise, it's a forwarded error, so we'll perform a
|
|
|
|
// wrapper encryption as normal.
|
|
|
|
fail.Reason = circuit.ErrorEncrypter.IntermediateEncrypt(
|
|
|
|
fail.Reason,
|
|
|
|
)
|
|
|
|
}
|
2018-05-23 02:46:55 +03:00
|
|
|
} else if !isFail && circuit.Outgoing != nil {
|
2018-02-28 09:18:52 +03:00
|
|
|
// If this is an HTLC settle, and it wasn't from a
|
|
|
|
// locally initiated HTLC, then we'll log a forwarding
|
|
|
|
// event so we can flush it to disk later.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): only do this once link actually
|
|
|
|
// fully settles?
|
2017-12-06 08:23:23 +03:00
|
|
|
localHTLC := packet.incomingChanID == sourceHop
|
|
|
|
if !localHTLC {
|
2018-02-28 09:18:52 +03:00
|
|
|
s.fwdEventMtx.Lock()
|
|
|
|
s.pendingFwdingEvents = append(
|
|
|
|
s.pendingFwdingEvents,
|
|
|
|
channeldb.ForwardingEvent{
|
|
|
|
Timestamp: time.Now(),
|
2017-12-06 08:23:23 +03:00
|
|
|
IncomingChanID: circuit.Incoming.ChanID,
|
|
|
|
OutgoingChanID: circuit.Outgoing.ChanID,
|
|
|
|
AmtIn: circuit.IncomingAmount,
|
|
|
|
AmtOut: circuit.OutgoingAmount,
|
2018-02-28 09:18:52 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
s.fwdEventMtx.Unlock()
|
|
|
|
}
|
2017-06-29 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// A blank IncomingChanID in a circuit indicates that it is a pending
|
|
|
|
// user-initiated payment.
|
|
|
|
if packet.incomingChanID == sourceHop {
|
2017-12-05 04:28:16 +03:00
|
|
|
return s.handleLocalDispatch(packet)
|
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// Check to see that the source link is online before removing
|
|
|
|
// the circuit.
|
2018-05-02 02:30:30 +03:00
|
|
|
return s.mailOrchestrator.Deliver(packet.incomingChanID, packet)
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.New("wrong update type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// failAddPacket encrypts a fail packet back to an add packet's source.
|
|
|
|
// The ciphertext will be derived from the failure message proivded by context.
|
|
|
|
// This method returns the failErr if all other steps complete successfully.
|
|
|
|
func (s *Switch) failAddPacket(packet *htlcPacket,
|
|
|
|
failure lnwire.FailureMessage, failErr error) error {
|
|
|
|
|
|
|
|
// Encrypt the failure so that the sender will be able to read the error
|
|
|
|
// message. Since we failed this packet, we use EncryptFirstHop to
|
|
|
|
// obfuscate the failure for their eyes only.
|
|
|
|
reason, err := packet.obfuscator.EncryptFirstHop(failure)
|
|
|
|
if err != nil {
|
2018-07-27 13:27:11 +03:00
|
|
|
err := fmt.Errorf("unable to obfuscate "+
|
2017-12-06 08:23:23 +03:00
|
|
|
"error: %v", err)
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error(failErr)
|
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
failPkt := &htlcPacket{
|
2018-07-27 13:46:56 +03:00
|
|
|
sourceRef: packet.sourceRef,
|
2017-12-06 08:23:23 +03:00
|
|
|
incomingChanID: packet.incomingChanID,
|
|
|
|
incomingHTLCID: packet.incomingHTLCID,
|
|
|
|
circuit: packet.circuit,
|
|
|
|
htlc: &lnwire.UpdateFailHTLC{
|
|
|
|
Reason: reason,
|
|
|
|
},
|
2018-05-02 02:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Route a fail packet back to the source link.
|
|
|
|
err = s.mailOrchestrator.Deliver(failPkt.incomingChanID, failPkt)
|
|
|
|
if err != nil {
|
2018-07-27 13:27:11 +03:00
|
|
|
err = fmt.Errorf("source chanid=%v unable to "+
|
2017-12-06 08:23:23 +03:00
|
|
|
"handle switch packet: %v",
|
|
|
|
packet.incomingChanID, err)
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return failErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeCircuit accepts a settle or fail htlc and the associated htlc packet and
|
|
|
|
// attempts to determine the source that forwarded this htlc. This method will
|
|
|
|
// set the incoming chan and htlc ID of the given packet if the source was
|
|
|
|
// found, and will properly [re]encrypt any failure messages.
|
|
|
|
func (s *Switch) closeCircuit(pkt *htlcPacket) (*PaymentCircuit, error) {
|
2018-03-13 04:52:52 +03:00
|
|
|
// If the packet has its source, that means it was failed locally by
|
|
|
|
// the outgoing link. We fail it here to make sure only one response
|
|
|
|
// makes it through the switch.
|
2017-12-06 08:23:23 +03:00
|
|
|
if pkt.hasSource {
|
|
|
|
circuit, err := s.circuits.FailCircuit(pkt.inKey())
|
|
|
|
switch err {
|
|
|
|
|
|
|
|
// Circuit successfully closed.
|
|
|
|
case nil:
|
|
|
|
return circuit, nil
|
|
|
|
|
2018-03-13 04:52:52 +03:00
|
|
|
// Circuit was previously closed, but has not been deleted.
|
|
|
|
// We'll just drop this response until the circuit has been
|
|
|
|
// fully removed.
|
2017-12-06 08:23:23 +03:00
|
|
|
case ErrCircuitClosing:
|
|
|
|
return nil, err
|
|
|
|
|
2018-03-13 04:52:52 +03:00
|
|
|
// Failed to close circuit because it does not exist. This is
|
|
|
|
// likely because the circuit was already successfully closed.
|
|
|
|
// Since this packet failed locally, there is no forwarding
|
|
|
|
// package entry to acknowledge.
|
2017-12-06 08:23:23 +03:00
|
|
|
case ErrUnknownCircuit:
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
// Unexpected error.
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 04:52:52 +03:00
|
|
|
// Otherwise, this is packet was received from the remote party. Use
|
|
|
|
// circuit map to find the incoming link to receive the settle/fail.
|
2017-12-06 08:23:23 +03:00
|
|
|
circuit, err := s.circuits.CloseCircuit(pkt.outKey())
|
|
|
|
switch err {
|
|
|
|
|
|
|
|
// Open circuit successfully closed.
|
|
|
|
case nil:
|
|
|
|
pkt.incomingChanID = circuit.Incoming.ChanID
|
|
|
|
pkt.incomingHTLCID = circuit.Incoming.HtlcID
|
|
|
|
pkt.circuit = circuit
|
|
|
|
pkt.sourceRef = &circuit.AddRef
|
|
|
|
|
2018-03-13 04:52:52 +03:00
|
|
|
pktType := "SETTLE"
|
|
|
|
if _, ok := pkt.htlc.(*lnwire.UpdateFailHTLC); ok {
|
|
|
|
pktType = "FAIL"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Closed completed %s circuit for %x: "+
|
|
|
|
"(%s, %d) <-> (%s, %d)", pktType, pkt.circuit.PaymentHash,
|
|
|
|
pkt.incomingChanID, pkt.incomingHTLCID,
|
|
|
|
pkt.outgoingChanID, pkt.outgoingHTLCID)
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
return circuit, nil
|
|
|
|
|
|
|
|
// Circuit was previously closed, but has not been deleted. We'll just
|
|
|
|
// drop this response until the circuit has been removed.
|
|
|
|
case ErrCircuitClosing:
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
// Failed to close circuit because it does not exist. This is likely
|
|
|
|
// because the circuit was already successfully closed.
|
|
|
|
case ErrUnknownCircuit:
|
2018-07-27 13:27:11 +03:00
|
|
|
err := fmt.Errorf("Unable to find target channel "+
|
2017-12-06 08:23:23 +03:00
|
|
|
"for HTLC settle/fail: channel ID = %s, "+
|
|
|
|
"HTLC ID = %d", pkt.outgoingChanID,
|
|
|
|
pkt.outgoingHTLCID)
|
|
|
|
log.Error(err)
|
|
|
|
|
|
|
|
// TODO(conner): ack settle/fail
|
|
|
|
if pkt.destRef != nil {
|
|
|
|
if err := s.ackSettleFail(*pkt.destRef); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
// Unexpected error.
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 04:52:52 +03:00
|
|
|
// ackSettleFail is used by the switch to ACK any settle/fail entries in the
|
|
|
|
// forwarding package of the outgoing link for a payment circuit. We do this if
|
|
|
|
// we're the originator of the payment, so the link stops attempting to
|
|
|
|
// re-broadcast.
|
2017-12-06 08:23:23 +03:00
|
|
|
func (s *Switch) ackSettleFail(settleFailRef channeldb.SettleFailRef) error {
|
2018-11-30 07:04:21 +03:00
|
|
|
return s.cfg.DB.Batch(func(tx *bbolt.Tx) error {
|
2017-12-06 08:23:23 +03:00
|
|
|
return s.cfg.SwitchPackager.AckSettleFails(tx, settleFailRef)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// teardownCircuit removes a pending or open circuit from the switch's circuit
|
|
|
|
// map and prints useful logging statements regarding the outcome.
|
|
|
|
func (s *Switch) teardownCircuit(pkt *htlcPacket) error {
|
|
|
|
var pktType string
|
|
|
|
switch htlc := pkt.htlc.(type) {
|
|
|
|
case *lnwire.UpdateFulfillHTLC:
|
|
|
|
pktType = "SETTLE"
|
|
|
|
case *lnwire.UpdateFailHTLC:
|
|
|
|
pktType = "FAIL"
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("cannot tear down packet of type: %T", htlc)
|
|
|
|
log.Errorf(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case pkt.circuit.HasKeystone():
|
|
|
|
log.Debugf("Tearing down open circuit with %s pkt, removing circuit=%v "+
|
|
|
|
"with keystone=%v", pktType, pkt.inKey(), pkt.outKey())
|
|
|
|
|
|
|
|
err := s.circuits.DeleteCircuits(pkt.inKey())
|
2017-05-01 19:58:08 +03:00
|
|
|
if err != nil {
|
2017-12-06 08:23:23 +03:00
|
|
|
log.Warnf("Failed to tear down open circuit (%s, %d) <-> (%s, %d) "+
|
|
|
|
"with payment_hash-%v using %s pkt",
|
|
|
|
pkt.incomingChanID, pkt.incomingHTLCID,
|
|
|
|
pkt.outgoingChanID, pkt.outgoingHTLCID,
|
|
|
|
pkt.circuit.PaymentHash, pktType)
|
2017-05-01 19:58:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
log.Debugf("Closed completed %s circuit for %x: "+
|
|
|
|
"(%s, %d) <-> (%s, %d)", pktType, pkt.circuit.PaymentHash,
|
|
|
|
pkt.incomingChanID, pkt.incomingHTLCID,
|
|
|
|
pkt.outgoingChanID, pkt.outgoingHTLCID)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
default:
|
2017-12-06 08:23:23 +03:00
|
|
|
log.Debugf("Tearing down incomplete circuit with %s for inkey=%v",
|
|
|
|
pktType, pkt.inKey())
|
2018-03-13 04:52:52 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
err := s.circuits.DeleteCircuits(pkt.inKey())
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to tear down pending %s circuit for %x: "+
|
|
|
|
"(%s, %d)", pktType, pkt.circuit.PaymentHash,
|
|
|
|
pkt.incomingChanID, pkt.incomingHTLCID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Removed pending onion circuit for %x: "+
|
|
|
|
"(%s, %d)", pkt.circuit.PaymentHash,
|
|
|
|
pkt.incomingChanID, pkt.incomingHTLCID)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
return nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:28:16 +03:00
|
|
|
// CloseLink creates and sends the close channel command to the target link
|
|
|
|
// directing the specified closure type. If the closure type if CloseRegular,
|
|
|
|
// then the last parameter should be the ideal fee-per-kw that will be used as
|
|
|
|
// a starting point for close negotiation.
|
2018-02-13 16:54:54 +03:00
|
|
|
func (s *Switch) CloseLink(chanPoint *wire.OutPoint, closeType ChannelCloseType,
|
2018-12-20 20:52:27 +03:00
|
|
|
targetFeePerKw lnwallet.SatPerKWeight) (chan interface{},
|
2018-02-13 16:54:54 +03:00
|
|
|
chan error) {
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef) abstract out the close updates.
|
2018-12-20 20:52:27 +03:00
|
|
|
updateChan := make(chan interface{}, 2)
|
2017-05-01 19:58:08 +03:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
command := &ChanClose{
|
2017-11-23 10:28:16 +03:00
|
|
|
CloseType: closeType,
|
|
|
|
ChanPoint: chanPoint,
|
|
|
|
Updates: updateChan,
|
|
|
|
TargetFeePerKw: targetFeePerKw,
|
|
|
|
Err: errChan,
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.chanCloseRequests <- command:
|
|
|
|
return updateChan, errChan
|
|
|
|
|
|
|
|
case <-s.quit:
|
2018-07-27 13:29:16 +03:00
|
|
|
errChan <- ErrSwitchExiting
|
2017-05-01 19:58:08 +03:00
|
|
|
close(updateChan)
|
|
|
|
return updateChan, errChan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 02:16:52 +03:00
|
|
|
// htlcForwarder is responsible for optimally forwarding (and possibly
|
|
|
|
// fragmenting) incoming/outgoing HTLCs amongst all active interfaces and their
|
|
|
|
// links. The duties of the forwarder are similar to that of a network switch,
|
|
|
|
// in that it facilitates multi-hop payments by acting as a central messaging
|
|
|
|
// bus. The switch communicates will active links to create, manage, and tear
|
|
|
|
// down active onion routed payments. Each active channel is modeled as
|
|
|
|
// networked device with metadata such as the available payment bandwidth, and
|
|
|
|
// total link capacity.
|
|
|
|
//
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
|
|
|
func (s *Switch) htlcForwarder() {
|
2017-05-01 19:58:08 +03:00
|
|
|
defer s.wg.Done()
|
|
|
|
|
|
|
|
defer func() {
|
2018-06-01 06:31:40 +03:00
|
|
|
s.blockEpochStream.Cancel()
|
|
|
|
|
|
|
|
// Remove all links once we've been signalled for shutdown.
|
2018-07-13 03:39:27 +03:00
|
|
|
var linksToStop []ChannelLink
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.Lock()
|
2017-06-17 00:49:38 +03:00
|
|
|
for _, link := range s.linkIndex {
|
2018-07-13 03:39:27 +03:00
|
|
|
activeLink := s.removeLink(link.ChanID())
|
|
|
|
if activeLink == nil {
|
|
|
|
log.Errorf("unable to remove ChannelLink(%v) "+
|
|
|
|
"on stop", link.ChanID())
|
|
|
|
continue
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2018-07-13 03:39:27 +03:00
|
|
|
linksToStop = append(linksToStop, activeLink)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2018-05-02 02:30:30 +03:00
|
|
|
for _, link := range s.pendingLinkIndex {
|
2018-07-13 03:39:27 +03:00
|
|
|
pendingLink := s.removeLink(link.ChanID())
|
|
|
|
if pendingLink == nil {
|
|
|
|
log.Errorf("unable to remove ChannelLink(%v) "+
|
|
|
|
"on stop", link.ChanID())
|
|
|
|
continue
|
2018-05-02 02:30:30 +03:00
|
|
|
}
|
2018-07-13 03:39:27 +03:00
|
|
|
linksToStop = append(linksToStop, pendingLink)
|
2018-05-02 02:30:30 +03:00
|
|
|
}
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.Unlock()
|
2018-02-28 09:18:52 +03:00
|
|
|
|
2018-07-13 03:39:27 +03:00
|
|
|
// Now that all pending and live links have been removed from
|
|
|
|
// the forwarding indexes, stop each one before shutting down.
|
2018-08-21 08:03:56 +03:00
|
|
|
// We'll shut them down in parallel to make exiting as fast as
|
|
|
|
// possible.
|
|
|
|
var wg sync.WaitGroup
|
2018-07-13 03:39:27 +03:00
|
|
|
for _, link := range linksToStop {
|
2018-08-21 08:03:56 +03:00
|
|
|
wg.Add(1)
|
|
|
|
go func(l ChannelLink) {
|
|
|
|
defer wg.Done()
|
|
|
|
l.Stop()
|
|
|
|
}(link)
|
2018-07-13 03:39:27 +03:00
|
|
|
}
|
2018-08-21 08:03:56 +03:00
|
|
|
wg.Wait()
|
2018-07-13 03:39:27 +03:00
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
// Before we exit fully, we'll attempt to flush out any
|
|
|
|
// forwarding events that may still be lingering since the last
|
|
|
|
// batch flush.
|
|
|
|
if err := s.FlushForwardingEvents(); err != nil {
|
|
|
|
log.Errorf("unable to flush forwarding events: %v", err)
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
// TODO(roasbeef): cleared vs settled distinction
|
2017-06-01 02:16:52 +03:00
|
|
|
var (
|
|
|
|
totalNumUpdates uint64
|
|
|
|
totalSatSent btcutil.Amount
|
|
|
|
totalSatRecv btcutil.Amount
|
|
|
|
)
|
2018-08-01 03:01:19 +03:00
|
|
|
s.cfg.LogEventTicker.Resume()
|
|
|
|
defer s.cfg.LogEventTicker.Stop()
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
// Every 15 seconds, we'll flush out the forwarding events that
|
|
|
|
// occurred during that period.
|
2018-08-01 03:01:19 +03:00
|
|
|
s.cfg.FwdEventTicker.Resume()
|
|
|
|
defer s.cfg.FwdEventTicker.Stop()
|
2018-02-28 09:18:52 +03:00
|
|
|
|
2018-06-01 06:31:40 +03:00
|
|
|
out:
|
2017-05-01 19:58:08 +03:00
|
|
|
for {
|
|
|
|
select {
|
2018-06-01 06:31:40 +03:00
|
|
|
case blockEpoch, ok := <-s.blockEpochStream.Epochs:
|
|
|
|
if !ok {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.StoreUint32(&s.bestHeight, uint32(blockEpoch.Height))
|
2018-06-26 06:25:59 +03:00
|
|
|
|
2017-07-15 06:08:29 +03:00
|
|
|
// A local close request has arrived, we'll forward this to the
|
|
|
|
// relevant link (if it exists) so the channel can be
|
|
|
|
// cooperatively closed (if possible).
|
2017-05-01 19:58:08 +03:00
|
|
|
case req := <-s.chanCloseRequests:
|
2017-08-04 02:57:56 +03:00
|
|
|
chanID := lnwire.NewChanIDFromOutPoint(req.ChanPoint)
|
2018-04-04 06:06:57 +03:00
|
|
|
|
|
|
|
s.indexMtx.RLock()
|
2017-08-04 02:57:56 +03:00
|
|
|
link, ok := s.linkIndex[chanID]
|
|
|
|
if !ok {
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RUnlock()
|
|
|
|
|
2018-07-27 13:27:11 +03:00
|
|
|
req.Err <- fmt.Errorf("no peer for channel with "+
|
2018-04-05 19:52:35 +03:00
|
|
|
"chan_id=%x", chanID[:])
|
2017-08-04 02:57:56 +03:00
|
|
|
continue
|
|
|
|
}
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RUnlock()
|
2017-08-04 02:57:56 +03:00
|
|
|
|
|
|
|
peerPub := link.Peer().PubKey()
|
|
|
|
log.Debugf("Requesting local channel close: peer=%v, "+
|
|
|
|
"chan_id=%x", link.Peer(), chanID[:])
|
|
|
|
|
|
|
|
go s.cfg.LocalChannelClose(peerPub[:], req)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-01-17 07:11:01 +03:00
|
|
|
case resolutionMsg := <-s.resolutionMsgs:
|
|
|
|
pkt := &htlcPacket{
|
|
|
|
outgoingChanID: resolutionMsg.SourceChan,
|
|
|
|
outgoingHTLCID: resolutionMsg.HtlcIndex,
|
|
|
|
isResolution: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolution messages will either be cancelling
|
|
|
|
// backwards an existing HTLC, or settling a previously
|
|
|
|
// outgoing HTLC. Based on this, we'll map the message
|
|
|
|
// to the proper htlcPacket.
|
|
|
|
if resolutionMsg.Failure != nil {
|
|
|
|
pkt.htlc = &lnwire.UpdateFailHTLC{}
|
|
|
|
} else {
|
2018-02-07 06:11:11 +03:00
|
|
|
pkt.htlc = &lnwire.UpdateFulfillHTLC{
|
2018-01-17 07:11:01 +03:00
|
|
|
PaymentPreimage: *resolutionMsg.PreImage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Received outside contract resolution, "+
|
|
|
|
"mapping to: %v", spew.Sdump(pkt))
|
|
|
|
|
|
|
|
// We don't check the error, as the only failure we can
|
|
|
|
// encounter is due to the circuit already being
|
|
|
|
// closed. This is fine, as processing this message is
|
|
|
|
// meant to be idempotent.
|
|
|
|
err := s.handlePacketForward(pkt)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to forward resolution msg: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the message processed, we'll now close out
|
|
|
|
close(resolutionMsg.doneChan)
|
|
|
|
|
2017-07-15 06:08:29 +03:00
|
|
|
// A new packet has arrived for forwarding, we'll interpret the
|
|
|
|
// packet concretely, then either forward it along, or
|
|
|
|
// interpret a return packet to a locally initialized one.
|
2017-06-17 00:49:38 +03:00
|
|
|
case cmd := <-s.htlcPlex:
|
2017-10-30 22:57:32 +03:00
|
|
|
cmd.err <- s.handlePacketForward(cmd.pkt)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
// When this time ticks, then it indicates that we should
|
|
|
|
// collect all the forwarding events since the last internal,
|
|
|
|
// and write them out to our log.
|
2018-08-01 03:01:19 +03:00
|
|
|
case <-s.cfg.FwdEventTicker.Ticks():
|
2017-12-06 08:23:23 +03:00
|
|
|
s.wg.Add(1)
|
2018-02-28 09:18:52 +03:00
|
|
|
go func() {
|
2017-12-06 08:23:23 +03:00
|
|
|
defer s.wg.Done()
|
|
|
|
|
2018-02-28 09:18:52 +03:00
|
|
|
if err := s.FlushForwardingEvents(); err != nil {
|
|
|
|
log.Errorf("unable to flush "+
|
|
|
|
"forwarding events: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-06-01 02:16:52 +03:00
|
|
|
// The log ticker has fired, so we'll calculate some forwarding
|
|
|
|
// stats for the last 10 seconds to display within the logs to
|
|
|
|
// users.
|
2018-08-01 03:01:19 +03:00
|
|
|
case <-s.cfg.LogEventTicker.Ticks():
|
2017-06-01 02:16:52 +03:00
|
|
|
// First, we'll collate the current running tally of
|
|
|
|
// our forwarding stats.
|
|
|
|
prevSatSent := totalSatSent
|
|
|
|
prevSatRecv := totalSatRecv
|
|
|
|
prevNumUpdates := totalNumUpdates
|
|
|
|
|
|
|
|
var (
|
|
|
|
newNumUpdates uint64
|
|
|
|
newSatSent btcutil.Amount
|
|
|
|
newSatRecv btcutil.Amount
|
|
|
|
)
|
|
|
|
|
|
|
|
// Next, we'll run through all the registered links and
|
|
|
|
// compute their up-to-date forwarding stats.
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RLock()
|
2017-06-17 00:49:38 +03:00
|
|
|
for _, link := range s.linkIndex {
|
2017-06-01 02:16:52 +03:00
|
|
|
// TODO(roasbeef): when links first registered
|
|
|
|
// stats printed.
|
2017-05-01 19:58:08 +03:00
|
|
|
updates, sent, recv := link.Stats()
|
2017-06-01 02:16:52 +03:00
|
|
|
newNumUpdates += updates
|
2017-08-22 09:36:43 +03:00
|
|
|
newSatSent += sent.ToSatoshis()
|
|
|
|
newSatRecv += recv.ToSatoshis()
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RUnlock()
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-06-01 02:16:52 +03:00
|
|
|
var (
|
|
|
|
diffNumUpdates uint64
|
|
|
|
diffSatSent btcutil.Amount
|
|
|
|
diffSatRecv btcutil.Amount
|
|
|
|
)
|
|
|
|
|
|
|
|
// If this is the first time we're computing these
|
|
|
|
// stats, then the diff is just the new value. We do
|
|
|
|
// this in order to avoid integer underflow issues.
|
|
|
|
if prevNumUpdates == 0 {
|
|
|
|
diffNumUpdates = newNumUpdates
|
|
|
|
diffSatSent = newSatSent
|
|
|
|
diffSatRecv = newSatRecv
|
|
|
|
} else {
|
|
|
|
diffNumUpdates = newNumUpdates - prevNumUpdates
|
|
|
|
diffSatSent = newSatSent - prevSatSent
|
|
|
|
diffSatRecv = newSatRecv - prevSatRecv
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-06-01 02:16:52 +03:00
|
|
|
// If the diff of num updates is zero, then we haven't
|
|
|
|
// forwarded anything in the last 10 seconds, so we can
|
|
|
|
// skip this update.
|
2017-05-01 19:58:08 +03:00
|
|
|
if diffNumUpdates == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-03 04:23:55 +03:00
|
|
|
// If the diff of num updates is negative, then some
|
|
|
|
// links may have been unregistered from the switch, so
|
|
|
|
// we'll update our stats to only include our registered
|
|
|
|
// links.
|
|
|
|
if int64(diffNumUpdates) < 0 {
|
|
|
|
totalNumUpdates = newNumUpdates
|
|
|
|
totalSatSent = newSatSent
|
|
|
|
totalSatRecv = newSatRecv
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-01 02:16:52 +03:00
|
|
|
// Otherwise, we'll log this diff, then accumulate the
|
|
|
|
// new stats into the running total.
|
2018-11-02 03:46:03 +03:00
|
|
|
log.Debugf("Sent %d satoshis and received %d satoshis "+
|
2018-04-03 04:23:55 +03:00
|
|
|
"in the last 10 seconds (%f tx/sec)",
|
|
|
|
diffSatSent, diffSatRecv,
|
2017-07-26 01:13:12 +03:00
|
|
|
float64(diffNumUpdates)/10)
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-06-01 02:16:52 +03:00
|
|
|
totalNumUpdates += diffNumUpdates
|
|
|
|
totalSatSent += diffSatSent
|
|
|
|
totalSatRecv += diffSatRecv
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts all helper goroutines required for the operation of the switch.
|
|
|
|
func (s *Switch) Start() error {
|
|
|
|
if !atomic.CompareAndSwapInt32(&s.started, 0, 1) {
|
|
|
|
log.Warn("Htlc Switch already started")
|
2017-07-09 02:30:20 +03:00
|
|
|
return errors.New("htlc switch already started")
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-01 02:16:52 +03:00
|
|
|
log.Infof("Starting HTLC Switch")
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-08-09 10:05:27 +03:00
|
|
|
blockEpochStream, err := s.cfg.Notifier.RegisterBlockEpochNtfn(nil)
|
2018-06-01 06:31:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.blockEpochStream = blockEpochStream
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
s.wg.Add(1)
|
2017-06-01 02:16:52 +03:00
|
|
|
go s.htlcForwarder()
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
if err := s.reforwardResponses(); err != nil {
|
2018-03-12 23:49:22 +03:00
|
|
|
s.Stop()
|
2017-12-06 08:23:23 +03:00
|
|
|
log.Errorf("unable to reforward responses: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// reforwardResponses for every known, non-pending channel, loads all associated
|
|
|
|
// forwarding packages and reforwards any Settle or Fail HTLCs found. This is
|
|
|
|
// used to resurrect the switch's mailboxes after a restart.
|
|
|
|
func (s *Switch) reforwardResponses() error {
|
2018-07-27 13:33:59 +03:00
|
|
|
openChannels, err := s.cfg.DB.FetchAllOpenChannels()
|
2017-12-06 08:23:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-27 13:33:59 +03:00
|
|
|
for _, openChannel := range openChannels {
|
|
|
|
shortChanID := openChannel.ShortChanID()
|
|
|
|
|
|
|
|
// Locally-initiated payments never need reforwarding.
|
|
|
|
if shortChanID == sourceHop {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the channel is pending, it should have no forwarding
|
|
|
|
// packages, and nothing to reforward.
|
|
|
|
if openChannel.IsPending {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channels in open or waiting-close may still have responses in
|
|
|
|
// their forwarding packages. We will continue to reattempt
|
|
|
|
// forwarding on startup until the channel is fully-closed.
|
|
|
|
//
|
|
|
|
// Load this channel's forwarding packages, and deliver them to
|
|
|
|
// the switch.
|
2017-12-06 08:23:23 +03:00
|
|
|
fwdPkgs, err := s.loadChannelFwdPkgs(shortChanID)
|
|
|
|
if err != nil {
|
2018-07-27 13:33:59 +03:00
|
|
|
log.Errorf("unable to load forwarding "+
|
|
|
|
"packages for %v: %v", shortChanID, err)
|
2017-12-06 08:23:23 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.reforwardSettleFails(fwdPkgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadChannelFwdPkgs loads all forwarding packages owned by the `source` short
|
|
|
|
// channel identifier.
|
2018-04-04 06:06:57 +03:00
|
|
|
func (s *Switch) loadChannelFwdPkgs(source lnwire.ShortChannelID) ([]*channeldb.FwdPkg, error) {
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
var fwdPkgs []*channeldb.FwdPkg
|
2018-11-30 07:04:21 +03:00
|
|
|
if err := s.cfg.DB.Update(func(tx *bbolt.Tx) error {
|
2017-12-06 08:23:23 +03:00
|
|
|
var err error
|
|
|
|
fwdPkgs, err = s.cfg.SwitchPackager.LoadChannelFwdPkgs(
|
|
|
|
tx, source,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fwdPkgs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// reforwardSettleFails parses the Settle and Fail HTLCs from the list of
|
|
|
|
// forwarding packages, and reforwards those that have not been acknowledged.
|
|
|
|
// This is intended to occur on startup, in order to recover the switch's
|
|
|
|
// mailboxes, and to ensure that responses can be propagated in case the
|
|
|
|
// outgoing link never comes back online.
|
|
|
|
//
|
|
|
|
// NOTE: This should mimic the behavior processRemoteSettleFails.
|
|
|
|
func (s *Switch) reforwardSettleFails(fwdPkgs []*channeldb.FwdPkg) {
|
|
|
|
for _, fwdPkg := range fwdPkgs {
|
2019-01-10 14:23:56 +03:00
|
|
|
settleFails, err := lnwallet.PayDescsFromRemoteLogUpdates(
|
2017-12-06 08:23:23 +03:00
|
|
|
fwdPkg.Source, fwdPkg.Height, fwdPkg.SettleFails,
|
|
|
|
)
|
2019-01-10 14:23:56 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unable to process remote log updates: %v",
|
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
|
|
|
|
switchPackets := make([]*htlcPacket, 0, len(settleFails))
|
|
|
|
for i, pd := range settleFails {
|
|
|
|
|
|
|
|
// Skip any settles or fails that have already been
|
|
|
|
// acknowledged by the incoming link that originated the
|
|
|
|
// forwarded Add.
|
|
|
|
if fwdPkg.SettleFailFilter.Contains(uint16(i)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch pd.EntryType {
|
|
|
|
|
|
|
|
// A settle for an HTLC we previously forwarded HTLC has
|
|
|
|
// been received. So we'll forward the HTLC to the
|
|
|
|
// switch which will handle propagating the settle to
|
|
|
|
// the prior hop.
|
|
|
|
case lnwallet.Settle:
|
|
|
|
settlePacket := &htlcPacket{
|
|
|
|
outgoingChanID: fwdPkg.Source,
|
|
|
|
outgoingHTLCID: pd.ParentIndex,
|
|
|
|
destRef: pd.DestRef,
|
|
|
|
htlc: &lnwire.UpdateFulfillHTLC{
|
|
|
|
PaymentPreimage: pd.RPreimage,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the packet to the batch to be forwarded, and
|
|
|
|
// notify the overflow queue that a spare spot has been
|
|
|
|
// freed up within the commitment state.
|
|
|
|
switchPackets = append(switchPackets, settlePacket)
|
|
|
|
|
|
|
|
// A failureCode message for a previously forwarded HTLC has been
|
|
|
|
// received. As a result a new slot will be freed up in our
|
|
|
|
// commitment state, so we'll forward this to the switch so the
|
|
|
|
// backwards undo can continue.
|
|
|
|
case lnwallet.Fail:
|
|
|
|
// Fetch the reason the HTLC was cancelled so we can
|
|
|
|
// continue to propagate it.
|
|
|
|
failPacket := &htlcPacket{
|
|
|
|
outgoingChanID: fwdPkg.Source,
|
|
|
|
outgoingHTLCID: pd.ParentIndex,
|
|
|
|
destRef: pd.DestRef,
|
|
|
|
htlc: &lnwire.UpdateFailHTLC{
|
|
|
|
Reason: lnwire.OpaqueReason(pd.FailReason),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the packet to the batch to be forwarded, and
|
|
|
|
// notify the overflow queue that a spare spot has been
|
|
|
|
// freed up within the commitment state.
|
|
|
|
switchPackets = append(switchPackets, failPacket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 23:17:39 +03:00
|
|
|
// Since this send isn't tied to a specific link, we pass a nil
|
|
|
|
// link quit channel, meaning the send will fail only if the
|
|
|
|
// switch receives a shutdown request.
|
|
|
|
errChan := s.ForwardPackets(nil, switchPackets...)
|
2017-12-06 08:23:23 +03:00
|
|
|
go handleBatchFwdErrs(errChan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleBatchFwdErrs waits on the given errChan until it is closed, logging the
|
|
|
|
// errors returned from any unsuccessful forwarding attempts.
|
|
|
|
func handleBatchFwdErrs(errChan chan error) {
|
|
|
|
for {
|
|
|
|
err, ok := <-errChan
|
|
|
|
if !ok {
|
|
|
|
// Err chan has been drained or switch is shutting down.
|
|
|
|
// Either way, return.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf("unhandled error while reforwarding htlc "+
|
|
|
|
"settle/fail over htlcswitch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// Stop gracefully stops all active helper goroutines, then waits until they've
|
|
|
|
// exited.
|
|
|
|
func (s *Switch) Stop() error {
|
|
|
|
if !atomic.CompareAndSwapInt32(&s.shutdown, 0, 1) {
|
|
|
|
log.Warn("Htlc Switch already stopped")
|
2017-07-09 02:30:20 +03:00
|
|
|
return errors.New("htlc switch already shutdown")
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-07-09 02:30:20 +03:00
|
|
|
log.Infof("HTLC Switch shutting down")
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
close(s.quit)
|
2017-12-06 08:23:23 +03:00
|
|
|
|
2018-03-12 23:49:22 +03:00
|
|
|
s.wg.Wait()
|
|
|
|
|
|
|
|
// Wait until all active goroutines have finished exiting before
|
|
|
|
// stopping the mailboxes, otherwise the mailbox map could still be
|
|
|
|
// accessed and modified.
|
2018-05-02 02:30:30 +03:00
|
|
|
s.mailOrchestrator.Stop()
|
2017-12-06 08:23:23 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLink is used to initiate the handling of the add link command. The
|
|
|
|
// request will be propagated and handled in the main goroutine.
|
|
|
|
func (s *Switch) AddLink(link ChannelLink) error {
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.Lock()
|
|
|
|
defer s.indexMtx.Unlock()
|
2018-02-04 05:14:09 +03:00
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
chanID := link.ChanID()
|
2017-06-17 00:49:38 +03:00
|
|
|
|
2018-07-13 01:59:00 +03:00
|
|
|
// First, ensure that this link is not already active in the switch.
|
2018-06-09 14:29:03 +03:00
|
|
|
_, err := s.getLink(chanID)
|
|
|
|
if err == nil {
|
2018-07-13 01:59:00 +03:00
|
|
|
return fmt.Errorf("unable to add ChannelLink(%v), already "+
|
|
|
|
"active", chanID)
|
2018-06-09 14:29:03 +03:00
|
|
|
}
|
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// Get and attach the mailbox for this link, which buffers packets in
|
|
|
|
// case there packets that we tried to deliver while this link was
|
|
|
|
// offline.
|
|
|
|
mailbox := s.mailOrchestrator.GetOrCreateMailBox(chanID)
|
2017-12-06 08:23:23 +03:00
|
|
|
link.AttachMailBox(mailbox)
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
if err := link.Start(); err != nil {
|
2018-05-02 02:30:30 +03:00
|
|
|
s.removeLink(chanID)
|
2017-05-01 19:58:08 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
shortChanID := link.ShortChanID()
|
|
|
|
if shortChanID == sourceHop {
|
|
|
|
log.Infof("Adding pending link chan_id=%v, short_chan_id=%v",
|
|
|
|
chanID, shortChanID)
|
|
|
|
|
|
|
|
s.pendingLinkIndex[chanID] = link
|
|
|
|
} else {
|
|
|
|
log.Infof("Adding live link chan_id=%v, short_chan_id=%v",
|
|
|
|
chanID, shortChanID)
|
|
|
|
|
|
|
|
s.addLiveLink(link)
|
|
|
|
s.mailOrchestrator.BindLiveShortChanID(
|
|
|
|
mailbox, chanID, shortChanID,
|
|
|
|
)
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// addLiveLink adds a link to all associated forwarding index, this makes it a
|
|
|
|
// candidate for forwarding HTLCs.
|
|
|
|
func (s *Switch) addLiveLink(link ChannelLink) {
|
|
|
|
// We'll add the link to the linkIndex which lets us quickly
|
|
|
|
// look up a channel when we need to close or register it, and
|
|
|
|
// the forwarding index which'll be used when forwarding HTLC's
|
|
|
|
// in the multi-hop setting.
|
|
|
|
s.linkIndex[link.ChanID()] = link
|
|
|
|
s.forwardingIndex[link.ShortChanID()] = link
|
2017-12-06 08:23:23 +03:00
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// Next we'll add the link to the interface index so we can
|
|
|
|
// quickly look up all the channels for a particular node.
|
|
|
|
peerPub := link.Peer().PubKey()
|
|
|
|
if _, ok := s.interfaceIndex[peerPub]; !ok {
|
2018-06-12 09:02:07 +03:00
|
|
|
s.interfaceIndex[peerPub] = make(map[lnwire.ChannelID]ChannelLink)
|
2017-12-06 08:23:23 +03:00
|
|
|
}
|
2018-06-12 09:02:07 +03:00
|
|
|
s.interfaceIndex[peerPub][link.ChanID()] = link
|
2019-01-23 04:45:51 +03:00
|
|
|
|
|
|
|
// Inform the channel notifier if the link has become active.
|
|
|
|
if link.EligibleToForward() {
|
|
|
|
s.cfg.NotifyActiveChannel(*link.ChannelPoint())
|
|
|
|
}
|
2018-05-02 02:30:30 +03:00
|
|
|
}
|
2017-12-06 08:23:23 +03:00
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// GetLink is used to initiate the handling of the get link command. The
|
|
|
|
// request will be propagated/handled to/in the main goroutine.
|
|
|
|
func (s *Switch) GetLink(chanID lnwire.ChannelID) (ChannelLink, error) {
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RLock()
|
|
|
|
defer s.indexMtx.RUnlock()
|
2018-02-08 04:47:16 +03:00
|
|
|
|
2018-06-09 14:29:03 +03:00
|
|
|
return s.getLink(chanID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLink returns the link stored in either the pending index or the live
|
|
|
|
// lindex.
|
|
|
|
func (s *Switch) getLink(chanID lnwire.ChannelID) (ChannelLink, error) {
|
2017-06-17 00:49:38 +03:00
|
|
|
link, ok := s.linkIndex[chanID]
|
|
|
|
if !ok {
|
2018-05-02 02:30:30 +03:00
|
|
|
link, ok = s.pendingLinkIndex[chanID]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrChannelLinkNotFound
|
|
|
|
}
|
2017-06-17 00:49:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLinkByShortID attempts to return the link which possesses the target
|
|
|
|
// short channel ID.
|
2018-04-04 06:06:57 +03:00
|
|
|
//
|
|
|
|
// NOTE: This MUST be called with the indexMtx held.
|
2017-06-17 00:49:38 +03:00
|
|
|
func (s *Switch) getLinkByShortID(chanID lnwire.ShortChannelID) (ChannelLink, error) {
|
|
|
|
link, ok := s.forwardingIndex[chanID]
|
2017-05-01 19:58:08 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrChannelLinkNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 11:03:14 +03:00
|
|
|
// HasActiveLink returns true if the given channel ID has a link in the link
|
2019-01-11 03:34:02 +03:00
|
|
|
// index AND the link is eligible to forward.
|
2018-08-09 11:03:14 +03:00
|
|
|
func (s *Switch) HasActiveLink(chanID lnwire.ChannelID) bool {
|
|
|
|
s.indexMtx.RLock()
|
|
|
|
defer s.indexMtx.RUnlock()
|
|
|
|
|
2019-01-11 03:34:02 +03:00
|
|
|
if link, ok := s.linkIndex[chanID]; ok {
|
|
|
|
return link.EligibleToForward()
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2018-08-09 11:03:14 +03:00
|
|
|
}
|
|
|
|
|
2018-07-13 03:39:27 +03:00
|
|
|
// RemoveLink purges the switch of any link associated with chanID. If a pending
|
|
|
|
// or active link is not found, this method does nothing. Otherwise, the method
|
|
|
|
// returns after the link has been completely shutdown.
|
|
|
|
func (s *Switch) RemoveLink(chanID lnwire.ChannelID) {
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.Lock()
|
2018-07-13 03:39:27 +03:00
|
|
|
link := s.removeLink(chanID)
|
|
|
|
s.indexMtx.Unlock()
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-07-13 03:39:27 +03:00
|
|
|
if link != nil {
|
|
|
|
link.Stop()
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeLink is used to remove and stop the channel link.
|
2018-04-04 06:06:57 +03:00
|
|
|
//
|
|
|
|
// NOTE: This MUST be called with the indexMtx held.
|
2018-07-13 03:39:27 +03:00
|
|
|
func (s *Switch) removeLink(chanID lnwire.ChannelID) ChannelLink {
|
2017-06-17 00:49:38 +03:00
|
|
|
log.Infof("Removing channel link with ChannelID(%v)", chanID)
|
|
|
|
|
2018-06-09 14:29:03 +03:00
|
|
|
link, err := s.getLink(chanID)
|
|
|
|
if err != nil {
|
2018-07-13 03:39:27 +03:00
|
|
|
return nil
|
2018-05-02 02:30:30 +03:00
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2019-01-23 04:45:51 +03:00
|
|
|
// Inform the Channel Notifier about the link becoming inactive.
|
|
|
|
s.cfg.NotifyInactiveChannel(*link.ChannelPoint())
|
|
|
|
|
2018-06-09 14:29:03 +03:00
|
|
|
// Remove the channel from live link indexes.
|
|
|
|
delete(s.pendingLinkIndex, link.ChanID())
|
|
|
|
delete(s.linkIndex, link.ChanID())
|
|
|
|
delete(s.forwardingIndex, link.ShortChanID())
|
|
|
|
|
2018-06-12 09:06:08 +03:00
|
|
|
// If the link has been added to the peer index, then we'll move to
|
|
|
|
// delete the entry within the index.
|
2018-06-09 14:29:03 +03:00
|
|
|
peerPub := link.Peer().PubKey()
|
2018-06-12 09:06:08 +03:00
|
|
|
if peerIndex, ok := s.interfaceIndex[peerPub]; ok {
|
|
|
|
delete(peerIndex, link.ChanID())
|
|
|
|
|
|
|
|
// If after deletion, there are no longer any links, then we'll
|
|
|
|
// remove the interface map all together.
|
|
|
|
if len(peerIndex) == 0 {
|
|
|
|
delete(s.interfaceIndex, peerPub)
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 19:58:08 +03:00
|
|
|
|
2018-07-13 03:39:27 +03:00
|
|
|
return link
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2018-02-04 05:14:09 +03:00
|
|
|
// UpdateShortChanID updates the short chan ID for an existing channel. This is
|
|
|
|
// required in the case of a re-org and re-confirmation or a channel, or in the
|
|
|
|
// case that a link was added to the switch before its short chan ID was known.
|
2018-05-02 02:30:30 +03:00
|
|
|
func (s *Switch) UpdateShortChanID(chanID lnwire.ChannelID) error {
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.Lock()
|
2018-05-02 02:30:30 +03:00
|
|
|
defer s.indexMtx.Unlock()
|
2018-02-04 05:14:09 +03:00
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// Locate the target link in the pending link index. If no such link
|
|
|
|
// exists, then we will ignore the request.
|
|
|
|
link, ok := s.pendingLinkIndex[chanID]
|
2018-02-04 05:14:09 +03:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("link %v not found", chanID)
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
oldShortChanID := link.ShortChanID()
|
|
|
|
|
|
|
|
// Try to update the link's short channel ID, returning early if this
|
|
|
|
// update failed.
|
|
|
|
shortChanID, err := link.UpdateShortChanID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject any blank short channel ids.
|
|
|
|
if shortChanID == sourceHop {
|
|
|
|
return fmt.Errorf("refusing trivial short_chan_id for chan_id=%v"+
|
|
|
|
"live link", chanID)
|
|
|
|
}
|
2018-02-04 05:14:09 +03:00
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
log.Infof("Updated short_chan_id for ChannelLink(%v): old=%v, new=%v",
|
|
|
|
chanID, oldShortChanID, shortChanID)
|
2018-02-04 05:14:09 +03:00
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// Since the link was in the pending state before, we will remove it
|
|
|
|
// from the pending link index and add it to the live link index so that
|
|
|
|
// it can be available in forwarding.
|
|
|
|
delete(s.pendingLinkIndex, chanID)
|
|
|
|
s.addLiveLink(link)
|
2018-04-04 06:06:57 +03:00
|
|
|
|
2018-05-02 02:30:30 +03:00
|
|
|
// Finally, alert the mail orchestrator to the change of short channel
|
|
|
|
// ID, and deliver any unclaimed packets to the link.
|
|
|
|
mailbox := s.mailOrchestrator.GetOrCreateMailBox(chanID)
|
|
|
|
s.mailOrchestrator.BindLiveShortChanID(
|
|
|
|
mailbox, chanID, shortChanID,
|
|
|
|
)
|
2018-02-04 05:14:09 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-17 02:02:02 +03:00
|
|
|
// GetLinksByInterface fetches all the links connected to a particular node
|
|
|
|
// identified by the serialized compressed form of its public key.
|
2017-06-17 00:49:38 +03:00
|
|
|
func (s *Switch) GetLinksByInterface(hop [33]byte) ([]ChannelLink, error) {
|
2018-04-04 06:06:57 +03:00
|
|
|
s.indexMtx.RLock()
|
|
|
|
defer s.indexMtx.RUnlock()
|
2018-02-08 04:47:16 +03:00
|
|
|
|
2018-04-04 06:06:57 +03:00
|
|
|
return s.getLinks(hop)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// getLinks is function which returns the channel links of the peer by hop
|
|
|
|
// destination id.
|
2018-04-04 06:06:57 +03:00
|
|
|
//
|
|
|
|
// NOTE: This MUST be called with the indexMtx held.
|
2017-06-17 00:49:38 +03:00
|
|
|
func (s *Switch) getLinks(destination [33]byte) ([]ChannelLink, error) {
|
|
|
|
links, ok := s.interfaceIndex[destination]
|
2017-05-01 19:58:08 +03:00
|
|
|
if !ok {
|
2018-08-29 06:10:02 +03:00
|
|
|
return nil, ErrNoLinksFound
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
channelLinks := make([]ChannelLink, 0, len(links))
|
2018-06-12 09:02:07 +03:00
|
|
|
for _, link := range links {
|
2017-06-17 00:49:38 +03:00
|
|
|
channelLinks = append(channelLinks, link)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:49:38 +03:00
|
|
|
return channelLinks, nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// removePendingPayment is the helper function which removes the pending user
|
|
|
|
// payment.
|
2018-08-21 07:21:15 +03:00
|
|
|
func (s *Switch) removePendingPayment(paymentID uint64) {
|
2017-05-01 19:58:08 +03:00
|
|
|
s.pendingMutex.Lock()
|
|
|
|
defer s.pendingMutex.Unlock()
|
|
|
|
|
2017-10-30 22:57:32 +03:00
|
|
|
delete(s.pendingPayments, paymentID)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// findPayment is the helper function which find the payment.
|
2018-08-10 23:59:50 +03:00
|
|
|
func (s *Switch) findPayment(paymentID uint64) *pendingPayment {
|
2017-05-01 19:58:08 +03:00
|
|
|
s.pendingMutex.RLock()
|
|
|
|
defer s.pendingMutex.RUnlock()
|
|
|
|
|
2017-10-30 22:57:32 +03:00
|
|
|
payment, ok := s.pendingPayments[paymentID]
|
|
|
|
if !ok {
|
2018-08-10 23:59:50 +03:00
|
|
|
log.Errorf("Cannot find pending payment with ID %d",
|
2017-10-30 22:57:32 +03:00
|
|
|
paymentID)
|
2018-08-10 23:59:50 +03:00
|
|
|
return nil
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2018-08-10 23:59:50 +03:00
|
|
|
|
|
|
|
return payment
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// CircuitModifier returns a reference to subset of the interfaces provided by
|
|
|
|
// the circuit map, to allow links to open and close circuits.
|
|
|
|
func (s *Switch) CircuitModifier() CircuitModifier {
|
|
|
|
return s.circuits
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:58:08 +03:00
|
|
|
// numPendingPayments is helper function which returns the overall number of
|
|
|
|
// pending user payments.
|
|
|
|
func (s *Switch) numPendingPayments() int {
|
2018-08-21 07:21:15 +03:00
|
|
|
s.pendingMutex.RLock()
|
|
|
|
defer s.pendingMutex.RUnlock()
|
|
|
|
|
2017-10-30 22:57:32 +03:00
|
|
|
return len(s.pendingPayments)
|
2017-05-01 19:58:08 +03:00
|
|
|
}
|
2017-10-24 01:50:26 +03:00
|
|
|
|
2017-12-06 08:23:23 +03:00
|
|
|
// commitCircuits persistently adds a circuit to the switch's circuit map.
|
|
|
|
func (s *Switch) commitCircuits(circuits ...*PaymentCircuit) (
|
|
|
|
*CircuitFwdActions, error) {
|
|
|
|
|
|
|
|
return s.circuits.CommitCircuits(circuits...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// openCircuits preemptively writes the keystones for Adds that are about to be
|
|
|
|
// added to a commitment txn.
|
|
|
|
func (s *Switch) openCircuits(keystones ...Keystone) error {
|
|
|
|
return s.circuits.OpenCircuits(keystones...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteCircuits persistently removes the circuit, and keystone if present,
|
|
|
|
// from the circuit map.
|
|
|
|
func (s *Switch) deleteCircuits(inKeys ...CircuitKey) error {
|
|
|
|
return s.circuits.DeleteCircuits(inKeys...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupCircuit queries the in memory representation of the circuit map to
|
|
|
|
// retrieve a particular circuit.
|
|
|
|
func (s *Switch) lookupCircuit(inKey CircuitKey) *PaymentCircuit {
|
|
|
|
return s.circuits.LookupCircuit(inKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookupOpenCircuit queries the in-memory representation of the circuit map for a
|
|
|
|
// circuit whose outgoing circuit key matches outKey.
|
|
|
|
func (s *Switch) lookupOpenCircuit(outKey CircuitKey) *PaymentCircuit {
|
|
|
|
return s.circuits.LookupOpenCircuit(outKey)
|
2017-10-24 01:50:26 +03:00
|
|
|
}
|
2018-02-28 09:18:52 +03:00
|
|
|
|
|
|
|
// FlushForwardingEvents flushes out the set of pending forwarding events to
|
|
|
|
// the persistent log. This will be used by the switch to periodically flush
|
|
|
|
// out the set of forwarding events to disk. External callers can also use this
|
|
|
|
// method to ensure all data is flushed to dis before querying the log.
|
|
|
|
func (s *Switch) FlushForwardingEvents() error {
|
|
|
|
// First, we'll obtain a copy of the current set of pending forwarding
|
|
|
|
// events.
|
|
|
|
s.fwdEventMtx.Lock()
|
|
|
|
|
|
|
|
// If we won't have any forwarding events, then we can exit early.
|
|
|
|
if len(s.pendingFwdingEvents) == 0 {
|
|
|
|
s.fwdEventMtx.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
events := make([]channeldb.ForwardingEvent, len(s.pendingFwdingEvents))
|
|
|
|
copy(events[:], s.pendingFwdingEvents[:])
|
|
|
|
|
|
|
|
// With the copy obtained, we can now clear out the header pointer of
|
|
|
|
// the current slice. This way, we can re-use the underlying storage
|
|
|
|
// allocated for the slice.
|
|
|
|
s.pendingFwdingEvents = s.pendingFwdingEvents[:0]
|
|
|
|
s.fwdEventMtx.Unlock()
|
|
|
|
|
|
|
|
// Finally, we'll write out the copied events to the persistent
|
|
|
|
// forwarding log.
|
|
|
|
return s.cfg.FwdingLog.AddForwardingEvents(events)
|
|
|
|
}
|
2018-06-01 06:31:40 +03:00
|
|
|
|
|
|
|
// BestHeight returns the best height known to the switch.
|
|
|
|
func (s *Switch) BestHeight() uint32 {
|
|
|
|
return atomic.LoadUint32(&s.bestHeight)
|
|
|
|
}
|