Integrate basic configuration functionality

This commit is contained in:
mkl 2016-02-22 21:24:56 -05:00 committed by Olaoluwa Osuntokun
parent 48667bdcbe
commit e07086a523
4 changed files with 131 additions and 21 deletions

104
config.go

@ -1 +1,105 @@
package main
import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
flags "github.com/btcsuite/go-flags"
"path/filepath"
"io/ioutil"
"os"
)
const (
defaultConfigFilename = "lnwallet.conf"
defaultDataDirname = "test_wal"
defaultRPCPort = 10009
defaultSPVMode = false
defaultPeerPort = 10011
defaultBTCDHost = "localhost:18334"
defaultBTCDUser = "user"
defaultBTCDPass = "passwd"
defaultBTCDCACertPath = ""
defaultUseRegtest = false
defaultSPVHostAdr = "localhost:18333"
defaultBTCDNoTLS = false
)
var (
lnwalletHomeDir = btcutil.AppDataDir("lnwallet", false)
defaultConfigFile = filepath.Join(lnwalletHomeDir, defaultConfigFilename)
defaultDataDir = filepath.Join(lnwalletHomeDir, defaultDataDirname)
)
type config struct {
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
DataDir string `short:"b" long:"datadir" description:"The directory to store lnd's data within"`
PeerPort int `long:"peerport" description:"The port to listen on for incoming p2p connections"`
RPCPort int `long:"rpcport" description:"The port for the rpc server"`
SPVMode bool `long:"spv" description:"assert to enter spv wallet mode"`
BTCDHost string `long:"btcdhost" description:"The BTCD RPC address. "`
BTCDUser string `long:"btcduser" description:"The BTCD RPC user"`
BTCDPass string `long:"btcdpass" description:"The BTCD RPC password"`
BTCDNoTLS bool `long:"btcdnotls" description:"Do not use TLS for RPC connection to BTCD"`
BTCDCACertPath string `long:"btcdcacert" description:"Path to certificate for BTCD RPC"`
UseRegtest bool `long:"regtest" description:"Use RegNet. If not specified TestNet3 is used"`
SPVHostAdr string `long:"spvhostadr" description:"Address of full bitcoin node. It is used in SPV mode."`
NetParams *chaincfg.Params
BTCDCACert []byte
}
// loadConfig initializes and parses the config using a config file and command
// line options.
//
// The configuration proceeds as follows:
// 1) Start with a default config with sane settings
// 2) Pre-parse the command line to check for an alternative config file
// 3) Load configuration file overwriting defaults with any specified options
// 4) Parse CLI options and overwrite/add any specified options
func loadConfig() (*config, error) {
defaultCfg := config{
ConfigFile: defaultConfigFile,
DataDir: defaultDataDir,
PeerPort: defaultPeerPort,
RPCPort: defaultRPCPort,
SPVMode: defaultSPVMode,
BTCDHost: defaultBTCDHost,
BTCDUser: defaultBTCDUser,
BTCDPass: defaultBTCDPass,
BTCDNoTLS: defaultBTCDNoTLS,
BTCDCACertPath: defaultBTCDCACertPath,
UseRegtest: defaultUseRegtest,
SPVHostAdr: defaultSPVHostAdr,
}
preCfg := defaultCfg
_, err := flags.Parse(&preCfg)
if err != nil {
return nil, err
}
cfg := defaultCfg
err = flags.IniParse(preCfg.ConfigFile, &cfg)
if err != nil {
return nil, err
}
_, err = flags.Parse(&cfg)
// Determine net parameters
if cfg.UseRegtest {
cfg.NetParams = &chaincfg.RegressionNetParams
} else {
cfg.NetParams = &chaincfg.TestNet3Params
}
// Read certificate if needed
if cfg.BTCDCACertPath!=""{
f, err := os.Open(cfg.BTCDCACertPath)
defer f.Close()
if err!=nil{
return nil, err
}
cert, err := ioutil.ReadAll(f)
if err!=nil{
return nil, err
}
cfg.BTCDCACert = cert
}
return &cfg, nil
}

45
lnd.go

@ -1,33 +1,30 @@
package main
import (
"flag"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"net"
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet"
)
var (
rpcPort = flag.Int("rpcport", 10009, "The port for the rpc server")
peerPort = flag.String("peerport", "10011", "The port to listen on for incoming p2p connections")
dataDir = flag.String("datadir", "test_wal", "The directory to store lnd's data within")
spvMode = flag.Bool("spv", false, "assert to enter spv wallet mode")
)
func main() {
flag.Parse()
if *spvMode == true {
shell()
loadedConfig, err := loadConfig()
if err != nil {
fmt.Printf("unable to load config: %v\n", err)
os.Exit(1)
}
if loadedConfig.SPVMode == true {
shell(loadedConfig.SPVHostAdr, loadedConfig.NetParams)
return
}
@ -43,8 +40,16 @@ func main() {
// logic, and exposes control via proxy state machines.
// TODO(roasbeef): accept config via cli flags, move to real config file
// afterwards
config := &lnwallet.Config{PrivatePass: []byte("hello"), DataDir: *dataDir}
config := &lnwallet.Config{
PrivatePass: []byte("hello"),
DataDir: loadedConfig.DataDir,
RpcHost: loadedConfig.BTCDHost,
RpcUser: loadedConfig.BTCDUser,
RpcPass: loadedConfig.BTCDPass,
RpcNoTLS: loadedConfig.BTCDNoTLS,
CACert: loadedConfig.BTCDCACert,
NetParams: loadedConfig.NetParams,
}
lnwallet, db, err := lnwallet.NewLightningWallet(config)
if err != nil {
fmt.Printf("unable to create wallet: %v\n", err)
@ -61,8 +66,8 @@ func main() {
// Set up the core server which will listen for incoming peer
// connections.
defaultListenAddr := []string{net.JoinHostPort("", *peerPort)}
server, err := newServer(defaultListenAddr, &chaincfg.TestNet3Params,
defaultListenAddr := []string{net.JoinHostPort("", strconv.Itoa(loadedConfig.PeerPort))}
server, err := newServer(defaultListenAddr, loadedConfig.NetParams,
lnwallet)
if err != nil {
fmt.Printf("unable to create server: %v\n", err)
@ -76,7 +81,7 @@ func main() {
lnrpc.RegisterLightningServer(grpcServer, server.rpcServer)
// Finally, start the grpc server listening for HTTP/2 connections.
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *rpcPort))
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", loadedConfig.RPCPort))
if err != nil {
grpclog.Fatalf("failed to listen: %v", err)
fmt.Printf("failed to listen: %v", err)

@ -40,6 +40,7 @@ type Config struct {
RpcHost string // localhost:18334
RpcUser string
RpcPass string
RpcNoTLS bool
RPCCert string
RPCKey string

@ -34,7 +34,7 @@ var (
SCon uspv.SPVCon // global here for now
)
func shell() {
func shell(SPVHostAdr string, Params *chaincfg.Params) {
fmt.Printf("LND spv shell v0.0\n")
fmt.Printf("Not yet well integrated, but soon.\n")