Skip to content

Commit

Permalink
fixing ibd and initial config
Browse files Browse the repository at this point in the history
- use go 1.16 embed to pull sample config into binary
- add GoGC and GoMaxProcs options to config, plus applying them in
  at startup
  - default config includes typical values for 4 core 8 thread pc with
  4gb+ memory
-  altered initial setup to actually write the now embedded btcd.conf
  • Loading branch information
mleku committed Oct 24, 2023
1 parent 72c44af commit c08425a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
6 changes: 6 additions & 0 deletions conf.go
@@ -0,0 +1,6 @@
package main

import _ "embed"

//go:embed sample-btcd.conf
var btcdConfFile string
58 changes: 47 additions & 11 deletions config.go
Expand Up @@ -6,6 +6,7 @@ package main

import (
"bufio"
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/hex"
Expand All @@ -16,6 +17,7 @@ import (
"os"
"path/filepath"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -67,6 +69,7 @@ const (
defaultTxIndex = false
defaultAddrIndex = false
pruneMinSize = 1536
defaultGOGC = 300
)

var (
Expand All @@ -77,6 +80,7 @@ var (
defaultRPCKeyFile = filepath.Join(defaultHomeDir, "rpc.key")
defaultRPCCertFile = filepath.Join(defaultHomeDir, "rpc.cert")
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname)
defaultGOMAXPROCS = runtime.NumCPU() * 3
)

// runServiceCommand is only set to a real function on Windows. It is used
Expand Down Expand Up @@ -121,6 +125,8 @@ type config struct {
DropTxIndex bool `long:"droptxindex" description:"Deletes the hash-based transaction index from the database on start up and then exits."`
ExternalIPs []string `long:"externalip" description:"Add an ip to the list of local addresses we claim to listen on to peers"`
Generate bool `long:"generate" description:"Generate (mine) bitcoins using the CPU"`
GoGC int `long:"gogc" description:"configures the ratio of the garbage collector (GOGC) - higher values will lead to greater memory usage but decrease time spent by Go runtime collecting garbage"`
GoMaxProcs int `long:"gomaxprocs" description:"sets the maximum number of goroutines to allow to run concurrently (GOMAXPROCS). Goroutines are not processor threads, they are better when greater than processor threads"`
FreeTxRelayLimit float64 `long:"limitfreerelay" description:"Limit relay of transactions with no transaction fee to the given amount in thousands of bytes per minute"`
Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 8333, testnet: 18333)"`
LogDir string `long:"logdir" description:"Directory to log output."`
Expand Down Expand Up @@ -442,6 +448,8 @@ func loadConfig() (*config, []string, error) {
Generate: defaultGenerate,
TxIndex: defaultTxIndex,
AddrIndex: defaultAddrIndex,
GoMaxProcs: defaultGOMAXPROCS,
GoGC: defaultGOGC,
}

// Service options which are only added on Windows.
Expand Down Expand Up @@ -541,6 +549,10 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err
}

// write the default configuration file to the shiny new home directory
os.WriteFile(filepath.Join(defaultHomeDir, defaultConfigFilename),
[]byte(btcdConfFile), 0600)

// Multiple networks can't be selected simultaneously.
numNets := 0
// Count number of network flags passed; assign active network params
Expand Down Expand Up @@ -1163,6 +1175,28 @@ func loadConfig() (*config, []string, error) {
return nil, nil, err
}

// Clamp GOMAXPROCS between 1 and 10x number of CPU threads
if cfg.GoMaxProcs < 1 {
cfg.GoMaxProcs = 1
}
if cfg.GoMaxProcs > runtime.NumCPU()*10 {
cfg.GoMaxProcs = runtime.NumCPU() * 10
}

// Set the configured value on the runtime.
runtime.GOMAXPROCS(cfg.GoMaxProcs)

// Clamp GOGC between 1 and 1000
if cfg.GoGC < 1 {
cfg.GoGC = 1
}
if cfg.GoGC > 1000 {
cfg.GoGC = 1000
}

// Set the configured value on the runtime.
debug.SetGCPercent(cfg.GoGC)

// Warn about missing config file only after all other configuration is
// done. This prevents the warning on help messages and invalid
// options. Note this should go directly before the return.
Expand All @@ -1182,12 +1216,12 @@ func createDefaultConfigFile(destinationPath string) error {
return err
}

// We assume sample config file path is same as binary
path, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return err
}
sampleConfigPath := filepath.Join(path, sampleConfigFilename)
// // We assume sample config file path is same as binary
// path, err := filepath.Abs(filepath.Dir(os.Args[0]))
// if err != nil {
// return err
// }
// sampleConfigPath := filepath.Join(path, sampleConfigFilename)

// We generate a random user and password
randomBytes := make([]byte, 20)
Expand All @@ -1203,11 +1237,11 @@ func createDefaultConfigFile(destinationPath string) error {
}
generatedRPCPass := base64.StdEncoding.EncodeToString(randomBytes)

src, err := os.Open(sampleConfigPath)
if err != nil {
return err
}
defer src.Close()
// src, err := os.Open(sampleConfigPath)
// if err != nil {
// return err
// }
// defer src.Close()

dest, err := os.OpenFile(destinationPath,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
Expand All @@ -1216,6 +1250,8 @@ func createDefaultConfigFile(destinationPath string) error {
}
defer dest.Close()

src := bytes.NewBuffer([]byte(btcdConfFile))

// We copy every line from the sample config file to the destination,
// only replacing the two lines for rpcuser and rpcpass
reader := bufio.NewReader(src)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -32,6 +32,8 @@ replace github.com/btcsuite/btcd/btcutil => ./btcutil

replace github.com/btcsuite/btcd/btcec => github.com/mleku/ec v1.0.0

replace crypto/sha256 => github.com/minio/sha256-simd v1.0.1

// The retract statements below fixes an accidental push of the tags of a btcd
// fork.
retract (
Expand Down
10 changes: 10 additions & 0 deletions sample-btcd.conf
Expand Up @@ -343,3 +343,13 @@
; be disabled if this option is not specified. The profile information can be
; accessed at http://localhost:<profileport>/debug/pprof once running.
; profile=6061

; This sets the value of GOGC - by default set to 300, which increases memory
; utilization slightly but dramatically improves performance.
; gogc = 300

; GOMAXPROCS value is by default set to the same as the number of CPU threads,
; however, goroutines are much lighter than threads and the default value will
; be set to 3 times the CPU threads from runtime.NumCPU(). Thus 24 would be a
; typical value.
; gomaxprocs = 24

0 comments on commit c08425a

Please sign in to comment.