From 695406c99fccbe0298da68b77fb53944c06a6c97 Mon Sep 17 00:00:00 2001 From: gop Date: Sat, 12 Aug 2023 10:04:47 -0500 Subject: [PATCH] Added a mining work refresh time to fix the miner stalls --- go.mod | 2 +- go.sum | 2 ++ main.go | 45 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 9e2a633..cd5c7df 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/INFURA/go-ethlibs v0.0.0-20230210163729-fc6ca4235802 github.com/TwiN/go-color v1.4.0 - github.com/dominant-strategies/go-quai v0.13.0-rc.0 + github.com/dominant-strategies/go-quai v0.15.0-rc.0 github.com/dominant-strategies/go-quai-stratum v0.1.1-0.20230411175350-8a5f55caee55 github.com/spf13/viper v1.14.0 ) diff --git a/go.sum b/go.sum index 631cd41..0659dd2 100644 --- a/go.sum +++ b/go.sum @@ -123,6 +123,8 @@ github.com/dominant-strategies/bn256 v0.0.0-20220930122411-fbf930a7493d h1:hkL13 github.com/dominant-strategies/bn256 v0.0.0-20220930122411-fbf930a7493d/go.mod h1:nvtPJPChairu4o4iX2XGrstOFpLaAgNYhrUCl5bSng4= github.com/dominant-strategies/go-quai v0.13.0-rc.0 h1:aZpS8S8KF9N9S7zagdfLMegWmp+sFSXjFuaQtWeFO/g= github.com/dominant-strategies/go-quai v0.13.0-rc.0/go.mod h1:LQ/o4Mrx1YV/4TJK2o3fKuJIx811LpirNCk1FrrxkTQ= +github.com/dominant-strategies/go-quai v0.15.0-rc.0 h1:lfrmmSEr8KImBwY719N6LDKwVZXNY2w8U5ocYEsOpvU= +github.com/dominant-strategies/go-quai v0.15.0-rc.0/go.mod h1:LQ/o4Mrx1YV/4TJK2o3fKuJIx811LpirNCk1FrrxkTQ= github.com/dominant-strategies/go-quai-stratum v0.1.1-0.20230411175350-8a5f55caee55 h1:TDyOiTSueb5XoQhGFK57dZZkEsQGpzGhk7ppSC9YIV0= github.com/dominant-strategies/go-quai-stratum v0.1.1-0.20230411175350-8a5f55caee55/go.mod h1:MYwAEiEynMvuQeEIJaxL0rn9tJ7xKg6XCr4cQ0i3TFo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= diff --git a/main.go b/main.go index 5fa4325..c4aaaee 100644 --- a/main.go +++ b/main.go @@ -24,9 +24,10 @@ import ( const ( // resultQueueSize is the size of channel listening to sealing result. - resultQueueSize = 10 - maxRetryDelay = 60 * 60 * 4 // 4 hours - USER_AGENT_VER = "0.1" + resultQueueSize = 10 + maxRetryDelay = 60 * 60 * 4 // 4 hours + USER_AGENT_VER = "0.1" + miningWorkRefreshRate = 2 * time.Second ) var ( @@ -58,6 +59,8 @@ type Miner struct { // Track previous block number for pretty printing previousNumber [common.HierarchyDepth]uint64 + miningWorkRefresh *time.Ticker + // Tracks the latest JSON RPC ID to send to the proxy or node. latestId uint64 } @@ -149,12 +152,13 @@ func main() { } m := &Miner{ - config: config, - engine: engine, - header: types.EmptyHeader(), - updateCh: make(chan *types.Header, resultQueueSize), - resultCh: make(chan *types.Header, resultQueueSize), - previousNumber: [common.HierarchyDepth]uint64{0, 0, 0}, + config: config, + engine: engine, + header: types.EmptyHeader(), + updateCh: make(chan *types.Header, resultQueueSize), + resultCh: make(chan *types.Header, resultQueueSize), + previousNumber: [common.HierarchyDepth]uint64{0, 0, 0}, + miningWorkRefresh: time.NewTicker(miningWorkRefreshRate), } log.Println("Starting Quai cpu miner in location ", config.Location) if config.Proxy { @@ -171,6 +175,8 @@ func main() { go m.resultLoop() go m.miningLoop() go m.hashratePrinter() + go m.refreshMiningWork() + defer m.miningWorkRefresh.Stop() <-exit } @@ -256,9 +262,17 @@ func (m *Miner) miningLoop() error { stopCh = nil } } + var header *types.Header for { select { - case header := <-m.updateCh: + case newHead := <-m.updateCh: + + if header != nil && newHead.SealHash() == header.SealHash() { + continue + } + + header := newHead + m.miningWorkRefresh.Reset(miningWorkRefreshRate) // Mine the header here // Return the valid header with proper nonce and mix digest // Interrupt previous sealing operation @@ -286,6 +300,17 @@ func (m *Miner) miningLoop() error { if err := m.engine.Seal(header, m.resultCh, stopCh); err != nil { log.Println("Block sealing failed", "err", err) } + default: + } + } +} + +// refreshMiningWork is a simple method to refresh the mining the work +func (m *Miner) refreshMiningWork() { + for { + select { + case <-m.miningWorkRefresh.C: + m.fetchPendingHeaderNode() } } }