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
|
|
|
"golang.org/x/crypto/ripemd160"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// htlcQueueSize...
|
|
|
|
// buffer bloat ;)
|
2016-07-22 02:21:27 +03:00
|
|
|
htlcQueueSize = 50
|
2016-07-10 02:35:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// link represents a an active channel capable of forwarding HTLC's. Each
|
|
|
|
// active channel registered with the htlc switch creates a new link which will
|
|
|
|
// be used for forwarding outgoing HTLC's. The link also has additional
|
|
|
|
// meta-data such as the current available bandwidth of the link (in satoshis)
|
|
|
|
// which aide the switch in optimally forwarding HTLC's.
|
|
|
|
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
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
dest wire.ShaHash
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
index uint32
|
|
|
|
srcLink wire.OutPoint
|
|
|
|
onion *sphinx.ProcessedPacket
|
|
|
|
|
2016-07-13 03:40:32 +03:00
|
|
|
msg lnwire.Message
|
2016-07-22 02:21:27 +03:00
|
|
|
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.
|
|
|
|
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
|
|
|
|
// link forwards an HTLC add request which initites the creation of the ciruit.
|
|
|
|
// The onion routing informtion contained within this message is used to
|
|
|
|
// identify the settle/clear ends of the circuit. A circuit may be re-used (not
|
|
|
|
// torndown) in the case that multiple HTLC's with the send RHash are sent.
|
|
|
|
type paymentCircuit struct {
|
|
|
|
// TODO(roasbeef): add reference count so know when to delete?
|
|
|
|
// * atomic int re
|
|
|
|
// * due to same r-value being re-used?
|
|
|
|
|
|
|
|
// NOTE: This integer must be used *atomically*.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
// HtlcSwitch is a central messaging bus for all incoming/outgoing HTLC's.
|
|
|
|
// Connected peers with active channels are treated as named interfaces which
|
|
|
|
// refer to active channels as links. A link is the switche'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
|
|
|
|
// HTLC's, forwarding HTLC's initiated from within the daemon, and additionally
|
|
|
|
// splitting up incoming/outgoing HTLC's to a particular interface amongst many
|
|
|
|
// 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
|
|
|
|
// pointer to the peer mangaing 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
|
|
|
|
interfaces map[wire.ShaHash][]*link
|
|
|
|
|
|
|
|
// onionIndex is a secondary index used to properly forward a message
|
|
|
|
// to the next hop within a Sphinx circuit.
|
|
|
|
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
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
// htlcPlex is the channel in which all connected links use to
|
|
|
|
// coordinate the setup/tear down 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.
|
2016-07-13 03:40:32 +03:00
|
|
|
htlcPlex chan *htlcPacket
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
// TODO(roasbeef): messaging chan to/from upper layer (routing - L3)
|
|
|
|
|
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.
|
|
|
|
func newHtlcSwitch() *htlcSwitch {
|
|
|
|
return &htlcSwitch{
|
|
|
|
chanIndex: make(map[wire.OutPoint]*link),
|
|
|
|
interfaces: make(map[wire.ShaHash][]*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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// fragmenting) incoming/outgoing HTLC's 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,
|
|
|
|
// and tearn down active onion routed payments.Each active channel is modeled
|
|
|
|
// as networked device with meta-data such as the available payment bandwidth,
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
wireMsg := htlcPkt.msg.(*lnwire.HTLCAddRequest)
|
|
|
|
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?
|
2016-09-22 05:41:26 +03:00
|
|
|
if 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
|
|
|
|
// staate so we can properly forward the ultimate
|
|
|
|
// settle message.
|
2016-07-22 02:21:27 +03:00
|
|
|
case *lnwire.HTLCAddRequest:
|
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)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
h.chanIndexMtx.RLock()
|
|
|
|
settleLink := h.chanIndex[pkt.srcLink]
|
|
|
|
h.chanIndexMtx.RUnlock()
|
|
|
|
|
|
|
|
// TODO(roasbeef): examine per-hop info to decide on link?
|
|
|
|
// * check clear has enough available sat
|
|
|
|
circuit := &paymentCircuit{
|
|
|
|
clear: clearLink[0],
|
|
|
|
settle: settleLink,
|
|
|
|
}
|
|
|
|
|
|
|
|
cKey := circuitKey(wireMsg.RedemptionHashes[0])
|
|
|
|
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
|
|
|
|
// continue propagating the HTLC accross the
|
|
|
|
// 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.
|
|
|
|
n :=
|
|
|
|
atomic.AddInt64(&circuit.clear.availableBandwidth,
|
|
|
|
-int64(pkt.amt))
|
|
|
|
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.
|
2016-07-22 02:21:27 +03:00
|
|
|
case *lnwire.HTLCSettleRequest:
|
2016-09-22 05:41:26 +03:00
|
|
|
rHash := fastsha256.Sum256(wireMsg.RedemptionProofs[0][:])
|
|
|
|
|
|
|
|
var cKey circuitKey
|
|
|
|
copy(cKey[:], rHash[:])
|
|
|
|
|
|
|
|
// If we initiated the payment then there won't
|
|
|
|
// be an active circuit so continue propagating
|
|
|
|
// the settle over. Therefore, we exit early.
|
|
|
|
circuit, ok := h.paymentCircuits[cKey]
|
|
|
|
if !ok {
|
|
|
|
hswcLog.Debugf("No existing circuit "+
|
|
|
|
"for %x", rHash[:])
|
|
|
|
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,
|
|
|
|
// subtracting from the limbo balacne and
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
case <-logTicker.C:
|
|
|
|
if numUpdates == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hswcLog.Infof("Sent %v satoshis, received %v satoshi in "+
|
|
|
|
"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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// networkAdmin is responsible for handline requests to register, unregister,
|
|
|
|
// and close any link. In the event that a unregister requests leaves an
|
|
|
|
// 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
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
var onionId [ripemd160.Size]byte
|
|
|
|
copy(onionId[:], btcutil.Hash160(req.peer.identityPub.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) {
|
|
|
|
hswcLog.Infof("unregistering active link, interface=%v, chan_point=%v",
|
|
|
|
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
|
|
|
|
|
|
|
// A request with a nil channel point indicates that all the current
|
|
|
|
// links for this channel should be cleared.
|
|
|
|
if req.chanPoint == nil {
|
|
|
|
hswcLog.Infof("purging all active links for interface %v",
|
|
|
|
hex.EncodeToString(chanInterface[:]))
|
|
|
|
|
|
|
|
for _, link := range links {
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.Lock()
|
2016-07-14 02:35:47 +03:00
|
|
|
delete(h.chanIndex, *link.chanPoint)
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.Unlock()
|
2016-07-14 02:35:47 +03:00
|
|
|
}
|
|
|
|
links = nil
|
|
|
|
} else {
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.Lock()
|
2016-07-14 02:35:47 +03:00
|
|
|
delete(h.chanIndex, *req.chanPoint)
|
2016-09-22 05:41:26 +03:00
|
|
|
h.chanIndexMtx.Unlock()
|
2016-07-14 02:35:47 +03:00
|
|
|
|
|
|
|
for i := 0; i < len(links); i++ {
|
|
|
|
chanLink := links[i]
|
|
|
|
if chanLink.chanPoint == req.chanPoint {
|
|
|
|
copy(links[i:], links[i+1:])
|
|
|
|
links[len(links)-1] = nil
|
|
|
|
links = links[:len(links)-1]
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2016-07-10 02:35:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 05:41:26 +03:00
|
|
|
// TODO(roasbeef): clean up/modify onion links
|
|
|
|
// * just have the interfaces index be keyed on hash160?
|
|
|
|
|
2016-07-10 02:35:33 +03:00
|
|
|
if len(links) == 0 {
|
|
|
|
hswcLog.Infof("interface %v has no active links, destroying",
|
|
|
|
hex.EncodeToString(chanInterface[:]))
|
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()
|
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 {
|
|
|
|
req.err <- fmt.Errorf("channel point %v not found", req.chanPoint)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hswcLog.Infof("requesting interface %v to close link %v",
|
|
|
|
hex.EncodeToString(targetLink.peer.lightningID[:]), req.chanPoint)
|
|
|
|
targetLink.peer.localCloseChanReqs <- req
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:30:22 +03:00
|
|
|
// handleLinkUpdate processes the link info update message by adjusting the
|
|
|
|
// channels available bandwidth by the delta specified within the message.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// unregisterLinkMsg is a message which requests the active ink be unregistered.
|
|
|
|
type unregisterLinkMsg struct {
|
|
|
|
chanInterface [32]byte
|
|
|
|
chanPoint *wire.OutPoint
|
|
|
|
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnregisterLink requets the htlcSwitch to unregiser the new active link. An
|
|
|
|
// unregistered link will no longer be considered a candidate to forward
|
|
|
|
// HTLC's.
|
|
|
|
func (h *htlcSwitch) UnregisterLink(chanInterface [32]byte, chanPoint *wire.OutPoint) {
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
h.linkControl <- &unregisterLinkMsg{chanInterface, chanPoint, done}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeChanReq represents a request to close a particular channel specified
|
|
|
|
// by its outpoint.
|
|
|
|
type closeLinkReq struct {
|
2016-09-12 22:42:26 +03:00
|
|
|
chanPoint *wire.OutPoint
|
|
|
|
forceClose bool
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// CloseLink closes an active link targetted by it's 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.
|
|
|
|
// TODO(roabeef): bool flag for timeout
|
|
|
|
func (h *htlcSwitch) CloseLink(chanPoint *wire.OutPoint,
|
|
|
|
forceClose bool) (chan *lnrpc.CloseStatusUpdate, chan error) {
|
|
|
|
|
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{
|
|
|
|
chanPoint: chanPoint,
|
|
|
|
forceClose: forceClose,
|
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
|
|
|
}
|
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
|
|
|
|
// meta-data related to the target link.
|
|
|
|
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}
|
|
|
|
}
|