Skip to content

Commit

Permalink
add the ability to parse zebrad.toml config file
Browse files Browse the repository at this point in the history
Fixes #462. If the file extension is .conf (as in zcash.conf), then
parse the file as an INI file. If the extension is .toml (as in
zebrad.toml), then interpret as TOML.
  • Loading branch information
Larry Ruane authored and LarryRuane committed Jun 27, 2024
1 parent 6071b06 commit 0bd5519
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 51 deletions.
46 changes: 0 additions & 46 deletions frontend/frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,52 +540,6 @@ func TestSendTransaction(t *testing.T) {
step = 0
}

var sampleconf = `
testnet = 1
rpcport = 18232
rpcbind = 127.0.0.1
rpcuser = testlightwduser
rpcpassword = testlightwdpassword
`

func TestNewZRPCFromConf(t *testing.T) {
connCfg, err := connFromConf([]byte(sampleconf))
if err != nil {
t.Fatal("connFromConf failed")
}
if connCfg.Host != "127.0.0.1:18232" {
t.Fatal("connFromConf returned unexpected Host")
}
if connCfg.User != "testlightwduser" {
t.Fatal("connFromConf returned unexpected User")
}
if connCfg.Pass != "testlightwdpassword" {
t.Fatal("connFromConf returned unexpected User")
}
if !connCfg.HTTPPostMode {
t.Fatal("connFromConf returned unexpected HTTPPostMode")
}
if !connCfg.DisableTLS {
t.Fatal("connFromConf returned unexpected DisableTLS")
}

// can't pass an integer
_, err = connFromConf(10)
if err == nil {
t.Fatal("connFromConf unexpected success")
}

// Can't verify returned values, but at least run it
_, err = NewZRPCFromConf([]byte(sampleconf))
if err != nil {
t.Fatal("NewZRPCFromClient failed")
}
_, err = NewZRPCFromConf(10)
if err == nil {
t.Fatal("NewZRPCFromClient unexpected success")
}
}

func TestMempoolFilter(t *testing.T) {
txidlist := []string{
"2e819d0bab5c819dc7d5f92d1bfb4127ce321daf847f6602",
Expand Down
45 changes: 40 additions & 5 deletions frontend/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"errors"
"fmt"
"net"
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/btcsuite/btcd/rpcclient"
"github.com/zcash/lightwalletd/common"
ini "gopkg.in/ini.v1"
)

// NewZRPCFromConf reads the zcashd configuration file.
func NewZRPCFromConf(confPath interface{}) (*rpcclient.Client, error) {
func NewZRPCFromConf(confPath string) (*rpcclient.Client, error) {
connCfg, err := connFromConf(confPath)
if err != nil {
return nil, err
Expand All @@ -36,12 +38,18 @@ func NewZRPCFromFlags(opts *common.Options) (*rpcclient.Client, error) {
return rpcclient.New(connCfg, nil)
}

// If passed a string, interpret as a path, open and read; if passed
// a byte slice, interpret as the config file content (used in testing).
func connFromConf(confPath interface{}) (*rpcclient.ConnConfig, error) {
func connFromConf(confPath string) (*rpcclient.ConnConfig, error) {
if filepath.Ext(confPath) == ".toml" {
return connFromToml(confPath)
} else {
return connFromIni(confPath)
}
}

func connFromIni(confPath string) (*rpcclient.ConnConfig, error) {
cfg, err := ini.Load(confPath)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
return nil, fmt.Errorf("failed to read config file in .conf format: %w", err)
}

rpcaddr := cfg.Section("").Key("rpcbind").String()
Expand Down Expand Up @@ -76,3 +84,30 @@ func connFromConf(confPath interface{}) (*rpcclient.ConnConfig, error) {
// not supported in HTTP POST mode.
return connCfg, nil
}

// If passed a string, interpret as a path, open and read; if passed
// a byte slice, interpret as the config file content (used in testing).
func connFromToml(confPath string) (*rpcclient.ConnConfig, error) {
var tomlConf struct {
Rpc struct {
Listen_addr string
RPCUser string
RPCPassword string
}
}
_, err := toml.DecodeFile(confPath, &tomlConf)
if err != nil {
return nil, fmt.Errorf("failed to read config file in .toml format: %w", err)
}
conf := rpcclient.ConnConfig{
Host: tomlConf.Rpc.Listen_addr,
User: tomlConf.Rpc.RPCUser,
Pass: tomlConf.Rpc.RPCPassword,
HTTPPostMode: true, // Zcash only supports HTTP POST mode
DisableTLS: true, // Zcash does not provide TLS by default
}

// Notice the notification parameter is nil since notifications are
// not supported in HTTP POST mode.
return &conf, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/zcash/lightwalletd
go 1.17

require (
github.com/BurntSushi/toml v0.3.1
github.com/btcsuite/btcd v0.24.0
github.com/golang/protobuf v1.5.3
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ cloud.google.com/go/workflows v1.12.3/go.mod h1:fmOUeeqEwPzIU81foMjTRQIdwQHADi/v
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
Expand Down

0 comments on commit 0bd5519

Please sign in to comment.