eth_submitWork时的问题

在向以太坊节点调用eth_submitWork时,偶尔会返回false

查看geth的日志,可以发现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.可以看到节点将任务保存在RemoteAgent.work
2.任务以hash_nononce为索引
3.提交任务时先验证该任务是否存在

错误的原因是提交的任务在RemoteAgent.work已经找不到

那么问题来了
1.任务什么时候生成
2.任务为什么会被删掉

任务生成的代码

//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()
######
任务在getwork时候才会保存到RemoteAgent.work中
######
        a.work[block.HashNoNonce()] = a.currentWork
        return res, nil
    }
    return res, errors.New("No work available yet, don't panic.")
}

删除任务的代码

//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 {
#######
当任务生成时间超过84s后 任务将会被删除
#######
                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()
        }
    }
}

结论:

  • 任务只会在getWork时才会生成
  • 任务在生成84s后删除,需要重新调用getwork
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文是对以太坊文档 Ethereum Frontier Guide 和 Ethereum Homestead 的整...
    趁风卷阅读 13,203评论 0 16
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • 金指尖的花园阅读 793评论 0 3
  • 1.我为什么想读这本书 ? 最早之前看过古典老师的《拆掉思维的墙》一书,对于《跃迁》这本书我也特别感兴趣,光是跃迁...
    美少女晓露阅读 955评论 0 5
  • 这个夏天,虽进入了五月底,但还是热烈不起来,经常大雨滂沱或细雨弥漫,总让人觉得还是凉凉的。加上一要好同事生病告假住...
    回首己陌路阅读 1,612评论 0 2