2015-12-26 09:09:17 +03:00
|
|
|
package main
|
2016-01-14 08:41:46 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2016-07-06 04:48:35 +03:00
|
|
|
"github.com/btcsuite/fastsha256"
|
2016-09-21 03:15:26 +03:00
|
|
|
"github.com/lightningnetwork/lightning-onion"
|
2016-10-28 05:49:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/brontide"
|
2016-08-04 22:37:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2016-03-23 04:49:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-08-31 02:52:53 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2016-01-16 21:38:48 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2016-10-28 05:49:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
2016-06-21 21:52:09 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2016-01-14 08:41:46 +03:00
|
|
|
|
2016-10-05 23:47:02 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/rt/graph"
|
2016-01-14 08:41:46 +03:00
|
|
|
)
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// server is the main server of the Lightning Network Daemon. The server
|
|
|
|
// houses global state pertianing to the wallet, database, and the rpcserver.
|
|
|
|
// Additionally, the server is also used as a central messaging bus to interact
|
|
|
|
// with any of its companion objects.
|
2016-01-14 08:41:46 +03:00
|
|
|
type server struct {
|
|
|
|
started int32 // atomic
|
|
|
|
shutdown int32 // atomic
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// identityPriv is the private key used to authenticate any incoming
|
|
|
|
// connections.
|
|
|
|
identityPriv *btcec.PrivateKey
|
2016-01-17 06:07:44 +03:00
|
|
|
|
2016-07-06 04:48:35 +03:00
|
|
|
// lightningID is the sha256 of the public key corresponding to our
|
|
|
|
// long-term identity private key.
|
|
|
|
lightningID [32]byte
|
|
|
|
|
2016-01-17 06:07:44 +03:00
|
|
|
listeners []net.Listener
|
|
|
|
peers map[int32]*peer
|
2016-01-14 08:41:46 +03:00
|
|
|
|
2016-09-12 22:33:22 +03:00
|
|
|
rpcServer *rpcServer
|
|
|
|
|
2016-08-04 22:37:50 +03:00
|
|
|
chainNotifier chainntnfs.ChainNotifier
|
2016-09-12 22:33:22 +03:00
|
|
|
|
|
|
|
bio lnwallet.BlockChainIO
|
|
|
|
lnwallet *lnwallet.LightningWallet
|
2016-06-21 21:52:09 +03:00
|
|
|
|
|
|
|
fundingMgr *fundingManager
|
|
|
|
chanDB *channeldb.DB
|
2016-01-14 08:41:46 +03:00
|
|
|
|
2016-11-29 06:43:57 +03:00
|
|
|
htlcSwitch *htlcSwitch
|
|
|
|
invoices *invoiceRegistry
|
|
|
|
breachArbiter *breachArbiter
|
2016-07-10 02:36:25 +03:00
|
|
|
|
2016-07-15 14:02:59 +03:00
|
|
|
routingMgr *routing.RoutingManager
|
|
|
|
|
2016-09-12 22:37:51 +03:00
|
|
|
utxoNursery *utxoNursery
|
|
|
|
|
2016-09-21 03:15:26 +03:00
|
|
|
sphinx *sphinx.Router
|
|
|
|
|
2016-01-14 08:41:46 +03:00
|
|
|
newPeers chan *peer
|
|
|
|
donePeers chan *peer
|
2016-01-17 06:07:44 +03:00
|
|
|
queries chan interface{}
|
2016-01-14 08:41:46 +03:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// newServer creates a new instance of the server which is to listen using the
|
|
|
|
// passed listener address.
|
2016-08-04 22:37:50 +03:00
|
|
|
func newServer(listenAddrs []string, notifier chainntnfs.ChainNotifier,
|
2016-09-12 22:33:22 +03:00
|
|
|
bio lnwallet.BlockChainIO, wallet *lnwallet.LightningWallet,
|
|
|
|
chanDB *channeldb.DB) (*server, error) {
|
2016-03-23 04:49:22 +03:00
|
|
|
|
2016-08-13 01:51:53 +03:00
|
|
|
privKey, err := wallet.GetIdentitykey()
|
2016-01-17 06:07:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := make([]net.Listener, len(listenAddrs))
|
|
|
|
for i, addr := range listenAddrs {
|
2016-10-28 05:49:10 +03:00
|
|
|
listeners[i], err = brontide.NewListener(privKey, addr)
|
2016-01-17 06:07:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 04:48:35 +03:00
|
|
|
serializedPubKey := privKey.PubKey().SerializeCompressed()
|
2016-01-17 06:07:44 +03:00
|
|
|
s := &server{
|
2016-11-29 06:43:57 +03:00
|
|
|
lnwallet: wallet,
|
2016-09-12 22:33:22 +03:00
|
|
|
bio: bio,
|
2016-08-04 22:37:50 +03:00
|
|
|
chainNotifier: notifier,
|
|
|
|
chanDB: chanDB,
|
2016-11-29 06:43:57 +03:00
|
|
|
|
|
|
|
invoices: newInvoiceRegistry(chanDB),
|
|
|
|
utxoNursery: newUtxoNursery(notifier, wallet),
|
|
|
|
|
|
|
|
identityPriv: privKey,
|
|
|
|
|
2016-09-21 03:15:26 +03:00
|
|
|
// TODO(roasbeef): derive proper onion key based on rotation
|
|
|
|
// schedule
|
|
|
|
sphinx: sphinx.NewRouter(privKey, activeNetParams.Params),
|
|
|
|
lightningID: fastsha256.Sum256(serializedPubKey),
|
2016-11-29 06:43:57 +03:00
|
|
|
|
|
|
|
listeners: listeners,
|
|
|
|
|
|
|
|
peers: make(map[int32]*peer),
|
|
|
|
newPeers: make(chan *peer, 100),
|
|
|
|
donePeers: make(chan *peer, 100),
|
|
|
|
queries: make(chan interface{}),
|
|
|
|
quit: make(chan struct{}),
|
2016-01-17 06:07:44 +03:00
|
|
|
}
|
|
|
|
|
2016-09-19 22:03:38 +03:00
|
|
|
// If the debug HTLC flag is on, then we invoice a "master debug"
|
|
|
|
// invoice which all outgoing payments will be sent and all incoming
|
|
|
|
// HTLC's with the debug R-Hash immediately settled.
|
|
|
|
if cfg.DebugHTLC {
|
|
|
|
kiloCoin := btcutil.Amount(btcutil.SatoshiPerBitcoin * 1000)
|
|
|
|
s.invoices.AddDebugInvoice(kiloCoin, *debugPre)
|
|
|
|
srvrLog.Debugf("Debug HTLC invoice inserted, preimage=%x, hash=%x",
|
|
|
|
debugPre[:], debugHash[:])
|
|
|
|
}
|
2016-07-13 03:14:07 +03:00
|
|
|
|
2016-09-12 22:37:51 +03:00
|
|
|
s.utxoNursery = newUtxoNursery(notifier, wallet)
|
|
|
|
|
2016-07-15 14:02:59 +03:00
|
|
|
// Create a new routing manager with ourself as the sole node within
|
|
|
|
// the graph.
|
2016-10-05 23:47:02 +03:00
|
|
|
selfVertex := serializedPubKey
|
|
|
|
routingMgrConfig := &routing.RoutingConfig{}
|
2016-11-29 06:43:57 +03:00
|
|
|
routingMgrConfig.SendMessage = func(receiver [33]byte, msg lnwire.Message) error {
|
2016-10-05 23:47:02 +03:00
|
|
|
receiverID := graph.NewVertex(receiver[:])
|
|
|
|
if receiverID == graph.NilVertex {
|
|
|
|
peerLog.Critical("receiverID == graph.NilVertex")
|
|
|
|
return fmt.Errorf("receiverID == graph.NilVertex")
|
|
|
|
}
|
|
|
|
|
|
|
|
var targetPeer *peer
|
|
|
|
for _, peer := range s.peers { // TODO: threadsafe api
|
|
|
|
nodePub := peer.addr.IdentityKey.SerializeCompressed()
|
|
|
|
nodeVertex := graph.NewVertex(nodePub[:])
|
|
|
|
|
2016-11-29 06:43:57 +03:00
|
|
|
// We found the target
|
2016-10-05 23:47:02 +03:00
|
|
|
if receiverID == nodeVertex {
|
|
|
|
targetPeer = peer
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetPeer != nil {
|
|
|
|
targetPeer.queueMsg(msg, nil)
|
|
|
|
} else {
|
|
|
|
srvrLog.Errorf("Can't find peer to send message %v",
|
|
|
|
receiverID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.routingMgr = routing.NewRoutingManager(graph.NewVertex(selfVertex), routingMgrConfig)
|
2016-09-26 20:35:10 +03:00
|
|
|
s.htlcSwitch = newHtlcSwitch(serializedPubKey, s.routingMgr)
|
2016-07-15 14:02:59 +03:00
|
|
|
|
2016-01-17 06:07:44 +03:00
|
|
|
s.rpcServer = newRpcServer(s)
|
|
|
|
|
2016-11-29 06:43:57 +03:00
|
|
|
s.breachArbiter = newBreachArbiter(wallet, chanDB, notifier, s.htlcSwitch)
|
|
|
|
|
|
|
|
s.fundingMgr = newFundingManager(wallet, s.breachArbiter)
|
|
|
|
|
|
|
|
// TODO(roasbeef): introduce closure and config system to decouple the
|
|
|
|
// initialization above ^
|
|
|
|
|
2016-01-17 06:07:44 +03:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// Start starts the main daemon server, all requested listeners, and any helper
|
|
|
|
// goroutines.
|
2016-08-04 22:37:50 +03:00
|
|
|
func (s *server) Start() error {
|
2016-06-21 21:52:09 +03:00
|
|
|
// Already running?
|
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
2016-08-04 22:37:50 +03:00
|
|
|
return nil
|
2016-01-17 06:09:02 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// Start all the listeners.
|
|
|
|
for _, l := range s.listeners {
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.listener(l)
|
2016-01-17 06:09:02 +03:00
|
|
|
}
|
|
|
|
|
2016-11-29 06:43:57 +03:00
|
|
|
// Start the notification server. This is used so channel management
|
2016-08-04 22:37:50 +03:00
|
|
|
// goroutines can be notified when a funding transaction reaches a
|
|
|
|
// sufficient number of confirmations, or when the input for the
|
2016-11-29 06:43:57 +03:00
|
|
|
// funding transaction is spent in an attempt at an uncooperative close
|
|
|
|
// by the counter party.
|
2016-08-04 22:37:50 +03:00
|
|
|
if err := s.chainNotifier.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.rpcServer.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.fundingMgr.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.htlcSwitch.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-12 22:37:51 +03:00
|
|
|
if err := s.utxoNursery.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-29 06:43:57 +03:00
|
|
|
if err := s.breachArbiter.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-15 14:02:59 +03:00
|
|
|
s.routingMgr.Start()
|
2016-06-21 21:52:09 +03:00
|
|
|
|
2016-07-14 02:34:23 +03:00
|
|
|
s.wg.Add(1)
|
2016-06-21 21:52:09 +03:00
|
|
|
go s.queryHandler()
|
2016-08-04 22:37:50 +03:00
|
|
|
|
|
|
|
return nil
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// Stop gracefully shutsdown the main daemon server. This function will signal
|
|
|
|
// any active goroutines, or helper objects to exit, then blocks until they've
|
|
|
|
// all successfully exited. Additionally, any/all listeners are closed.
|
|
|
|
func (s *server) Stop() error {
|
|
|
|
// Bail if we're already shutting down.
|
|
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop all the listeners.
|
|
|
|
for _, listener := range s.listeners {
|
|
|
|
if err := listener.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:37:50 +03:00
|
|
|
// Shutdown the wallet, funding manager, and the rpc server.
|
|
|
|
s.chainNotifier.Stop()
|
2016-06-21 21:52:09 +03:00
|
|
|
s.rpcServer.Stop()
|
|
|
|
s.fundingMgr.Stop()
|
2016-07-15 14:02:59 +03:00
|
|
|
s.routingMgr.Stop()
|
2016-08-04 22:37:50 +03:00
|
|
|
s.htlcSwitch.Stop()
|
2016-09-12 22:37:51 +03:00
|
|
|
s.utxoNursery.Stop()
|
2016-11-29 06:43:57 +03:00
|
|
|
s.breachArbiter.Stop()
|
2016-08-04 22:37:50 +03:00
|
|
|
|
|
|
|
s.lnwallet.Shutdown()
|
2016-07-15 14:02:59 +03:00
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// Signal all the lingering goroutines to quit.
|
|
|
|
close(s.quit)
|
|
|
|
s.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForShutdown blocks all goroutines have been stopped.
|
|
|
|
func (s *server) WaitForShutdown() {
|
|
|
|
s.wg.Wait()
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// addPeer adds the passed peer to the server's global state of all active
|
|
|
|
// peers.
|
|
|
|
func (s *server) addPeer(p *peer) {
|
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore new peers if we're shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
p.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.peers[p.id] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// removePeer removes the passed peer from the server's state of all active
|
|
|
|
// peers.
|
|
|
|
func (s *server) removePeer(p *peer) {
|
2016-07-14 02:34:23 +03:00
|
|
|
srvrLog.Debugf("removing peer %v", p)
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore deleting peers if we're shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
p.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(s.peers, p.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectPeerMsg is a message requesting the server to open a connection to a
|
|
|
|
// particular peer. This message also houses an error channel which will be
|
|
|
|
// used to report success/failure.
|
2016-01-17 06:09:02 +03:00
|
|
|
type connectPeerMsg struct {
|
2016-10-28 05:49:10 +03:00
|
|
|
addr *lnwire.NetAddress
|
2016-06-21 21:52:09 +03:00
|
|
|
resp chan int32
|
|
|
|
err chan error
|
2016-01-17 06:09:02 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// listPeersMsg is a message sent to the server in order to obtain a listing
|
|
|
|
// of all currently active channels.
|
|
|
|
type listPeersMsg struct {
|
|
|
|
resp chan []*peer
|
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
// openChanReq is a message sent to the server in order to request the
|
2016-09-14 01:35:41 +03:00
|
|
|
// initiation of a channel funding workflow to the peer with either the specified
|
|
|
|
// relative peer ID, or a global lightning ID.
|
2016-06-21 22:32:32 +03:00
|
|
|
type openChanReq struct {
|
2016-09-14 01:35:41 +03:00
|
|
|
targetPeerID int32
|
2016-10-28 05:49:10 +03:00
|
|
|
targetPubkey *btcec.PublicKey
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): make enums in lnwire
|
|
|
|
channelType uint8
|
|
|
|
coinType uint64
|
|
|
|
|
|
|
|
localFundingAmt btcutil.Amount
|
|
|
|
remoteFundingAmt btcutil.Amount
|
|
|
|
|
|
|
|
numConfs uint32
|
|
|
|
|
2016-08-31 02:52:53 +03:00
|
|
|
updates chan *lnrpc.OpenStatusUpdate
|
|
|
|
err chan error
|
2016-06-21 22:32:32 +03:00
|
|
|
}
|
|
|
|
|
2016-07-27 21:32:27 +03:00
|
|
|
// queryHandler handles any requests to modify the server's internal state of
|
2016-07-14 02:34:23 +03:00
|
|
|
// all active peers, or query/mutate the server's global state. Additionally,
|
|
|
|
// any queries directed at peers will be handled by this goroutine.
|
2016-06-21 21:52:09 +03:00
|
|
|
//
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
2016-01-14 08:41:46 +03:00
|
|
|
func (s *server) queryHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2016-07-14 02:34:23 +03:00
|
|
|
// New peers.
|
|
|
|
case p := <-s.newPeers:
|
|
|
|
s.addPeer(p)
|
|
|
|
|
|
|
|
// Finished peers.
|
|
|
|
case p := <-s.donePeers:
|
|
|
|
s.removePeer(p)
|
|
|
|
|
2016-01-17 06:09:02 +03:00
|
|
|
case query := <-s.queries:
|
2016-06-21 21:52:09 +03:00
|
|
|
// TODO(roasbeef): make all goroutines?
|
2016-01-17 06:09:02 +03:00
|
|
|
switch msg := query.(type) {
|
|
|
|
case *connectPeerMsg:
|
2016-06-21 21:52:09 +03:00
|
|
|
s.handleConnectPeer(msg)
|
|
|
|
case *listPeersMsg:
|
|
|
|
s.handleListPeers(msg)
|
2016-06-21 22:32:32 +03:00
|
|
|
case *openChanReq:
|
|
|
|
s.handleOpenChanReq(msg)
|
2016-01-17 06:09:02 +03:00
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// handleListPeers sends a lice of all currently active peers to the original
|
|
|
|
// caller.
|
2016-06-21 21:52:09 +03:00
|
|
|
func (s *server) handleListPeers(msg *listPeersMsg) {
|
|
|
|
peers := make([]*peer, 0, len(s.peers))
|
|
|
|
for _, peer := range s.peers {
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
2016-01-17 06:09:02 +03:00
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
msg.resp <- peers
|
2016-01-17 06:09:02 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// handleConnectPeer attempts to establish a connection to the address enclosed
|
|
|
|
// within the passed connectPeerMsg. This function is *async*, a goroutine will
|
|
|
|
// be spawned in order to finish the request, and respond to the caller.
|
|
|
|
func (s *server) handleConnectPeer(msg *connectPeerMsg) {
|
|
|
|
addr := msg.addr
|
|
|
|
|
|
|
|
// Ensure we're not already connected to this
|
|
|
|
// peer.
|
2016-10-28 05:49:10 +03:00
|
|
|
targetPub := msg.addr.IdentityKey
|
2016-06-21 21:52:09 +03:00
|
|
|
for _, peer := range s.peers {
|
2016-10-28 05:49:10 +03:00
|
|
|
if peer.addr.IdentityKey.IsEqual(targetPub) {
|
2016-06-21 21:52:09 +03:00
|
|
|
msg.err <- fmt.Errorf(
|
|
|
|
"already connected to peer: %v",
|
2016-10-28 05:49:10 +03:00
|
|
|
peer.addr,
|
2016-06-21 21:52:09 +03:00
|
|
|
)
|
|
|
|
msg.resp <- -1
|
2016-07-17 04:00:52 +03:00
|
|
|
return
|
2016-06-21 21:52:09 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// Launch a goroutine to connect to the requested peer so we can
|
|
|
|
// continue to handle queries.
|
|
|
|
//
|
2016-06-21 21:52:09 +03:00
|
|
|
// TODO(roasbeef): semaphore to limit the number of goroutines for
|
|
|
|
// async requests.
|
|
|
|
go func() {
|
2016-10-28 05:49:10 +03:00
|
|
|
srvrLog.Debugf("connecting to %v", addr)
|
2016-06-21 21:52:09 +03:00
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// Attempt to connect to the remote node. If the we can't make
|
|
|
|
// the connection, or the crypto negotation breaks down, then
|
|
|
|
// return an error to the caller.
|
|
|
|
conn, err := brontide.Dial(s.identityPriv, addr)
|
|
|
|
if err != nil {
|
2016-06-21 21:52:09 +03:00
|
|
|
msg.err <- err
|
|
|
|
msg.resp <- -1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// Now that we've established a connection, create a peer, and
|
|
|
|
// it to the set of currently active peers.
|
|
|
|
peer, err := newPeer(conn, s, msg.addr, false)
|
2016-01-14 08:41:46 +03:00
|
|
|
if err != nil {
|
2016-06-21 21:52:09 +03:00
|
|
|
srvrLog.Errorf("unable to create peer %v", err)
|
2016-07-14 02:34:23 +03:00
|
|
|
conn.Close()
|
2016-06-21 21:52:09 +03:00
|
|
|
msg.resp <- -1
|
|
|
|
msg.err <- err
|
|
|
|
return
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
2016-01-17 06:09:02 +03:00
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// TODO(roasbeef): update IP address for link-node
|
|
|
|
// * also mark last-seen, do it one single transaction?
|
|
|
|
|
2016-01-17 06:09:02 +03:00
|
|
|
peer.Start()
|
2016-05-23 23:54:34 +03:00
|
|
|
s.newPeers <- peer
|
2016-06-21 21:52:09 +03:00
|
|
|
|
|
|
|
msg.resp <- peer.id
|
|
|
|
msg.err <- nil
|
|
|
|
}()
|
|
|
|
}
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
// handleOpenChanReq first locates the target peer, and if found hands off the
|
|
|
|
// request to the funding manager allowing it to initiate the channel funding
|
|
|
|
// workflow.
|
|
|
|
func (s *server) handleOpenChanReq(req *openChanReq) {
|
|
|
|
// First attempt to locate the target peer to open a channel with, if
|
|
|
|
// we're unable to locate the peer then this request will fail.
|
|
|
|
var targetPeer *peer
|
|
|
|
for _, peer := range s.peers { // TODO(roasbeef): threadsafe api
|
|
|
|
// We found the the target
|
2016-10-28 05:49:10 +03:00
|
|
|
if peer.addr.IdentityKey.IsEqual(req.targetPubkey) ||
|
|
|
|
req.targetPeerID == peer.id {
|
2016-06-21 22:32:32 +03:00
|
|
|
targetPeer = peer
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetPeer == nil {
|
2016-10-28 05:49:10 +03:00
|
|
|
req.err <- fmt.Errorf("unable to find peer nodeID(%x), "+
|
|
|
|
"peerID(%v)", req.targetPubkey.SerializeCompressed(),
|
|
|
|
req.targetPeerID)
|
2016-06-21 22:32:32 +03:00
|
|
|
return
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// Spawn a goroutine to send the funding workflow request to the funding
|
|
|
|
// manager. This allows the server to continue handling queries instead of
|
|
|
|
// blocking on this request which is exporeted as a synchronous request to
|
|
|
|
// the outside world.
|
2016-08-31 02:52:53 +03:00
|
|
|
// TODO(roasbeef): server semaphore to restrict num goroutines
|
|
|
|
go s.fundingMgr.initFundingWorkflow(targetPeer, req)
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 22:32:32 +03:00
|
|
|
// ConnectToPeer requests that the server connect to a Lightning Network peer
|
|
|
|
// at the specified address. This function will *block* until either a
|
|
|
|
// connection is established, or the initial handshake process fails.
|
2016-10-28 05:49:10 +03:00
|
|
|
func (s *server) ConnectToPeer(addr *lnwire.NetAddress) (int32, error) {
|
2016-06-21 22:32:32 +03:00
|
|
|
reply := make(chan int32, 1)
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
|
|
|
s.queries <- &connectPeerMsg{addr, reply, errChan}
|
|
|
|
|
|
|
|
return <-reply, <-errChan
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenChannel sends a request to the server to open a channel to the specified
|
|
|
|
// peer identified by ID with the passed channel funding paramters.
|
2016-10-28 05:49:10 +03:00
|
|
|
func (s *server) OpenChannel(peerID int32, nodeKey *btcec.PublicKey,
|
|
|
|
localAmt, remoteAmt btcutil.Amount,
|
2016-08-31 02:52:53 +03:00
|
|
|
numConfs uint32) (chan *lnrpc.OpenStatusUpdate, chan error) {
|
2016-06-21 22:32:32 +03:00
|
|
|
|
|
|
|
errChan := make(chan error, 1)
|
2016-08-31 02:52:53 +03:00
|
|
|
updateChan := make(chan *lnrpc.OpenStatusUpdate, 1)
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2016-09-14 01:35:41 +03:00
|
|
|
req := &openChanReq{
|
|
|
|
targetPeerID: peerID,
|
2016-10-28 05:49:10 +03:00
|
|
|
targetPubkey: nodeKey,
|
2016-06-21 22:32:32 +03:00
|
|
|
localFundingAmt: localAmt,
|
|
|
|
remoteFundingAmt: remoteAmt,
|
|
|
|
numConfs: numConfs,
|
2016-09-14 01:35:41 +03:00
|
|
|
updates: updateChan,
|
|
|
|
err: errChan,
|
2016-06-21 22:32:32 +03:00
|
|
|
}
|
2016-09-14 01:35:41 +03:00
|
|
|
|
|
|
|
s.queries <- req
|
2016-06-21 22:32:32 +03:00
|
|
|
|
2016-08-31 02:52:53 +03:00
|
|
|
return updateChan, errChan
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
// Peers returns a slice of all active peers.
|
|
|
|
func (s *server) Peers() []*peer {
|
|
|
|
resp := make(chan []*peer)
|
|
|
|
|
|
|
|
s.queries <- &listPeersMsg{resp}
|
|
|
|
|
|
|
|
return <-resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// listener is a goroutine dedicated to accepting in coming peer connections
|
|
|
|
// from the passed listener.
|
|
|
|
//
|
|
|
|
// NOTE: This MUST be run as a goroutine.
|
|
|
|
func (s *server) listener(l net.Listener) {
|
|
|
|
srvrLog.Infof("Server listening on %s", l.Addr())
|
|
|
|
for atomic.LoadInt32(&s.shutdown) == 0 {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// Only log the error message if we aren't currently
|
|
|
|
// shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) == 0 {
|
|
|
|
srvrLog.Errorf("Can't accept connection: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
srvrLog.Tracef("New inbound connection from %v", conn.RemoteAddr())
|
2016-10-28 05:49:10 +03:00
|
|
|
|
|
|
|
brontideConn := conn.(*brontide.Conn)
|
|
|
|
peerAddr := &lnwire.NetAddress{
|
|
|
|
IdentityKey: brontideConn.RemotePub(),
|
|
|
|
Address: conn.RemoteAddr().(*net.TCPAddr),
|
|
|
|
ChainNet: activeNetParams.Net,
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := newPeer(conn, s, peerAddr, true)
|
2016-06-21 21:52:09 +03:00
|
|
|
if err != nil {
|
|
|
|
srvrLog.Errorf("unable to create peer: %v", err)
|
2016-07-14 02:34:23 +03:00
|
|
|
conn.Close()
|
2016-06-21 21:52:09 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-10-28 05:49:10 +03:00
|
|
|
// TODO(roasbeef): update IP address for link-node
|
|
|
|
// * also mark last-seen, do it one single transaction?
|
|
|
|
|
2016-06-21 21:52:09 +03:00
|
|
|
peer.Start()
|
|
|
|
s.newPeers <- peer
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|