lnd: decouple rpcServer from server, wait till chain synced before starting server

This commit overhauls the way that lnd is created with the goal of
ensuring the chain backends are fully synced up before the daemon
itself starts. The rpcServer has been slightly decoupled from the
server itself s.t we can start the rpcServer independently of the
server. This is required as we’ll now wait (unless we’re in simnet
mode) for the chain to fully sync up before we even _start_ any of the
server’s goroutines.
This commit is contained in:
Olaoluwa Osuntokun committed 2017-06-05 19:22:12 -07:00
1 parent 467ccfde41
commit aec8c8dc77
3 files changed
+126 -79

No files matched your search

+4 -68
View File
@@ -2,7 +2,6 @@ package main
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
@@ -18,7 +17,6 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/discovery"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing"
"github.com/roasbeef/btcd/btcec"
@@ -57,8 +55,6 @@ type server struct {
inboundPeers map[string]*peer
outboundPeers map[string]*peer
rpcServer *rpcServer
cc *chainControl
fundingMgr *fundingManager
@@ -103,12 +99,10 @@ type server struct {
// newServer creates a new instance of the server which is to listen using the
// passed listener address.
func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl) (*server, error) {
privKey, err := cc.wallet.GetIdentitykey()
if err != nil {
return nil, err
}
privKey.Curve = btcec.S256()
func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl,
privKey *btcec.PrivateKey) (*server, error) {
var err error
listeners := make([]net.Listener, len(listenAddrs))
for i, addr := range listenAddrs {
@@ -264,59 +258,9 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl) (*s
return nil, err
}
s.rpcServer = newRPCServer(s)
s.breachArbiter = newBreachArbiter(cc.wallet, chanDB, cc.chainNotifier,
s.htlcSwitch, s.cc.chainIO, s.cc.feeEstimator)
var chanIDSeed [32]byte
if _, err := rand.Read(chanIDSeed[:]); err != nil {
return nil, err
}
s.fundingMgr, err = newFundingManager(fundingConfig{
IDKey: s.identityPriv.PubKey(),
Wallet: cc.wallet,
ChainIO: s.cc.chainIO,
Notifier: s.cc.chainNotifier,
FeeEstimator: s.cc.feeEstimator,
SignMessage: func(pubKey *btcec.PublicKey, msg []byte) (*btcec.Signature, error) {
if pubKey.IsEqual(s.identityPriv.PubKey()) {
return s.nodeSigner.SignMessage(pubKey, msg)
}
return cc.msgSigner.SignMessage(pubKey, msg)
},
SendAnnouncement: func(msg lnwire.Message) error {
s.discoverSrv.ProcessLocalAnnouncement(msg,
s.identityPriv.PubKey())
return nil
},
ArbiterChan: s.breachArbiter.newContracts,
SendToPeer: s.sendToPeer,
FindPeer: s.findPeer,
TempChanIDSeed: chanIDSeed,
FindChannel: func(chanID lnwire.ChannelID) (*lnwallet.LightningChannel, error) {
dbChannels, err := chanDB.FetchAllChannels()
if err != nil {
return nil, err
}
for _, channel := range dbChannels {
if chanID.IsChanPoint(channel.ChanID) {
return lnwallet.NewLightningChannel(
cc.signer, cc.chainNotifier,
cc.feeEstimator,
channel)
}
}
return nil, fmt.Errorf("unable to find channel")
},
})
if err != nil {
return nil, err
}
// TODO(roasbeef): introduce closure and config system to decouple the
// initialization above ^
@@ -357,12 +301,6 @@ func (s *server) Start() error {
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
}
@@ -403,8 +341,6 @@ func (s *server) Stop() error {
// Shutdown the wallet, funding manager, and the rpc server.
s.cc.chainNotifier.Stop()
s.rpcServer.Stop()
s.fundingMgr.Stop()
s.chanRouter.Stop()
s.htlcSwitch.Stop()
s.utxoNursery.Stop()