2015-12-17 03:42:52 +03:00
|
|
|
package main
|
|
|
|
|
2015-12-30 03:23:27 +03:00
|
|
|
import (
|
|
|
|
"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"
|
2015-12-30 03:23:27 +03:00
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2016-01-16 21:38:48 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2015-12-30 03:23:27 +03:00
|
|
|
)
|
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
var (
|
|
|
|
cfg *config
|
|
|
|
shutdownChannel = make(chan struct{})
|
|
|
|
)
|
|
|
|
|
2015-12-17 03:42:52 +03:00
|
|
|
func main() {
|
2016-03-23 04:50:11 +03:00
|
|
|
// Use all processor cores.
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
2015-12-30 03:23:27 +03:00
|
|
|
|
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 {
|
|
|
|
fmt.Printf("unable to load config: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if loadedConfig.SPVMode == true {
|
2016-03-23 04:50:11 +03:00
|
|
|
shell(loadedConfig.SPVHostAdr, activeNetParams)
|
2016-02-03 10:37:29 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-17 06:01:06 +03:00
|
|
|
go func() {
|
|
|
|
listenAddr := net.JoinHostPort("", "5009")
|
|
|
|
profileRedirect := http.RedirectHandler("/debug/pprof",
|
|
|
|
http.StatusSeeOther)
|
|
|
|
http.Handle("/", profileRedirect)
|
|
|
|
fmt.Println(http.ListenAndServe(listenAddr, nil))
|
|
|
|
}()
|
|
|
|
|
2016-03-23 04:50:11 +03:00
|
|
|
// Open the channeldb, which is dedicated to storing channel, and
|
|
|
|
// network related meta-data.
|
2016-04-24 22:37:09 +03:00
|
|
|
chanDB, err := channeldb.Open(loadedConfig.DataDir, activeNetParams)
|
2016-03-23 04:50:11 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("unable to open channeldb: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer chanDB.Close()
|
|
|
|
|
|
|
|
// Read btcd's for lnwallet's convenience.
|
|
|
|
f, err := os.Open(loadedConfig.RPCCert)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cert, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-12-30 03:23:27 +03:00
|
|
|
// Create, and start the lnwallet, which handles the core payment channel
|
|
|
|
// logic, and exposes control via proxy state machines.
|
2016-02-23 05:24:56 +03:00
|
|
|
config := &lnwallet.Config{
|
|
|
|
PrivatePass: []byte("hello"),
|
2016-03-23 04:50:11 +03:00
|
|
|
DataDir: filepath.Join(loadedConfig.DataDir, "lnwallet"),
|
|
|
|
RpcHost: loadedConfig.RPCHost,
|
|
|
|
RpcUser: loadedConfig.RPCUser,
|
|
|
|
RpcPass: loadedConfig.RPCPass,
|
|
|
|
CACert: cert,
|
|
|
|
NetParams: activeNetParams,
|
2016-02-23 05:24:56 +03:00
|
|
|
}
|
2016-03-25 00:32:55 +03:00
|
|
|
wallet, err := lnwallet.NewLightningWallet(config, chanDB)
|
2015-12-30 03:23:27 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to create wallet: %v\n", err)
|
2015-12-30 05:59:16 +03:00
|
|
|
os.Exit(1)
|
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)
|
2015-12-30 05:59:16 +03:00
|
|
|
os.Exit(1)
|
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-03-25 00:32:55 +03:00
|
|
|
ec := &lnwallet.WaddrmgrEncryptorDecryptor{wallet.Manager}
|
|
|
|
chanDB.RegisterCryptoSystem(ec)
|
|
|
|
|
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{
|
|
|
|
net.JoinHostPort("", strconv.Itoa(loadedConfig.PeerPort)),
|
|
|
|
}
|
2016-03-25 00:32:55 +03:00
|
|
|
server, err := newServer(defaultListenAddrs, 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-01-17 06:09:41 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
server.Start()
|
|
|
|
|
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
|
|
|
|
|
|
|
// Finally, start the grpc server listening for HTTP/2 connections.
|
2016-02-23 05:24:56 +03:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", loadedConfig.RPCPort))
|
2015-12-30 03:23:27 +03:00
|
|
|
if err != nil {
|
|
|
|
grpclog.Fatalf("failed to listen: %v", err)
|
|
|
|
fmt.Printf("failed to listen: %v", err)
|
2015-12-30 05:59:16 +03:00
|
|
|
os.Exit(1)
|
2015-12-30 03:23:27 +03:00
|
|
|
}
|
2016-03-23 04:50:11 +03:00
|
|
|
go func() {
|
|
|
|
rpcsLog.Infof("RPC server listening on %s", lis.Addr())
|
|
|
|
grpcServer.Serve(lis)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for shutdown signal from either a graceful server stop or from
|
|
|
|
// the interrupt handler.
|
|
|
|
<-shutdownChannel
|
|
|
|
ltndLog.Info("Shutdown complete")
|
2015-12-17 03:42:52 +03:00
|
|
|
}
|