Skip to content

Commit

Permalink
miner: Unexport everything.
Browse files Browse the repository at this point in the history
The CPU miner is a tool for developers and automated tests which is not
intended to be imported or reused by other projects, and as such none of
its code needs to be exported.
  • Loading branch information
jholdstock committed Aug 7, 2024
1 parent 0829d87 commit 80ab27d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
46 changes: 23 additions & 23 deletions cmd/miner/client.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
Expand All @@ -55,34 +55,34 @@ 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()
return method
}

// 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)
Expand All @@ -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)
Expand All @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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.
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand All @@ -444,18 +444,18 @@ 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),
req: make(map[uint64]string),
started: time.Now().Unix(),
}

m.core = NewCPUMiner(m)
m.core = newCPUMiner(m)
return m
}
38 changes: 15 additions & 23 deletions cmd/miner/cpuminer.go
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 == "" ||
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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),
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/miner/main.go
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 80ab27d

Please sign in to comment.