diff --git a/cmd/miner/client.go b/cmd/miner/client.go index 389b5ba2..fe32d064 100644 --- a/cmd/miner/client.go +++ b/cmd/miner/client.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2021 The Decred developers +// Copyright (c) 2019-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -19,23 +19,23 @@ import ( "github.com/decred/dcrpool/pool" ) -// Work represents the data received from a work notification. It comprises of +// work represents the data received from a work notification. It comprises of // hex encoded block header and pool target data. -type Work struct { +type work struct { jobID string header []byte target *big.Rat } -// Miner represents a stratum mining client. -type Miner struct { +// miner represents a stratum mining client. +type miner struct { id uint64 // update atomically. conn net.Conn - core *CPUMiner + core *cpuMiner encoder *json.Encoder reader *bufio.Reader - work *Work + work *work workMtx sync.RWMutex config *config req map[uint64]string @@ -55,14 +55,14 @@ type Miner struct { } // recordRequest logs a request as an id/method pair. -func (m *Miner) recordRequest(id uint64, method string) { +func (m *miner) recordRequest(id uint64, method string) { m.reqMtx.Lock() m.req[id] = method m.reqMtx.Unlock() } // fetchRequest fetches the method of the recorded request id. -func (m *Miner) fetchRequest(id uint64) string { +func (m *miner) fetchRequest(id uint64) string { m.reqMtx.RLock() method := m.req[id] m.reqMtx.RUnlock() @@ -70,19 +70,19 @@ func (m *Miner) fetchRequest(id uint64) string { } // deleteRequest removes the provided request id from the id cache. -func (m *Miner) deleteRequest(id uint64) { +func (m *miner) deleteRequest(id uint64) { m.reqMtx.Lock() delete(m.req, id) m.reqMtx.Unlock() } // nextID returns the next message id for the client. -func (m *Miner) nextID() uint64 { +func (m *miner) nextID() uint64 { return atomic.AddUint64(&m.id, 1) } // authenticate sends a stratum miner authentication message. -func (m *Miner) authenticate() error { +func (m *miner) authenticate() error { id := m.nextID() req := pool.AuthorizeRequest(&id, m.config.User, m.config.Address) err := m.encoder.Encode(req) @@ -96,7 +96,7 @@ func (m *Miner) authenticate() error { } // subscribe sends a stratum miner subscribe message. -func (m *Miner) subscribe() error { +func (m *miner) subscribe() error { id := m.nextID() req := pool.SubscribeRequest(&id, m.config.UserAgent, m.notifyID) err := m.encoder.Encode(req) @@ -111,7 +111,7 @@ func (m *Miner) subscribe() error { // keepAlive checks the state of the connection to the pool and reconnects // if needed. This should be run as a goroutine. -func (m *Miner) keepAlive(ctx context.Context) { +func (m *miner) keepAlive(ctx context.Context) { for { select { case <-ctx.Done(): @@ -157,7 +157,7 @@ func (m *Miner) keepAlive(ctx context.Context) { // read receives incoming data and passes the message received for // processing. It must be run as a goroutine. -func (m *Miner) read(ctx context.Context) { +func (m *miner) read(ctx context.Context) { for { select { case <-ctx.Done(): @@ -182,7 +182,7 @@ func (m *Miner) read(ctx context.Context) { m.connectedMtx.Unlock() m.workMtx.Lock() - m.work = new(Work) + m.work = new(work) m.workMtx.Unlock() // Signal the solver to abort hashing. @@ -229,7 +229,7 @@ func (m *Miner) read(ctx context.Context) { // listen reads and processes incoming messages from the pool client. It must // be run as a goroutine. -func (m *Miner) process(ctx context.Context) { +func (m *miner) process(ctx context.Context) { for { select { case <-ctx.Done(): @@ -427,7 +427,7 @@ func (m *Miner) process(ctx context.Context) { } // run handles the process life cycles of the miner. -func (m *Miner) run(ctx context.Context) { +func (m *miner) run(ctx context.Context) { m.wg.Add(3) go m.read(ctx) go m.keepAlive(ctx) @@ -444,11 +444,11 @@ func (m *Miner) run(ctx context.Context) { log.Infof("Miner terminated.") } -// NewMiner creates a stratum mining client. -func NewMiner(cfg *config, cancel context.CancelFunc) *Miner { - m := &Miner{ +// newMiner creates a stratum mining client. +func newMiner(cfg *config, cancel context.CancelFunc) *miner { + m := &miner{ config: cfg, - work: new(Work), + work: new(work), cancel: cancel, chainCh: make(chan struct{}), readCh: make(chan []byte), @@ -456,6 +456,6 @@ func NewMiner(cfg *config, cancel context.CancelFunc) *Miner { started: time.Now().Unix(), } - m.core = NewCPUMiner(m) + m.core = newCPUMiner(m) return m } diff --git a/cmd/miner/cpuminer.go b/cmd/miner/cpuminer.go index a25fadf8..67a89c0e 100644 --- a/cmd/miner/cpuminer.go +++ b/cmd/miner/cpuminer.go @@ -1,5 +1,5 @@ // Copyright (c) 2014-2016 The btcsuite developers -// Copyright (c) 2015-2023 The Decred developers +// Copyright (c) 2015-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -31,27 +31,27 @@ const ( hpsUpdateSecs = 5 ) -// SubmitWorkData encapsulates fields needed to create a stratum submit message. -type SubmitWorkData struct { +// submitWorkData encapsulates fields needed to create a stratum submit message. +type submitWorkData struct { nTime string nonce string extraNonce2 string } -// CPUMiner provides facilities for solving blocks using the CPU in a +// cpuMiner provides facilities for solving blocks using the CPU in a // concurrency-safe manner. It consists of a hash rate monitor and // worker goroutines which solve the received block. -type CPUMiner struct { - miner *Miner +type cpuMiner struct { + miner *miner rateCh chan float64 updateHashes chan uint64 - workData *SubmitWorkData + workData *submitWorkData workCh chan *pool.Request } // hashRateMonitor tracks number of hashes per second the mining process is // performing. It must be run as a goroutine. -func (m *CPUMiner) hashRateMonitor(ctx context.Context) { +func (m *cpuMiner) hashRateMonitor(ctx context.Context) { var hashRate float64 var totalHashes uint64 ticker := time.NewTicker(time.Second * hpsUpdateSecs) @@ -88,7 +88,7 @@ func (m *CPUMiner) hashRateMonitor(ctx context.Context) { // This function will return early with false when conditions that trigger a // stale block such as a new block showing up or periodically when there are // new transactions and enough time has elapsed without finding a solution. -func (m *CPUMiner) solveBlock(ctx context.Context, headerB []byte, target *big.Rat) bool { +func (m *cpuMiner) solveBlock(ctx context.Context, headerB []byte, target *big.Rat) bool { ticker := time.NewTicker(333 * time.Millisecond) defer ticker.Stop() @@ -188,7 +188,7 @@ func (m *CPUMiner) solveBlock(ctx context.Context, headerB []byte, target *big.R // solve is the main work horse of generateblocks. It attempts to solve blocks // while detecting when it is performing stale work. When a block is solved it // is sent via the work channel. -func (m *CPUMiner) solve(ctx context.Context) { +func (m *cpuMiner) solve(ctx context.Context) { for { m.miner.workMtx.RLock() if m.miner.work.target == nil || m.miner.work.jobID == "" || @@ -248,7 +248,7 @@ func (m *CPUMiner) solve(ctx context.Context) { // generateBlocks handles sending solved block submissions to the mining pool. // It must be run as a goroutine. -func (m *CPUMiner) generateBlocks(ctx context.Context) { +func (m *cpuMiner) generateBlocks(ctx context.Context) { for { select { case <-ctx.Done(): @@ -277,21 +277,13 @@ func (m *CPUMiner) generateBlocks(ctx context.Context) { } } -// HashesPerSecond returns the number of hashes per second the mining process -// is performing. -// -// This function is safe for concurrent access. -func (m *CPUMiner) HashesPerSecond() float64 { - return <-m.rateCh -} - -// NewCPUMiner returns a new instance of a CPU miner for the provided client. +// newCPUMiner returns a new instance of a CPU miner for the provided client. // Use Start to begin the mining process. -func NewCPUMiner(m *Miner) *CPUMiner { - return &CPUMiner{ +func newCPUMiner(m *miner) *cpuMiner { + return &cpuMiner{ rateCh: make(chan float64), updateHashes: make(chan uint64), - workData: new(SubmitWorkData), + workData: new(submitWorkData), miner: m, workCh: make(chan *pool.Request), } diff --git a/cmd/miner/main.go b/cmd/miner/main.go index 3152c108..228a3213 100644 --- a/cmd/miner/main.go +++ b/cmd/miner/main.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2021 The Decred developers +// Copyright (c) 2019-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -38,7 +38,7 @@ func main() { // Initialize and run the client. ctx, cancel := context.WithCancel(context.Background()) - miner := NewMiner(cfg, cancel) + miner := newMiner(cfg, cancel) log.Infof("Version: %s", version()) log.Infof("Runtime: Go version %s", runtime.Version()) diff --git a/config.go b/config.go index 17b48959..bdf1363b 100644 --- a/config.go +++ b/config.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2023 The Decred developers +// Copyright (c) 2019-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -393,7 +393,7 @@ func loadConfig(appName string) (*config, []string, error) { // Show the version and exit if the version flag was specified. if preCfg.ShowVersion { fmt.Printf("%s version %s (Go version %s %s/%s)\n", appName, - Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) + version, runtime.Version(), runtime.GOOS, runtime.GOARCH) os.Exit(0) } @@ -581,8 +581,8 @@ func loadConfig(appName string) (*config, []string, error) { } // Add default ports for the active network if there are no ports specified. - cfg.DcrdRPCHost = normalizeAddress(cfg.DcrdRPCHost, cfg.net.DcrdRPCServerPort) - cfg.WalletGRPCHost = normalizeAddress(cfg.WalletGRPCHost, cfg.net.WalletGRPCServerPort) + cfg.DcrdRPCHost = normalizeAddress(cfg.DcrdRPCHost, cfg.net.dcrdRPCServerPort) + cfg.WalletGRPCHost = normalizeAddress(cfg.WalletGRPCHost, cfg.net.walletGRPCServerPort) cfg.MinerListen = normalizeAddress(cfg.MinerListen, defaultMinerPort) cfg.GUIListen = normalizeAddress(cfg.GUIListen, defaultGUIPort) diff --git a/dcrpool.go b/dcrpool.go index 38778dac..73c45ce7 100644 --- a/dcrpool.go +++ b/dcrpool.go @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2023 The Decred developers +// Copyright (c) 2019-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -82,7 +82,7 @@ func newGUI(cfg *config, hub *pool.Hub) (*gui.GUI, error) { TLSCertFile: cfg.GUITLSCert, TLSKeyFile: cfg.GUITLSKey, ActiveNetName: cfg.net.Params.Name, - BlockExplorerURL: cfg.net.BlockExplorerURL, + BlockExplorerURL: cfg.net.blockExplorerURL, PaymentMethod: cfg.PaymentMethod, Designation: cfg.Designation, PoolFee: cfg.PoolFee, @@ -140,7 +140,7 @@ func realMain() error { // Show version and home dir at startup. mpLog.Infof("%s version %s (Go version %s %s/%s)", appName, - Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) + version, runtime.Version(), runtime.GOOS, runtime.GOARCH) mpLog.Infof("Home dir: %s", cfg.HomeDir) var db pool.Database diff --git a/params.go b/params.go index 54669e16..0b4ffb8b 100644 --- a/params.go +++ b/params.go @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2021 The Decred developers +// Copyright (c) 2020-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -10,28 +10,28 @@ import ( type params struct { *chaincfg.Params - DcrdRPCServerPort string - WalletGRPCServerPort string - BlockExplorerURL string + dcrdRPCServerPort string + walletGRPCServerPort string + blockExplorerURL string } var mainNetParams = params{ Params: chaincfg.MainNetParams(), - DcrdRPCServerPort: "9109", - WalletGRPCServerPort: "9111", - BlockExplorerURL: "https://dcrdata.decred.org", + dcrdRPCServerPort: "9109", + walletGRPCServerPort: "9111", + blockExplorerURL: "https://dcrdata.decred.org", } var testNet3Params = params{ Params: chaincfg.TestNet3Params(), - DcrdRPCServerPort: "19109", - WalletGRPCServerPort: "19111", - BlockExplorerURL: "https://testnet.dcrdata.org", + dcrdRPCServerPort: "19109", + walletGRPCServerPort: "19111", + blockExplorerURL: "https://testnet.dcrdata.org", } var simNetParams = params{ Params: chaincfg.SimNetParams(), - DcrdRPCServerPort: "19556", - WalletGRPCServerPort: "19558", - BlockExplorerURL: "...", + dcrdRPCServerPort: "19556", + walletGRPCServerPort: "19558", + blockExplorerURL: "...", } diff --git a/version.go b/version.go index 7578036f..41eff734 100644 --- a/version.go +++ b/version.go @@ -1,5 +1,5 @@ // Copyright (c) 2013-2014 The btcsuite developers -// Copyright (c) 2015-2023 The Decred developers +// Copyright (c) 2015-2024 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -18,60 +18,60 @@ var ( // // The expected process for setting the version in releases is as follows: // - Create a release branch of the form 'release-vMAJOR.MINOR' - // - Modify the Version variable below on that branch to: + // - Modify the version variable below on that branch to: // - Remove the pre-release portion // - Set the build metadata to 'release.local' - // - Update the Version variable below on the master branch to the next + // - Update the version variable below on the master branch to the next // expected version while retaining a pre-release of 'pre' // // These steps ensure that building from source produces versions that are - // distinct from reproducible builds that override the Version via linker + // distinct from reproducible builds that override the version via linker // flags. - // Version is the application version per the semantic versioning 2.0.0 spec + // version is the application version per the semantic versioning 2.0.0 spec // (https://semver.org/). // // It is defined as a variable so it can be overridden during the build // process with: - // '-ldflags "-X main.Version=fullsemver"' + // '-ldflags "-X main.version=fullsemver"' // if needed. // // It MUST be a full semantic version per the semantic versioning spec or // the app will panic at runtime. Of particular note is the pre-release // and build metadata portions MUST only contain characters from // semanticAlphabet. - Version = "1.3.0-pre" + version = "1.3.0-pre" - // NOTE: The following values are set via init by parsing the above Version + // NOTE: The following values are set via init by parsing the above version // string. // These fields are the individual semantic version components that define // the application version. - Major uint32 - Minor uint32 - Patch uint32 - PreRelease string - BuildMetadata string + major uint32 + minor uint32 + patch uint32 + preRelease string + buildMetadata string ) func init() { - parsedSemVer, err := semver.Parse(Version) + parsedSemVer, err := semver.Parse(version) if err != nil { panic(err) } - Major = parsedSemVer.Major - Minor = parsedSemVer.Minor - Patch = parsedSemVer.Patch - PreRelease = parsedSemVer.PreRelease - BuildMetadata = parsedSemVer.BuildMetadata - if BuildMetadata == "" { - BuildMetadata = vcsCommitID() - if BuildMetadata != "" { - Version = fmt.Sprintf("%d.%d.%d", Major, Minor, Patch) - if PreRelease != "" { - Version += "-" + PreRelease + major = parsedSemVer.Major + minor = parsedSemVer.Minor + patch = parsedSemVer.Patch + preRelease = parsedSemVer.PreRelease + buildMetadata = parsedSemVer.BuildMetadata + if buildMetadata == "" { + buildMetadata = vcsCommitID() + if buildMetadata != "" { + version = fmt.Sprintf("%d.%d.%d", major, minor, patch) + if preRelease != "" { + version += "-" + preRelease } - Version += "+" + BuildMetadata + version += "+" + buildMetadata } } }