在向以太坊節(jié)點(diǎn)調(diào)用eth_submitWork時视哑,偶爾會返回false
查看geth的日志险领,可以發(fā)現(xiàn)false 的原因是:"Work submitted but none pending"褐缠, 那這個又是指什么呢
在go-ethereum的源碼中找到報錯的位置
//miner/remote_agent.go
// SubmitWork tries to inject a pow solution into the remote agent, returning
// whether the solution was accepted or not (not can be both a bad pow as well as
// any other error, like no work pending).
func (a *RemoteAgent) SubmitWork(nonce types.BlockNonce, mixDigest, hash common.Hash) bool {
a.mu.Lock()
defer a.mu.Unlock()
// Make sure the work submitted is present
work := a.work[hash]
if work == nil {
log.Info("Work submitted but none pending", "hash", hash)
return false
}
// Make sure the Engine solutions is indeed valid
result := work.Block.Header()
result.Nonce = nonce
result.MixDigest = mixDigest
if err := a.engine.VerifySeal(a.chain, result); err != nil {
log.Warn("Invalid proof-of-work submitted", "hash", hash, "err", err)
return false
}
block := work.Block.WithSeal(result)
// Solutions seems to be valid, return to the miner and notify acceptance
a.returnCh <- &Result{work, block}
delete(a.work, hash)
return true
}
1.可以看到節(jié)點(diǎn)將任務(wù)保存在RemoteAgent.work
2.任務(wù)以hash_nononce為索引
3.提交任務(wù)時先驗(yàn)證該任務(wù)是否存在
錯誤的原因是提交的任務(wù)在RemoteAgent.work已經(jīng)找不到
那么問題來了
1.任務(wù)什么時候生成
2.任務(wù)為什么會被刪掉
任務(wù)生成的代碼
//miner/remote_agent.go
func (a *RemoteAgent) GetWork() ([3]string, error) {
a.mu.Lock()
defer a.mu.Unlock()
var res [3]string
if a.currentWork != nil {
block := a.currentWork.Block
res[0] = block.HashNoNonce().Hex()
seedHash := ethash.SeedHash(block.NumberU64())
res[1] = common.BytesToHash(seedHash).Hex()
// Calculate the "target" to be returned to the external miner
n := big.NewInt(1)
n.Lsh(n, 255)
n.Div(n, block.Difficulty())
n.Lsh(n, 1)
res[2] = common.BytesToHash(n.Bytes()).Hex()
######
任務(wù)在getwork時候才會保存到RemoteAgent.work中
######
a.work[block.HashNoNonce()] = a.currentWork
return res, nil
}
return res, errors.New("No work available yet, don't panic.")
}
刪除任務(wù)的代碼
//miner/remote_agent.go
// loop monitors mining events on the work and quit channels, updating the internal
// state of the remote miner until a termination is requested.
//
// Note, the reason the work and quit channels are passed as parameters is because
// RemoteAgent.Start() constantly recreates these channels, so the loop code cannot
// assume data stability in these member fields.
func (a *RemoteAgent) loop(workCh chan *Work, quitCh chan struct{}) {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-quitCh:
return
case work := <-workCh:
a.mu.Lock()
a.currentWork = work
a.mu.Unlock()
case <-ticker.C:
// cleanup
a.mu.Lock()
for hash, work := range a.work {
#######
當(dāng)任務(wù)生成時間超過84s后 任務(wù)將會被刪除
#######
if time.Since(work.createdAt) > 7*(12*time.Second) {
delete(a.work, hash)
}
}
a.mu.Unlock()
a.hashrateMu.Lock()
for id, hashrate := range a.hashrate {
if time.Since(hashrate.ping) > 10*time.Second {
delete(a.hashrate, id)
}
}
a.hashrateMu.Unlock()
}
}
}
結(jié)論:
- 任務(wù)只會在getWork時才會生成
- 任務(wù)在生成84s后刪除,需要重新調(diào)用getwork