Choose correct ports for testnet and mainnet.

This copies the functionality of btcd for choosing the ports for the
HTTP client (for btcd) and server (for frontends).  On testnet, the
following ports are used as default:

 - btcd: 18334
 - frontends: 18332

When running with the (currently disabled) --mainnet flag, btcwallet
will choose the following ports by default:

 - btcd: 8334
 - frontends: 8332

Both ports can be overridden no matter the chosen network using the -b
and -p flags.
This commit is contained in:
Josh Rickmar 2013-10-16 17:29:48 -04:00
parent b5b684127c
commit 310dc010ac
4 changed files with 88 additions and 8 deletions

2
cmd.go
View file

@ -26,6 +26,7 @@ import (
"github.com/conformal/btcwallet/wallet"
"github.com/conformal/btcwire"
"github.com/conformal/seelog"
"github.com/davecgh/go-spew/spew"
"os"
"path/filepath"
"sync"
@ -526,6 +527,7 @@ func main() {
wallets.m[""] = w
wallets.Unlock()
}
spew.Dump(cfg)
go func() {
// Start HTTP server to listen and send messages to frontend and btcd

View file

@ -18,6 +18,7 @@ package main
import (
"fmt"
"github.com/conformal/btcwire"
"github.com/conformal/go-flags"
"os"
"path/filepath"
@ -26,9 +27,8 @@ import (
const (
defaultConfigFilename = "btcwallet.conf"
defaultBtcdPort = 8334
defaultBtcNet = btcwire.TestNet3
defaultLogLevel = "info"
defaultServerPort = 8332
)
var (
@ -38,10 +38,10 @@ var (
type config struct {
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
BtcdPort int `short:"b" long:"btcdport" description:"Port to connect to btcd on"`
BtcdPort string `short:"b" long:"btcdport" description:"Port to connect to btcd on (default: 18334, mainnet: 18332)"`
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level {trace, debug, info, warn, error, critical}"`
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
SvrPort int `short:"p" long:"serverport" description:"Port to serve frontend websocket connections on"`
SvrPort string `short:"p" long:"serverport" description:"Port to serve frontend websocket connections on (default: 18332, mainnet: 8332)"`
DataDir string `short:"D" long:"datadir" description:"Directory to store wallets and transactions"`
Username string `short:"u" long:"username" description:"Username for btcd authorization"`
Password string `short:"P" long:"password" description:"Password for btcd authorization"`
@ -66,6 +66,20 @@ func btcwalletHomeDir() string {
return "."
}
// updateConfigWithActiveParams update the passed config with parameters
// from the active net params if the relevant options in the passed config
// object are the default so options specified by the user on the command line
// are not overridden.
func updateConfigWithActiveParams(cfg *config) {
if cfg.BtcdPort == netParams(defaultBtcNet).btcdPort {
cfg.BtcdPort = activeNetParams.btcdPort
}
if cfg.SvrPort == netParams(defaultBtcNet).svrPort {
cfg.SvrPort = activeNetParams.svrPort
}
}
// filesExists reports whether the named file or directory exists.
func fileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
@ -93,8 +107,8 @@ func loadConfig() (*config, []string, error) {
cfg := config{
DebugLevel: defaultLogLevel,
ConfigFile: defaultConfigFile,
BtcdPort: defaultBtcdPort,
SvrPort: defaultServerPort,
BtcdPort: netParams(defaultBtcNet).btcdPort,
SvrPort: netParams(defaultBtcNet).svrPort,
DataDir: defaultDataDir,
}
@ -147,5 +161,11 @@ func loadConfig() (*config, []string, error) {
// TODO(jrick): Enable mainnet support again when ready.
cfg.MainNet = false
// Choose the active network params based on the mainnet net flag.
if cfg.MainNet {
activeNetParams = netParams(btcwire.MainNet)
}
updateConfigWithActiveParams(&cfg)
return &cfg, remainingArgs, nil
}

58
params.go Normal file
View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"github.com/conformal/btcwire"
)
var activeNetParams = netParams(defaultBtcNet)
// params is used to group parameters for various networks such as the main
// network and test networks.
type params struct {
btcdPort string
svrPort string
}
// mainNetParams contains parameters specific running btcwallet and
// btcd on the main network (btcwire.MainNet).
var mainNetParams = params{
btcdPort: "8334",
svrPort: "8332",
}
// testNet3Params contains parameters specific running btcwallet and
// btcd on the test network (version 3) (btcwire.TestNet3).
var testNet3Params = params{
btcdPort: "18334",
svrPort: "18332",
}
// netParams returns parameters specific to the passed bitcoin network.
func netParams(btcnet btcwire.BitcoinNet) *params {
switch btcnet {
case btcwire.TestNet3:
return &testNet3Params
// Return main net by default.
case btcwire.MainNet:
fallthrough
default:
return &mainNetParams
}
}

View file

@ -441,7 +441,7 @@ func FrontendListenAndServe() error {
// TODO(jrick): We need some sort of authentication before websocket
// connections are allowed, and perhaps TLS on the server as well.
http.Handle("/frontend", websocket.Handler(frontendReqsNotifications))
return http.ListenAndServe(fmt.Sprintf(":%d", cfg.SvrPort), nil)
return http.ListenAndServe(fmt.Sprintf(":%s", cfg.SvrPort), nil)
}
// BtcdConnect connects to a running btcd instance over a websocket
@ -450,7 +450,7 @@ func FrontendListenAndServe() error {
func BtcdConnect(reply chan error) {
// btcd requires basic authorization, so we use a custom config with
// the Authorization header set.
server := fmt.Sprintf("ws://localhost:%d/wallet", cfg.BtcdPort)
server := fmt.Sprintf("ws://localhost:%s/wallet", cfg.BtcdPort)
login := cfg.Username + ":" + cfg.Password
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
config, err := websocket.NewConfig(server, "http://localhost/")