Skip to content

Commit

Permalink
Added a mining work refresh time to fix the miner stalls
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Sep 7, 2023
1 parent c4f1126 commit 695406c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
45 changes: 35 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -171,6 +175,8 @@ func main() {
go m.resultLoop()
go m.miningLoop()
go m.hashratePrinter()
go m.refreshMiningWork()
defer m.miningWorkRefresh.Stop()
<-exit
}

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

0 comments on commit 695406c

Please sign in to comment.