mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[NOD-102] Change mining simulator to use cli args and add DisableTLS option (#244)
* [NOD-102] Change mining simulator to work with cli arguments and add DisableTLS option * [NOD-102] Add config.go * [NOD-102] Change config missing aruments errors
This commit is contained in:
parent
46f9604562
commit
8630b65bb2
@ -5,10 +5,8 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var addressListPath string
|
||||
|
||||
func getAddressList() ([]string, error) {
|
||||
file, err := os.Open(addressListPath)
|
||||
func getAddressList(cfg *config) ([]string, error) {
|
||||
file, err := os.Open(cfg.AddressListPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
33
mining/simulator/config.go
Normal file
33
mining/simulator/config.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/jessevdk/go-flags"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
AddressListPath string `long:"addresslist" description:"Path to a list of nodes' JSON-RPC endpoints" required:"true"`
|
||||
CertificatePath string `long:"cert" description:"Path to certificate accepted by JSON-RPC endpoint"`
|
||||
DisableTLS bool `long:"notls" description:"Disable TLS"`
|
||||
}
|
||||
|
||||
func parseConfig() (*config, error) {
|
||||
cfg := &config{}
|
||||
parser := flags.NewParser(cfg, flags.PrintErrors|flags.HelpFlag)
|
||||
_, err := parser.Parse()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if cfg.CertificatePath == "" && !cfg.DisableTLS {
|
||||
return nil, errors.New("--notls has to be disabled if --cert is used")
|
||||
}
|
||||
|
||||
if cfg.CertificatePath != "" && cfg.DisableTLS {
|
||||
return nil, errors.New("--cert should be omitted if --notls is used")
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
@ -8,23 +8,29 @@ import (
|
||||
"github.com/daglabs/btcd/rpcclient"
|
||||
)
|
||||
|
||||
var certificatePath string
|
||||
|
||||
func connectToServers(addressList []string) ([]*rpcclient.Client, error) {
|
||||
func connectToServers(cfg *config, addressList []string) ([]*rpcclient.Client, error) {
|
||||
clients := make([]*rpcclient.Client, len(addressList))
|
||||
|
||||
cert, err := ioutil.ReadFile(certificatePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error reading certificates file: %s", err)
|
||||
var cert []byte
|
||||
if !cfg.DisableTLS {
|
||||
var err error
|
||||
cert, err = ioutil.ReadFile(cfg.CertificatePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error reading certificates file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, address := range addressList {
|
||||
connCfg := &rpcclient.ConnConfig{
|
||||
Host: address,
|
||||
Endpoint: "ws",
|
||||
User: "user",
|
||||
Pass: "pass",
|
||||
Certificates: cert,
|
||||
Host: address,
|
||||
Endpoint: "ws",
|
||||
User: "user",
|
||||
Pass: "pass",
|
||||
DisableTLS: cfg.DisableTLS,
|
||||
}
|
||||
|
||||
if !cfg.DisableTLS {
|
||||
connCfg.Certificates = cert
|
||||
}
|
||||
|
||||
client, err := rpcclient.New(connCfg, nil)
|
||||
|
@ -3,8 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/user"
|
||||
"path"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"sync/atomic"
|
||||
|
||||
@ -16,17 +15,18 @@ var isRunning int32
|
||||
func main() {
|
||||
defer handlePanic()
|
||||
|
||||
err := initPaths()
|
||||
cfg, err := parseConfig()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Error initializing paths: %s", err))
|
||||
fmt.Fprintf(os.Stderr, "Error parsing command-line arguments: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
addressList, err := getAddressList()
|
||||
addressList, err := getAddressList(cfg)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Couldn't load address list: %s", err))
|
||||
}
|
||||
|
||||
clients, err := connectToServers(addressList)
|
||||
clients, err := connectToServers(cfg, addressList)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Error connecting to servers: %s", err))
|
||||
}
|
||||
@ -40,20 +40,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func initPaths() error {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error getting current user: %s", err)
|
||||
}
|
||||
|
||||
basePath := ".btcd/mining_simulator"
|
||||
|
||||
certificatePath = path.Join(usr.HomeDir, basePath, "rpc.cert")
|
||||
addressListPath = path.Join(usr.HomeDir, basePath, "addresses")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func disconnect(clients []*rpcclient.Client) {
|
||||
for _, client := range clients {
|
||||
client.Disconnect()
|
||||
|
Loading…
x
Reference in New Issue
Block a user