2015-12-17 03:42:52 +03:00
|
|
|
package main
|
|
|
|
|
2015-12-30 03:23:27 +03:00
|
|
|
import (
|
2016-08-30 07:39:01 +03:00
|
|
|
"encoding/hex"
|
2015-12-30 03:23:27 +03:00
|
|
|
"fmt"
|
2016-03-23 04:50:11 +03:00
|
|
|
"io/ioutil"
|
2015-12-30 03:23:27 +03:00
|
|
|
"net"
|
2016-01-17 06:09:41 +03:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2015-12-30 05:59:16 +03:00
|
|
|
"os"
|
2016-03-23 04:50:11 +03:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2016-02-23 05:24:56 +03:00
|
|
|
"strconv"
|
2017-01-06 00:15:58 +03:00
|
|
|
"strings"
|
2015-12-30 03:23:27 +03:00
|
|
|
|
2016-10-16 01:35:26 +03:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2016-10-16 00:38:47 +03:00
|
|
|
proxy "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
2016-08-04 22:37:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
|
2016-03-23 04:50:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-01-16 21:38:48 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2016-08-13 01:56:57 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
|
2016-10-16 00:38:47 +03:00
|
|
|
|
2016-08-04 22:37:50 +03:00
|
|
|
"github.com/roasbeef/btcrpcclient"
|
2015-12-30 03:23:27 +03:00
|
|
|
)
|
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
var (
|
|
|
|
cfg *config
|
|
|
|
shutdownChannel = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
2016-07-13 03:03:29 +03: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 04:50:11 +03:00
|
|
|
// Load the configuration, and parse any command line options. This
|
|
|
|
// function will also set up logging properly.
|
2016-02-23 05:24:56 +03:00
|
|
|
loadedConfig, err := loadConfig()
|
|
|
|
if err != nil {
|
2016-07-13 03:03:29 +03:00
|
|
|
return err
|
2016-02-23 05:24:56 +03:00
|
|
|
}
|
2016-03-23 04:50:11 +03:00
|
|
|
cfg = loadedConfig
|
|
|
|
defer backendLog.Flush()
|
|
|
|
|
|
|
|
// Show version at startup.
|
|
|
|
ltndLog.Infof("Version %s", version())
|
2016-02-23 05:24:56 +03:00
|
|
|
|
2016-06-21 07:42:07 +03: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 06:01:06 +03:00
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
// Open the channeldb, which is dedicated to storing channel, and
|
2017-01-13 08:01:50 +03:00
|
|
|
// network related metadata.
|
2016-12-22 23:09:19 +03:00
|
|
|
chanDB, err := channeldb.Open(cfg.DataDir)
|
2016-03-23 04:50:11 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("unable to open channeldb: ", err)
|
2016-07-13 03:03:29 +03:00
|
|
|
return err
|
2016-03-23 04:50:11 +03:00
|
|
|
}
|
|
|
|
defer chanDB.Close()
|
|
|
|
|
2016-08-30 07:39:01 +03:00
|
|
|
// Next load btcd's TLS cert for the RPC connection. If a raw cert was
|
2016-11-29 05:44:14 +03:00
|
|
|
// specified in the config, then we'll set that directly. Otherwise, we
|
2016-08-30 07:39:01 +03:00
|
|
|
// attempt to read the cert from the path specified in the config.
|
|
|
|
var rpcCert []byte
|
2016-10-15 16:45:51 +03:00
|
|
|
if cfg.RawRPCCert != "" {
|
|
|
|
rpcCert, err = hex.DecodeString(cfg.RawRPCCert)
|
2016-08-30 07:39:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-15 16:45:51 +03:00
|
|
|
certFile, err := os.Open(cfg.RPCCert)
|
2016-08-30 07:39:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rpcCert, err = ioutil.ReadAll(certFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := certFile.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-23 04:50:11 +03:00
|
|
|
}
|
2016-08-30 07:39:01 +03:00
|
|
|
|
2017-01-06 00:15:58 +03:00
|
|
|
// If the specified host for the btcd RPC server already has a port
|
|
|
|
// specified, then we use that directly. Otherwise, we assume the
|
|
|
|
// default port according to the selected chain parameters.
|
|
|
|
var btcdHost string
|
|
|
|
if strings.Contains(cfg.RPCHost, ":") {
|
|
|
|
btcdHost = cfg.RPCHost
|
|
|
|
} else {
|
|
|
|
btcdHost = fmt.Sprintf("%v:%v", cfg.RPCHost, activeNetParams.rpcPort)
|
2016-03-23 04:50:11 +03:00
|
|
|
}
|
|
|
|
|
2016-10-15 16:45:51 +03:00
|
|
|
btcdUser := cfg.RPCUser
|
|
|
|
btcdPass := cfg.RPCPass
|
2016-08-04 22:37:50 +03:00
|
|
|
|
|
|
|
// TODO(roasbeef): parse config here and select chosen notifier instead
|
|
|
|
rpcConfig := &btcrpcclient.ConnConfig{
|
|
|
|
Host: btcdHost,
|
|
|
|
Endpoint: "ws",
|
|
|
|
User: btcdUser,
|
|
|
|
Pass: btcdPass,
|
|
|
|
Certificates: rpcCert,
|
|
|
|
DisableTLS: false,
|
|
|
|
DisableConnectOnNew: true,
|
|
|
|
DisableAutoReconnect: false,
|
|
|
|
}
|
|
|
|
notifier, err := btcdnotify.New(rpcConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-15 16:45:51 +03:00
|
|
|
// TODO(roasbeef): parse config here select chosen WalletController
|
2016-08-13 01:56:57 +03:00
|
|
|
walletConfig := &btcwallet.Config{
|
2016-02-23 05:24:56 +03:00
|
|
|
PrivatePass: []byte("hello"),
|
2016-10-15 16:45:51 +03:00
|
|
|
DataDir: filepath.Join(cfg.DataDir, "lnwallet"),
|
2017-01-06 00:15:58 +03:00
|
|
|
RpcHost: btcdHost,
|
2016-10-15 16:45:51 +03:00
|
|
|
RpcUser: cfg.RPCUser,
|
|
|
|
RpcPass: cfg.RPCPass,
|
2016-08-30 07:39:01 +03:00
|
|
|
CACert: rpcCert,
|
2016-07-14 04:37:50 +03:00
|
|
|
NetParams: activeNetParams.Params,
|
2016-02-23 05:24:56 +03:00
|
|
|
}
|
2016-08-13 01:56:57 +03:00
|
|
|
wc, err := btcwallet.New(walletConfig)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to create wallet controller: %v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
signer := wc
|
|
|
|
bio := wc
|
|
|
|
|
|
|
|
// Create, and start the lnwallet, which handles the core payment
|
|
|
|
// channel logic, and exposes control via proxy state machines.
|
|
|
|
wallet, err := lnwallet.NewLightningWallet(chanDB, notifier,
|
|
|
|
wc, signer, bio, activeNetParams.Params)
|
2015-12-30 03:23:27 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to create wallet: %v\n", err)
|
2016-07-13 03:03:29 +03:00
|
|
|
return err
|
2015-12-30 03:23:27 +03:00
|
|
|
}
|
2016-03-25 00:32:55 +03:00
|
|
|
if err := wallet.Startup(); err != nil {
|
2015-12-30 03:23:27 +03:00
|
|
|
fmt.Printf("unable to start wallet: %v\n", err)
|
2016-07-13 03:03:29 +03:00
|
|
|
return err
|
2015-12-30 03:23:27 +03:00
|
|
|
}
|
2016-03-23 04:50:11 +03:00
|
|
|
ltndLog.Info("LightningWallet opened")
|
2015-12-30 05:59:16 +03:00
|
|
|
|
2016-01-17 06:09:41 +03:00
|
|
|
// Set up the core server which will listen for incoming peer
|
|
|
|
// connections.
|
2016-03-23 04:50:11 +03:00
|
|
|
defaultListenAddrs := []string{
|
2016-10-15 16:45:51 +03:00
|
|
|
net.JoinHostPort("", strconv.Itoa(cfg.PeerPort)),
|
2016-03-23 04:50:11 +03:00
|
|
|
}
|
2016-09-12 22:33:22 +03:00
|
|
|
server, err := newServer(defaultListenAddrs, notifier, bio, wallet, chanDB)
|
2016-01-17 06:09:41 +03:00
|
|
|
if err != nil {
|
2016-03-25 00:32:55 +03:00
|
|
|
srvrLog.Errorf("unable to create server: %v\n", err)
|
2016-07-13 03:03:29 +03:00
|
|
|
return err
|
2016-01-17 06:09:41 +03:00
|
|
|
}
|
2016-08-04 22:37:50 +03:00
|
|
|
if err := server.Start(); err != nil {
|
2016-12-24 23:50:16 +03:00
|
|
|
srvrLog.Errorf("unable to create to start server: %v\n", err)
|
2016-08-04 22:37:50 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-01-17 06:09:41 +03:00
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
addInterruptHandler(func() {
|
|
|
|
ltndLog.Infof("Gracefully shutting down the server...")
|
|
|
|
server.Stop()
|
|
|
|
server.WaitForShutdown()
|
|
|
|
})
|
|
|
|
|
2015-12-30 03:23:27 +03:00
|
|
|
// Initialize, and register our implementation of the gRPC server.
|
|
|
|
var opts []grpc.ServerOption
|
|
|
|
grpcServer := grpc.NewServer(opts...)
|
2016-01-17 06:09:41 +03:00
|
|
|
lnrpc.RegisterLightningServer(grpcServer, server.rpcServer)
|
2015-12-30 03:23:27 +03:00
|
|
|
|
2016-10-16 00:38:47 +03: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 03:23:27 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to listen: %v", err)
|
2016-07-13 03:03:29 +03:00
|
|
|
return err
|
2015-12-30 03:23:27 +03:00
|
|
|
}
|
2016-11-16 23:42:00 +03:00
|
|
|
defer lis.Close()
|
2016-03-23 04:50:11 +03:00
|
|
|
go func() {
|
|
|
|
rpcsLog.Infof("RPC server listening on %s", lis.Addr())
|
|
|
|
grpcServer.Serve(lis)
|
|
|
|
}()
|
|
|
|
|
2016-10-16 00:38:47 +03:00
|
|
|
// Finally, start the REST proxy for our gRPC server above.
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
mux := proxy.NewServeMux()
|
|
|
|
swaggerPattern := proxy.MustPattern(proxy.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "swagger"}, ""))
|
|
|
|
// TODO(roasbeef): accept path to swagger file as command-line option
|
|
|
|
mux.Handle("GET", swaggerPattern, func(w http.ResponseWriter, r *http.Request, p map[string]string) {
|
|
|
|
http.ServeFile(w, r, "lnrpc/rpc.swagger.json")
|
|
|
|
})
|
|
|
|
proxyOpts := []grpc.DialOption{grpc.WithInsecure()}
|
|
|
|
err = lnrpc.RegisterLightningHandlerFromEndpoint(ctx, mux, grpcEndpoint, proxyOpts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
rpcsLog.Infof("gRPC proxy started")
|
|
|
|
http.ListenAndServe(":8080", mux)
|
|
|
|
}()
|
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
// Wait for shutdown signal from either a graceful server stop or from
|
|
|
|
// the interrupt handler.
|
|
|
|
<-shutdownChannel
|
|
|
|
ltndLog.Info("Shutdown complete")
|
2016-07-13 03:03:29 +03: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 {
|
2016-07-14 04:37:50 +03:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2016-07-13 03:03:29 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-12-17 03:42:52 +03:00
|
|
|
}
|