2015-12-26 09:09:17 +03:00
|
|
|
package main
|
2016-01-14 08:41:46 +03:00
|
|
|
|
|
|
|
import (
|
2016-01-17 06:07:44 +03:00
|
|
|
"encoding/hex"
|
2016-01-14 08:41:46 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2016-01-17 06:07:44 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2016-03-23 04:49:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-01-17 06:07:44 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lndc"
|
2016-01-16 21:38:48 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2016-01-14 08:41:46 +03:00
|
|
|
|
2016-01-17 06:07:44 +03:00
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
2016-01-14 08:41:46 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// server...
|
|
|
|
type server struct {
|
|
|
|
started int32 // atomic
|
|
|
|
shutdown int32 // atomic
|
|
|
|
|
2016-01-17 06:07:44 +03:00
|
|
|
longTermPriv *btcec.PrivateKey
|
|
|
|
|
|
|
|
listeners []net.Listener
|
|
|
|
peers map[int32]*peer
|
2016-01-14 08:41:46 +03:00
|
|
|
|
|
|
|
rpcServer *rpcServer
|
|
|
|
lnwallet *lnwallet.LightningWallet
|
2016-03-23 04:49:22 +03:00
|
|
|
chanDB *channeldb.DB
|
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-01-17 06:07:44 +03:00
|
|
|
// newServer...
|
2016-03-23 04:49:22 +03:00
|
|
|
func newServer(listenAddrs []string, wallet *lnwallet.LightningWallet,
|
|
|
|
chanDB *channeldb.DB) (*server, error) {
|
|
|
|
|
|
|
|
privKey, err := getIdentityPrivKey(chanDB, wallet)
|
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 {
|
|
|
|
listeners[i], err = lndc.NewListener(privKey, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &server{
|
2016-03-23 04:49:22 +03:00
|
|
|
chanDB: chanDB,
|
2016-01-17 06:07:44 +03:00
|
|
|
longTermPriv: privKey,
|
|
|
|
listeners: listeners,
|
|
|
|
peers: make(map[int32]*peer),
|
|
|
|
newPeers: make(chan *peer, 100),
|
|
|
|
donePeers: make(chan *peer, 100),
|
|
|
|
lnwallet: wallet,
|
|
|
|
queries: make(chan interface{}),
|
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rpcServer = newRpcServer(s)
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2016-01-14 08:41:46 +03:00
|
|
|
// addPeer...
|
|
|
|
func (s *server) addPeer(p *peer) {
|
2016-01-17 06:09:02 +03:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore new peers if we're shutting down.
|
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
|
|
|
p.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.peers[p.peerId] = p
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// removePeer...
|
|
|
|
func (s *server) removePeer(p *peer) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// peerManager...
|
|
|
|
func (s *server) peerManager() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// New peers.
|
|
|
|
case p := <-s.newPeers:
|
|
|
|
s.addPeer(p)
|
|
|
|
// Finished peers.
|
|
|
|
case p := <-s.donePeers:
|
|
|
|
s.removePeer(p)
|
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2016-01-17 06:09:02 +03:00
|
|
|
// connectPeerMsg...
|
|
|
|
type connectPeerMsg struct {
|
2016-01-17 21:45:07 +03:00
|
|
|
addr *lndc.LNAdr
|
2016-01-17 06:09:02 +03:00
|
|
|
reply chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryHandler...
|
2016-01-14 08:41:46 +03:00
|
|
|
func (s *server) queryHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
2016-01-17 06:09:02 +03:00
|
|
|
case query := <-s.queries:
|
|
|
|
switch msg := query.(type) {
|
|
|
|
case *connectPeerMsg:
|
|
|
|
addr := msg.addr
|
|
|
|
|
|
|
|
// Ensure we're not already connected to this
|
|
|
|
// peer.
|
|
|
|
for _, peer := range s.peers {
|
|
|
|
if peer.lightningAddr.String() ==
|
|
|
|
addr.String() {
|
|
|
|
msg.reply <- fmt.Errorf(
|
|
|
|
"already connected to peer: %v",
|
|
|
|
peer.lightningAddr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch a goroutine to connect to the requested
|
|
|
|
// peer so we can continue to handle queries.
|
|
|
|
go func() {
|
|
|
|
// For the lndc crypto handshake, we
|
|
|
|
// either need a compressed pubkey, or a
|
|
|
|
// 20-byte pkh.
|
|
|
|
var remoteId []byte
|
2016-01-17 21:45:07 +03:00
|
|
|
if addr.PubKey == nil {
|
2016-01-19 11:05:02 +03:00
|
|
|
remoteId = addr.Base58Adr.ScriptAddress()
|
2016-01-17 06:09:02 +03:00
|
|
|
} else {
|
2016-01-17 21:45:07 +03:00
|
|
|
remoteId = addr.PubKey.SerializeCompressed()
|
2016-01-17 06:09:02 +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.
|
2016-01-17 21:45:07 +03:00
|
|
|
ipAddr := addr.NetAddr.String()
|
2016-01-17 22:20:40 +03:00
|
|
|
conn := lndc.NewConn(nil)
|
|
|
|
if err := conn.Dial(
|
|
|
|
s.longTermPriv, ipAddr, remoteId); err != nil {
|
2016-01-17 06:09:02 +03:00
|
|
|
msg.reply <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've established a connection,
|
|
|
|
// create a peer, and it to the set of
|
|
|
|
// currently active peers.
|
|
|
|
peer := newPeer(conn, s)
|
|
|
|
s.newPeers <- peer
|
|
|
|
|
|
|
|
msg.reply <- nil
|
|
|
|
}()
|
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
case <-s.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
2016-01-17 06:09:02 +03:00
|
|
|
// ConnectToPeer...
|
2016-01-17 21:45:07 +03:00
|
|
|
func (s *server) ConnectToPeer(addr *lndc.LNAdr) error {
|
2016-01-17 06:09:02 +03:00
|
|
|
reply := make(chan error, 1)
|
|
|
|
|
|
|
|
s.queries <- &connectPeerMsg{addr, reply}
|
|
|
|
|
|
|
|
return <-reply
|
|
|
|
}
|
|
|
|
|
2016-01-14 08:41:46 +03:00
|
|
|
// AddPeer...
|
|
|
|
func (s *server) AddPeer(p *peer) {
|
|
|
|
s.newPeers <- p
|
|
|
|
}
|
|
|
|
|
|
|
|
// listener...
|
|
|
|
func (s *server) listener(l net.Listener) {
|
2016-03-23 04:49:22 +03:00
|
|
|
srvrLog.Infof("Server listening on %s", l.Addr())
|
2016-01-14 08:41:46 +03:00
|
|
|
for atomic.LoadInt32(&s.shutdown) == 0 {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
2016-04-24 22:40:23 +03:00
|
|
|
// 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)
|
|
|
|
}
|
2016-01-14 08:41:46 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-01-17 06:09:02 +03:00
|
|
|
|
2016-03-23 04:49:22 +03:00
|
|
|
srvrLog.Tracef("New inbound connection from %v", conn.RemoteAddr())
|
2016-01-17 06:09:02 +03:00
|
|
|
peer := newPeer(conn, s)
|
|
|
|
peer.Start()
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start...
|
|
|
|
func (s *server) Start() {
|
|
|
|
// Already running?
|
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
s.wg.Add(2)
|
2016-01-14 08:41:46 +03:00
|
|
|
go s.peerManager()
|
2016-01-17 06:09:02 +03:00
|
|
|
go s.queryHandler()
|
2016-01-14 08:41:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop...
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.rpcServer.Stop()
|
2016-04-24 22:40:40 +03:00
|
|
|
s.lnwallet.Shutdown()
|
2016-01-14 08:41:46 +03:00
|
|
|
|
|
|
|
// Signal all the lingering goroutines to quit.
|
|
|
|
close(s.quit)
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-17 06:05:13 +03:00
|
|
|
|
|
|
|
// getIdentityPrivKey gets the identity private key out of the wallet DB.
|
2016-03-23 04:49:22 +03:00
|
|
|
func getIdentityPrivKey(c *channeldb.DB, w *lnwallet.LightningWallet) (*btcec.PrivateKey, error) {
|
|
|
|
adr, err := c.GetIdAdr()
|
2016-01-17 06:05:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 04:49:22 +03:00
|
|
|
ltndLog.Infof("got ID address: %s", adr.String())
|
|
|
|
adr2, err := w.Manager.Address(adr)
|
2016-01-17 06:05:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 04:49:22 +03:00
|
|
|
ltndLog.Infof("pubkey: %v", hex.EncodeToString(adr2.(waddrmgr.ManagedPubKeyAddress).PubKey().SerializeCompressed()))
|
2016-01-17 06:05:13 +03:00
|
|
|
priv, err := adr2.(waddrmgr.ManagedPubKeyAddress).PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return priv, nil
|
|
|
|
}
|
2016-03-23 04:49:22 +03:00
|
|
|
|
|
|
|
// WaitForShutdown blocks all goroutines have been stopped.
|
|
|
|
func (s *server) WaitForShutdown() {
|
|
|
|
s.wg.Wait()
|
|
|
|
}
|