Skip to content
This repository has been archived by the owner on Mar 2, 2023. It is now read-only.

Commit

Permalink
Fix ability to specify backend server and default server
Browse files Browse the repository at this point in the history
For electrum, the address must be HOST:tPORT or HOST:sPORT. If addr is
not specified, we default to the initial hardcoded node.

For btcd, the address must be HOST:PORT. If addr is not specifed, we
default to localhost:8334.
  • Loading branch information
alokmenghrajani committed Oct 18, 2018
1 parent 31b141f commit 041952c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
4 changes: 4 additions & 0 deletions backend/electrum/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func NewNode(addr, port string, network utils.Network) (*Node, error) {
}

if port[0] == 't' {
// TCP
var p string
if len(port) == 1 {
p = defaultTCP
Expand All @@ -147,13 +148,16 @@ func NewNode(addr, port string, network utils.Network) (*Node, error) {
}
t, err = NewTCPTransport(fmt.Sprintf("%s:%s", a, p))
} else if port[0] == 's' {
// TLS
var p string
if len(port) == 1 {
p = defaultSSL
} else {
p = port[1:]
}
t, err = NewSSLTransport(fmt.Sprintf("%s:%s", a, p))
} else {
panic(fmt.Sprintf("port (%s) must start with t or s", port))
}

if err != nil {
Expand Down
50 changes: 33 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"math"
"net"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -37,7 +36,7 @@ var (
findBlock = app.Command("find-block", "Finds the block height for a given date/time.")
findBlockTimestamp = findBlock.Arg("timestamp", "Date/time to resolve. E.g. \"2006-01-02 15:04:05 MST\"").Required().String()
findBlockBackend = findBlock.Flag("backend", "electrum | btcd | electrum-recorder | btcd-recorder | fixture").Default("electrum").Enum("electrum", "btcd", "electrum-recorder", "btcd-recorder", "fixture")
findBlockAddr = findBlock.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").TCP()
findBlockAddr = findBlock.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").String()
findBlockRpcUser = findBlock.Flag("rpcuser", "RPC username").PlaceHolder("USER").String()
findBlockRpcPass = findBlock.Flag("rpcpass", "RPC password").PlaceHolder("PASSWORD").String()
findBlockFixtureFile = findBlock.Flag("fixture-file", "Fixture file to use for recording or replaying data.").PlaceHolder("FILEPATH").String()
Expand All @@ -48,7 +47,7 @@ var (
computeBalanceM = computeBalance.Flag("m", "number of signatures (quorum)").Short('m').Default("1").Int()
computeBalanceN = computeBalance.Flag("n", "number of public keys").Short('n').Default("1").Int()
computeBalanceBackend = computeBalance.Flag("backend", "electrum | btcd | electrum-recorder | btcd-recorder | fixture").Default("electrum").Enum("electrum", "btcd", "electrum-recorder", "btcd-recorder", "fixture")
computeBalanceAddr = computeBalance.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").TCP()
computeBalanceAddr = computeBalance.Flag("addr", "Backend to connect to initially. Defaults to a hardcoded node for Electrum and localhost for Btcd.").PlaceHolder("HOST:PORT").String()
computeBalanceRpcUser = computeBalance.Flag("rpcuser", "RPC username").PlaceHolder("USER").String()
computeBalanceRpcPass = computeBalance.Flag("rpcpass", "RPC password").PlaceHolder("PASSWORD").String()
computeBalanceFixtureFile = computeBalance.Flag("fixture-file", "Fixture file to use for recording or replaying data.").PlaceHolder("FILEPATH").String()
Expand Down Expand Up @@ -303,13 +302,14 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) {
var err error
switch *computeBalanceBackend {
case "electrum":
addr, port := getServer(network, *computeBalanceAddr)
addr, port := getServer(network, "electrum", *computeBalanceAddr)
b, err = backend.NewElectrumBackend(addr, port, network)
if err != nil {
return nil, err
}
case "btcd":
b, err = backend.NewBtcdBackend((*computeBalanceAddr).String(), *computeBalanceRpcUser,
addr, port := getServer(network, "btcd", *computeBalanceAddr)
b, err = backend.NewBtcdBackend(fmt.Sprintf("%s:%s", addr, port), *computeBalanceRpcUser,
*computeBalanceRpcPass, network)
if err != nil {
return nil, err
Expand All @@ -318,7 +318,7 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) {
if *computeBalanceFixtureFile == "" {
panic("electrum-recorder backend requires output --fixture-file.")
}
addr, port := getServer(network, *computeBalanceAddr)
addr, port := getServer(network, "electrum", *computeBalanceAddr)
b, err = backend.NewElectrumBackend(addr, port, network)
if err != nil {
return nil, err
Expand All @@ -328,7 +328,8 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) {
if *computeBalanceFixtureFile == "" {
panic("btcd-recorder backend requires output --fixture-file.")
}
b, err = backend.NewBtcdBackend((*computeBalanceAddr).String(), *computeBalanceRpcUser,
addr, port := getServer(network, "btcd", *computeBalanceAddr)
b, err = backend.NewBtcdBackend(fmt.Sprintf("%s:%s", addr, port), *computeBalanceRpcUser,
*computeBalanceRpcPass, network)
if err != nil {
return nil, err
Expand All @@ -348,17 +349,32 @@ func computeBalanceBuildBackend(network Network) (backend.Backend, error) {
return b, err
}

// pick a default server for each network if none provided
// TODO: default server should be localhost for Btcd backend.
func getServer(network Network, addr *net.TCPAddr) (string, string) {
if addr != nil {
return addr.IP.String(), strconv.Itoa(addr.Port)
// pick a default server for electrum or localhost for btcd
func getServer(network Network, backend, addr string) (string, string) {
if addr != "" {
host, port, err := net.SplitHostPort(addr)
PanicOnError(err)
return host, port
}
switch network {
case "mainnet":
return "electrum.petrkr.net", "s50002"
case "testnet":
return "electrum_testnet_unlimited.criptolayer.net", "s50102"
switch backend {
case "electrum":
switch network {
case "mainnet":
return "electrum.petrkr.net", "s50002"
case "testnet":
return "electrum_testnet_unlimited.criptolayer.net", "s50102"
default:
panic("unreachable")
}
case "btcd":
switch network {
case "mainnet":
return "localhost", "8334"
case "testnet":
return "localhost", "18334"
default:
panic("unreachable")
}
default:
panic("unreachable")
}
Expand Down

0 comments on commit 041952c

Please sign in to comment.