Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexport everything in the main package(s) #439

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
@@ -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.

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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions dcrpool.go
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions params.go
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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: "...",
}
Loading
Loading