2016-06-21 22:32:32 +03:00
|
|
|
package main
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2016-07-22 02:21:27 +03:00
|
|
|
"time"
|
2016-07-10 02:35:33 +03:00
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
"github.com/btcsuite/fastsha256"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/lightningnetwork/lightning-onion"
|
2016-07-10 02:35:33 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-08-31 02:52:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2016-07-10 02:35:33 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2016-09-26 20:35:10 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2017-01-06 00:56:27 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2016-07-10 02:35:33 +03:00
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2016-11-29 05:44:14 +03:00
|
|
|
"golang.org/x/crypto/ripemd160"
|
2016-07-10 02:35:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// htlcQueueSize...
|
|
|
|
// buffer bloat ;)
|
2016-07-22 02:21:27 +03:00
|
|
|
htlcQueueSize = 50
|
2016-07-10 02:35:33 +03:00
|
|
|
)
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// link represents an active channel capable of forwarding HTLCs. Each
|
2016-07-10 02:35:33 +03:00
|
|
|
// active channel registered with the htlc switch creates a new link which will
|
2017-01-13 08:01:50 +03:00
|
|
|
// be used for forwarding outgoing HTLCs. The link also has additional
|
|
|
|
// metadata such as the current available bandwidth of the link (in satoshis)
|
|
|
|
// which aid the switch in optimally forwarding HTLCs.
|
2016-07-10 02:35:33 +03:00
|
|
|
type link struct {
|
|
|
|
capacity btcutil.Amount
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
availableBandwidth int64 // atomic
|
2016-07-10 02:35:33 +03:00
|
|
|
|
2016-07-17 04:20:13 +03:00
|
|
|
linkChan chan *htlcPacket
|
2016-07-10 02:35:33 +03:00
|
|
|
|
|
|
|
peer *peer
|
|
|
|
|
|
|
|
chanPoint *wire.OutPoint
|
|
|
|
}
|
|
|
|
|
2016-07-22 02:21:27 +03:00
|
|
|
// htlcPacket is a wrapper around an lnwire message which adds, times out, or
|
2016-07-10 02:35:33 +03:00
|
|
|
// settles an active HTLC. The dest field denotes the name of the interface to
|
|
|
|
// forward this htlcPacket on.
|
|
|
|
type htlcPacket struct {
|
2016-09-22 05:41:26 +03:00
|
|
|
sync.RWMutex
|
|
|
|
|
2017-01-06 00:56:27 +03:00
|
|
|
dest chainhash.Hash
|
2016-07-10 02:35:33 +03:00
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
srcLink wire.OutPoint
|
|
|
|
onion *sphinx.ProcessedPacket
|
|
|
|
|
2016-07-13 03:40:32 +03:00
|
|
|
msg lnwire.Message
|
2017-01-08 07:23:06 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): refactor and add type to pkt message
|
|
|
|
payHash [32]byte
|
|
|
|
amt btcutil.Amount
|
2016-07-17 04:20:13 +03:00
|
|
|
|
|
|
|
err chan error
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
// circuitKey uniquely identifies an active Sphinx (onion routing) circuit
|
|
|
|
// between two open channels. Currently, the rHash of the HTLC which created
|
|
|
|
// the circuit is used to uniquely identify each circuit.
|
2016-09-26 20:35:10 +03:00
|
|
|
// TODO(roasbeef): need to also add in the settle/clear channel points in order
|
|
|
|
// to support fragmenting payments on the link layer: 1 to N, N to N, etc.
|
2016-09-22 05:41:26 +03:00
|
|
|
type circuitKey [32]byte
|
|
|
|
|
|
|
|
// paymentCircuit represents an active Sphinx (onion routing) circuit between
|
|
|
|
// two active links within the htlcSwitch. A payment circuit is created once a
|
2017-01-08 07:23:06 +03:00
|
|
|
// link forwards an HTLC add request which initiates the creation of the
|
|
|
|
// circuit. The onion routing information contained within this message is
|
|
|
|
// used to identify the settle/clear ends of the circuit. A circuit may be
|
2017-01-13 08:01:50 +03:00
|
|
|
// re-used (not torndown) in the case that multiple HTLCs with the send RHash
|
2017-01-08 07:23:06 +03:00
|
|
|
// are sent.
|
2016-09-22 05:41:26 +03:00
|
|
|
type paymentCircuit struct {
|
|
|
|
// TODO(roasbeef): add reference count so know when to delete?
|
|
|
|
// * atomic int re
|
|
|
|
// * due to same r-value being re-used?
|
|
|
|
|
|
|
|
refCount uint32
|
|
|
|
|
|
|
|
// clear is the link the htlcSwitch will forward the HTLC add message
|
|
|
|
// that initiated the circuit to. Once the message is forwarded, the
|
|
|
|
// payment circuit is considered "active" from the POV of the switch as
|
|
|
|
// both the incoming/outgoing channels have the cleared HTLC within
|
|
|
|
// their latest state.
|
|
|
|
clear *link
|
|
|
|
|
|
|
|
// settle is the link the htlcSwitch will forward the HTLC settle it
|
|
|
|
// receives from the outgoing peer to. Once the switch forwards the
|
|
|
|
// settle message to this link, the payment circuit is considered
|
|
|
|
// complete unless the reference count on the circuit is greater than
|
|
|
|
// 1.
|
|
|
|
settle *link
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// htlcSwitch is a central messaging bus for all incoming/outgoing HTLCs.
|
2016-07-10 02:35:33 +03:00
|
|
|
// Connected peers with active channels are treated as named interfaces which
|
2017-01-08 07:14:50 +03:00
|
|
|
// refer to active channels as links. A link is the switch's message
|
2016-07-10 02:35:33 +03:00
|
|
|
// 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
|
2017-01-13 08:01:50 +03:00
|
|
|
// HTLCs, forwarding HTLCs initiated from within the daemon, and additionally
|
|
|
|
// splitting up incoming/outgoing HTLCs to a particular interface amongst many
|
2016-07-10 02:35:33 +03:00
|
|
|
// links (payment fragmentation).
|
2016-09-22 05:41:26 +03:00
|
|
|
// TODO(roasbeef): active sphinx circuits need to be synced to disk
|
2016-07-10 02:35:33 +03:00
|
|
|
type htlcSwitch struct {
|
|
|
|
started int32 // atomic
|
|
|
|
shutdown int32 // atomic
|
|
|
|
|
2016-09-12 22:42:26 +03:00
|
|
|
// chanIndex maps a channel's outpoint to a link which contains
|
|
|
|
// additional information about the channel, and additionally houses a
|
2017-01-08 07:14:50 +03:00
|
|
|
// pointer to the peer managing the channel.
|
2016-09-22 05:41:26 +03:00
|
|
|
chanIndexMtx sync.RWMutex
|
|
|
|
chanIndex map[wire.OutPoint]*link
|
2016-09-12 22:42:26 +03:00
|
|
|
|
|
|
|
// interfaces maps a node's ID to the set of links (active channels) we
|
|
|
|
// currently have open with that peer.
|
2016-09-22 05:41:26 +03:00
|
|
|
// TODO(roasbeef): combine w/ onionIndex?
|
|
|
|
interfaceMtx sync.RWMutex
|
2017-01-06 00:56:27 +03:00
|
|
|
interfaces map[chainhash.Hash][]*link
|
2016-09-22 05:41:26 +03:00
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// onionIndex is an index used to properly forward a message
|
|
|
|
// to the next hop within a Sphinx circuit. Within the sphinx packets,
|
|
|
|
// the "next-hop" destination is encoded as the hash160 of the node's
|
|
|
|
// public key serialized in compressed format.
|
2016-09-22 05:41:26 +03:00
|
|
|
onionMtx sync.RWMutex
|
|
|
|
onionIndex map[[ripemd160.Size]byte][]*link
|
|
|
|
|
|
|
|
// paymentCircuits maps a circuit key to an active payment circuit
|
|
|
|
// amongst two oepn channels. This map is used to properly clear/settle
|
|
|
|
// onion routed payments within the network.
|
|
|
|
paymentCircuits map[circuitKey]*paymentCircuit
|
|
|
|
|
|
|
|
// linkControl is a channel used by connected links to notify the
|
|
|
|
// switch of a non-multi-hop triggered link state update.
|
2016-07-10 02:35:33 +03:00
|
|
|
linkControl chan interface{}
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
// outgoingPayments is a channel that outgoing payments initiated by
|
|
|
|
// the RPC system.
|
2016-07-10 02:35:33 +03:00
|
|
|
outgoingPayments chan *htlcPacket
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// htlcPlex is the channel which all connected links use to
|
|
|
|
// coordinate the setup/teardown of Sphinx (onion routing) payment
|
2016-09-22 05:41:26 +03:00
|
|
|
// circuits. Active links forward any add/settle messages over this
|
|
|
|
// channel each state transition, sending new adds/settles which are
|
|
|
|
// fully locked in.
|
2016-07-13 03:40:32 +03:00
|
|
|
htlcPlex chan *htlcPacket
|
|
|
|
|
2016-07-22 02:21:27 +03:00
|
|
|
// TODO(roasbeef): sampler to log sat/sec and tx/sec
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newHtlcSwitch creates a new htlcSwitch.
|
2016-12-27 08:42:23 +03:00
|
|
|
func newHtlcSwitch() *htlcSwitch {
|
2016-07-10 02:35:33 +03:00
|
|
|
return &htlcSwitch{
|
|
|
|
chanIndex: make(map[wire.OutPoint]*link),
|
2017-01-06 00:56:27 +03:00
|
|
|
interfaces: make(map[chainhash.Hash][]*link),
|
2016-09-22 05:41:26 +03:00
|
|
|
onionIndex: make(map[[ripemd160.Size]byte][]*link),
|
|
|
|
paymentCircuits: make(map[circuitKey]*paymentCircuit),
|
2016-07-10 02:35:33 +03:00
|
|
|
linkControl: make(chan interface{}),
|
2016-07-13 03:40:32 +03:00
|
|
|
htlcPlex: make(chan *htlcPacket, htlcQueueSize),
|
2016-07-22 02:21:27 +03:00
|
|
|
outgoingPayments: make(chan *htlcPacket, htlcQueueSize),
|
2016-08-04 08:25:32 +03:00
|
|
|
quit: make(chan struct{}),
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts all helper goroutines required for the operation of the switch.
|
|
|
|
func (h *htlcSwitch) Start() error {
|
|
|
|
if !atomic.CompareAndSwapInt32(&h.started, 0, 1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-29 05:44:14 +03:00
|
|
|
hswcLog.Tracef("Starting HTLC switch")
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
h.wg.Add(2)
|
|
|
|
go h.networkAdmin()
|
|
|
|
go h.htlcForwarder()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop gracefully stops all active helper goroutines, then waits until they've
|
|
|
|
// exited.
|
|
|
|
func (h *htlcSwitch) Stop() error {
|
|
|
|
if !atomic.CompareAndSwapInt32(&h.shutdown, 0, 1) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-29 05:44:14 +03:00
|
|
|
hswcLog.Infof("HLTC switch shutting down")
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
close(h.quit)
|
|
|
|
h.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-13 03:40:32 +03:00
|
|
|
// SendHTLC queues a HTLC packet for forwarding over the designated interface.
|
|
|
|
// In the event that the interface has insufficient capacity for the payment,
|
|
|
|
// an error is returned. Additionally, if the interface cannot be found, an
|
|
|
|
// alternative error is returned.
|
|
|
|
func (h *htlcSwitch) SendHTLC(htlcPkt *htlcPacket) error {
|
2016-07-17 04:20:13 +03:00
|
|
|
htlcPkt.err = make(chan error, 1)
|
|
|
|
|
2016-07-13 03:40:32 +03:00
|
|
|
h.outgoingPayments <- htlcPkt
|
2016-07-17 04:20:13 +03:00
|
|
|
|
|
|
|
return <-htlcPkt.err
|
2016-07-13 03:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
// htlcForwarder is responsible for optimally forwarding (and possibly
|
2017-01-13 08:01:50 +03:00
|
|
|
// fragmenting) incoming/outgoing HTLCs amongst all active interfaces and
|
2016-07-25 03:03:40 +03:00
|
|
|
// 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
|
2016-09-22 05:41:26 +03:00
|
|
|
// messaging bus. The switch communicates will active links to create, manage,
|
2017-01-13 08:01:50 +03:00
|
|
|
// and tear down active onion routed payments. Each active channel is modeled
|
|
|
|
// as networked device with metadata such as the available payment bandwidth,
|
2016-09-22 05:41:26 +03:00
|
|
|
// and total link capacity.
|
2016-07-10 02:35:33 +03:00
|
|
|
func (h *htlcSwitch) htlcForwarder() {
|
2016-07-17 04:20:13 +03:00
|
|
|
// TODO(roasbeef): track pending payments here instead of within each peer?
|
2016-09-22 05:41:26 +03:00
|
|
|
// Examine settles/timeouts from htlcPlex. Add src to htlcPacket, key by
|
2016-07-17 04:20:13 +03:00
|
|
|
// (src, htlcKey).
|
2016-07-22 02:21:27 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): cleared vs settled distinction
|
|
|
|
var numUpdates uint64
|
|
|
|
var satSent, satRecv btcutil.Amount
|
|
|
|
logTicker := time.NewTicker(10 * time.Second)
|
2016-07-10 02:35:33 +03:00
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2016-07-13 03:40:32 +03:00
|
|
|
case htlcPkt := <-h.outgoingPayments:
|
2016-07-25 03:03:40 +03:00
|
|
|
dest := htlcPkt.dest
|
2016-09-22 05:41:26 +03:00
|
|
|
h.interfaceMtx.RLock()
|
2016-07-25 03:03:40 +03:00
|
|
|
chanInterface, ok := h.interfaces[dest]
|
2016-09-22 05:41:26 +03:00
|
|
|
h.interfaceMtx.RUnlock()
|
2016-07-13 03:40:32 +03:00
|
|
|
if !ok {
|
2016-09-22 05:41:26 +03:00
|
|
|
err := fmt.Errorf("Unable to locate link %x",
|
|
|
|
dest[:])
|
2016-07-17 04:20:13 +03:00
|
|
|
hswcLog.Errorf(err.Error())
|
|
|
|
htlcPkt.err <- err
|
2016-07-13 03:40:32 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-02-16 15:41:16 +03:00
|
|
|
wireMsg := htlcPkt.msg.(*lnwire.UpdateAddHTLC)
|
2016-07-13 03:40:32 +03:00
|
|
|
amt := btcutil.Amount(wireMsg.Amount)
|
|
|
|
|
2016-07-25 03:03:40 +03:00
|
|
|
// Handle this send request in a distinct goroutine in
|
|
|
|
// order to avoid a possible deadlock between the htlc
|
|
|
|
// switch and channel's htlc manager.
|
2016-07-13 03:40:32 +03:00
|
|
|
for _, link := range chanInterface {
|
|
|
|
// TODO(roasbeef): implement HTLC fragmentation
|
2016-07-25 03:03:40 +03:00
|
|
|
// * avoid full channel depletion at higher
|
|
|
|
// level (here) instead of within state
|
|
|
|
// machine?
|
2017-01-08 07:15:17 +03:00
|
|
|
if atomic.LoadInt64(&link.availableBandwidth) < int64(amt) {
|
2016-07-25 03:03:40 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:32:27 +03:00
|
|
|
hswcLog.Tracef("Sending %v to %x", amt, dest[:])
|
2016-07-13 03:40:32 +03:00
|
|
|
|
2016-07-25 03:03:40 +03:00
|
|
|
go func() {
|
2016-07-17 04:20:13 +03:00
|
|
|
link.linkChan <- htlcPkt
|
2016-07-25 03:03:40 +03:00
|
|
|
}()
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
n := atomic.AddInt64(&link.availableBandwidth,
|
|
|
|
-int64(amt))
|
|
|
|
hswcLog.Tracef("Decrementing link %v bandwidth to %v",
|
|
|
|
link.chanPoint, n)
|
2016-07-13 03:40:32 +03:00
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
continue out
|
2016-07-13 03:40:32 +03:00
|
|
|
}
|
2016-07-25 03:03:40 +03:00
|
|
|
|
|
|
|
hswcLog.Errorf("Unable to send payment, insufficient capacity")
|
|
|
|
htlcPkt.err <- fmt.Errorf("Insufficient capacity")
|
2016-07-22 02:21:27 +03:00
|
|
|
case pkt := <-h.htlcPlex:
|
|
|
|
// TODO(roasbeef): properly account with cleared vs settled
|
2016-09-22 05:41:26 +03:00
|
|
|
numUpdates += 1
|
|
|
|
|
|
|
|
hswcLog.Tracef("plex packet: %v", newLogClosure(func() string {
|
|
|
|
return spew.Sdump(pkt)
|
|
|
|
}))
|
|
|
|
|
|
|
|
switch wireMsg := pkt.msg.(type) {
|
|
|
|
// A link has just forwarded us a new HTLC, therefore
|
|
|
|
// we initiate the payment circuit within our internal
|
2017-02-16 15:41:16 +03:00
|
|
|
// state so we can properly forward the ultimate settle
|
|
|
|
// message.
|
|
|
|
case *lnwire.UpdateAddHTLC:
|
|
|
|
payHash := wireMsg.PaymentHash
|
2017-01-08 07:23:06 +03:00
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
// Create the two ends of the payment circuit
|
|
|
|
// required to ensure completion of this new
|
|
|
|
// payment.
|
|
|
|
nextHop := pkt.onion.NextHop
|
|
|
|
h.onionMtx.RLock()
|
|
|
|
clearLink, ok := h.onionIndex[nextHop]
|
|
|
|
h.onionMtx.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
hswcLog.Errorf("unable to find dest end of "+
|
|
|
|
"circuit: %x", nextHop)
|
2017-01-08 07:23:06 +03:00
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// We were unable to locate the
|
2017-01-08 07:23:06 +03:00
|
|
|
// next-hop as encoded within the
|
|
|
|
// Sphinx packet. Therefore, we send a
|
|
|
|
// cancellation message back to the
|
|
|
|
// source of the packet so they can
|
|
|
|
// propagate the message back to the
|
|
|
|
// origin.
|
|
|
|
cancelPkt := &htlcPacket{
|
|
|
|
payHash: payHash,
|
2017-02-16 15:41:16 +03:00
|
|
|
msg: &lnwire.UpdateFailHTLC{
|
|
|
|
Reason: []byte{uint8(lnwire.UnknownDestination)},
|
2017-01-08 07:23:06 +03:00
|
|
|
},
|
|
|
|
err: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
h.chanIndexMtx.RLock()
|
|
|
|
cancelLink := h.chanIndex[pkt.srcLink]
|
|
|
|
h.chanIndexMtx.RUnlock()
|
|
|
|
|
|
|
|
cancelLink.linkChan <- cancelPkt
|
2016-09-22 05:41:26 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
h.chanIndexMtx.RLock()
|
|
|
|
settleLink := h.chanIndex[pkt.srcLink]
|
|
|
|
h.chanIndexMtx.RUnlock()
|
|
|
|
|
2017-01-08 07:23:06 +03:00
|
|
|
// If the link we're attempting to forward the
|
|
|
|
// HTLC over has insufficient capacity, then
|
|
|
|
// we'll cancel the HTLC as the payment cannot
|
|
|
|
// succeed.
|
|
|
|
linkBandwidth := atomic.LoadInt64(&clearLink[0].availableBandwidth)
|
|
|
|
if linkBandwidth < int64(wireMsg.Amount) {
|
|
|
|
hswcLog.Errorf("unable to forward HTLC "+
|
|
|
|
"link %v has insufficient "+
|
|
|
|
"capacity, have %v need %v",
|
|
|
|
clearLink[0].chanPoint, linkBandwidth,
|
|
|
|
int64(wireMsg.Amount))
|
|
|
|
|
|
|
|
pkt := &htlcPacket{
|
|
|
|
payHash: payHash,
|
2017-02-16 15:41:16 +03:00
|
|
|
msg: &lnwire.UpdateFailHTLC{
|
|
|
|
Reason: []byte{uint8(lnwire.InsufficientCapacity)},
|
2017-01-08 07:23:06 +03:00
|
|
|
},
|
|
|
|
err: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
settleLink.linkChan <- pkt
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
circuit := &paymentCircuit{
|
|
|
|
clear: clearLink[0],
|
|
|
|
settle: settleLink,
|
|
|
|
}
|
|
|
|
|
2017-02-16 15:41:16 +03:00
|
|
|
cKey := circuitKey(wireMsg.PaymentHash)
|
2016-09-22 05:41:26 +03:00
|
|
|
h.paymentCircuits[cKey] = circuit
|
|
|
|
|
|
|
|
hswcLog.Debugf("Creating onion circuit for %x: %v<->%v",
|
|
|
|
cKey[:], clearLink[0].chanPoint,
|
|
|
|
settleLink.chanPoint)
|
|
|
|
|
|
|
|
// With the circuit initiated, send the htlcPkt
|
|
|
|
// to the clearing link within the circuit to
|
2016-10-26 15:25:42 +03:00
|
|
|
// continue propagating the HTLC across the
|
2016-09-22 05:41:26 +03:00
|
|
|
// network.
|
|
|
|
circuit.clear.linkChan <- &htlcPacket{
|
|
|
|
msg: wireMsg,
|
|
|
|
err: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce the available bandwidth for the link
|
|
|
|
// as it will clear the above HTLC, increasing
|
|
|
|
// the limbo balance within the channel.
|
2016-09-26 20:35:10 +03:00
|
|
|
n := atomic.AddInt64(&circuit.clear.availableBandwidth,
|
|
|
|
-int64(pkt.amt))
|
2016-09-22 05:41:26 +03:00
|
|
|
hswcLog.Tracef("Decrementing link %v bandwidth to %v",
|
|
|
|
circuit.clear.chanPoint, n)
|
|
|
|
|
2016-07-22 02:21:27 +03:00
|
|
|
satRecv += pkt.amt
|
2016-09-22 05:41:26 +03:00
|
|
|
|
|
|
|
// We've just received a settle message which means we
|
|
|
|
// can finalize the payment circuit by forwarding the
|
|
|
|
// settle msg to the link which initially created the
|
|
|
|
// circuit.
|
2017-02-16 15:41:16 +03:00
|
|
|
case *lnwire.UpdateFufillHTLC:
|
|
|
|
rHash := fastsha256.Sum256(wireMsg.PaymentPreimage[:])
|
2016-09-22 05:41:26 +03:00
|
|
|
var cKey circuitKey
|
|
|
|
copy(cKey[:], rHash[:])
|
|
|
|
|
|
|
|
// If we initiated the payment then there won't
|
2016-09-26 20:35:10 +03:00
|
|
|
// be an active circuit to continue propagating
|
2016-09-22 05:41:26 +03:00
|
|
|
// the settle over. Therefore, we exit early.
|
|
|
|
circuit, ok := h.paymentCircuits[cKey]
|
|
|
|
if !ok {
|
|
|
|
hswcLog.Debugf("No existing circuit "+
|
2017-01-08 07:14:50 +03:00
|
|
|
"for %x to settle", rHash[:])
|
2016-09-22 05:41:26 +03:00
|
|
|
satSent += pkt.amt
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hswcLog.Debugf("Closing completed onion "+
|
|
|
|
"circuit for %x: %v<->%v", rHash[:],
|
|
|
|
circuit.clear.chanPoint,
|
|
|
|
circuit.settle.chanPoint)
|
|
|
|
|
|
|
|
circuit.settle.linkChan <- &htlcPacket{
|
|
|
|
msg: wireMsg,
|
|
|
|
err: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increase the available bandwidth for the
|
|
|
|
// link as it will settle the above HTLC,
|
2017-01-08 07:14:50 +03:00
|
|
|
// subtracting from the limbo balance and
|
2016-09-22 05:41:26 +03:00
|
|
|
// incrementing its local balance.
|
|
|
|
n := atomic.AddInt64(&circuit.settle.availableBandwidth,
|
|
|
|
int64(pkt.amt))
|
|
|
|
hswcLog.Tracef("Incrementing link %v bandwidth to %v",
|
|
|
|
circuit.settle.chanPoint, n)
|
|
|
|
|
2016-07-22 02:21:27 +03:00
|
|
|
satSent += pkt.amt
|
2017-01-08 07:23:06 +03:00
|
|
|
|
|
|
|
delete(h.paymentCircuits, cKey)
|
|
|
|
|
|
|
|
// We've just received an HTLC cancellation triggered
|
|
|
|
// by an upstream peer somewhere within the ultimate
|
|
|
|
// route. In response, we'll terminate the payment
|
|
|
|
// circuit and propagate the error backwards.
|
2017-02-16 15:41:16 +03:00
|
|
|
case *lnwire.UpdateFailHTLC:
|
2017-01-13 08:01:50 +03:00
|
|
|
// In order to properly handle the error, we'll
|
2017-01-08 07:23:06 +03:00
|
|
|
// need to look up the original circuit that
|
|
|
|
// the incoming HTLC created.
|
|
|
|
circuit, ok := h.paymentCircuits[pkt.payHash]
|
|
|
|
if !ok {
|
|
|
|
hswcLog.Debugf("No existing circuit "+
|
|
|
|
"for %x to cancel", pkt.payHash)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since an outgoing HTLC we sent on the clear
|
2017-01-13 08:01:50 +03:00
|
|
|
// link has been cancelled, we update the
|
2017-01-08 07:23:06 +03:00
|
|
|
// bandwidth of the clear link, restoring the
|
|
|
|
// value of the HTLC worth.
|
|
|
|
n := atomic.AddInt64(&circuit.clear.availableBandwidth,
|
|
|
|
int64(pkt.amt))
|
|
|
|
hswcLog.Debugf("HTLC %x has been cancelled, "+
|
|
|
|
"incrementing link %v bandwidth to %v", pkt.payHash,
|
|
|
|
circuit.clear.chanPoint, n)
|
|
|
|
|
|
|
|
// With our link info updated, we now continue
|
|
|
|
// the error propagation by sending the
|
|
|
|
// cancellation message over the link that sent
|
|
|
|
// us the incoming HTLC.
|
|
|
|
circuit.settle.linkChan <- &htlcPacket{
|
|
|
|
msg: wireMsg,
|
|
|
|
payHash: pkt.payHash,
|
|
|
|
err: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(h.paymentCircuits, pkt.payHash)
|
2016-07-22 02:21:27 +03:00
|
|
|
}
|
|
|
|
case <-logTicker.C:
|
|
|
|
if numUpdates == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
hswcLog.Infof("Sent %v satoshis, received %v satoshis in "+
|
2016-07-22 02:21:27 +03:00
|
|
|
"the last 10 seconds (%v tx/sec)",
|
|
|
|
satSent.ToUnit(btcutil.AmountSatoshi),
|
|
|
|
satRecv.ToUnit(btcutil.AmountSatoshi),
|
|
|
|
float64(numUpdates)/10)
|
|
|
|
satSent = 0
|
|
|
|
satRecv = 0
|
|
|
|
numUpdates = 0
|
2016-07-10 02:35:33 +03:00
|
|
|
case <-h.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.wg.Done()
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// networkAdmin is responsible for handling requests to register, unregister,
|
|
|
|
// and close any link. In the event that an unregister request leaves an
|
2016-07-10 02:35:33 +03:00
|
|
|
// interface with no active links, that interface is garbage collected.
|
|
|
|
func (h *htlcSwitch) networkAdmin() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-h.linkControl:
|
|
|
|
switch req := msg.(type) {
|
|
|
|
case *closeLinkReq:
|
|
|
|
h.handleCloseLink(req)
|
|
|
|
case *registerLinkMsg:
|
|
|
|
h.handleRegisterLink(req)
|
|
|
|
case *unregisterLinkMsg:
|
|
|
|
h.handleUnregisterLink(req)
|
2016-08-26 02:30:22 +03:00
|
|
|
case *linkInfoUpdateMsg:
|
|
|
|
h.handleLinkUpdate(req)
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
case <-h.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleRegisterLink registers a new link within the channel index, and also
|
|
|
|
// adds the link to the existing set of links for the target interface.
|
|
|
|
func (h *htlcSwitch) handleRegisterLink(req *registerLinkMsg) {
|
|
|
|
chanPoint := req.linkInfo.ChannelPoint
|
|
|
|
newLink := &link{
|
|
|
|
capacity: req.linkInfo.Capacity,
|
2016-09-22 05:41:26 +03:00
|
|
|
availableBandwidth: int64(req.linkInfo.LocalBalance),
|
2016-07-10 02:35:33 +03:00
|
|
|
linkChan: req.linkChan,
|
|
|
|
peer: req.peer,
|
|
|
|
chanPoint: chanPoint,
|
|
|
|
}
|
2016-09-22 05:41:26 +03:00
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// First update the channel index with this new channel point. The
|
|
|
|
// channel index will be used to quickly lookup channels in order to:
|
|
|
|
// close them, update their link capacity, or possibly during multi-hop
|
|
|
|
// HTLC forwarding.
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.Lock()
|
2016-07-10 02:35:33 +03:00
|
|
|
h.chanIndex[*chanPoint] = newLink
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.Unlock()
|
2016-07-10 02:35:33 +03:00
|
|
|
|
|
|
|
interfaceID := req.peer.lightningID
|
2016-09-22 05:41:26 +03:00
|
|
|
|
|
|
|
h.interfaceMtx.Lock()
|
2016-07-10 02:35:33 +03:00
|
|
|
h.interfaces[interfaceID] = append(h.interfaces[interfaceID], newLink)
|
2016-09-22 05:41:26 +03:00
|
|
|
h.interfaceMtx.Unlock()
|
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// Next, update the onion index which is used to look up the
|
|
|
|
// settle/clear links during multi-hop payments and to dispatch
|
2017-01-13 08:01:50 +03:00
|
|
|
// outgoing payments initiated by a local subsystem.
|
2016-09-22 05:41:26 +03:00
|
|
|
var onionId [ripemd160.Size]byte
|
2016-10-28 05:49:10 +03:00
|
|
|
copy(onionId[:], btcutil.Hash160(req.peer.addr.IdentityKey.SerializeCompressed()))
|
2016-07-10 02:35:33 +03:00
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
h.onionMtx.Lock()
|
|
|
|
h.onionIndex[onionId] = h.interfaces[interfaceID]
|
|
|
|
h.onionMtx.Unlock()
|
|
|
|
|
|
|
|
hswcLog.Infof("registering new link, interface=%x, onion_link=%x, "+
|
|
|
|
"chan_point=%v, capacity=%v", interfaceID[:], onionId,
|
|
|
|
chanPoint, newLink.capacity)
|
2016-07-10 02:35:33 +03:00
|
|
|
|
|
|
|
if req.done != nil {
|
|
|
|
req.done <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleUnregisterLink unregisters a currently active link. If the deletion of
|
|
|
|
// this link leaves the interface empty, then the interface entry itself is
|
|
|
|
// also deleted.
|
|
|
|
func (h *htlcSwitch) handleUnregisterLink(req *unregisterLinkMsg) {
|
2016-11-29 05:44:14 +03:00
|
|
|
hswcLog.Debugf("unregistering active link, interface=%v, chan_point=%v",
|
2016-07-10 02:35:33 +03:00
|
|
|
hex.EncodeToString(req.chanInterface[:]), req.chanPoint)
|
|
|
|
|
|
|
|
chanInterface := req.chanInterface
|
2016-09-22 05:41:26 +03:00
|
|
|
|
|
|
|
h.interfaceMtx.RLock()
|
2016-07-10 02:35:33 +03:00
|
|
|
links := h.interfaces[chanInterface]
|
2016-09-22 05:41:26 +03:00
|
|
|
h.interfaceMtx.RUnlock()
|
2016-07-14 02:35:47 +03:00
|
|
|
|
2017-01-08 07:14:50 +03:00
|
|
|
h.chanIndexMtx.Lock()
|
|
|
|
defer h.chanIndexMtx.Unlock()
|
|
|
|
|
|
|
|
h.onionMtx.Lock()
|
|
|
|
defer h.onionMtx.Unlock()
|
|
|
|
|
2016-07-14 02:35:47 +03:00
|
|
|
// A request with a nil channel point indicates that all the current
|
|
|
|
// links for this channel should be cleared.
|
|
|
|
if req.chanPoint == nil {
|
2016-11-29 05:44:14 +03:00
|
|
|
hswcLog.Debugf("purging all active links for interface %v",
|
2016-07-14 02:35:47 +03:00
|
|
|
hex.EncodeToString(chanInterface[:]))
|
|
|
|
|
|
|
|
for _, link := range links {
|
|
|
|
delete(h.chanIndex, *link.chanPoint)
|
|
|
|
}
|
2016-12-27 08:42:23 +03:00
|
|
|
|
2016-07-14 02:35:47 +03:00
|
|
|
links = nil
|
|
|
|
} else {
|
|
|
|
delete(h.chanIndex, *req.chanPoint)
|
|
|
|
|
|
|
|
for i := 0; i < len(links); i++ {
|
|
|
|
chanLink := links[i]
|
|
|
|
if chanLink.chanPoint == req.chanPoint {
|
2016-10-17 01:37:42 +03:00
|
|
|
// We perform an in-place delete by sliding
|
|
|
|
// every element down one, then slicing off the
|
|
|
|
// last element. Additionally, we update the
|
|
|
|
// slice reference within the source map to
|
|
|
|
// ensure full deletion.
|
2016-07-14 02:35:47 +03:00
|
|
|
copy(links[i:], links[i+1:])
|
|
|
|
links[len(links)-1] = nil
|
2016-10-17 01:37:42 +03:00
|
|
|
h.interfaceMtx.Lock()
|
|
|
|
h.interfaces[chanInterface] = links[:len(links)-1]
|
|
|
|
h.interfaceMtx.Unlock()
|
2016-07-14 02:35:47 +03:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(links) == 0 {
|
2016-11-29 05:44:14 +03:00
|
|
|
hswcLog.Debugf("interface %v has no active links, destroying",
|
2016-07-10 02:35:33 +03:00
|
|
|
hex.EncodeToString(chanInterface[:]))
|
2017-01-08 07:14:50 +03:00
|
|
|
|
|
|
|
// Delete the peer from the onion index so that the
|
2017-01-13 08:01:50 +03:00
|
|
|
// htlcForwarder knows not to attempt to forward any further
|
|
|
|
// HTLCs in this direction.
|
2017-01-08 07:14:50 +03:00
|
|
|
var onionId [ripemd160.Size]byte
|
|
|
|
copy(onionId[:], btcutil.Hash160(req.remoteID))
|
|
|
|
delete(h.onionIndex, onionId)
|
|
|
|
|
|
|
|
// Finally, delete the interface itself so that outgoing
|
|
|
|
// payments don't select this path.
|
2016-09-22 05:41:26 +03:00
|
|
|
h.interfaceMtx.Lock()
|
2016-07-10 02:35:33 +03:00
|
|
|
delete(h.interfaces, chanInterface)
|
2016-09-22 05:41:26 +03:00
|
|
|
h.interfaceMtx.Unlock()
|
2017-01-08 07:14:50 +03:00
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.done != nil {
|
|
|
|
req.done <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCloseLink sends a message to the peer responsible for the target
|
|
|
|
// channel point, instructing it to initiate a cooperative channel closure.
|
|
|
|
func (h *htlcSwitch) handleCloseLink(req *closeLinkReq) {
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.RLock()
|
2016-07-10 02:35:33 +03:00
|
|
|
targetLink, ok := h.chanIndex[*req.chanPoint]
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.RUnlock()
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
if !ok {
|
2017-01-08 07:14:50 +03:00
|
|
|
req.err <- fmt.Errorf("channel point %v not found, or peer "+
|
|
|
|
"offline", req.chanPoint)
|
2016-07-10 02:35:33 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-29 05:44:14 +03:00
|
|
|
hswcLog.Debugf("requesting interface %v to close link %v",
|
2016-07-10 02:35:33 +03:00
|
|
|
hex.EncodeToString(targetLink.peer.lightningID[:]), req.chanPoint)
|
|
|
|
targetLink.peer.localCloseChanReqs <- req
|
2016-11-29 06:43:57 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): if type was CloseBreach initiate force closure with
|
|
|
|
// all other channels (if any) we have with the remote peer.
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
|
2016-08-26 02:30:22 +03:00
|
|
|
// handleLinkUpdate processes the link info update message by adjusting the
|
2017-01-13 08:01:50 +03:00
|
|
|
// channel's available bandwidth by the delta specified within the message.
|
2016-08-26 02:30:22 +03:00
|
|
|
func (h *htlcSwitch) handleLinkUpdate(req *linkInfoUpdateMsg) {
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.RLock()
|
2016-08-26 02:30:22 +03:00
|
|
|
link := h.chanIndex[*req.targetLink]
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.RUnlock()
|
|
|
|
|
|
|
|
atomic.AddInt64(&link.availableBandwidth, int64(req.bandwidthDelta))
|
2016-08-26 02:30:22 +03:00
|
|
|
|
|
|
|
hswcLog.Tracef("adjusting bandwidth of link %v by %v", req.targetLink,
|
|
|
|
req.bandwidthDelta)
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
// registerLinkMsg is message which requests a new link to be registered.
|
|
|
|
type registerLinkMsg struct {
|
|
|
|
peer *peer
|
|
|
|
linkInfo *channeldb.ChannelSnapshot
|
|
|
|
|
2016-07-17 04:20:13 +03:00
|
|
|
linkChan chan *htlcPacket
|
2016-07-10 02:35:33 +03:00
|
|
|
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterLink requests the htlcSwitch to register a new active link. The new
|
2016-07-17 04:20:13 +03:00
|
|
|
// link encapsulates an active channel. The htlc plex channel is returned. The
|
|
|
|
// plex channel allows the switch to properly de-multiplex incoming/outgoing
|
|
|
|
// HTLC messages forwarding them to their proper destination in the multi-hop
|
|
|
|
// settings.
|
2016-07-10 02:35:33 +03:00
|
|
|
func (h *htlcSwitch) RegisterLink(p *peer, linkInfo *channeldb.ChannelSnapshot,
|
2016-07-17 04:20:13 +03:00
|
|
|
linkChan chan *htlcPacket) chan *htlcPacket {
|
2016-07-10 02:35:33 +03:00
|
|
|
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
req := ®isterLinkMsg{p, linkInfo, linkChan, done}
|
|
|
|
h.linkControl <- req
|
|
|
|
|
|
|
|
<-done
|
2016-07-13 03:40:32 +03:00
|
|
|
|
|
|
|
return h.htlcPlex
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
// unregisterLinkMsg is a message which requests the active link be unregistered.
|
2016-07-10 02:35:33 +03:00
|
|
|
type unregisterLinkMsg struct {
|
|
|
|
chanInterface [32]byte
|
|
|
|
chanPoint *wire.OutPoint
|
|
|
|
|
2016-10-24 05:00:09 +03:00
|
|
|
// remoteID is the identity public key of the node we're removing the
|
|
|
|
// link between. The public key is expected to be serialized in
|
|
|
|
// compressed form.
|
2016-09-26 20:35:10 +03:00
|
|
|
// TODO(roasbeef): redo interface map
|
|
|
|
remoteID []byte
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// UnregisterLink requests the htlcSwitch to register the new active link. An
|
2016-07-10 02:35:33 +03:00
|
|
|
// unregistered link will no longer be considered a candidate to forward
|
2017-01-13 08:01:50 +03:00
|
|
|
// HTLCs.
|
2016-09-26 20:35:10 +03:00
|
|
|
func (h *htlcSwitch) UnregisterLink(remotePub *btcec.PublicKey, chanPoint *wire.OutPoint) {
|
2016-07-10 02:35:33 +03:00
|
|
|
done := make(chan struct{}, 1)
|
2016-09-26 20:35:10 +03:00
|
|
|
rawPub := remotePub.SerializeCompressed()
|
2016-07-10 02:35:33 +03:00
|
|
|
|
2016-09-26 20:35:10 +03:00
|
|
|
h.linkControl <- &unregisterLinkMsg{
|
|
|
|
chanInterface: fastsha256.Sum256(rawPub),
|
|
|
|
chanPoint: chanPoint,
|
|
|
|
remoteID: rawPub,
|
|
|
|
done: done,
|
|
|
|
}
|
2016-07-10 02:35:33 +03:00
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// LinkCloseType is an enum which signals the type of channel closure the switch
|
2016-11-29 05:44:14 +03:00
|
|
|
// should execute.
|
|
|
|
type LinkCloseType uint8
|
|
|
|
|
|
|
|
const (
|
2017-01-04 03:02:51 +03:00
|
|
|
// CloseRegular indicates a regular cooperative channel closure should
|
|
|
|
// be attempted.
|
2016-11-29 05:44:14 +03:00
|
|
|
CloseRegular LinkCloseType = iota
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// CloseBreach indicates that a channel breach has been detected, and
|
2016-11-29 05:44:14 +03:00
|
|
|
// the link should immediately be marked as unavailable.
|
|
|
|
CloseBreach
|
|
|
|
)
|
|
|
|
|
|
|
|
// closeChanReq represents a request to close a particular channel specified by
|
|
|
|
// its outpoint.
|
2016-07-10 02:35:33 +03:00
|
|
|
type closeLinkReq struct {
|
2016-11-29 05:44:14 +03:00
|
|
|
CloseType LinkCloseType
|
|
|
|
|
|
|
|
chanPoint *wire.OutPoint
|
2016-07-10 02:35:33 +03:00
|
|
|
|
2016-08-31 02:52:53 +03:00
|
|
|
updates chan *lnrpc.CloseStatusUpdate
|
|
|
|
err chan error
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
|
2017-01-13 08:01:50 +03:00
|
|
|
// CloseLink closes an active link targetted by its channel point. Closing the
|
2016-09-12 22:42:26 +03:00
|
|
|
// link initiates a cooperative channel closure iff forceClose is false. If
|
|
|
|
// forceClose is true, then a unilateral channel closure is executed.
|
2016-11-29 05:44:14 +03:00
|
|
|
// TODO(roasbeef): consolidate with UnregisterLink?
|
2016-09-12 22:42:26 +03:00
|
|
|
func (h *htlcSwitch) CloseLink(chanPoint *wire.OutPoint,
|
2016-11-29 05:44:14 +03:00
|
|
|
closeType LinkCloseType) (chan *lnrpc.CloseStatusUpdate, chan error) {
|
2016-09-12 22:42:26 +03:00
|
|
|
|
2016-08-31 02:52:53 +03:00
|
|
|
updateChan := make(chan *lnrpc.CloseStatusUpdate, 1)
|
2016-07-10 02:35:33 +03:00
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
2016-09-12 22:42:26 +03:00
|
|
|
h.linkControl <- &closeLinkReq{
|
2016-11-29 05:44:14 +03:00
|
|
|
CloseType: closeType,
|
|
|
|
chanPoint: chanPoint,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
2016-09-12 22:42:26 +03:00
|
|
|
}
|
2016-07-10 02:35:33 +03:00
|
|
|
|
2016-08-31 02:52:53 +03:00
|
|
|
return updateChan, errChan
|
2016-06-21 22:32:32 +03:00
|
|
|
}
|
2016-08-26 02:30:22 +03:00
|
|
|
|
|
|
|
// linkInfoUpdateMsg encapsulates a request for the htlc switch to update the
|
2017-01-13 08:01:50 +03:00
|
|
|
// metadata related to the target link.
|
2016-08-26 02:30:22 +03:00
|
|
|
type linkInfoUpdateMsg struct {
|
|
|
|
targetLink *wire.OutPoint
|
|
|
|
|
|
|
|
bandwidthDelta btcutil.Amount
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateLink sends a message to the switch to update the available bandwidth
|
|
|
|
// within the link by the passed satoshi delta. This function may be used when
|
|
|
|
// re-anchoring to boost the capacity of a channel, or once a peer settles an
|
|
|
|
// HTLC invoice.
|
|
|
|
func (h *htlcSwitch) UpdateLink(chanPoint *wire.OutPoint, bandwidthDelta btcutil.Amount) {
|
|
|
|
h.linkControl <- &linkInfoUpdateMsg{chanPoint, bandwidthDelta}
|
|
|
|
}
|