2017-03-09 01:27:46 +03:00
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-06-25 15:31:41 +03:00
|
|
|
"sync"
|
2017-03-09 01:27:46 +03:00
|
|
|
"sync/atomic"
|
|
|
|
|
2018-07-31 10:17:17 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-03-15 06:02:33 +03:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-03-19 21:40:25 +03:00
|
|
|
"github.com/go-errors/errors"
|
2017-03-09 01:27:46 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2017-08-22 09:43:20 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2017-03-09 01:27:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TopologyClient represents an intent to receive notifications from the
|
|
|
|
// channel router regarding changes to the topology of the channel graph. The
|
|
|
|
// TopologyChanges channel will be sent upon with new updates to the channel
|
|
|
|
// graph in real-time as they're encountered.
|
|
|
|
type TopologyClient struct {
|
2017-03-14 06:32:59 +03:00
|
|
|
// TopologyChanges is a receive only channel that new channel graph
|
2017-03-09 01:27:46 +03:00
|
|
|
// updates will be sent over.
|
2017-03-14 06:32:59 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): chan for each update type instead?
|
2017-03-09 01:27:46 +03:00
|
|
|
TopologyChanges <-chan *TopologyChange
|
|
|
|
|
|
|
|
// Cancel is a function closure that should be executed when the client
|
|
|
|
// wishes to cancel their notification intent. Doing so allows the
|
|
|
|
// ChannelRouter to free up resources.
|
|
|
|
Cancel func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// topologyClientUpdate is a message sent to the channel router to either
|
|
|
|
// register a new topology client or re-register an existing client.
|
|
|
|
type topologyClientUpdate struct {
|
|
|
|
// cancel indicates if the update to the client is cancelling an
|
|
|
|
// existing client's notifications. If not then this update will be to
|
|
|
|
// register a new set of notifications.
|
|
|
|
cancel bool
|
|
|
|
|
|
|
|
// clientID is the unique identifier for this client. Any further
|
|
|
|
// updates (deleting or adding) to this notification client will be
|
|
|
|
// dispatched according to the target clientID.
|
|
|
|
clientID uint64
|
|
|
|
|
|
|
|
// ntfnChan is a *send-only* channel in which notifications should be
|
|
|
|
// sent over from router -> client.
|
|
|
|
ntfnChan chan<- *TopologyChange
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeTopology returns a new topology client which can be used by the
|
2017-12-18 05:40:05 +03:00
|
|
|
// caller to receive notifications whenever a change in the channel graph
|
2017-03-09 01:27:46 +03:00
|
|
|
// topology occurs. Changes that will be sent at notifications include: new
|
|
|
|
// nodes appearing, node updating their attributes, new channels, channels
|
|
|
|
// closing, and updates in the routing policies of a channel's directed edges.
|
|
|
|
func (r *ChannelRouter) SubscribeTopology() (*TopologyClient, error) {
|
2017-03-15 06:02:33 +03:00
|
|
|
// We'll first atomically obtain the next ID for this client from the
|
2017-03-09 01:27:46 +03:00
|
|
|
// incrementing client ID counter.
|
|
|
|
clientID := atomic.AddUint64(&r.ntfnClientCounter, 1)
|
|
|
|
|
2017-03-15 06:02:33 +03:00
|
|
|
log.Debugf("New graph topology client subscription, client %v",
|
|
|
|
clientID)
|
|
|
|
|
|
|
|
ntfnChan := make(chan *TopologyChange, 10)
|
2017-03-09 01:27:46 +03:00
|
|
|
|
|
|
|
select {
|
|
|
|
case r.ntfnClientUpdates <- &topologyClientUpdate{
|
|
|
|
cancel: false,
|
|
|
|
clientID: clientID,
|
|
|
|
ntfnChan: ntfnChan,
|
|
|
|
}:
|
|
|
|
case <-r.quit:
|
|
|
|
return nil, errors.New("ChannelRouter shutting down")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TopologyClient{
|
|
|
|
TopologyChanges: ntfnChan,
|
|
|
|
Cancel: func() {
|
|
|
|
select {
|
|
|
|
case r.ntfnClientUpdates <- &topologyClientUpdate{
|
|
|
|
cancel: true,
|
|
|
|
clientID: clientID,
|
|
|
|
}:
|
|
|
|
case <-r.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// topologyClient is a data-structure use by the channel router to couple the
|
|
|
|
// client's notification channel along with a special "exit" channel that can
|
|
|
|
// be used to cancel all lingering goroutines blocked on a send to the
|
|
|
|
// notification channel.
|
|
|
|
type topologyClient struct {
|
|
|
|
// ntfnChan is a send-only channel that's used to propagate
|
|
|
|
// notification s from the channel router to an instance of a
|
|
|
|
// topologyClient client.
|
|
|
|
ntfnChan chan<- *TopologyChange
|
|
|
|
|
|
|
|
// exit is a channel that is used internally by the channel router to
|
|
|
|
// cancel any active un-consumed goroutine notifications.
|
|
|
|
exit chan struct{}
|
2017-06-25 15:31:41 +03:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
2017-03-09 01:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// notifyTopologyChange notifies all registered clients of a new change in
|
|
|
|
// graph topology in a non-blocking.
|
|
|
|
func (r *ChannelRouter) notifyTopologyChange(topologyDiff *TopologyChange) {
|
2017-11-30 03:24:27 +03:00
|
|
|
r.RLock()
|
|
|
|
numClients := len(r.topologyClients)
|
|
|
|
r.RUnlock()
|
2018-06-21 09:43:37 +03:00
|
|
|
|
|
|
|
// Do not reacquire the lock twice unnecessarily.
|
|
|
|
if numClients == 0 {
|
|
|
|
return
|
2017-03-16 22:16:24 +03:00
|
|
|
}
|
2017-03-15 06:02:33 +03:00
|
|
|
|
2018-06-21 09:43:37 +03:00
|
|
|
log.Tracef("Sending topology notification to %v clients %v",
|
|
|
|
numClients,
|
|
|
|
newLogClosure(func() string {
|
|
|
|
return spew.Sdump(topologyDiff)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
2017-11-30 03:24:27 +03:00
|
|
|
r.RLock()
|
2017-03-09 01:27:46 +03:00
|
|
|
for _, client := range r.topologyClients {
|
2017-06-25 15:31:41 +03:00
|
|
|
client.wg.Add(1)
|
|
|
|
|
2017-06-25 15:48:47 +03:00
|
|
|
go func(c *topologyClient) {
|
2017-06-25 15:31:41 +03:00
|
|
|
defer c.wg.Done()
|
|
|
|
|
2017-03-09 01:27:46 +03:00
|
|
|
select {
|
|
|
|
|
|
|
|
// In this case we'll try to send the notification
|
|
|
|
// directly to the upstream client consumer.
|
2017-03-09 07:44:32 +03:00
|
|
|
case c.ntfnChan <- topologyDiff:
|
2017-03-09 01:27:46 +03:00
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// If the client cancels the notifications, then we'll
|
2017-03-09 01:27:46 +03:00
|
|
|
// exit early.
|
2017-03-09 07:44:32 +03:00
|
|
|
case <-c.exit:
|
2017-03-09 01:27:46 +03:00
|
|
|
|
|
|
|
// Similarly, if the ChannelRouter itself exists early,
|
|
|
|
// then we'll also exit ourselves.
|
|
|
|
case <-r.quit:
|
|
|
|
|
|
|
|
}
|
2017-03-15 06:02:33 +03:00
|
|
|
}(client)
|
2017-03-09 01:27:46 +03:00
|
|
|
}
|
2017-11-30 03:24:27 +03:00
|
|
|
r.RUnlock()
|
2017-03-09 01:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TopologyChange represents a new set of modifications to the channel graph.
|
|
|
|
// Topology changes will be dispatched in real-time as the ChannelGraph
|
|
|
|
// validates and process modifications to the authenticated channel graph.
|
|
|
|
type TopologyChange struct {
|
|
|
|
// NodeUpdates is a slice of nodes which are either new to the channel
|
|
|
|
// graph, or have had their attributes updated in an authenticated
|
|
|
|
// manner.
|
|
|
|
NodeUpdates []*NetworkNodeUpdate
|
|
|
|
|
|
|
|
// ChanelEdgeUpdates is a slice of channel edges which are either newly
|
|
|
|
// opened and authenticated, or have had their routing policies
|
|
|
|
// updated.
|
|
|
|
ChannelEdgeUpdates []*ChannelEdgeUpdate
|
|
|
|
|
|
|
|
// ClosedChannels contains a slice of close channel summaries which
|
|
|
|
// described which block a channel was closed at, and also carry
|
|
|
|
// supplemental information such as the capacity of the former channel.
|
|
|
|
ClosedChannels []*ClosedChanSummary
|
|
|
|
}
|
|
|
|
|
|
|
|
// isEmpty returns true if the TopologyChange is empty. A TopologyChange is
|
|
|
|
// considered empty, if it contains no *new* updates of any type.
|
|
|
|
func (t *TopologyChange) isEmpty() bool {
|
|
|
|
return len(t.NodeUpdates) == 0 && len(t.ChannelEdgeUpdates) == 0 &&
|
|
|
|
len(t.ClosedChannels) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClosedChanSummary is a summary of a channel that was detected as being
|
|
|
|
// closed by monitoring the blockchain. Once a channel's funding point has been
|
|
|
|
// spent, the channel will automatically be marked as closed by the
|
|
|
|
// ChainNotifier.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): add nodes involved?
|
|
|
|
type ClosedChanSummary struct {
|
|
|
|
// ChanID is the short-channel ID which uniquely identifies the
|
|
|
|
// channel.
|
|
|
|
ChanID uint64
|
|
|
|
|
|
|
|
// Capacity was the total capacity of the channel before it was closed.
|
|
|
|
Capacity btcutil.Amount
|
|
|
|
|
|
|
|
// ClosedHeight is the height in the chain that the channel was closed
|
|
|
|
// at.
|
|
|
|
ClosedHeight uint32
|
|
|
|
|
|
|
|
// ChanPoint is the funding point, or the multi-sig utxo which
|
|
|
|
// previously represented the channel.
|
|
|
|
ChanPoint wire.OutPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// createCloseSummaries takes in a slice of channels closed at the target block
|
|
|
|
// height and creates a slice of summaries which of each channel closure.
|
|
|
|
func createCloseSummaries(blockHeight uint32,
|
|
|
|
closedChans ...*channeldb.ChannelEdgeInfo) []*ClosedChanSummary {
|
|
|
|
|
|
|
|
closeSummaries := make([]*ClosedChanSummary, len(closedChans))
|
|
|
|
for i, closedChan := range closedChans {
|
|
|
|
closeSummaries[i] = &ClosedChanSummary{
|
|
|
|
ChanID: closedChan.ChannelID,
|
|
|
|
Capacity: closedChan.Capacity,
|
|
|
|
ClosedHeight: blockHeight,
|
|
|
|
ChanPoint: closedChan.ChannelPoint,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closeSummaries
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkNodeUpdate is an update for a node within the Lightning Network. A
|
|
|
|
// NetworkNodeUpdate is sent out either when a new node joins the network, or a
|
2018-02-07 06:13:07 +03:00
|
|
|
// node broadcasts a new update with a newer time stamp that supersedes its
|
2017-03-09 01:27:46 +03:00
|
|
|
// old update. All updates are properly authenticated.
|
|
|
|
type NetworkNodeUpdate struct {
|
|
|
|
// Addresses is a slice of all the node's known addresses.
|
|
|
|
Addresses []net.Addr
|
|
|
|
|
|
|
|
// IdentityKey is the identity public key of the target node. This is
|
|
|
|
// used to encrypt onion blobs as well as to authenticate any new
|
|
|
|
// updates.
|
|
|
|
IdentityKey *btcec.PublicKey
|
|
|
|
|
|
|
|
// GlobalFeatures is a set of opaque bytes that describe the set of
|
|
|
|
// features supported by the node.
|
|
|
|
GlobalFeatures []byte
|
|
|
|
|
|
|
|
// Alias is the alias or nick name of the node.
|
|
|
|
Alias string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChannelEdgeUpdate is an update for a new channel within the ChannelGraph.
|
|
|
|
// This update is sent out once a new authenticated channel edge is discovered
|
|
|
|
// within the network. These updates are directional, so if a channel is fully
|
|
|
|
// public, then there will be two updates sent out: one for each direction
|
|
|
|
// within the channel. Each update will carry that particular routing edge
|
|
|
|
// policy for the channel direction.
|
|
|
|
//
|
|
|
|
// An edge is a channel in the direction of AdvertisingNode -> ConnectingNode.
|
|
|
|
type ChannelEdgeUpdate struct {
|
|
|
|
// ChanID is the unique short channel ID for the channel. This encodes
|
|
|
|
// where in the blockchain the channel's funding transaction was
|
|
|
|
// originally confirmed.
|
|
|
|
ChanID uint64
|
|
|
|
|
2017-03-14 06:32:59 +03:00
|
|
|
// ChanPoint is the outpoint which represents the multi-sig funding
|
|
|
|
// output for the channel.
|
|
|
|
ChanPoint wire.OutPoint
|
|
|
|
|
2017-03-09 01:27:46 +03:00
|
|
|
// Capacity is the capacity of the newly created channel.
|
|
|
|
Capacity btcutil.Amount
|
|
|
|
|
|
|
|
// MinHTLC is the minimum HTLC amount that this channel will forward.
|
2017-08-22 09:43:20 +03:00
|
|
|
MinHTLC lnwire.MilliSatoshi
|
2017-03-09 01:27:46 +03:00
|
|
|
|
|
|
|
// BaseFee is the base fee that will charged for all HTLC's forwarded
|
|
|
|
// across the this channel direction.
|
2017-08-22 09:43:20 +03:00
|
|
|
BaseFee lnwire.MilliSatoshi
|
2017-03-09 01:27:46 +03:00
|
|
|
|
|
|
|
// FeeRate is the fee rate that will be shared for all HTLC's forwarded
|
|
|
|
// across this channel direction.
|
2017-08-22 09:43:20 +03:00
|
|
|
FeeRate lnwire.MilliSatoshi
|
2017-03-09 01:27:46 +03:00
|
|
|
|
|
|
|
// TimeLockDelta is the time-lock expressed in blocks that will be
|
|
|
|
// added to outgoing HTLC's from incoming HTLC's. This value is the
|
|
|
|
// difference of the incoming and outgoing HTLC's time-locks routed
|
|
|
|
// through this hop.
|
|
|
|
TimeLockDelta uint16
|
|
|
|
|
|
|
|
// AdvertisingNode is the node that's advertising this edge.
|
|
|
|
AdvertisingNode *btcec.PublicKey
|
|
|
|
|
|
|
|
// ConnectingNode is the node that the advertising node connects to.
|
|
|
|
ConnectingNode *btcec.PublicKey
|
2018-06-14 05:44:48 +03:00
|
|
|
|
|
|
|
// Disabled, if true, signals that the channel is unavailable to relay
|
|
|
|
// payments.
|
|
|
|
Disabled bool
|
2017-03-09 01:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// appendTopologyChange appends the passed update message to the passed
|
|
|
|
// TopologyChange, properly identifying which type of update the message
|
|
|
|
// constitutes. This function will also fetch any required auxiliary
|
|
|
|
// information required to create the topology change update from the graph
|
|
|
|
// database.
|
|
|
|
func addToTopologyChange(graph *channeldb.ChannelGraph, update *TopologyChange,
|
2017-03-19 21:40:25 +03:00
|
|
|
msg interface{}) error {
|
2017-03-09 01:27:46 +03:00
|
|
|
|
|
|
|
switch m := msg.(type) {
|
|
|
|
|
|
|
|
// Any node announcement maps directly to a NetworkNodeUpdate struct.
|
|
|
|
// No further data munging or db queries are required.
|
2017-03-19 21:40:25 +03:00
|
|
|
case *channeldb.LightningNode:
|
2018-01-31 07:26:26 +03:00
|
|
|
pubKey, err := m.PubKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-15 06:02:33 +03:00
|
|
|
nodeUpdate := &NetworkNodeUpdate{
|
2017-02-17 12:29:23 +03:00
|
|
|
Addresses: m.Addresses,
|
2018-01-31 07:26:26 +03:00
|
|
|
IdentityKey: pubKey,
|
2017-03-19 21:40:25 +03:00
|
|
|
Alias: m.Alias,
|
2017-03-15 06:02:33 +03:00
|
|
|
}
|
|
|
|
nodeUpdate.IdentityKey.Curve = nil
|
|
|
|
|
|
|
|
update.NodeUpdates = append(update.NodeUpdates, nodeUpdate)
|
2017-03-09 01:27:46 +03:00
|
|
|
return nil
|
|
|
|
|
|
|
|
// We ignore initial channel announcements as we'll only send out
|
|
|
|
// updates once the individual edges themselves have been updated.
|
2017-03-19 21:40:25 +03:00
|
|
|
case *channeldb.ChannelEdgeInfo:
|
2017-03-09 01:27:46 +03:00
|
|
|
return nil
|
|
|
|
|
|
|
|
// Any new ChannelUpdateAnnouncements will generate a corresponding
|
|
|
|
// ChannelEdgeUpdate notification.
|
2017-03-19 21:40:25 +03:00
|
|
|
case *channeldb.ChannelEdgePolicy:
|
2017-03-09 01:27:46 +03:00
|
|
|
// We'll need to fetch the edge's information from the database
|
|
|
|
// in order to get the information concerning which nodes are
|
|
|
|
// being connected.
|
2017-03-19 21:40:25 +03:00
|
|
|
edgeInfo, _, _, err := graph.FetchChannelEdgesByID(m.ChannelID)
|
2017-03-09 01:27:46 +03:00
|
|
|
if err != nil {
|
2017-03-19 21:40:25 +03:00
|
|
|
return errors.Errorf("unable fetch channel edge: %v",
|
|
|
|
err)
|
2017-03-09 01:27:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the flag is one, then the advertising node is actually
|
|
|
|
// the second node.
|
|
|
|
sourceNode := edgeInfo.NodeKey1
|
|
|
|
connectingNode := edgeInfo.NodeKey2
|
2019-01-12 20:59:43 +03:00
|
|
|
if m.ChannelFlags&lnwire.ChanUpdateDirection == 1 {
|
2017-03-09 01:27:46 +03:00
|
|
|
sourceNode = edgeInfo.NodeKey2
|
|
|
|
connectingNode = edgeInfo.NodeKey1
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:26:26 +03:00
|
|
|
aNode, err := sourceNode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cNode, err := connectingNode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-09 01:27:46 +03:00
|
|
|
edgeUpdate := &ChannelEdgeUpdate{
|
2017-03-19 21:40:25 +03:00
|
|
|
ChanID: m.ChannelID,
|
2017-03-14 06:32:59 +03:00
|
|
|
ChanPoint: edgeInfo.ChannelPoint,
|
2017-03-09 01:27:46 +03:00
|
|
|
TimeLockDelta: m.TimeLockDelta,
|
|
|
|
Capacity: edgeInfo.Capacity,
|
2017-03-19 21:40:25 +03:00
|
|
|
MinHTLC: m.MinHTLC,
|
|
|
|
BaseFee: m.FeeBaseMSat,
|
|
|
|
FeeRate: m.FeeProportionalMillionths,
|
2018-01-31 07:26:26 +03:00
|
|
|
AdvertisingNode: aNode,
|
|
|
|
ConnectingNode: cNode,
|
2019-01-12 20:59:43 +03:00
|
|
|
Disabled: m.ChannelFlags&lnwire.ChanUpdateDisabled != 0,
|
2017-03-09 01:27:46 +03:00
|
|
|
}
|
2017-03-15 06:02:33 +03:00
|
|
|
edgeUpdate.AdvertisingNode.Curve = nil
|
|
|
|
edgeUpdate.ConnectingNode.Curve = nil
|
2017-03-09 01:27:46 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): add bit to toggle
|
|
|
|
update.ChannelEdgeUpdates = append(update.ChannelEdgeUpdates,
|
|
|
|
edgeUpdate)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unable to add to topology change, "+
|
|
|
|
"unknown message type %T", msg)
|
|
|
|
}
|
|
|
|
}
|