以太坊geth GPU挖矿分析

以太坊geth 默认是使用CPU挖矿,如果需要使用GPU 挖矿,需要单独的挖矿程序ethminer.exe

https://github.com/ethereum-mining/ethminer   ethminer.exe

ethminer 是一个独立的挖矿软件,可以连接geth 的rpc端口,通过getwork协议与geth交互

geth中GPU挖矿主要通是通过miner包中的remote-agent.go来完成的

1得到计算任务

通过getwork方法来完成geth指派的工作

func (a *RemoteAgent) GetWork() ([3]string, error) {
	fmt.Println("remote_agent.go GetWrok()")
	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()

		a.work[block.HashNoNonce()] = a.currentWork
		return res, nil
	}
	return res, errors.New("No work available yet, don't panic.")
}

2、提交计算结果

Sumitwork用来提交计算结果

// 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 {
	fmt.Println("remote_agent.go SubmitWork()")
	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
}

 

你可能感兴趣的:(区块链)