Skip to content

Commit

Permalink
feat: make hash rate display interval configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney committed Jul 13, 2024
1 parent 630fccb commit f60b9f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
12 changes: 7 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Config struct {
Indexer IndexerConfig `yaml:"indexer"`
Submit SubmitConfig `yaml:"submit"`
Wallet WalletConfig `yaml:"wallet"`
Worker WorkerConfig `yaml:"worker"`
Miner MinerConfig `yaml:"miner"`
Logging LoggingConfig `yaml:"logging"`
Metrics MetricsConfig `yaml:"metrics"`
Debug DebugConfig `yaml:"debug"`
Expand Down Expand Up @@ -61,8 +61,9 @@ type WalletConfig struct {
Mnemonic string `yaml:"mnemonic" envconfig:"MNEMONIC"`
}

type WorkerConfig struct {
Count int `yaml:"count" envconfig:"WORKER_COUNT"`
type MinerConfig struct {
WorkerCount int `yaml:"workers" envconfig:"WORKER_COUNT"`
HashRateInterval int `yaml:"hashRateInterval" envconfig:"HASH_RATE_INTERVAL"`
}

type LoggingConfig struct {
Expand Down Expand Up @@ -100,8 +101,9 @@ var globalConfig = &Config{
},
// The default worker config is somewhat conservative: worker count is set
// to half of the available logical CPUs
Worker: WorkerConfig{
Count: runtime.NumCPU() / 2,
Miner: MinerConfig{
WorkerCount: runtime.NumCPU() / 2,
HashRateInterval: 60,
},
Network: "mainnet",
Profile: "tuna-v1",
Expand Down
15 changes: 8 additions & 7 deletions internal/miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import (
)

const (
hashRateLogInterval = 60 * time.Second
restartTimeout = 2 * time.Minute
restartTimeout = 2 * time.Minute
)

type Manager struct {
Expand All @@ -49,7 +48,7 @@ var globalManager = &Manager{}
func (m *Manager) Reset() {
m.workerWaitGroup = sync.WaitGroup{}
m.doneChan = make(chan any)
m.resultChan = make(chan Result, config.GetConfig().Worker.Count)
m.resultChan = make(chan Result, config.GetConfig().Miner.WorkerCount)
}

func (m *Manager) Stop() {
Expand Down Expand Up @@ -100,8 +99,8 @@ func (m *Manager) Start(blockData any) {
m.scheduleHashRateLog()
// Start workers
m.Reset()
logger.Infof("starting %d workers", cfg.Worker.Count)
for i := 0; i < cfg.Worker.Count; i++ {
logger.Infof("starting %d workers", cfg.Miner.WorkerCount)
for i := 0; i < cfg.Miner.WorkerCount; i++ {
miner := New(
&(m.workerWaitGroup),
m.resultChan,
Expand Down Expand Up @@ -130,10 +129,12 @@ func (m *Manager) Start(blockData any) {
}

func (m *Manager) scheduleHashRateLog() {
m.hashLogTimer = time.AfterFunc(hashRateLogInterval, m.hashRateLog)
cfg := config.GetConfig()
m.hashLogTimer = time.AfterFunc(time.Duration(cfg.Miner.HashRateInterval)*time.Second, m.hashRateLog)
}

func (m *Manager) hashRateLog() {
cfg := config.GetConfig()
logger := logging.GetLogger()
hashCount := m.hashCounter.Load()
// Handle counter rollover
Expand All @@ -144,7 +145,7 @@ func (m *Manager) hashRateLog() {
}
hashCountDiff := hashCount - m.hashLogLastCount
m.hashLogLastCount = hashCount
secondDivisor := uint64(hashRateLogInterval / time.Second)
secondDivisor := uint64(cfg.Miner.HashRateInterval)
hashCountPerSec := hashCountDiff / secondDivisor
logger.Infof("hash rate: %d/s", hashCountPerSec)
m.scheduleHashRateLog()
Expand Down

0 comments on commit f60b9f0

Please sign in to comment.