2015-12-17 01:42:52 +01:00
|
|
|
package main
|
|
|
|
|
2015-12-30 01:23:27 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2016-01-17 04:09:41 +01:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2015-12-30 03:59:16 +01:00
|
|
|
"os"
|
2016-03-23 02:50:11 +01:00
|
|
|
"runtime"
|
2016-02-23 03:24:56 +01:00
|
|
|
"strconv"
|
2015-12-30 01:23:27 +01:00
|
|
|
|
2016-10-16 00:35:26 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-03-23 02:50:11 +01:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2017-04-01 14:33:49 +02:00
|
|
|
flags "github.com/btcsuite/go-flags"
|
2016-10-15 23:38:47 +02:00
|
|
|
proxy "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2016-03-23 02:50:11 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-01-16 19:38:48 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2015-12-30 01:23:27 +01:00
|
|
|
)
|
|
|
|
|
2016-03-23 02:50:11 +01:00
|
|
|
var (
|
2017-05-03 04:49:14 +02:00
|
|
|
cfg *config
|
|
|
|
shutdownChannel = make(chan struct{})
|
|
|
|
registeredChains = newChainRegistry()
|
2016-03-23 02:50:11 +01:00
|
|
|
)
|
|
|
|
|
2016-07-13 02:03:29 +02:00
|
|
|
// lndMain is the true entry point for lnd. This function is required since
|
|
|
|
// defers created in the top-level scope of a main method aren't executed if
|
|
|
|
// os.Exit() is called.
|
|
|
|
func lndMain() error {
|
2016-03-23 02:50:11 +01:00
|
|
|
// Load the configuration, and parse any command line options. This
|
|
|
|
// function will also set up logging properly.
|
2016-02-23 03:24:56 +01:00
|
|
|
loadedConfig, err := loadConfig()
|
|
|
|
if err != nil {
|
2016-07-13 02:03:29 +02:00
|
|
|
return err
|
2016-02-23 03:24:56 +01:00
|
|
|
}
|
2016-03-23 02:50:11 +01:00
|
|
|
cfg = loadedConfig
|
|
|
|
defer backendLog.Flush()
|
|
|
|
|
|
|
|
// Show version at startup.
|
|
|
|
ltndLog.Infof("Version %s", version())
|
2016-02-23 03:24:56 +01:00
|
|
|
|
2016-06-21 06:42:07 +02:00
|
|
|
// Enable http profiling server if requested.
|
|
|
|
if cfg.Profile != "" {
|
|
|
|
go func() {
|
|
|
|
listenAddr := net.JoinHostPort("", cfg.Profile)
|
|
|
|
profileRedirect := http.RedirectHandler("/debug/pprof",
|
|
|
|
http.StatusSeeOther)
|
|
|
|
http.Handle("/", profileRedirect)
|
|
|
|
fmt.Println(http.ListenAndServe(listenAddr, nil))
|
|
|
|
}()
|
|
|
|
}
|
2016-01-17 04:01:06 +01:00
|
|
|
|
2016-03-23 02:50:11 +01:00
|
|
|
// Open the channeldb, which is dedicated to storing channel, and
|
2017-01-13 06:01:50 +01:00
|
|
|
// network related metadata.
|
2016-12-22 21:09:19 +01:00
|
|
|
chanDB, err := channeldb.Open(cfg.DataDir)
|
2016-03-23 02:50:11 +01:00
|
|
|
if err != nil {
|
2017-05-03 05:03:13 +02:00
|
|
|
ltndLog.Errorf("unable to open channeldb: %v", err)
|
2016-07-13 02:03:29 +02:00
|
|
|
return err
|
2016-03-23 02:50:11 +01:00
|
|
|
}
|
|
|
|
defer chanDB.Close()
|
|
|
|
|
2017-05-18 20:49:32 +02:00
|
|
|
// With the information parsed from the configuration, create valid
|
|
|
|
// instances of the paertinent interfaces required to operate the
|
|
|
|
// Lightning Network Daemon.
|
2017-05-25 02:46:37 +02:00
|
|
|
activeChainControl, chainCleanUp, err := newChainControlFromConfig(cfg, chanDB)
|
2015-12-30 01:23:27 +01:00
|
|
|
if err != nil {
|
2017-05-18 20:49:32 +02:00
|
|
|
fmt.Printf("unable to create chain control: %v\n", err)
|
2016-07-13 02:03:29 +02:00
|
|
|
return err
|
2015-12-30 01:23:27 +01:00
|
|
|
}
|
2017-05-25 02:46:37 +02:00
|
|
|
if chainCleanUp != nil {
|
|
|
|
defer chainCleanUp()
|
|
|
|
}
|
2017-03-27 19:25:44 +02:00
|
|
|
|
2017-05-03 04:49:14 +02:00
|
|
|
// Finally before we start the server, we'll register the "holy
|
|
|
|
// trinity" of interface for our current "home chain" with the active
|
|
|
|
// chainRegistry interface.
|
|
|
|
primaryChain := registeredChains.PrimaryChain()
|
2017-05-18 20:49:32 +02:00
|
|
|
registeredChains.RegisterChain(primaryChain, activeChainControl)
|
2017-05-03 04:49:14 +02:00
|
|
|
|
2017-05-18 20:49:32 +02:00
|
|
|
// Set up the core server which will listen for incoming peer
|
|
|
|
// connections.
|
|
|
|
defaultListenAddrs := []string{
|
|
|
|
net.JoinHostPort("", strconv.Itoa(cfg.PeerPort)),
|
2017-05-11 02:34:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 04:49:14 +02:00
|
|
|
// With all the relevant chains initialized, we can finally start the
|
|
|
|
// server itself.
|
2017-05-18 20:49:32 +02:00
|
|
|
server, err := newServer(defaultListenAddrs, chanDB, activeChainControl)
|
2016-01-17 04:09:41 +01:00
|
|
|
if err != nil {
|
2016-03-24 22:32:55 +01:00
|
|
|
srvrLog.Errorf("unable to create server: %v\n", err)
|
2016-07-13 02:03:29 +02:00
|
|
|
return err
|
2016-01-17 04:09:41 +01:00
|
|
|
}
|
2016-08-04 21:37:50 +02:00
|
|
|
if err := server.Start(); err != nil {
|
2016-12-24 21:50:16 +01:00
|
|
|
srvrLog.Errorf("unable to create to start server: %v\n", err)
|
2016-08-04 21:37:50 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-01-17 04:09:41 +01:00
|
|
|
|
2016-03-23 02:50:11 +01:00
|
|
|
addInterruptHandler(func() {
|
|
|
|
ltndLog.Infof("Gracefully shutting down the server...")
|
|
|
|
server.Stop()
|
|
|
|
server.WaitForShutdown()
|
|
|
|
})
|
|
|
|
|
2015-12-30 01:23:27 +01:00
|
|
|
// Initialize, and register our implementation of the gRPC server.
|
|
|
|
var opts []grpc.ServerOption
|
|
|
|
grpcServer := grpc.NewServer(opts...)
|
2016-01-17 04:09:41 +01:00
|
|
|
lnrpc.RegisterLightningServer(grpcServer, server.rpcServer)
|
2015-12-30 01:23:27 +01:00
|
|
|
|
2016-10-15 23:38:47 +02:00
|
|
|
// Next, Start the grpc server listening for HTTP/2 connections.
|
|
|
|
grpcEndpoint := fmt.Sprintf("localhost:%d", loadedConfig.RPCPort)
|
|
|
|
lis, err := net.Listen("tcp", grpcEndpoint)
|
2015-12-30 01:23:27 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to listen: %v", err)
|
2016-07-13 02:03:29 +02:00
|
|
|
return err
|
2015-12-30 01:23:27 +01:00
|
|
|
}
|
2016-11-16 21:42:00 +01:00
|
|
|
defer lis.Close()
|
2016-03-23 02:50:11 +01:00
|
|
|
go func() {
|
|
|
|
rpcsLog.Infof("RPC server listening on %s", lis.Addr())
|
|
|
|
grpcServer.Serve(lis)
|
|
|
|
}()
|
|
|
|
|
2016-10-15 23:38:47 +02:00
|
|
|
// Finally, start the REST proxy for our gRPC server above.
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
mux := proxy.NewServeMux()
|
|
|
|
proxyOpts := []grpc.DialOption{grpc.WithInsecure()}
|
2017-02-16 14:10:47 +01:00
|
|
|
err = lnrpc.RegisterLightningHandlerFromEndpoint(ctx, mux, grpcEndpoint,
|
|
|
|
proxyOpts)
|
2016-10-15 23:38:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
rpcsLog.Infof("gRPC proxy started")
|
|
|
|
http.ListenAndServe(":8080", mux)
|
|
|
|
}()
|
|
|
|
|
2016-03-23 02:50:11 +01:00
|
|
|
// Wait for shutdown signal from either a graceful server stop or from
|
|
|
|
// the interrupt handler.
|
|
|
|
<-shutdownChannel
|
|
|
|
ltndLog.Info("Shutdown complete")
|
2016-07-13 02:03:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Use all processor cores.
|
|
|
|
// TODO(roasbeef): remove this if required version # is > 1.6?
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
|
|
// Call the "real" main in a nested manner so the defers will properly
|
|
|
|
// be executed in the case of a graceful shutdown.
|
|
|
|
if err := lndMain(); err != nil {
|
2017-04-01 14:33:49 +02:00
|
|
|
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2016-07-13 02:03:29 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-12-17 01:42:52 +01:00
|
|
|
}
|