ax(mining): remove banned fmt import from simulated_miner.go
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Replace fmt.Errorf with typed MiningError constructors (ErrMinerExists,
ErrMinerNotRunning) and fmt.Sprintf log lines with string concatenation
using strconv, eliminating the banned fmt import per AX conventions.

Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
Claude 2026-04-02 16:30:03 +01:00
parent 152952e101
commit 10dd88281b
No known key found for this signature in database
GPG key ID: AF404715446AEB41

View file

@ -2,9 +2,9 @@ package mining
import (
"context"
"fmt"
"math"
"math/rand"
"strconv"
"sync"
"time"
)
@ -103,7 +103,7 @@ func (m *SimulatedMiner) Start(config *Config) error {
m.mutex.Lock()
if m.Running {
m.mutex.Unlock()
return fmt.Errorf("simulated miner %s is already running", m.Name)
return ErrMinerExists(m.Name)
}
m.Running = true
@ -113,10 +113,11 @@ func (m *SimulatedMiner) Start(config *Config) error {
m.stopChan = make(chan struct{})
m.HashrateHistory = make([]HashratePoint, 0)
m.LowResHistory = make([]HashratePoint, 0)
timestamp := time.Now().Format("15:04:05")
m.logs = []string{
fmt.Sprintf("[%s] Simulated miner starting...", time.Now().Format("15:04:05")),
fmt.Sprintf("[%s] Connecting to %s", time.Now().Format("15:04:05"), m.poolName),
fmt.Sprintf("[%s] Pool connected, algorithm: %s", time.Now().Format("15:04:05"), m.Algorithm),
"[" + timestamp + "] Simulated miner starting...",
"[" + timestamp + "] Connecting to " + m.poolName,
"[" + timestamp + "] Pool connected, algorithm: " + m.Algorithm,
}
m.mutex.Unlock()
@ -132,12 +133,12 @@ func (m *SimulatedMiner) Stop() error {
defer m.mutex.Unlock()
if !m.Running {
return fmt.Errorf("simulated miner %s is not running", m.Name)
return ErrMinerNotRunning(m.Name)
}
close(m.stopChan)
m.Running = false
m.logs = append(m.logs, fmt.Sprintf("[%s] Miner stopped", time.Now().Format("15:04:05")))
m.logs = append(m.logs, "["+time.Now().Format("15:04:05")+"] Miner stopped")
return nil
}
@ -249,11 +250,11 @@ func (m *SimulatedMiner) simulateShare() {
// 2% chance of rejected share
if rand.Float64() < 0.02 {
m.rejected++
m.logs = append(m.logs, fmt.Sprintf("[%s] Share rejected (stale)", time.Now().Format("15:04:05")))
m.logs = append(m.logs, "["+time.Now().Format("15:04:05")+"] Share rejected (stale)")
} else {
m.shares++
diff := m.difficultyBase + rand.Intn(m.difficultyBase/2)
m.logs = append(m.logs, fmt.Sprintf("[%s] Share accepted (%d/%d) diff %d", time.Now().Format("15:04:05"), m.shares, m.rejected, diff))
m.logs = append(m.logs, "["+time.Now().Format("15:04:05")+"] Share accepted ("+strconv.Itoa(m.shares)+"/"+strconv.Itoa(m.rejected)+") diff "+strconv.Itoa(diff))
}
// Keep last 100 log lines
@ -263,13 +264,14 @@ func (m *SimulatedMiner) simulateShare() {
}
// metrics, err := miner.GetStats(ctx)
// fmt.Printf("hashrate: %d H/s, shares: %d\n", metrics.Hashrate, metrics.Shares)
// _ = metrics.Hashrate // current H/s
// _ = metrics.Shares // accepted share count
func (m *SimulatedMiner) GetStats(ctx context.Context) (*PerformanceMetrics, error) {
m.mutex.RLock()
defer m.mutex.RUnlock()
if !m.Running {
return nil, fmt.Errorf("simulated miner %s is not running", m.Name)
return nil, ErrMinerNotRunning(m.Name)
}
// Calculate current hashrate from recent history
@ -409,10 +411,10 @@ func (m *SimulatedMiner) WriteStdin(input string) error {
defer m.mutex.Unlock()
if !m.Running {
return fmt.Errorf("simulated miner %s is not running", m.Name)
return ErrMinerNotRunning(m.Name)
}
m.logs = append(m.logs, fmt.Sprintf("[%s] stdin: %s", time.Now().Format("15:04:05"), input))
m.logs = append(m.logs, "["+time.Now().Format("15:04:05")+"] stdin: "+input)
return nil
}