diff --git a/pkg/mining/simulated_miner.go b/pkg/mining/simulated_miner.go index 46ec044..0374afa 100644 --- a/pkg/mining/simulated_miner.go +++ b/pkg/mining/simulated_miner.go @@ -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 }